Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(go): Detect repository name from the metadata URL instead of module #30388

Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE html>
<html>
<head>
<meta name="go-import" content="gitlab.com/example/module git https://gitlab.com/example/module.git">
<meta name="go-source" content="gitlab.com/example/module https://gitlab.com/example/module https://gitlab.com/example/module/-/tree/master{/dir} https://gitlab.com/example/module/-/blob/master{/dir}/{file}#L{line}">
</head>
<body>go get https://gitlab.com/example/module</body>
</html>
19 changes: 18 additions & 1 deletion lib/modules/datasource/go/base.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,23 @@ describe('modules/datasource/go/base', () => {
});
});

it('supports Go submodules in GitLab repo', async () => {
httpMock
.scope('https://gitlab.com')
.get('/example/module/submodule?go-get=1')
.reply(200, Fixtures.get('go-get-submodule-gitlab.html'));

const res = await BaseGoDatasource.getDatasource(
'gitlab.com/example/module/submodule',
);

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

it('supports GitLab deps', async () => {
httpMock
.scope('https://gitlab.com')
Expand Down Expand Up @@ -163,7 +180,7 @@ describe('modules/datasource/go/base', () => {

expect(res).toEqual({
datasource: GitlabTagsDatasource.id,
packageName: 'group/subgroup/my.git.module',
packageName: 'group/subgroup',
registryUrl: 'https://gitlab.com',
});
});
Expand Down
29 changes: 7 additions & 22 deletions lib/modules/datasource/go/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ export class BaseGoDatasource {
private static readonly gitlabHttpsRegExp = regEx(
/^(?<httpsRegExpUrl>https:\/\/[^/]*gitlab\.[^/]*)\/(?<httpsRegExpName>.+?)(?:\/v\d+)?[/]?$/,
);
private static readonly gitlabRegExp = regEx(
/^(?<regExpUrl>gitlab\.[^/]*)\/(?<regExpPath>.+?)(?:\/v\d+)?[/]?$/,
);
private static readonly gitVcsRegexp = regEx(
/^(?:[^/]+)\/(?<module>.*)\.git(?:$|\/)/,
);
Expand Down Expand Up @@ -154,30 +151,18 @@ export class BaseGoDatasource {
};
}

const gitlabUrl =
BaseGoDatasource.gitlabHttpsRegExp.exec(metadataUrl)?.groups
?.httpsRegExpUrl;
const gitlabUrlName =
BaseGoDatasource.gitlabHttpsRegExp.exec(metadataUrl)?.groups
?.httpsRegExpName;
const gitlabModuleName =
BaseGoDatasource.gitlabRegExp.exec(goModule)?.groups?.regExpPath;
const vcsIndicatedModule =
BaseGoDatasource.gitVcsRegexp.exec(goModule)?.groups?.module;
if (gitlabUrl && gitlabUrlName) {
if (gitlabModuleName?.startsWith(gitlabUrlName)) {
const packageName = vcsIndicatedModule ?? gitlabModuleName;
return {
datasource: GitlabTagsDatasource.id,
registryUrl: gitlabUrl,
packageName,
};
}

const metadataUrlMatchGroups =
BaseGoDatasource.gitlabHttpsRegExp.exec(metadataUrl)?.groups;
if (metadataUrlMatchGroups) {
const { httpsRegExpUrl, httpsRegExpName } = metadataUrlMatchGroups;
const packageName = vcsIndicatedModule ?? httpsRegExpName;
return {
datasource: GitlabTagsDatasource.id,
registryUrl: gitlabUrl,
packageName: gitlabUrlName,
registryUrl: httpsRegExpUrl,
packageName,
};
}

Expand Down
Loading