From 5d045c1e316281aff106f2cdb3a9646ee40ab6b4 Mon Sep 17 00:00:00 2001 From: Dmitry <Karpov.Dmitry80@gmail.com> Date: Tue, 12 Mar 2019 08:27:49 +0200 Subject: [PATCH] feat(package-rules): add datasources selector (#3344) --- lib/config/definitions.js | 12 +++++++ lib/config/validation.js | 1 + lib/util/package-rules.js | 8 +++++ .../__snapshots__/validation.spec.js.snap | 2 +- test/util/package-rules.spec.js | 36 +++++++++++++++++++ website/docs/configuration-options.md | 11 ++++++ 6 files changed, 69 insertions(+), 1 deletion(-) diff --git a/lib/config/definitions.js b/lib/config/definitions.js index 0ea5912e41..a40f8b686d 100644 --- a/lib/config/definitions.js +++ b/lib/config/definitions.js @@ -431,6 +431,18 @@ const options = [ cli: false, env: false, }, + { + name: 'datasources', + description: + 'List of datasources to match (e.g. ["orb"]). Valid only within `packageRules` object', + type: 'list', + allowString: true, + parent: 'packageRules', + stage: 'package', + mergeable: true, + cli: false, + env: false, + }, { name: 'depTypeList', description: diff --git a/lib/config/validation.js b/lib/config/validation.js index 97095a3bc1..3fff9e8b62 100644 --- a/lib/config/validation.js +++ b/lib/config/validation.js @@ -137,6 +137,7 @@ async function validateConfig(config, isPreset, parentPath) { 'languages', 'baseBranchList', 'managers', + 'datasources', 'depTypeList', 'packageNames', 'packagePatterns', diff --git a/lib/util/package-rules.js b/lib/util/package-rules.js index e492763a35..65df85a08e 100644 --- a/lib/util/package-rules.js +++ b/lib/util/package-rules.js @@ -24,6 +24,7 @@ function applyPackageRules(inputConfig) { language, baseBranch, manager, + datasource, } = config; const packageRules = config.packageRules || []; logger.trace( @@ -36,6 +37,7 @@ function applyPackageRules(inputConfig) { languages, baseBranchList, managers, + datasources, depTypeList, packageNames, packagePatterns, @@ -50,6 +52,7 @@ function applyPackageRules(inputConfig) { languages = languages || []; baseBranchList = baseBranchList || []; managers = managers || []; + datasources = datasources || []; depTypeList = depTypeList || []; packageNames = packageNames || []; packagePatterns = packagePatterns || []; @@ -97,6 +100,11 @@ function applyPackageRules(inputConfig) { positiveMatch = positiveMatch || isMatch; negativeMatch = negativeMatch || !isMatch; } + if (datasources.length) { + const isMatch = datasources.includes(datasource); + positiveMatch = positiveMatch || isMatch; + negativeMatch = negativeMatch || !isMatch; + } if (updateTypes.length) { const isMatch = updateTypes.includes(updateType) || diff --git a/test/config/__snapshots__/validation.spec.js.snap b/test/config/__snapshots__/validation.spec.js.snap index 5550789092..43b378917a 100644 --- a/test/config/__snapshots__/validation.spec.js.snap +++ b/test/config/__snapshots__/validation.spec.js.snap @@ -44,7 +44,7 @@ Array [ }, Object { "depName": "Configuration Error", - "message": "packageRules: Each packageRule must contain at least one selector (paths, languages, baseBranchList, managers, depTypeList, packageNames, packagePatterns, excludePackageNames, excludePackagePatterns, sourceUrlPrefixes, updateTypes). If you wish for configuration to apply to all packages, it is not necessary to place it inside a packageRule at all.", + "message": "packageRules: Each packageRule must contain at least one selector (paths, languages, baseBranchList, managers, datasources, depTypeList, packageNames, packagePatterns, excludePackageNames, excludePackagePatterns, sourceUrlPrefixes, updateTypes). If you wish for configuration to apply to all packages, it is not necessary to place it inside a packageRule at all.", }, Object { "depName": "Configuration Error", diff --git a/test/util/package-rules.spec.js b/test/util/package-rules.spec.js index 65d21cf885..254492e77a 100644 --- a/test/util/package-rules.spec.js +++ b/test/util/package-rules.spec.js @@ -213,6 +213,23 @@ describe('applyPackageRules()', () => { const res = applyPackageRules({ ...config, ...dep }); expect(res.x).toBeUndefined(); }); + it('filters datasources with matching datasource', () => { + const config = { + packageRules: [ + { + datasources: ['orb', 'docker'], + x: 1, + }, + ], + }; + const dep = { + depType: 'dependencies', + datasource: 'orb', + baseBranch: 'master', + }; + const res = applyPackageRules({ ...config, ...dep }); + expect(res.x).toBe(1); + }); it('filters branches with matching branch', () => { const config = { packageRules: [ @@ -223,11 +240,29 @@ describe('applyPackageRules()', () => { ], }; const dep = { + depType: 'dependencies', + datasource: 'orb', baseBranch: 'master', }; const res = applyPackageRules({ ...config, ...dep }); expect(res.x).toBe(1); }); + it('filters datasources with non-matching datasource', () => { + const config = { + packageRules: [ + { + datasources: ['orb'], + x: 1, + }, + ], + }; + const dep = { + depType: 'dependencies', + baseBranch: 'staging', + }; + const res = applyPackageRules({ ...config, ...dep }); + expect(res.x).toBeUndefined(); + }); it('filters branches with non-matching branch', () => { const config = { packageRules: [ @@ -238,6 +273,7 @@ describe('applyPackageRules()', () => { ], }; const dep = { + depType: 'dependencies', baseBranch: 'staging', }; const res = applyPackageRules({ ...config, ...dep }); diff --git a/website/docs/configuration-options.md b/website/docs/configuration-options.md index 71fa9491f5..b97962dbd4 100644 --- a/website/docs/configuration-options.md +++ b/website/docs/configuration-options.md @@ -585,6 +585,17 @@ Use this field to restrict rules to a particular package manager. e.g. }] ``` +### datasources + +Use this field to restrict rules to a particular datasource. e.g. + +``` + "packageRules": [{ + "datasources": ["orb"], + "labels": ["circleci-orb!!"] + }] +``` + ### matchCurrentVersion `matchCurrentVersion` can be an exact semver version or a semver range. -- GitLab