Skip to content

Commit

Permalink
feat(github-tags): Leverage long-term cache for digest retrieval (#15888
Browse files Browse the repository at this point in the history
)
  • Loading branch information
zharinov committed Jun 4, 2022
1 parent a73942c commit 7f05729
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 88 deletions.
78 changes: 28 additions & 50 deletions lib/modules/datasource/github-tags/index.spec.ts
Expand Up @@ -33,85 +33,63 @@ describe('modules/datasource/github-tags/index', () => {

describe('getDigest', () => {
const packageName = 'some/dep';
const tag = 'v1.2.0';

it('returns null if no token', async () => {
it('returns commit digest', async () => {
httpMock
.scope(githubApiHost)
.get(`/repos/${packageName}/commits?per_page=1`)
.reply(200, []);
.reply(200, [{ sha: 'abcdef' }]);

const res = await github.getDigest({ packageName }, null);
expect(res).toBeNull();

expect(res).toBe('abcdef');
});

it('returns digest', async () => {
it('returns null for missing commit', async () => {
httpMock
.scope(githubApiHost)
.get(`/repos/${packageName}/commits?per_page=1`)
.reply(200, [{ sha: 'abcdef' }]);
.reply(200, []);

const res = await github.getDigest({ packageName }, null);
expect(res).toBe('abcdef');
});

it('returns commit digest', async () => {
httpMock
.scope(githubApiHost)
.get(`/repos/${packageName}/git/refs/tags/${tag}`)
.reply(200, { object: { type: 'commit', sha: 'ddd111' } });
const res = await github.getDigest({ packageName }, tag);
expect(res).toBe('ddd111');
expect(res).toBeNull();
});

it('returns tagged commit digest', async () => {
httpMock
.scope(githubApiHost)
.get(`/repos/${packageName}/git/refs/tags/${tag}`)
.reply(200, {
object: { type: 'tag', url: `${githubApiHost}/some-url` },
})
.get('/some-url')
.reply(200, { object: { type: 'commit', sha: 'ddd111' } });
const res = await github.getDigest({ packageName }, tag);
expect(res).toBe('ddd111');
});
it('returns tag digest', async () => {
tagsCacheGetItems.mockResolvedValueOnce([
{ version: 'v1.0.0', releaseTimestamp: '2021-01-01', hash: 'aaa' },
{ version: 'v2.0.0', releaseTimestamp: '2022-01-01', hash: 'bbb' },
]);

it('warns if unknown ref', async () => {
httpMock
.scope(githubApiHost)
.get(`/repos/${packageName}/git/refs/tags/${tag}`)
.reply(200, { object: { sha: 'ddd111' } });
const res = await github.getDigest({ packageName }, tag);
expect(res).toBeNull();
const res = await github.getDigest({ packageName }, 'v2.0.0');

expect(res).toBe('bbb');
});

it('returns null for missed tagged digest', async () => {
httpMock
.scope(githubApiHost)
.get(`/repos/${packageName}/git/refs/tags/${tag}`)
.reply(200, {});
const res = await github.getDigest({ packageName: 'some/dep' }, 'v1.2.0');
it('returns null for missing tag', async () => {
tagsCacheGetItems.mockResolvedValueOnce([
{ version: 'v1.0.0', releaseTimestamp: '2021-01-01', hash: 'aaa' },
{ version: 'v2.0.0', releaseTimestamp: '2022-01-01', hash: 'bbb' },
]);

const res = await github.getDigest({ packageName }, 'v3.0.0');

expect(res).toBeNull();
});

it('supports ghe', async () => {
it('supports GHE', async () => {
httpMock
.scope(githubEnterpriseApiHost)
.get(`/api/v3/repos/${packageName}/git/refs/tags/${tag}`)
.reply(200, { object: { type: 'commit', sha: 'ddd111' } })
.get(`/api/v3/repos/${packageName}/commits?per_page=1`)
.reply(200, [{ sha: 'abcdef' }]);

const sha1 = await github.getDigest(
const res = await github.getDigest(
{ packageName, registryUrl: githubEnterpriseApiHost },
null
);
const sha2 = await github.getDigest(
{ packageName: 'some/dep', registryUrl: githubEnterpriseApiHost },
'v1.2.0'
);

expect(sha1).toBe('abcdef');
expect(sha2).toBe('ddd111');
expect(res).toBe('abcdef');
});
});

Expand Down
37 changes: 10 additions & 27 deletions lib/modules/datasource/github-tags/index.ts
Expand Up @@ -9,7 +9,6 @@ import type {
ReleaseResult,
} from '../types';
import { CacheableGithubTags } from './cache';
import type { TagResponse } from './types';

export class GithubTagsDatasource extends GithubReleasesDatasource {
static override readonly id = 'github-tags';
Expand All @@ -21,37 +20,21 @@ export class GithubTagsDatasource extends GithubReleasesDatasource {
this.tagsCache = new CacheableGithubTags(this.http);
}

@cache({
ttlMinutes: 120,
namespace: `datasource-${GithubTagsDatasource.id}`,
key: (registryUrl: string, githubRepo: string, tag: string) =>
`${registryUrl}:${githubRepo}:tag-${tag}`,
})
async getTagCommit(
registryUrl: string | undefined,
githubRepo: string,
packageName: string,
tag: string
): Promise<string | null> {
const apiBaseUrl = getApiBaseUrl(registryUrl);
let digest: string | null = null;
try {
const url = `${apiBaseUrl}repos/${githubRepo}/git/refs/tags/${tag}`;
const res = (await this.http.getJson<TagResponse>(url)).body.object;
if (res.type === 'commit') {
digest = res.sha;
} else if (res.type === 'tag') {
digest = (await this.http.getJson<TagResponse>(res.url)).body.object
.sha;
} else {
logger.warn({ res }, 'Unknown git tag refs type');
}
} catch (err) {
logger.debug(
{ githubRepo, err },
'Error getting tag commit from GitHub repo'
);
let result: string | null = null;
const tagReleases = await this.tagsCache.getItems({
packageName,
registryUrl,
});
const tagRelease = tagReleases.find(({ version }) => version === tag);
if (tagRelease) {
result = tagRelease.hash;
}
return digest;
return result;
}

@cache({
Expand Down
11 changes: 0 additions & 11 deletions lib/modules/datasource/github-tags/types.ts

This file was deleted.

0 comments on commit 7f05729

Please sign in to comment.