Skip to content

Commit

Permalink
feat(packageRules): add experimental env var for matching against pac…
Browse files Browse the repository at this point in the history
…kage names (#19784)
  • Loading branch information
Gabriel-Ladzaretti committed Jan 12, 2023
1 parent 6bee386 commit 2de4260
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 7 deletions.
4 changes: 4 additions & 0 deletions docs/usage/self-hosted-experimental.md
Expand Up @@ -63,6 +63,10 @@ Source: [AWS s3 documentation - Interface BucketEndpointInputConfig](https://doc

If set, Renovate will terminate the whole process group of a terminated child process spawned by Renovate.

## `RENOVATE_X_MATCH_PACKAGE_NAMES_MORE`

If set, Renovate will try to match against `packageName` after trying `depName` When using `matchPackageNames` & `matchPackagePatterns` matchers.

## `RENOVATE_X_AUTODISCOVER_REPO_SORT`

<!-- prettier-ignore -->
Expand Down
1 change: 1 addition & 0 deletions lib/config/types.ts
Expand Up @@ -442,6 +442,7 @@ export interface PackageRuleInputConfig extends Record<string, unknown> {
depType?: string;
depTypes?: string[];
depName?: string;
packageName?: string | null;
currentValue?: string | null;
currentVersion?: string;
lockedVersion?: string;
Expand Down
41 changes: 41 additions & 0 deletions lib/util/package-rules/index.spec.ts
Expand Up @@ -1113,4 +1113,45 @@ describe('util/package-rules/index', () => {
expect(res1.x).toBeUndefined();
expect(res2.x).toBe(1);
});

describe('test matchers supporting RENOVATE_X_MATCH_PACKAGE_NAMES_MORE', () => {
const processEnvOrg: NodeJS.ProcessEnv = process.env;

afterEach(() => {
process.env = processEnvOrg;
});

it.each`
matcherName | isXEnvEnabled | expected
${'matchPackageNames'} | ${false} | ${undefined}
${'matchPackagePatterns'} | ${false} | ${undefined}
${'matchPackageNames'} | ${true} | ${1}
${'matchPackagePatterns'} | ${true} | ${1}
`(
'tests $matcherName selector when experimental env is $isXEnvEnabled (expected res=$expected)',
({ matcherName, isXEnvEnabled, expected }) => {
if (isXEnvEnabled) {
process.env.RENOVATE_X_MATCH_PACKAGE_NAMES_MORE = 'true';
}
const config: TestConfig = {
packageRules: [
{
[matcherName]: ['does-match'],
x: 1,
},
],
};

const res = applyPackageRules({
...config,
depName: 'does-not-match',
packageName: 'does-match',
});

applyPackageRules(config); // coverage

expect(res.x).toBe(expected);
}
);
});
});
16 changes: 14 additions & 2 deletions lib/util/package-rules/package-names.ts
Expand Up @@ -4,7 +4,7 @@ import { Matcher } from './base';

export class PackageNameMatcher extends Matcher {
override matches(
{ depName }: PackageRuleInputConfig,
{ depName, packageName }: PackageRuleInputConfig,
{ matchPackageNames }: PackageRule
): boolean | null {
if (is.undefined(matchPackageNames)) {
Expand All @@ -13,7 +13,19 @@ export class PackageNameMatcher extends Matcher {
if (is.undefined(depName)) {
return false;
}
return matchPackageNames.includes(depName);

if (matchPackageNames.includes(depName)) {
return true;
}

if (
is.string(packageName) &&
process.env.RENOVATE_X_MATCH_PACKAGE_NAMES_MORE
) {
return matchPackageNames.includes(packageName);
}

return false;
}

override excludes(
Expand Down
30 changes: 25 additions & 5 deletions lib/util/package-rules/package-patterns.ts
Expand Up @@ -7,7 +7,7 @@ import { massagePattern } from './utils';

export class PackagePatternsMatcher extends Matcher {
override matches(
{ depName, updateType }: PackageRuleInputConfig,
{ depName, packageName }: PackageRuleInputConfig,
{ matchPackagePatterns }: PackageRule
): boolean | null {
if (is.undefined(matchPackagePatterns)) {
Expand All @@ -18,19 +18,30 @@ export class PackagePatternsMatcher extends Matcher {
return false;
}

const namesToMatchAgainst = [depName];

if (
is.string(packageName) &&
process.env.RENOVATE_X_MATCH_PACKAGE_NAMES_MORE
) {
namesToMatchAgainst.push(packageName);
}

let isMatch = false;
for (const packagePattern of matchPackagePatterns) {
const packageRegex = regEx(massagePattern(packagePattern));
if (packageRegex.test(depName)) {
logger.trace(`${depName} matches against ${String(packageRegex)}`);
if (
namesToMatchAgainst.some((p) =>
isPackagePatternMatch(packagePattern, p)
)
) {
isMatch = true;
}
}
return isMatch;
}

override excludes(
{ depName, updateType }: PackageRuleInputConfig,
{ depName }: PackageRuleInputConfig,
{ excludePackagePatterns }: PackageRule
): boolean | null {
// ignore lockFileMaintenance for backwards compatibility
Expand All @@ -52,3 +63,12 @@ export class PackagePatternsMatcher extends Matcher {
return isMatch;
}
}

function isPackagePatternMatch(pckPattern: string, pck: string): boolean {
const re = regEx(massagePattern(pckPattern));
if (re.test(pck)) {
logger.trace(`${pck} matches against ${String(re)}`);
return true;
}
return false;
}

0 comments on commit 2de4260

Please sign in to comment.