From 9aaadd1f4ad97dfee80f7ce46b39900f5c0e4a23 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Sat, 8 May 2021 15:57:14 +0200
Subject: [PATCH] feat: sourceRepoSlug (#9916)

---
 lib/util/template/index.ts                    |  1 +
 .../repository/updates/flatten.spec.ts        | 30 +++++++++++++++++--
 lib/workers/repository/updates/flatten.ts     | 10 +++++++
 3 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/lib/util/template/index.ts b/lib/util/template/index.ts
index 9cf66eb151..8eb152a074 100644
--- a/lib/util/template/index.ts
+++ b/lib/util/template/index.ts
@@ -88,6 +88,7 @@ export const allowedFields = {
   releaseNotes: 'A ChangeLogNotes object for the release',
   repository: 'The current repository',
   semanticPrefix: 'The fully generated semantic prefix for commit messages',
+  sourceRepoSlug: 'The slugified pathname of the sourceUrl, if present',
   sourceUrl: 'The source URL for the package',
   updateType: 'One of digest, pin, rollback, patch, minor, major',
   upgrades: 'An array of upgrade objects in the branch',
diff --git a/lib/workers/repository/updates/flatten.spec.ts b/lib/workers/repository/updates/flatten.spec.ts
index 27ec2dbdd9..984673ed05 100644
--- a/lib/workers/repository/updates/flatten.spec.ts
+++ b/lib/workers/repository/updates/flatten.spec.ts
@@ -44,8 +44,24 @@ describe(getName(), () => {
             packageFile: 'package.json',
             lockFiles: ['package-lock.json'],
             deps: [
-              { depName: '@org/a', updates: [{ newValue: '1.0.0' }] },
-              { depName: 'foo', updates: [{ newValue: '2.0.0' }] },
+              {
+                depName: '@org/a',
+                updates: [
+                  {
+                    newValue: '1.0.0',
+                    sourceUrl: 'https://github.com/org/repo',
+                  },
+                ],
+              },
+              {
+                depName: 'foo',
+                updates: [
+                  {
+                    newValue: '2.0.0',
+                    sourceUrl: 'https://github.com/org/repo',
+                  },
+                ],
+              },
               {
                 updateTypes: ['pin'],
                 updates: [{ newValue: '2.0.0' }],
@@ -54,7 +70,12 @@ describe(getName(), () => {
           },
           {
             packageFile: 'backend/package.json',
-            deps: [{ depName: 'bar', updates: [{ newValue: '3.0.0' }] }],
+            deps: [
+              {
+                depName: 'bar',
+                updates: [{ newValue: '3.0.0', sourceUrl: 3 }],
+              },
+            ],
           },
           {
             packageFile: 'frontend/package.json',
@@ -68,6 +89,7 @@ describe(getName(), () => {
               {
                 depName: 'amd64/node',
                 language: LANGUAGE_DOCKER,
+                sourceUrl: 'https://github.com/nodejs/node',
                 updates: [{ newValue: '10.0.1' }],
               },
             ],
@@ -78,6 +100,7 @@ describe(getName(), () => {
               {
                 depName: 'calico/node',
                 language: LANGUAGE_DOCKER,
+                sourceUrl: 'https://calico.com',
                 updates: [{ newValue: '3.2.0', updateType: 'minor' }],
               },
             ],
@@ -109,6 +132,7 @@ describe(getName(), () => {
       };
       const res = await flattenUpdates(config, packageFiles);
       expect(res).toHaveLength(13);
+      expect(res.filter((update) => update.sourceRepoSlug)).toHaveLength(3);
       expect(
         res.filter((r) => r.updateType === 'lockFileMaintenance')
       ).toHaveLength(2);
diff --git a/lib/workers/repository/updates/flatten.ts b/lib/workers/repository/updates/flatten.ts
index 7c7ef30088..a6becaed19 100644
--- a/lib/workers/repository/updates/flatten.ts
+++ b/lib/workers/repository/updates/flatten.ts
@@ -7,6 +7,7 @@ import type { RenovateConfig } from '../../../config/types';
 import { getDefaultConfig } from '../../../datasource';
 import { get } from '../../../manager';
 import { applyPackageRules } from '../../../util/package-rules';
+import { parseUrl } from '../../../util/url';
 import type { BranchUpgradeConfig } from '../../types';
 import { generateBranchName } from './branch-name';
 
@@ -26,6 +27,15 @@ export function applyUpdateConfig(input: BranchUpgradeConfig): any {
         .replace(/-+/, '-')
         .toLowerCase()
     : undefined;
+  if (updateConfig.sourceUrl) {
+    const parsedSourceUrl = parseUrl(updateConfig.sourceUrl);
+    if (parsedSourceUrl?.pathname) {
+      updateConfig.sourceRepoSlug = parsedSourceUrl.pathname
+        .replace(/^\//, '') // remove leading slash
+        .replace(/\//g, '-') // change slashes to hyphens
+        .replace(/-+/g, '-'); // remove multiple hyphens
+    }
+  }
   generateBranchName(updateConfig);
   return updateConfig;
 }
-- 
GitLab