diff --git a/docs/usage/configuration-options.md b/docs/usage/configuration-options.md index 5e98e7db6d4609f5c48bcb9d4bef894a0bdfe88f..f423844643b060ff6c42facd26c66258df072660 100644 --- a/docs/usage/configuration-options.md +++ b/docs/usage/configuration-options.md @@ -1489,6 +1489,20 @@ Use this field to restrict rules to a particular branch. e.g. } ``` +This field also supports Regular Expressions if they begin and end with `/`. e.g. + +```json +{ + "packageRules": [ + { + "matchBaseBranches": ["/^release\\/.*/"], + "excludePackagePatterns": ["^eslint"], + "enabled": false + } + ] +} +``` + ### matchManagers Use this field to restrict rules to a particular package manager. e.g. diff --git a/lib/config/options/index.ts b/lib/config/options/index.ts index c4bff82781079ebf3068f93d1100b61e3824e04d..5b966d52dfd2db1df9f850004da09874f47a36a2 100644 --- a/lib/config/options/index.ts +++ b/lib/config/options/index.ts @@ -852,7 +852,7 @@ const options: RenovateOptions[] = [ { name: 'matchBaseBranches', description: - 'List of branches to match (e.g. ["master"]). Valid only within `packageRules` object.', + 'List of strings containing exact matches (e.g. `["main"]`) and/or regex expressions (e.g. `["/^release\\/.*/"]`). Valid only within `packageRules` object.', type: 'array', subType: 'string', allowString: true, diff --git a/lib/util/package-rules.spec.ts b/lib/util/package-rules.spec.ts index 2d8d0d22268c5f5943f964a785b860f24fe38f7f..c39a6e7cae1810c68c12865befd99401ae1e5422 100644 --- a/lib/util/package-rules.spec.ts +++ b/lib/util/package-rules.spec.ts @@ -381,6 +381,41 @@ describe('util/package-rules', () => { const res = applyPackageRules({ ...config, ...dep }); expect(res.x).toBeUndefined(); }); + it('filters branches with matching branch regex', () => { + const config: TestConfig = { + packageRules: [ + { + matchBaseBranches: ['/^release\\/.*/'], + x: 1, + }, + ], + }; + const dep = { + depType: 'dependencies', + datasource: OrbDatasource.id, + baseBranch: 'release/5.8', + }; + const res = applyPackageRules({ ...config, ...dep }); + expect(res.x).toBe(1); + }); + + it('filters branches with non-matching branch regex', () => { + const config: TestConfig = { + packageRules: [ + { + matchBaseBranches: ['/^release\\/.*/'], + x: 1, + }, + ], + }; + const dep = { + depType: 'dependencies', + datasource: OrbDatasource.id, + baseBranch: 'master', + }; + const res = applyPackageRules({ ...config, ...dep }); + expect(res.x).toBeUndefined(); + }); it('filters updateType', () => { const config: TestConfig = { packageRules: [ diff --git a/lib/util/package-rules.ts b/lib/util/package-rules.ts index 92228a94fc9557feb390696ccf495958dcbf485d..b951688a6638d6548ba2fb98595732d7b76113b3 100644 --- a/lib/util/package-rules.ts +++ b/lib/util/package-rules.ts @@ -100,7 +100,14 @@ function matchesRule( positiveMatch = true; } if (matchBaseBranches.length) { - const isMatch = matchBaseBranches.includes(baseBranch); + const isMatch = matchBaseBranches.some((matchBaseBranch): boolean => { + const isAllowedPred = configRegexPredicate(matchBaseBranch); + if (isAllowedPred) { + return isAllowedPred(baseBranch); + } + return matchBaseBranch === baseBranch; + }); + if (!isMatch) { return false; }