Skip to content

Commit

Permalink
fix(go): Detect repository name from the metadata URL instead of modu…
Browse files Browse the repository at this point in the history
…le (#30388)
  • Loading branch information
zharinov authored Aug 2, 2024
1 parent bae6f8d commit 7143d6d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
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

0 comments on commit 7143d6d

Please sign in to comment.