Skip to content

Commit

Permalink
fix(gomod): support .git indicator on private ee gitlab (#17105)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
bastianccm and viceice committed Aug 17, 2022
1 parent 67098bf commit 2bfe93a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
@@ -0,0 +1,10 @@
<html>
<head>
<meta name="go-import" content="my.custom.domain/golang/subgroup git https://my.custom.domain/golang/subgroup.git" />
<meta name="go-source"
content="my.custom.domain/golang/subgroup https://my.custom.domain/golang/subgroup https://my.custom.domain/golang/subgroup/-/tree/master{/dir} https://my.custom.domain/golang/subgroup/-/blob/master{/dir}/{file}#L{line}" />
</head>

<body>go get https://my.custom.domain/golang/subgroup</body>

</html>
35 changes: 35 additions & 0 deletions lib/modules/datasource/go/base.spec.ts
Expand Up @@ -148,6 +148,23 @@ describe('modules/datasource/go/base', () => {
});
});

it('does not fail for names containing .git', async () => {
httpMock
.scope('https://gitlab.com')
.get('/group/subgroup/my.git.module?go-get=1')
.reply(200, Fixtures.get('go-get-gitlab.html'));

const res = await BaseGoDatasource.getDatasource(
'gitlab.com/group/subgroup/my.git.module'
);

expect(res).toEqual({
datasource: GitlabTagsDatasource.id,
packageName: 'group/subgroup/my.git.module',
registryUrl: 'https://gitlab.com',
});
});

it('supports GitLab with URL mismatch', async () => {
const mismatchingResponse = Fixtures.get('go-get-github.html').replace(
'https://github.com/golang/text/',
Expand Down Expand Up @@ -238,6 +255,24 @@ describe('modules/datasource/go/base', () => {
});
});

it('supports GitLab EE deps in private subgroup with vcs indicator', async () => {
hostRules.find.mockReturnValue({ token: 'some-token' });
httpMock
.scope('https://my.custom.domain')
.get('/golang/subgroup/myrepo.git/v2?go-get=1')
.reply(200, Fixtures.get('go-get-gitlab-ee-private-subgroup.html'));

const res = await BaseGoDatasource.getDatasource(
'my.custom.domain/golang/subgroup/myrepo.git/v2'
);

expect(res).toEqual({
datasource: GitlabTagsDatasource.id,
packageName: 'golang/subgroup/myrepo',
registryUrl: 'https://my.custom.domain',
});
});

it('supports GitLab EE monorepo deps in subgroup', async () => {
hostRules.find.mockReturnValue({ token: 'some-token' });
httpMock
Expand Down
21 changes: 16 additions & 5 deletions lib/modules/datasource/go/base.ts
Expand Up @@ -20,6 +20,9 @@ export class BaseGoDatasource {
private static readonly gitlabRegExp = regEx(
/^(?<regExpUrl>gitlab\.[^/]*)\/(?<regExpPath>.+?)(?:\/v\d+)?[/]?$/
);
private static readonly gitVcsRegexp = regEx(
/^(?:[^/]+)\/(?<module>.*)\.git(?:$|\/)/
);

private static readonly id = 'go';
private static readonly http = new Http(BaseGoDatasource.id);
Expand Down Expand Up @@ -112,14 +115,12 @@ export class BaseGoDatasource {
BaseGoDatasource.gitlabRegExp.exec(goModule)?.groups?.regExpPath;
if (gitlabUrl && gitlabUrlName) {
if (gitlabModuleName?.startsWith(gitlabUrlName)) {
if (gitlabModuleName.includes('.git')) {
const vcsIndicatedModule = BaseGoDatasource.gitVcsRegexp.exec(goModule);
if (vcsIndicatedModule?.groups?.module) {
return {
datasource: GitlabTagsDatasource.id,
registryUrl: gitlabUrl,
packageName: gitlabModuleName.substring(
0,
gitlabModuleName.indexOf('.git')
),
packageName: vcsIndicatedModule.groups?.module,
};
}
return {
Expand Down Expand Up @@ -149,6 +150,16 @@ export class BaseGoDatasource {

const registryUrl = `${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) {
return {
datasource: GitlabTagsDatasource.id,
registryUrl,
packageName: vcsIndicatedModule.groups?.module,
};
}

return {
datasource: GitlabTagsDatasource.id,
registryUrl,
Expand Down

0 comments on commit 2bfe93a

Please sign in to comment.