From b00cca0f1a7f3d21237525f9a518eed7ac58fd5f Mon Sep 17 00:00:00 2001
From: Yanis Benson <yanis.benson@gmail.com>
Date: Mon, 6 Sep 2021 17:04:09 +0300
Subject: [PATCH] fix(manager/npm): support git@github.com repository urls
 (#11362)

---
 .../extract/__snapshots__/index.spec.ts.snap  | 13 +++++++
 lib/manager/npm/extract/index.spec.ts         |  1 +
 lib/manager/npm/extract/index.ts              | 34 +++++++++++++------
 3 files changed, 38 insertions(+), 10 deletions(-)

diff --git a/lib/manager/npm/extract/__snapshots__/index.spec.ts.snap b/lib/manager/npm/extract/__snapshots__/index.spec.ts.snap
index 5037b09dd1..27ab170d49 100644
--- a/lib/manager/npm/extract/__snapshots__/index.spec.ts.snap
+++ b/lib/manager/npm/extract/__snapshots__/index.spec.ts.snap
@@ -296,6 +296,19 @@ Object {
       "prettyDepType": "dependency",
       "sourceUrl": "https://github.com/owner/n",
     },
+    Object {
+      "currentRawValue": "git@github.com:owner/o.git#v2.0.0",
+      "currentValue": "v2.0.0",
+      "datasource": "github-tags",
+      "depName": "o",
+      "depType": "dependencies",
+      "gitRef": true,
+      "githubRepo": "owner/o",
+      "lookupName": "owner/o",
+      "pinDigests": false,
+      "prettyDepType": "dependency",
+      "sourceUrl": "https://github.com/owner/o",
+    },
   ],
   "lernaClient": undefined,
   "lernaPackages": undefined,
diff --git a/lib/manager/npm/extract/index.spec.ts b/lib/manager/npm/extract/index.spec.ts
index f67f4420b6..e513d19e92 100644
--- a/lib/manager/npm/extract/index.spec.ts
+++ b/lib/manager/npm/extract/index.spec.ts
@@ -332,6 +332,7 @@ describe('manager/npm/extract/index', () => {
           l: 'github:owner/l.git#abcdef0',
           m: 'https://github.com/owner/m.git#v1.0.0',
           n: 'git+https://github.com/owner/n#v2.0.0',
+          o: 'git@github.com:owner/o.git#v2.0.0',
         },
       };
       const pJsonStr = JSON.stringify(pJson);
diff --git a/lib/manager/npm/extract/index.ts b/lib/manager/npm/extract/index.ts
index 7b2156cfce..4cf3967905 100644
--- a/lib/manager/npm/extract/index.ts
+++ b/lib/manager/npm/extract/index.ts
@@ -30,6 +30,9 @@ function parseDepName(depType: string, key: string): string {
   return depName;
 }
 
+const RE_REPOSITORY_GITHUB_SSH_FORMAT =
+  /(?:git@)github.com:([^/]+)\/([^/.]+)(?:\.git)?/;
+
 export async function extractPackageFile(
   content: string,
   fileName: string,
@@ -258,17 +261,28 @@ export async function extractPackageFile(
       return dep;
     }
     const [depNamePart, depRefPart] = hashSplit;
-    const githubOwnerRepo = depNamePart
-      .replace(/^github:/, '')
-      .replace(/^git\+/, '')
-      .replace(/^https:\/\/github\.com\//, '')
-      .replace(/\.git$/, '');
-    const githubRepoSplit = githubOwnerRepo.split('/');
-    if (githubRepoSplit.length !== 2) {
-      dep.skipReason = SkipReason.UnknownVersion;
-      return dep;
+
+    let githubOwnerRepo: string;
+    let githubOwner: string;
+    let githubRepo: string;
+    const matchUrlSshFormat = RE_REPOSITORY_GITHUB_SSH_FORMAT.exec(depNamePart);
+    if (matchUrlSshFormat === null) {
+      githubOwnerRepo = depNamePart
+        .replace(/^github:/, '')
+        .replace(/^git\+/, '')
+        .replace(/^https:\/\/github\.com\//, '')
+        .replace(/\.git$/, '');
+      const githubRepoSplit = githubOwnerRepo.split('/');
+      if (githubRepoSplit.length !== 2) {
+        dep.skipReason = SkipReason.UnknownVersion;
+        return dep;
+      }
+      [githubOwner, githubRepo] = githubRepoSplit;
+    } else {
+      githubOwner = matchUrlSshFormat[1];
+      githubRepo = matchUrlSshFormat[2];
+      githubOwnerRepo = `${githubOwner}/${githubRepo}`;
     }
-    const [githubOwner, githubRepo] = githubRepoSplit;
     const githubValidRegex = /^[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}$/;
     if (
       !githubValidRegex.test(githubOwner) ||
-- 
GitLab