From 7db49b39e41a948887b1859a05ce24d3f877a79c Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Fri, 7 Jul 2023 09:47:04 +0200
Subject: [PATCH] feat: matchPackagePrefixes include packageName (#23186)

---
 docs/usage/configuration-options.md           |  3 +++
 .../package-rules/package-prefixes.spec.ts    | 26 +++++++++++++++++++
 lib/util/package-rules/package-prefixes.ts    | 20 ++++++++++++--
 3 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/docs/usage/configuration-options.md b/docs/usage/configuration-options.md
index 18d9aa4cfc..5e4077e9d6 100644
--- a/docs/usage/configuration-options.md
+++ b/docs/usage/configuration-options.md
@@ -2288,6 +2288,9 @@ See also `excludePackagePrefixes`.
 
 Like the earlier `matchPackagePatterns` example, the above will configure `rangeStrategy` to `replace` for any package starting with `angular`.
 
+`matchPackagePrefixes` will match against `packageName` first, and then `depName`, however `depName` matching is deprecated and will be removed in a future major release.
+If matching against `depName`, use `matchDepPatterns` instead.
+
 ### matchSourceUrlPrefixes
 
 Here's an example of where you use this to group together all packages from the `renovatebot` GitHub org:
diff --git a/lib/util/package-rules/package-prefixes.spec.ts b/lib/util/package-rules/package-prefixes.spec.ts
index a5640d5e64..cd0cd5f294 100644
--- a/lib/util/package-rules/package-prefixes.spec.ts
+++ b/lib/util/package-rules/package-prefixes.spec.ts
@@ -15,6 +15,32 @@ describe('util/package-rules/package-prefixes', () => {
       );
       expect(result).toBeFalse();
     });
+
+    it('should return true if packageName matched', () => {
+      const result = packagePrefixesMatcher.matches(
+        {
+          depName: 'abc1',
+          packageName: 'def1',
+        },
+        {
+          matchPackagePrefixes: ['def'],
+        }
+      );
+      expect(result).toBeTrue();
+    });
+
+    it('should return true but warn if depName matched', () => {
+      const result = packagePrefixesMatcher.matches(
+        {
+          depName: 'abc1',
+          packageName: 'def1',
+        },
+        {
+          matchPackagePrefixes: ['abc'],
+        }
+      );
+      expect(result).toBeTrue();
+    });
   });
 
   describe('exclude', () => {
diff --git a/lib/util/package-rules/package-prefixes.ts b/lib/util/package-rules/package-prefixes.ts
index 2df3ce7daf..0c1955d6f3 100644
--- a/lib/util/package-rules/package-prefixes.ts
+++ b/lib/util/package-rules/package-prefixes.ts
@@ -1,20 +1,36 @@
 import is from '@sindresorhus/is';
 import type { PackageRule, PackageRuleInputConfig } from '../../config/types';
+import { logger } from '../../logger';
 import { Matcher } from './base';
 
 export class PackagePrefixesMatcher extends Matcher {
   override matches(
-    { depName }: PackageRuleInputConfig,
+    { depName, packageName }: PackageRuleInputConfig,
     { matchPackagePrefixes }: PackageRule
   ): boolean | null {
     if (is.undefined(matchPackagePrefixes)) {
       return null;
     }
+
     if (is.undefined(depName)) {
       return false;
     }
 
-    return matchPackagePrefixes.some((prefix) => depName.startsWith(prefix));
+    if (
+      is.string(packageName) &&
+      matchPackagePrefixes.some((prefix) => packageName.startsWith(prefix))
+    ) {
+      return true;
+    }
+    if (matchPackagePrefixes.some((prefix) => depName.startsWith(prefix))) {
+      logger.once.warn(
+        { packageName, depName },
+        'Use matchDepPatterns instead of matchPackagePrefixes'
+      );
+      return true;
+    }
+
+    return false;
   }
 
   override excludes(
-- 
GitLab