From 248d7c7719bcd2072f68206ca30ad8c3df4eb6e7 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Sun, 27 Jun 2021 10:01:06 +0200
Subject: [PATCH] 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>
---
 lib/util/package-rules.spec.ts | 19 +++++++++++++++++++
 lib/util/package-rules.ts      | 10 +++++++++-
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/lib/util/package-rules.spec.ts b/lib/util/package-rules.spec.ts
index 5bd2d52ae0..a4774a4684 100644
--- a/lib/util/package-rules.spec.ts
+++ b/lib/util/package-rules.spec.ts
@@ -714,4 +714,23 @@ describe('applyPackageRules()', () => {
       applyPackageRules({ ...config1, packageRules: null })
     ).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');
+  });
 });
diff --git a/lib/util/package-rules.ts b/lib/util/package-rules.ts
index 413e9df0f3..1fa2903377 100644
--- a/lib/util/package-rules.ts
+++ b/lib/util/package-rules.ts
@@ -1,5 +1,6 @@
 import is from '@sindresorhus/is';
 import minimatch from 'minimatch';
+import slugify from 'slugify';
 import { mergeChildConfig } from '../config';
 import type { PackageRule, PackageRuleInputConfig } from '../config/types';
 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
     if (matchesRule(config, packageRule)) {
       // 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.matchPackagePatterns;
       delete config.matchPackagePrefixes;
-- 
GitLab