From 2aa6b43d8affb8a29c8d5aa10cbd3e471bba8d6f Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Sat, 24 Jun 2023 18:51:37 +0200 Subject: [PATCH] feat(package-rules)!: match packageName for matchPackageNames (#22703) --- docs/usage/self-hosted-experimental.md | 9 ---- lib/util/package-rules/index.spec.ts | 41 ------------------- lib/util/package-rules/package-names.spec.ts | 26 ++++++++++++ lib/util/package-rules/package-names.ts | 14 ++++--- .../package-rules/package-patterns.spec.ts | 26 ++++++++++++ lib/util/package-rules/package-patterns.ts | 38 ++++++++++------- 6 files changed, 83 insertions(+), 71 deletions(-) diff --git a/docs/usage/self-hosted-experimental.md b/docs/usage/self-hosted-experimental.md index 47fd66b1cfd0be1..010813cb665080d 100644 --- a/docs/usage/self-hosted-experimental.md +++ b/docs/usage/self-hosted-experimental.md @@ -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. diff --git a/lib/util/package-rules/index.spec.ts b/lib/util/package-rules/index.spec.ts index 0718b5243126a8c..e34456503d66d28 100644 --- a/lib/util/package-rules/index.spec.ts +++ b/lib/util/package-rules/index.spec.ts @@ -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); - } - ); - }); }); diff --git a/lib/util/package-rules/package-names.spec.ts b/lib/util/package-rules/package-names.spec.ts index 1e6641ab94bc6e0..a2d4fbf75787b66 100644 --- a/lib/util/package-rules/package-names.spec.ts +++ b/lib/util/package-rules/package-names.spec.ts @@ -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', () => { diff --git a/lib/util/package-rules/package-names.ts b/lib/util/package-rules/package-names.ts index 385cf164ac4654b..8e647a8014e7b9f 100644 --- a/lib/util/package-rules/package-names.ts +++ b/lib/util/package-rules/package-names.ts @@ -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 { @@ -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; diff --git a/lib/util/package-rules/package-patterns.spec.ts b/lib/util/package-rules/package-patterns.spec.ts index f5f6614df7cde23..133867f7ee3d868 100644 --- a/lib/util/package-rules/package-patterns.spec.ts +++ b/lib/util/package-rules/package-patterns.spec.ts @@ -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(); + }); }); }); diff --git a/lib/util/package-rules/package-patterns.ts b/lib/util/package-rules/package-patterns.ts index 2b7b29e5807e63f..a7fc03e2c029114 100644 --- a/lib/util/package-rules/package-patterns.ts +++ b/lib/util/package-rules/package-patterns.ts @@ -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); + 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(