From 49f5ac0b9b3105cd69cf91ff9eb759e9b233f0f1 Mon Sep 17 00:00:00 2001
From: elmaso1620 <elmaso1620@users.noreply.github.com>
Date: Fri, 21 Jan 2022 12:03:06 +0000
Subject: [PATCH] feat: expose sourceRepo, sourceRepoOrg and sourceRepoName
 (#13620)

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
Co-authored-by: Rhys Arkins <rhys@arkins.net>
---
 lib/util/template/index.ts                    |  3 ++
 .../repository/updates/flatten.spec.ts        | 37 ++++++++++++++++++-
 lib/workers/repository/updates/flatten.ts     | 12 ++++++
 lib/workers/types.ts                          |  3 ++
 4 files changed, 54 insertions(+), 1 deletion(-)

diff --git a/lib/util/template/index.ts b/lib/util/template/index.ts
index a57d8fd43e..833198b904 100644
--- a/lib/util/template/index.ts
+++ b/lib/util/template/index.ts
@@ -116,6 +116,9 @@ export const allowedFields = {
   releaseNotes: 'A ChangeLogNotes object for the release',
   repository: 'The current repository',
   semanticPrefix: 'The fully generated semantic prefix for commit messages',
+  sourceRepo: 'The repository in the sourceUrl, if present',
+  sourceRepoName: 'The repository name in the sourceUrl, if present',
+  sourceRepoOrg: 'The repository organization in the sourceUrl, if present',
   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, replacement',
diff --git a/lib/workers/repository/updates/flatten.spec.ts b/lib/workers/repository/updates/flatten.spec.ts
index 84c3a997eb..c6868350ae 100644
--- a/lib/workers/repository/updates/flatten.spec.ts
+++ b/lib/workers/repository/updates/flatten.spec.ts
@@ -142,7 +142,42 @@ describe('workers/repository/updates/flatten', () => {
       };
       const res = await flattenUpdates(config, packageFiles);
       expect(res).toHaveLength(14);
-      expect(res.filter((update) => update.sourceRepoSlug)).toHaveLength(3);
+      expect(
+        res.filter((update) => update.sourceRepoSlug)[0].sourceRepoSlug
+      ).toBe('org-repo');
+      expect(res.filter((update) => update.sourceRepo)[0].sourceRepo).toBe(
+        'org/repo'
+      );
+      expect(
+        res.filter((update) => update.sourceRepoOrg)[0].sourceRepoOrg
+      ).toBe('org');
+      expect(
+        res.filter((update) => update.sourceRepoName)[0].sourceRepoName
+      ).toBe('repo');
+      expect(
+        res.filter((update) => update.sourceRepoSlug)[1].sourceRepoSlug
+      ).toBe('org-repo');
+      expect(res.filter((update) => update.sourceRepo)[1].sourceRepo).toBe(
+        'org/repo'
+      );
+      expect(
+        res.filter((update) => update.sourceRepoOrg)[1].sourceRepoOrg
+      ).toBe('org');
+      expect(
+        res.filter((update) => update.sourceRepoName)[1].sourceRepoName
+      ).toBe('repo');
+      expect(
+        res.filter((update) => update.sourceRepoSlug)[2].sourceRepoSlug
+      ).toBe('nodejs-node');
+      expect(res.filter((update) => update.sourceRepo)[2].sourceRepo).toBe(
+        'nodejs/node'
+      );
+      expect(
+        res.filter((update) => update.sourceRepoOrg)[2].sourceRepoOrg
+      ).toBe('nodejs');
+      expect(
+        res.filter((update) => update.sourceRepoName)[2].sourceRepoName
+      ).toBe('node');
       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 36cd141140..36ddcc1c76 100644
--- a/lib/workers/repository/updates/flatten.ts
+++ b/lib/workers/repository/updates/flatten.ts
@@ -42,6 +42,18 @@ export function applyUpdateConfig(input: BranchUpgradeConfig): any {
         .replace(regEx(/^\//), '') // remove leading slash
         .replace(regEx(/\//g), '-') // change slashes to hyphens
         .replace(regEx(/-+/g), '-'); // remove multiple hyphens
+      updateConfig.sourceRepo = parsedSourceUrl.pathname.replace(
+        regEx(/^\//),
+        ''
+      ); // remove leading slash
+      updateConfig.sourceRepoOrg = updateConfig.sourceRepo.replace(
+        regEx(/\/.*/g),
+        ''
+      ); // remove everything after first slash
+      updateConfig.sourceRepoName = updateConfig.sourceRepo.replace(
+        regEx(/.*\//g),
+        ''
+      ); // remove everything up to the last slash
     }
   }
   generateBranchName(updateConfig);
diff --git a/lib/workers/types.ts b/lib/workers/types.ts
index d311d0c18a..a914885830 100644
--- a/lib/workers/types.ts
+++ b/lib/workers/types.ts
@@ -69,6 +69,9 @@ export interface BranchUpgradeConfig
   changelogUrl?: string;
   dependencyUrl?: string;
   sourceUrl?: string;
+  sourceRepo?: string;
+  sourceRepoOrg?: string;
+  sourceRepoName?: string;
 }
 
 export type PrBlockedBy =
-- 
GitLab