From fba16b45f80fa4c1cbd58540fc6bb80bbe7db2f2 Mon Sep 17 00:00:00 2001
From: Tanuel <tanuel.mategi@gmail.com>
Date: Wed, 1 Apr 2020 22:09:37 +0200
Subject: [PATCH] fix(git-refs): git-refs always returning null (#5848)

---
 lib/datasource/git-refs/index.spec.ts |  2 +-
 lib/datasource/git-refs/index.ts      | 29 ++++++++++++++------
 lib/datasource/git-tags/index.spec.ts |  2 +-
 lib/datasource/git-tags/index.ts      | 38 ++++++++++++---------------
 4 files changed, 40 insertions(+), 31 deletions(-)

diff --git a/lib/datasource/git-refs/index.spec.ts b/lib/datasource/git-refs/index.spec.ts
index 92e389fca1..1466081d04 100644
--- a/lib/datasource/git-refs/index.spec.ts
+++ b/lib/datasource/git-refs/index.spec.ts
@@ -31,7 +31,7 @@ describe('datasource/git-refs', () => {
       simpleGit.mockReturnValue({
         listRemote() {
           return Promise.resolve(
-            'commithash1\trefs/tags/0.0.1\ncommithash2\trefs/tags/v0.0.2\ncommithash3\trefs/tags/v0.0.2^{}\ncommithash4\trefs/heads/v0.0.3\ncommithash5\trefs/tags/v0.0.3'
+            'commithash1\trefs/tags/0.0.1\ncommithash2\trefs/tags/v0.0.2\ncommithash3\trefs/tags/v0.0.2^{}\ncommithash4\trefs/heads/v0.0.3\ncommithash5\trefs/tags/v0.0.3\n'
           );
         },
       });
diff --git a/lib/datasource/git-refs/index.ts b/lib/datasource/git-refs/index.ts
index f7e774ceee..acf69abd18 100644
--- a/lib/datasource/git-refs/index.ts
+++ b/lib/datasource/git-refs/index.ts
@@ -32,23 +32,36 @@ export async function getRawRefs({
     }
 
     // fetch remote tags
-    const lsRemote = await git.listRemote([lookupName, '--sort=-v:refname']);
+    const lsRemote = await git.listRemote([
+      '--sort=-v:refname',
+      '--tags',
+      '--heads',
+      '--refs',
+      lookupName,
+    ]);
 
     if (!lsRemote) {
       return null;
     }
 
-    const refs = lsRemote.replace(/^.+?refs\//gm, '').split('\n');
+    const refs = lsRemote
+      .trim()
+      .replace(/^.+?refs\//gm, '')
+      .split('\n');
 
-    const result = refs.map(ref => ({
-      type: /(.*?)\//.exec(ref)[1],
-      value: /\/(.*)/.exec(ref)[1],
-    }));
+    const refMatch = /(?<type>\w+)\/(?<value>.*)/;
+    const result = refs.map(ref => {
+      const match = refMatch.exec(ref);
+      return {
+        type: match.groups.type,
+        value: match.groups.value,
+      };
+    });
 
     await renovateCache.set(cacheNamespace, lookupName, result, cacheMinutes);
     return result;
   } catch (err) {
-    logger.debug({ err }, `Git-Raw-Refs lookup error in ${lookupName}`);
+    logger.error({ err }, `Git-Raw-Refs lookup error in ${lookupName}`);
   }
   return null;
 }
@@ -78,7 +91,7 @@ export async function getPkgReleases({
 
     return result;
   } catch (err) {
-    logger.debug({ err }, `Git-Refs lookup error in ${lookupName}`);
+    logger.error({ err }, `Git-Refs lookup error in ${lookupName}`);
   }
   return null;
 }
diff --git a/lib/datasource/git-tags/index.spec.ts b/lib/datasource/git-tags/index.spec.ts
index ba55b79405..007d26c24b 100644
--- a/lib/datasource/git-tags/index.spec.ts
+++ b/lib/datasource/git-tags/index.spec.ts
@@ -31,7 +31,7 @@ describe('datasource/git-tags', () => {
       simpleGit.mockReturnValue({
         listRemote() {
           return Promise.resolve(
-            'commithash1\trefs/tags/0.0.1\ncommithash2\trefs/tags/v0.0.2\ncommithash3\trefs/tags/v0.0.2^{}'
+            'commithash1\trefs/tags/0.0.1\ncommithash2\trefs/tags/v0.0.2\ncommithash3\trefs/tags/v0.0.2^{}\n'
           );
         },
       });
diff --git a/lib/datasource/git-tags/index.ts b/lib/datasource/git-tags/index.ts
index 52caabcd90..94392a7297 100644
--- a/lib/datasource/git-tags/index.ts
+++ b/lib/datasource/git-tags/index.ts
@@ -1,6 +1,5 @@
 import { ReleaseResult, GetReleasesConfig } from '../common';
 import * as semver from '../../versioning/semver';
-import { logger } from '../../logger';
 import * as gitRefs from '../git-refs';
 
 export const id = 'git-tags';
@@ -8,28 +7,25 @@ export const id = 'git-tags';
 export async function getPkgReleases({
   lookupName,
 }: GetReleasesConfig): Promise<ReleaseResult | null> {
-  try {
-    // fetch remote tags
-    const rawRefs: gitRefs.RawRefs[] = await gitRefs.getRawRefs({ lookupName });
+  const rawRefs: gitRefs.RawRefs[] = await gitRefs.getRawRefs({ lookupName });
 
-    const tags = rawRefs
-      .filter(ref => ref.type === 'tags')
-      .map(ref => ref.value)
-      .filter(tag => semver.isVersion(tag));
+  if (rawRefs === null) {
+    return null;
+  }
+  const tags = rawRefs
+    .filter(ref => ref.type === 'tags')
+    .map(ref => ref.value)
+    .filter(tag => semver.isVersion(tag));
 
-    const sourceUrl = lookupName.replace(/\.git$/, '').replace(/\/$/, '');
+  const sourceUrl = lookupName.replace(/\.git$/, '').replace(/\/$/, '');
 
-    const result: ReleaseResult = {
-      sourceUrl,
-      releases: tags.map(tag => ({
-        version: tag,
-        gitRef: tag,
-      })),
-    };
+  const result: ReleaseResult = {
+    sourceUrl,
+    releases: tags.map(tag => ({
+      version: tag,
+      gitRef: tag,
+    })),
+  };
 
-    return result;
-  } catch (err) {
-    logger.debug({ err }, `Git-Tags lookup error in ${lookupName}`);
-  }
-  return null;
+  return result;
 }
-- 
GitLab