Skip to content

Commit

Permalink
feat(package-rules)!: match packageName for matchPackageNames (#22703)
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins authored Jun 24, 2023
1 parent 5794854 commit 7cc60bb
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 71 deletions.
9 changes: 0 additions & 9 deletions docs/usage/self-hosted-experimental.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,6 @@ 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, you'll get the following behavior.

When using `matchPackageNames` and `matchPackagePatterns` matchers:

1. Renovate first tries to match against `depName`
2. If `depName` doesn't match then Renovate tries to match against `packageName`

## `RENOVATE_X_MERGE_CONFIDENCE_API_BASE_URL`

If set, Renovate will query this API for Merge Confidence data.
Expand Down
41 changes: 0 additions & 41 deletions lib/util/package-rules/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1210,45 +1210,4 @@ 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);
}
);
});
});
26 changes: 26 additions & 0 deletions lib/util/package-rules/package-names.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,32 @@ describe('util/package-rules/package-names', () => {
);
expect(result).toBeFalse();
});

it('should matchPackageName', () => {
const result = packageNameMatcher.matches(
{
depName: 'abc',
packageName: 'def',
},
{
matchPackageNames: ['def'],
}
);
expect(result).toBeTrue();
});

it('should fall back to matching depName', () => {
const result = packageNameMatcher.matches(
{
depName: 'abc',
packageName: 'def',
},
{
matchPackageNames: ['abc'],
}
);
expect(result).toBeTrue();
});
});

describe('exclude', () => {
Expand Down
14 changes: 8 additions & 6 deletions lib/util/package-rules/package-names.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import is from '@sindresorhus/is';
import type { PackageRule, PackageRuleInputConfig } from '../../config/types';
import { logger } from '../../logger';
import { Matcher } from './base';

export class PackageNameMatcher extends Matcher {
Expand All @@ -14,15 +15,16 @@ export class PackageNameMatcher extends Matcher {
return false;
}

if (matchPackageNames.includes(depName)) {
if (is.string(packageName) && matchPackageNames.includes(packageName)) {
return true;
}

if (
is.string(packageName) &&
process.env.RENOVATE_X_MATCH_PACKAGE_NAMES_MORE
) {
return matchPackageNames.includes(packageName);
if (matchPackageNames.includes(depName)) {
logger.once.warn(
{ packageName, depName },
'Use matchDepNames instead of matchPackageNames'
);
return true;
}

return false;
Expand Down
26 changes: 26 additions & 0 deletions lib/util/package-rules/package-patterns.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,31 @@ describe('util/package-rules/package-patterns', () => {
);
expect(result).toBeFalse();
});

it('should match packageName', () => {
const result = packageNameMatcher.matches(
{
depName: 'abc',
packageName: 'def',
},
{
matchPackagePatterns: ['def'],
}
);
expect(result).toBeTrue();
});

it('should fall back to matching depName', () => {
const result = packageNameMatcher.matches(
{
depName: 'abc',
packageName: 'def',
},
{
matchPackagePatterns: ['abc'],
}
);
expect(result).toBeTrue();
});
});
});
38 changes: 23 additions & 15 deletions lib/util/package-rules/package-patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ import { regEx } from '../regex';
import { Matcher } from './base';
import { massagePattern } from './utils';

function matchPatternsAgainstName(
matchPackagePatterns: string[],
name: string
): boolean {
let isMatch = false;
for (const packagePattern of matchPackagePatterns) {
if (isPackagePatternMatch(packagePattern, name)) {
isMatch = true;
}
}
return isMatch;
}

export class PackagePatternsMatcher extends Matcher {
override matches(
{ depName, packageName }: PackageRuleInputConfig,
Expand All @@ -18,26 +31,21 @@ export class PackagePatternsMatcher extends Matcher {
return false;
}

const namesToMatchAgainst = [depName];

if (
is.string(packageName) &&
process.env.RENOVATE_X_MATCH_PACKAGE_NAMES_MORE
matchPatternsAgainstName(matchPackagePatterns, packageName)
) {
namesToMatchAgainst.push(packageName);
return true;
}

let isMatch = false;
for (const packagePattern of matchPackagePatterns) {
if (
namesToMatchAgainst.some((p) =>
isPackagePatternMatch(packagePattern, p)
)
) {
isMatch = true;
}
if (matchPatternsAgainstName(matchPackagePatterns, depName)) {
logger.once.warn(
{ packageName, depName },
'Use matchDepPatterns instead of matchPackagePatterns'
);
return true;
}
return isMatch;

return false;
}

override excludes(
Expand Down

0 comments on commit 7cc60bb

Please sign in to comment.