Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(manager/regex): depName requirement if using the recursive strategy #16225

Merged
merged 15 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/usage/configuration-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -2314,7 +2314,7 @@ This can be used to narrow down the search area to prevent multiple matches.
But the `recursive` strategy still allows the matching of multiple dependencies as described below.
All matches of the first `matchStrings` pattern are detected, then each of these matches will used as basis be used as the input for the next `matchStrings` pattern, and so on.
If the next `matchStrings` pattern has multiple matches then it will split again.
This process will be followed as long there is a match plus a next `matchingStrings` pattern is available or a dependency is detected.
This process will be followed as long there is a match plus a next `matchingStrings` pattern is available.

Matched groups will be available in subsequent matching layers.

Expand Down
30 changes: 30 additions & 0 deletions lib/modules/manager/regex/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,4 +419,34 @@ describe('modules/manager/regex/index', () => {
expect(res).toMatchSnapshot();
expect(res?.deps).toHaveLength(4);
});

it('extracts with recursive strategy and without depName', async () => {
const config: CustomExtractConfig = {
matchStrings: [
'jacoco\\s*{[^}]*}',
'toolVersion\\s*=\\s*\\"(?<currentValue>\\S*)\\"\\s*',
],
matchStringsStrategy: 'recursive',
depNameTemplate: 'org.jacoco:jacoco',
datasourceTemplate: 'maven',
};
const res = await extractPackageFile(
`
jacoco {
toolVersion = "0.8.7"
}
`,
'build.gradle.kts',
config
);
expect(res).toMatchObject({
deps: [
{
depName: 'org.jacoco:jacoco',
currentValue: '0.8.7',
datasource: 'maven',
},
],
});
});
});
30 changes: 15 additions & 15 deletions lib/modules/manager/regex/strategies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { regEx } from '../../../util/regex';
import type { CustomExtractConfig, PackageDependency } from '../types';
import {
createDependency,
isValidDependency,
mergeExtractionTemplate,
mergeGroups,
regexMatchAll,
Expand All @@ -22,7 +23,8 @@ export function handleAny(
config
)
)
.filter(is.truthy);
.filter(is.truthy)
.filter(isValidDependency);
viceice marked this conversation as resolved.
Show resolved Hide resolved
}

export function handleCombination(
Expand All @@ -44,7 +46,9 @@ export function handleCombination(
replaceString: match?.groups?.currentValue ? match[0] : undefined,
}))
.reduce((base, addition) => mergeExtractionTemplate(base, addition));
return [createDependency(extraction, config)].filter(is.truthy);
return [createDependency(extraction, config)]
.filter(is.truthy)
.filter(isValidDependency);
}

export function handleRecursive(
Expand All @@ -59,21 +63,17 @@ export function handleRecursive(
);
// abort if we have no matchString anymore
if (!regexes[index]) {
return [];
const result = createDependency(
{
groups: combinedGroups,
replaceString: content,
},
config
);
return result ? [result] : [];
secustor marked this conversation as resolved.
Show resolved Hide resolved
}
return regexMatchAll(regexes[index], content)
.flatMap((match) => {
// if we have a depName and a currentValue which have the minimal viable definition
if (match?.groups?.depName && match?.groups?.currentValue) {
return createDependency(
{
groups: mergeGroups(combinedGroups, match.groups),
replaceString: match[0],
},
config
);
}

return handleRecursive(
match[0],
packageFile,
Expand All @@ -82,5 +82,5 @@ export function handleRecursive(
mergeGroups(combinedGroups, match.groups ?? {})
);
})
.filter(is.truthy);
.filter(isValidDependency);
}
8 changes: 8 additions & 0 deletions lib/modules/manager/regex/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,11 @@ export function mergeExtractionTemplate(
replaceString: addition.replaceString ?? base.replaceString,
};
}

export function isValidDependency({
depName,
currentValue,
}: PackageDependency): boolean {
// check if all the fields are set
return Boolean(depName) && Boolean(currentValue);
secustor marked this conversation as resolved.
Show resolved Hide resolved
}