Skip to content
Snippets Groups Projects
Commit e4dead2e authored by Rhys Arkins's avatar Rhys Arkins
Browse files

feat(package-rules)!: match packageName for matchPackageNames (#22703)

Closes #20926

BREAKING CHANGE: matchPackageNames now matches both depName (existing) and packageName (new)
parent ad0479a1
Branches
No related tags found
No related merge requests found
......@@ -78,15 +78,6 @@ You can set the config file Renovate should read with the `RENOVATE_CONFIG_FILE`
The process that runs Renovate must have the correct permissions to delete the config file.
## `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.
......
......@@ -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);
}
);
});
});
......@@ -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', () => {
......
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 {
......@@ -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;
......
......@@ -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();
});
});
});
......@@ -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,
......@@ -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);
}
let isMatch = false;
for (const packagePattern of matchPackagePatterns) {
if (
namesToMatchAgainst.some((p) =>
isPackagePatternMatch(packagePattern, p)
)
) {
isMatch = true;
return true;
}
if (matchPatternsAgainstName(matchPackagePatterns, depName)) {
logger.once.warn(
{ packageName, depName },
'Use matchDepPatterns instead of matchPackagePatterns'
);
return true;
}
return isMatch;
return false;
}
override excludes(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment