From 25021725a920cfc452bc5256ad7ca0ea8beb0418 Mon Sep 17 00:00:00 2001
From: Sergei Zharinov <zharinov@users.noreply.github.com>
Date: Thu, 9 Feb 2023 15:11:21 +0300
Subject: [PATCH] fix(go): Avoid tag fetching for `v0.0.0` (#20307)

---
 lib/modules/datasource/go/index.spec.ts      | 45 +++++++++++++++++---
 lib/modules/datasource/go/index.ts           |  5 ++-
 lib/modules/datasource/go/releases-direct.ts |  6 +--
 3 files changed, 45 insertions(+), 11 deletions(-)

diff --git a/lib/modules/datasource/go/index.spec.ts b/lib/modules/datasource/go/index.spec.ts
index 6f5575968b..29246de56a 100644
--- a/lib/modules/datasource/go/index.spec.ts
+++ b/lib/modules/datasource/go/index.spec.ts
@@ -17,11 +17,13 @@ jest.mock('./releases-direct', () => {
   return {
     GoDirectDatasource: jest.fn().mockImplementation(() => {
       return {
-        git: { getDigest: () => getDigestGitMock() },
-        github: { getDigest: () => getDigestGithubMock() },
-        gitlab: { getDigest: () => getDigestGitlabMock() },
-        bitbucket: { getDigest: () => getDigestBitbucketMock() },
-        getReleases: () => getReleasesDirectMock(),
+        git: { getDigest: (...args: any[]) => getDigestGitMock(...args) },
+        github: { getDigest: (...args: any[]) => getDigestGithubMock(...args) },
+        gitlab: { getDigest: (...args: any[]) => getDigestGitlabMock(...args) },
+        bitbucket: {
+          getDigest: (...args: any[]) => getDigestBitbucketMock(...args),
+        },
+        getReleases: (...args: any[]) => getReleasesDirectMock(...args),
       };
     }),
   };
@@ -158,7 +160,7 @@ describe('modules/datasource/go/index', () => {
       expect(res).toBe('abcdefabcdefabcdefabcdef');
     });
 
-    it('returns digest', async () => {
+    it('returns github digest', async () => {
       httpMock
         .scope('https://golang.org/')
         .get('/x/text?go-get=1')
@@ -166,9 +168,38 @@ describe('modules/datasource/go/index', () => {
       getDigestGithubMock.mockResolvedValueOnce('abcdefabcdefabcdefabcdef');
       const res = await datasource.getDigest(
         { packageName: 'golang.org/x/text' },
-        null
+        'v1.2.3'
       );
       expect(res).toBe('abcdefabcdefabcdefabcdef');
+      expect(getDigestGithubMock).toHaveBeenCalledWith(
+        {
+          datasource: 'github-tags',
+          packageName: 'golang/text',
+          registryUrl: 'https://github.com',
+        },
+        'v1.2.3'
+      );
+    });
+
+    it('returns github default branch digest', async () => {
+      httpMock
+        .scope('https://golang.org/')
+        .get('/x/text?go-get=1')
+        .reply(200, Fixtures.get('go-get-github.html'));
+      getDigestGithubMock.mockResolvedValueOnce('abcdefabcdefabcdefabcdef');
+      const res = await datasource.getDigest(
+        { packageName: 'golang.org/x/text' },
+        'v0.0.0'
+      );
+      expect(res).toBe('abcdefabcdefabcdefabcdef');
+      expect(getDigestGithubMock).toHaveBeenCalledWith(
+        {
+          datasource: 'github-tags',
+          packageName: 'golang/text',
+          registryUrl: 'https://github.com',
+        },
+        undefined
+      );
     });
 
     it('support bitbucket digest', async () => {
diff --git a/lib/modules/datasource/go/index.ts b/lib/modules/datasource/go/index.ts
index 8bd8f4629a..6bd4934a63 100644
--- a/lib/modules/datasource/go/index.ts
+++ b/lib/modules/datasource/go/index.ts
@@ -65,8 +65,11 @@ export class GoDatasource extends Datasource {
     }
 
     // ignore vX.Y.Z-(0.)? pseudo versions that are used Go Modules - look up default branch instead
+    // ignore v0.0.0 versions to fetch the digest of default branch, not the commit of non-existing tag `v0.0.0`
     const tag =
-      value && !GoDatasource.pversionRegexp.test(value) ? value : undefined;
+      value && !GoDatasource.pversionRegexp.test(value) && value !== 'v0.0.0'
+        ? value
+        : undefined;
 
     switch (source.datasource) {
       case GitTagsDatasource.id: {
diff --git a/lib/modules/datasource/go/releases-direct.ts b/lib/modules/datasource/go/releases-direct.ts
index 30f5068c7e..07171bef6f 100644
--- a/lib/modules/datasource/go/releases-direct.ts
+++ b/lib/modules/datasource/go/releases-direct.ts
@@ -6,7 +6,7 @@ import { Datasource } from '../datasource';
 import { GitTagsDatasource } from '../git-tags';
 import { GithubTagsDatasource } from '../github-tags';
 import { GitlabTagsDatasource } from '../gitlab-tags';
-import type { DatasourceApi, GetReleasesConfig, ReleaseResult } from '../types';
+import type { GetReleasesConfig, ReleaseResult } from '../types';
 import { BaseGoDatasource } from './base';
 import { getSourceUrl } from './common';
 
@@ -15,8 +15,8 @@ export class GoDirectDatasource extends Datasource {
 
   git: GitTagsDatasource;
   github: GithubTagsDatasource;
-  gitlab: DatasourceApi;
-  bitbucket: DatasourceApi;
+  gitlab: GitlabTagsDatasource;
+  bitbucket: BitBucketTagsDatasource;
 
   constructor() {
     super(GoDirectDatasource.id);
-- 
GitLab