Skip to content

Commit

Permalink
fix(datasource/go): Fixes splitting of gitlab go package name (#26477)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
ManuelB and viceice committed Jan 10, 2024
1 parent 9ce2d94 commit 54a0233
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
42 changes: 42 additions & 0 deletions lib/modules/datasource/go/base.spec.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -34,6 +35,7 @@ describe('modules/datasource/go/base', () => {
beforeEach(() => {
hostRules.find.mockReturnValue({});
hostRules.hosts.mockReturnValue([]);
GlobalConfig.reset();
});

describe('meta name=go-source', () => {
Expand Down Expand Up @@ -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 =
'<meta name="go-import" content="example.com/gitlab/my-project/my-repo.git git https://example.com/gitlab/my-project/my-repo" />';
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/',
});
});
});
});
});
27 changes: 24 additions & 3 deletions 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';
Expand Down Expand Up @@ -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,
};
}

Expand Down

0 comments on commit 54a0233

Please sign in to comment.