From 54a02330967fce657ca3e044dc171423ddc90fd3 Mon Sep 17 00:00:00 2001 From: Manuel Blechschmidt Date: Wed, 10 Jan 2024 17:21:51 +0100 Subject: [PATCH] fix(datasource/go): Fixes splitting of gitlab go package name (#26477) Co-authored-by: Michael Kriese --- lib/modules/datasource/go/base.spec.ts | 42 ++++++++++++++++++++++++++ lib/modules/datasource/go/base.ts | 27 +++++++++++++++-- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/lib/modules/datasource/go/base.spec.ts b/lib/modules/datasource/go/base.spec.ts index 4ef0db7ab6a83f..7bfbe623ae5589 100644 --- a/lib/modules/datasource/go/base.spec.ts +++ b/lib/modules/datasource/go/base.spec.ts @@ -2,6 +2,7 @@ import { mockDeep } from 'jest-mock-extended'; import { Fixtures } from '../../../../test/fixtures'; import * as httpMock from '../../../../test/http-mock'; import { mocked } from '../../../../test/util'; +import { GlobalConfig } from '../../../config/global'; import * as _hostRules from '../../../util/host-rules'; import { GitTagsDatasource } from '../git-tags'; import { GithubTagsDatasource } from '../github-tags'; @@ -34,6 +35,7 @@ describe('modules/datasource/go/base', () => { beforeEach(() => { hostRules.find.mockReturnValue({}); hostRules.hosts.mockReturnValue([]); + GlobalConfig.reset(); }); describe('meta name=go-source', () => { @@ -394,6 +396,46 @@ describe('modules/datasource/go/base', () => { expect(res).toBeNull(); }); + + it('it correctly splits a URL where the endpoint is contained', async () => { + hostRules.hostType.mockReturnValue('gitlab'); + + GlobalConfig.set({ endpoint: 'https://example.com/gitlab/api/v4/' }); + + const meta = + ''; + httpMock + .scope('https://example.com') + .get('/gitlab/my-project/my-repo.git?go-get=1') + .reply(200, meta); + + const res = await BaseGoDatasource.getDatasource( + 'example.com/gitlab/my-project/my-repo.git', + ); + + expect(res).toEqual({ + datasource: GitlabTagsDatasource.id, + packageName: 'my-project/my-repo', + registryUrl: 'https://example.com/gitlab/', + }); + + GlobalConfig.set({ endpoint: 'https://example.com/gitlab/' }); + + httpMock + .scope('https://example.com') + .get('/gitlab/my-project/my-repo.git?go-get=1') + .reply(200, meta); + + const res2 = await BaseGoDatasource.getDatasource( + 'example.com/gitlab/my-project/my-repo.git', + ); + + expect(res2).toEqual({ + datasource: GitlabTagsDatasource.id, + packageName: 'my-project/my-repo', + registryUrl: 'https://example.com/gitlab/', + }); + }); }); }); }); diff --git a/lib/modules/datasource/go/base.ts b/lib/modules/datasource/go/base.ts index 876f8745eb7c3b..f109920d3d1a3b 100644 --- a/lib/modules/datasource/go/base.ts +++ b/lib/modules/datasource/go/base.ts @@ -1,5 +1,6 @@ // TODO: types (#22198) import URL from 'node:url'; +import { GlobalConfig } from '../../../config/global'; import { logger } from '../../../logger'; import { detectPlatform } from '../../../util/common'; import * as hostRules from '../../../util/host-rules'; @@ -154,17 +155,37 @@ export class BaseGoDatasource { const parsedUrl = URL.parse(goSourceUrl); // TODO: `parsedUrl.pathname` can be undefined - const packageName = trimLeadingSlash(`${parsedUrl.pathname}`); + let packageName = trimLeadingSlash(`${parsedUrl.pathname}`); - const registryUrl = `${parsedUrl.protocol}//${parsedUrl.host}`; + const endpoint = GlobalConfig.get('endpoint')!; + + const endpointPrefix = regEx('https://[^/]*/(.*?/)(api/v4/?)?').exec( + endpoint, + ); + + if (endpointPrefix) { + packageName = packageName.replace(endpointPrefix[1], ''); + } + + const registryUrl = endpointPrefix + ? endpoint.replace(regEx('api/v4/?$'), '') + : `${parsedUrl.protocol}//${parsedUrl.host}`; // a .git path indicates a concrete git repository, which can be different from metadata returned by gitlab const vcsIndicatedModule = BaseGoDatasource.gitVcsRegexp.exec(goModule); if (vcsIndicatedModule?.groups?.module) { + if (endpointPrefix) { + packageName = vcsIndicatedModule.groups?.module.replace( + endpointPrefix[1], + '', + ); + } else { + packageName = vcsIndicatedModule.groups?.module; + } return { datasource: GitlabTagsDatasource.id, registryUrl, - packageName: vcsIndicatedModule.groups?.module, + packageName, }; }