fix(package-rules): add groupSlug to matched package rule if necessary (#10621)

* fix(package-rules): add groupSlug to matched package rule if necessary

* Update lib/util/package-rules.spec.ts

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
This commit is contained in:
Rhys Arkins 2021-06-27 10:01:06 +02:00 committed by GitHub
parent 3be44469bb
commit 248d7c7719
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 1 deletions

View file

@ -714,4 +714,23 @@ describe('applyPackageRules()', () => {
applyPackageRules({ ...config1, packageRules: null }) applyPackageRules({ ...config1, packageRules: null })
).toMatchSnapshot(); ).toMatchSnapshot();
}); });
it('creates groupSlug if necessary', () => {
const config: TestConfig = {
depName: 'foo',
packageRules: [
{
matchPackagePatterns: ['*'],
groupName: 'A',
groupSlug: 'a',
},
{
matchPackagePatterns: ['*'],
groupName: 'B',
},
],
};
const res = applyPackageRules(config);
expect(res.groupSlug).toEqual('b');
});
}); });

View file

@ -1,5 +1,6 @@
import is from '@sindresorhus/is'; import is from '@sindresorhus/is';
import minimatch from 'minimatch'; import minimatch from 'minimatch';
import slugify from 'slugify';
import { mergeChildConfig } from '../config'; import { mergeChildConfig } from '../config';
import type { PackageRule, PackageRuleInputConfig } from '../config/types'; import type { PackageRule, PackageRuleInputConfig } from '../config/types';
import { logger } from '../logger'; import { logger } from '../logger';
@ -262,7 +263,14 @@ export function applyPackageRules<T extends PackageRuleInputConfig>(
// This rule is considered matched if there was at least one positive match and no negative matches // This rule is considered matched if there was at least one positive match and no negative matches
if (matchesRule(config, packageRule)) { if (matchesRule(config, packageRule)) {
// Package rule config overrides any existing config // Package rule config overrides any existing config
config = mergeChildConfig(config, packageRule); const toApply = { ...packageRule };
if (config.groupSlug && packageRule.groupName && !packageRule.groupSlug) {
// Need to apply groupSlug otherwise the existing one will take precedence
toApply.groupSlug = slugify(packageRule.groupName, {
lower: true,
});
}
config = mergeChildConfig(config, toApply);
delete config.matchPackageNames; delete config.matchPackageNames;
delete config.matchPackagePatterns; delete config.matchPackagePatterns;
delete config.matchPackagePrefixes; delete config.matchPackagePrefixes;