Skip to content

Commit

Permalink
feat(datasource/go): support gitea tags (#32555)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice authored Nov 15, 2024
1 parent a79fd60 commit 85dd149
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 14 deletions.
3 changes: 0 additions & 3 deletions lib/modules/datasource/go/__snapshots__/index.spec.ts.snap

This file was deleted.

19 changes: 11 additions & 8 deletions lib/modules/datasource/go/base.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ const hostRules = mocked(_hostRules);
describe('modules/datasource/go/base', () => {
describe('simple cases', () => {
it.each`
module | datasource | packageName
${'gopkg.in/foo'} | ${'github-tags'} | ${'go-foo/foo'}
${'gopkg.in/foo/bar'} | ${'github-tags'} | ${'foo/bar'}
${'github.com/foo/bar'} | ${'github-tags'} | ${'foo/bar'}
${'bitbucket.org/foo/bar'} | ${'bitbucket-tags'} | ${'foo/bar'}
${'code.cloudfoundry.org/lager'} | ${'github-tags'} | ${'cloudfoundry/lager'}
${'dev.azure.com/foo/bar/_git/baz.git'} | ${'git-tags'} | ${'https://dev.azure.com/foo/bar/_git/baz'}
${'dev.azure.com/foo/bar/baz.git'} | ${'git-tags'} | ${'https://dev.azure.com/foo/bar/_git/baz'}
module | datasource | packageName
${'gopkg.in/foo'} | ${'github-tags'} | ${'go-foo/foo'}
${'gopkg.in/foo/bar'} | ${'github-tags'} | ${'foo/bar'}
${'github.com/foo/bar'} | ${'github-tags'} | ${'foo/bar'}
${'bitbucket.org/foo/bar'} | ${'bitbucket-tags'} | ${'foo/bar'}
${'code.cloudfoundry.org/lager'} | ${'github-tags'} | ${'cloudfoundry/lager'}
${'dev.azure.com/foo/bar/_git/baz.git'} | ${'git-tags'} | ${'https://dev.azure.com/foo/bar/_git/baz'}
${'dev.azure.com/foo/bar/baz.git'} | ${'git-tags'} | ${'https://dev.azure.com/foo/bar/_git/baz'}
${'gitea.com/go-chi/cache'} | ${'gitea-tags'} | ${'go-chi/cache'}
${'code.forgejo.org/go-chi/cache'} | ${'gitea-tags'} | ${'go-chi/cache'}
${'codeberg.org/eviedelta/detctime/durationparser'} | ${'gitea-tags'} | ${'eviedelta/detctime'}
`(
'$module -> $datasource: $packageName',
async ({ module, datasource, packageName }) => {
Expand Down
33 changes: 33 additions & 0 deletions lib/modules/datasource/go/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '../../../util/url';
import { BitbucketTagsDatasource } from '../bitbucket-tags';
import { GitTagsDatasource } from '../git-tags';
import { GiteaTagsDatasource } from '../gitea-tags';
import { GithubTagsDatasource } from '../github-tags';
import { GitlabTagsDatasource } from '../gitlab-tags';
import type { DataSource } from './types';
Expand Down Expand Up @@ -94,6 +95,38 @@ export class BaseGoDatasource {
}
}

//#region known gitea compatible hosts
if (goModule.startsWith('gitea.com/')) {
const split = goModule.split('/');
const packageName = `${split[1]}/${split[2]}`;
return {
datasource: GiteaTagsDatasource.id,
packageName,
registryUrl: 'https://gitea.com',
};
}

if (goModule.startsWith('code.forgejo.org/')) {
const split = goModule.split('/');
const packageName = `${split[1]}/${split[2]}`;
return {
datasource: GiteaTagsDatasource.id,
packageName,
registryUrl: 'https://code.forgejo.org',
};
}

if (goModule.startsWith('codeberg.org/')) {
const split = goModule.split('/');
const packageName = `${split[1]}/${split[2]}`;
return {
datasource: GiteaTagsDatasource.id,
packageName,
registryUrl: 'https://codeberg.org',
};
}
//#endregion

return await BaseGoDatasource.goGetDatasource(goModule);
}

Expand Down
17 changes: 14 additions & 3 deletions lib/modules/datasource/go/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const hostRules = mocked(_hostRules);

const getReleasesDirectMock = jest.fn();

const getDigestGiteaMock = jest.fn();
const getDigestGithubMock = jest.fn();
const getDigestGitlabMock = jest.fn();
const getDigestGitMock = jest.fn();
Expand All @@ -19,6 +20,7 @@ jest.mock('./releases-direct', () => {
GoDirectDatasource: jest.fn().mockImplementation(() => {
return {
git: { getDigest: (...args: any[]) => getDigestGitMock(...args) },
gitea: { getDigest: (...args: any[]) => getDigestGiteaMock(...args) },
github: { getDigest: (...args: any[]) => getDigestGithubMock(...args) },
gitlab: { getDigest: (...args: any[]) => getDigestGitlabMock(...args) },
bitbucket: {
Expand Down Expand Up @@ -189,9 +191,18 @@ describe('modules/datasource/go/index', () => {
},
undefined,
);
expect(res).toMatchSnapshot();
expect(res).not.toBeNull();
expect(res).toBeDefined();
expect(res).toBe('123');
});

it('support gitea digest', async () => {
getDigestGiteaMock.mockResolvedValueOnce('123');
const res = await datasource.getDigest(
{
packageName: 'gitea.com/go-chi/cache',
},
undefined,
);
expect(res).toBe('123');
});

describe('GOPROXY', () => {
Expand Down
4 changes: 4 additions & 0 deletions lib/modules/datasource/go/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { id as semverId } from '../../versioning/semver';
import { BitbucketTagsDatasource } from '../bitbucket-tags';
import { Datasource } from '../datasource';
import { GitTagsDatasource } from '../git-tags';
import { GiteaTagsDatasource } from '../gitea-tags';
import { GithubTagsDatasource } from '../github-tags';
import { GitlabTagsDatasource } from '../gitlab-tags';
import type { DigestConfig, GetReleasesConfig, ReleaseResult } from '../types';
Expand Down Expand Up @@ -98,6 +99,9 @@ export class GoDatasource extends Datasource {
case GitTagsDatasource.id: {
return this.direct.git.getDigest(source, tag);
}
case GiteaTagsDatasource.id: {
return this.direct.gitea.getDigest(source, tag);
}
case GithubTagsDatasource.id: {
return this.direct.github.getDigest(source, tag);
}
Expand Down
61 changes: 61 additions & 0 deletions lib/modules/datasource/go/releases-direct.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,67 @@ describe('modules/datasource/go/releases-direct', () => {
expect(res).toBeDefined();
});

it('support gitea', async () => {
getDatasourceSpy.mockResolvedValueOnce({
datasource: 'gitea-tags',
registryUrl: 'https://gitea.com',
packageName: 'go-chi/cache',
});
httpMock
.scope('https://gitea.com/')
.get('/api/v1/repos/go-chi/cache/tags')
.reply(200, [
{
name: 'v0.1.0',
commit: {
sha: 'd73d815ec22c421e7192a414594ac798c73c89e5',
created: '2022-05-15T16:29:42Z',
},
},
{
name: 'v0.2.0',
commit: {
sha: '3976707232cb68751ff2ddf42547ff95c6878a97',
created: '2022-05-15T17:23:28Z',
},
},
{
name: 'v0.2.1',
commit: {
sha: '2963b104773ead7ed28c00181c03318885d909dc',
created: '2024-09-06T23:44:34Z',
},
},
]);
const res = await datasource.getReleases({
packageName: 'gitea.com/go-chi/cache',
});
expect(res).toEqual({
registryUrl: 'https://gitea.com',
releases: [
{
gitRef: 'v0.1.0',
newDigest: 'd73d815ec22c421e7192a414594ac798c73c89e5',
releaseTimestamp: '2022-05-15T16:29:42Z',
version: 'v0.1.0',
},
{
gitRef: 'v0.2.0',
newDigest: '3976707232cb68751ff2ddf42547ff95c6878a97',
releaseTimestamp: '2022-05-15T17:23:28Z',
version: 'v0.2.0',
},
{
gitRef: 'v0.2.1',
newDigest: '2963b104773ead7ed28c00181c03318885d909dc',
releaseTimestamp: '2024-09-06T23:44:34Z',
version: 'v0.2.1',
},
],
sourceUrl: null,
});
});

it('support git', async () => {
getDatasourceSpy.mockResolvedValueOnce({
datasource: 'git-tags',
Expand Down
6 changes: 6 additions & 0 deletions lib/modules/datasource/go/releases-direct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { regEx } from '../../../util/regex';
import { BitbucketTagsDatasource } from '../bitbucket-tags';
import { Datasource } from '../datasource';
import { GitTagsDatasource } from '../git-tags';
import { GiteaTagsDatasource } from '../gitea-tags';
import { GithubTagsDatasource } from '../github-tags';
import { GitlabTagsDatasource } from '../gitlab-tags';
import type { GetReleasesConfig, Release, ReleaseResult } from '../types';
Expand Down Expand Up @@ -60,6 +61,7 @@ export class GoDirectDatasource extends Datasource {
static readonly id = 'go-direct';

git: GitTagsDatasource;
readonly gitea = new GiteaTagsDatasource();
github: GithubTagsDatasource;
gitlab: GitlabTagsDatasource;
bitbucket: BitbucketTagsDatasource;
Expand Down Expand Up @@ -108,6 +110,10 @@ export class GoDirectDatasource extends Datasource {
res = await this.git.getReleases(source);
break;
}
case GiteaTagsDatasource.id: {
res = await this.gitea.getReleases(source);
break;
}
case GithubTagsDatasource.id: {
res = await this.github.getReleases(source);
break;
Expand Down

0 comments on commit 85dd149

Please sign in to comment.