From 01acebd82d589f1d4e4e3d00557b102eb2d015de Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Thu, 18 Nov 2021 15:50:53 +0100
Subject: [PATCH] fix(datasource): better massage github sourceUrl (#12737)

---
 .../__snapshots__/metadata.spec.ts.snap       | 24 +++++++++++++++++++
 lib/datasource/metadata.spec.ts               | 22 +++++++++++++++++
 lib/datasource/metadata.ts                    |  8 ++++++-
 3 files changed, 53 insertions(+), 1 deletion(-)

diff --git a/lib/datasource/__snapshots__/metadata.spec.ts.snap b/lib/datasource/__snapshots__/metadata.spec.ts.snap
index d114fca01e..8c58352c4e 100644
--- a/lib/datasource/__snapshots__/metadata.spec.ts.snap
+++ b/lib/datasource/__snapshots__/metadata.spec.ts.snap
@@ -135,3 +135,27 @@ Object {
   "sourceUrl": "https://gitlab.com/meno/dropzone",
 }
 `;
+
+exports[`datasource/metadata Should massage github sourceUrls 1`] = `
+Object {
+  "releases": Array [
+    Object {
+      "releaseTimestamp": "2018-07-13T10:14:17.000Z",
+      "version": "2.0.0",
+    },
+    Object {
+      "releaseTimestamp": "2017-10-24T10:09:16.000Z",
+      "version": "2.0.0.dev1",
+    },
+    Object {
+      "releaseTimestamp": "2019-01-20T19:59:28.000Z",
+      "version": "2.1.0",
+    },
+    Object {
+      "releaseTimestamp": "2019-07-16T18:29:00.000Z",
+      "version": "2.2.0",
+    },
+  ],
+  "sourceUrl": "https://github.com/some/repo",
+}
+`;
diff --git a/lib/datasource/metadata.spec.ts b/lib/datasource/metadata.spec.ts
index 71ff2932c4..81fceb357f 100644
--- a/lib/datasource/metadata.spec.ts
+++ b/lib/datasource/metadata.spec.ts
@@ -76,6 +76,28 @@ describe('datasource/metadata', () => {
     });
   });
 
+  it('Should massage github sourceUrls', () => {
+    const dep: ReleaseResult = {
+      sourceUrl: 'https://some.github.com/repo',
+      releases: [
+        { version: '2.0.0', releaseTimestamp: '2018-07-13T10:14:17.000Z' },
+        {
+          version: '2.0.0.dev1',
+          releaseTimestamp: '2017-10-24T10:09:16.000Z',
+        },
+        { version: '2.1.0', releaseTimestamp: '2019-01-20T19:59:28.000Z' },
+        { version: '2.2.0', releaseTimestamp: '2019-07-16T18:29:00.000Z' },
+      ],
+    };
+    const datasource = PypiDatasource.id;
+    const lookupName = 'django-filter';
+
+    addMetaData(dep, datasource, lookupName);
+    expect(dep).toMatchSnapshot({
+      sourceUrl: 'https://github.com/some/repo',
+    });
+  });
+
   it('Should handle parsing of sourceUrls correctly for GitLab also', () => {
     const dep: ReleaseResult = {
       sourceUrl: 'https://gitlab.com/meno/dropzone/tree/master',
diff --git a/lib/datasource/metadata.ts b/lib/datasource/metadata.ts
index 95b611da71..d9685047d4 100644
--- a/lib/datasource/metadata.ts
+++ b/lib/datasource/metadata.ts
@@ -107,10 +107,16 @@ const manualSourceUrls = {
   },
 };
 
+const githubPages = regEx('^https://([^.]+).github.com/([^/]+)$');
+const gitPrefix = regEx('^git:/?/?');
+
 function massageGithubUrl(url: string): string {
   return url
     .replace('http:', 'https:')
-    .replace(regEx(/^git:\/?\/?/), 'https://')
+    .replace('http+git:', 'https:')
+    .replace('https+git:', 'https:')
+    .replace(gitPrefix, 'https://')
+    .replace(githubPages, 'https://github.com/$1/$2')
     .replace('www.github.com', 'github.com')
     .split('/')
     .slice(0, 5)
-- 
GitLab