diff --git a/lib/modules/datasource/github-tags/index.spec.ts b/lib/modules/datasource/github-tags/index.spec.ts index e3315fa0844549..e93874312132a2 100644 --- a/lib/modules/datasource/github-tags/index.spec.ts +++ b/lib/modules/datasource/github-tags/index.spec.ts @@ -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'); }); }); diff --git a/lib/modules/datasource/github-tags/index.ts b/lib/modules/datasource/github-tags/index.ts index 03a3bb573c35ab..dafdad72bbf9b0 100644 --- a/lib/modules/datasource/github-tags/index.ts +++ b/lib/modules/datasource/github-tags/index.ts @@ -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'; @@ -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 { - 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(url)).body.object; - if (res.type === 'commit') { - digest = res.sha; - } else if (res.type === 'tag') { - digest = (await this.http.getJson(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({ diff --git a/lib/modules/datasource/github-tags/types.ts b/lib/modules/datasource/github-tags/types.ts deleted file mode 100644 index e8b5888166c0b2..00000000000000 --- a/lib/modules/datasource/github-tags/types.ts +++ /dev/null @@ -1,11 +0,0 @@ -export interface TagResponse { - object: { - type: string; - url: string; - sha: string; - }; -} - -export interface GitHubTag { - name: string; -}