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(datasource/go): Fixes splitting of gitlab go package name #26477

Merged
merged 14 commits into from Jan 10, 2024
44 changes: 44 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 @@ -394,6 +395,49 @@ 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/' });
ManuelB marked this conversation as resolved.
Show resolved Hide resolved

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/',
});

GlobalConfig.reset();
hostRules.hostType.mockReturnValue('');
ManuelB marked this conversation as resolved.
Show resolved Hide resolved
});
});
});
});
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 =
endpoint?.replace(regEx('api/v4/?$'), '') ??
`${parsedUrl.protocol}//${parsedUrl.host}`;
ManuelB marked this conversation as resolved.
Show resolved Hide resolved

// 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