-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
index.ts
97 lines (89 loc) · 2.95 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { logger } from '../../../logger';
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';
import type { DigestConfig, GetReleasesConfig, ReleaseResult } from '../types';
export class GithubTagsDatasource extends Datasource {
static readonly id = 'github-tags';
override readonly defaultRegistryUrls = ['https://github.com'];
override http: GithubHttp;
constructor() {
super(GithubTagsDatasource.id);
this.http = new GithubHttp(GithubTagsDatasource.id);
}
async getTagCommit(
registryUrl: string | undefined,
githubRepo: 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<GithubRestRef>(url)).body.object;
if (res.type === 'commit') {
digest = res.sha;
} else if (res.type === 'tag') {
digest = (await this.http.getJson<GithubRestRef>(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'
);
}
return digest;
}
async getCommit(
registryUrl: string | undefined,
githubRepo: string
): Promise<string | null> {
const apiBaseUrl = getApiBaseUrl(registryUrl);
let digest: string | null = null;
try {
const url = `${apiBaseUrl}repos/${githubRepo}/commits?per_page=1`;
const res = await this.http.getJson<{ sha: string }[]>(url);
digest = res.body[0].sha;
} catch (err) {
logger.debug(
{ githubRepo, err, registryUrl },
'Error getting latest commit from GitHub repo'
);
}
return digest;
}
/**
* github.getDigest
*
* The `newValue` supplied here should be a valid tag for the docker image.
*
* Returns the latest commit hash for the repository.
*/
override getDigest(
{ packageName: repo, registryUrl }: Partial<DigestConfig>,
newValue?: string
): Promise<string | null> {
return newValue
? this.getTagCommit(registryUrl, repo!, newValue)
: this.getCommit(registryUrl, repo!);
}
override async getReleases(
config: GetReleasesConfig
): Promise<ReleaseResult> {
const { registryUrl, packageName: repo } = config;
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;
}
}