Skip to content

Commit

Permalink
feat(github-tags): Use GraphQL helper for getReleases (renovatebot#…
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov authored and pull[bot] committed Oct 13, 2022
1 parent 23df6c4 commit a016596
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 39 deletions.
50 changes: 29 additions & 21 deletions lib/modules/datasource/github-tags/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { getPkgReleases } from '..';
import * as httpMock from '../../../../test/http-mock';
import * as _hostRules from '../../../util/host-rules';
import * as githubGraphql from '../../../util/github/graphql';
import * as hostRules from '../../../util/host-rules';
import { GithubTagsDatasource } from '.';

jest.mock('../../../util/host-rules');
const hostRules: any = _hostRules;

const githubApiHost = 'https://api.github.com';
const githubEnterpriseApiHost = 'https://git.enterprise.com';

Expand All @@ -14,8 +12,8 @@ describe('modules/datasource/github-tags/index', () => {

beforeEach(() => {
jest.resetAllMocks();
hostRules.hosts = jest.fn(() => []);
hostRules.find.mockReturnValue({
jest.spyOn(hostRules, 'hosts').mockReturnValue([]);
jest.spyOn(hostRules, 'find').mockReturnValue({
token: 'some-token',
});
});
Expand Down Expand Up @@ -107,29 +105,39 @@ describe('modules/datasource/github-tags/index', () => {
});

describe('getReleases', () => {
beforeEach(() => {
jest.resetAllMocks();
hostRules.hosts = jest.fn(() => []);
hostRules.find.mockReturnValue({
token: 'some-token',
});
});

const depName = 'some/dep2';

it('returns tags', async () => {
const tags = [{ name: 'v1.0.0' }, { name: 'v1.1.0' }];
httpMock
.scope(githubApiHost)
.get(`/repos/${depName}/tags?per_page=100`)
.reply(200, tags);
jest.spyOn(githubGraphql, 'queryTags').mockResolvedValueOnce([
{
version: 'v1.0.0',
gitRef: 'v1.0.0',
releaseTimestamp: '2021-01-01',
newDigest: '123',
},
{
version: 'v2.0.0',
gitRef: 'v2.0.0',
releaseTimestamp: '2022-01-01',
newDigest: 'abc',
},
]);

const res = await getPkgReleases({ datasource: github.id, depName });

expect(res).toEqual({
registryUrl: 'https://github.com',
releases: [
{ gitRef: 'v1.0.0', version: 'v1.0.0' },
{ gitRef: 'v1.1.0', version: 'v1.1.0' },
{
gitRef: 'v1.0.0',
version: 'v1.0.0',
releaseTimestamp: '2021-01-01T00:00:00.000Z',
},
{
gitRef: 'v2.0.0',
version: 'v2.0.0',
releaseTimestamp: '2022-01-01T00:00:00.000Z',
},
],

sourceUrl: 'https://github.com/some/dep2',
Expand Down
28 changes: 10 additions & 18 deletions lib/modules/datasource/github-tags/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { logger } from '../../../logger';
import type { GithubRestRef, GithubRestTag } from '../../../util/github/types';
import { queryTags } from '../../../util/github/graphql';
import type { GithubRestRef } from '../../../util/github/types';
import { getApiBaseUrl, getSourceUrl } from '../../../util/github/url';
import { GithubHttp } from '../../../util/http/github';
import { Datasource } from '../datasource';
Expand Down Expand Up @@ -83,23 +84,14 @@ export class GithubTagsDatasource extends Datasource {
config: GetReleasesConfig
): Promise<ReleaseResult> {
const { registryUrl, packageName: repo } = config;
const apiBaseUrl = getApiBaseUrl(registryUrl);
// tag
const url = `${apiBaseUrl}repos/${repo}/tags?per_page=100`;

const versions = (
await this.http.getJson<GithubRestTag[]>(url, {
paginate: true,
})
).body.map((o) => o.name);

const dependency: ReleaseResult = {
sourceUrl: getSourceUrl(repo, registryUrl),
releases: versions.map((version) => ({
version,
gitRef: version,
})),
};
const sourceUrl = getSourceUrl(repo, registryUrl);
const tags = await queryTags(config, this.http);
const releases = tags.map(({ version, releaseTimestamp, gitRef }) => ({
version,
releaseTimestamp,
gitRef,
}));
const dependency: ReleaseResult = { sourceUrl, releases };
return dependency;
}
}

0 comments on commit a016596

Please sign in to comment.