/
index.ts
167 lines (159 loc) · 4.1 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { logger } from '../../logger';
import * as globalCache from '../../util/cache/global';
import { GithubHttp } from '../../util/http/github';
import { DigestConfig, GetReleasesConfig, ReleaseResult } from '../common';
export const id = 'github-tags';
const http = new GithubHttp();
const cacheNamespace = 'datasource-github-tags';
function getCacheKey(repo: string, type: string): string {
return `${repo}:${type}`;
}
interface TagResponse {
object: {
type: string;
url: string;
sha: string;
};
}
async function getTagCommit(
githubRepo: string,
tag: string
): Promise<string | null> {
const cachedResult = await globalCache.get<string>(
cacheNamespace,
getCacheKey(githubRepo, `tag-${tag}`)
);
// istanbul ignore if
if (cachedResult) {
return cachedResult;
}
let digest: string;
try {
const url = `https://api.github.com/repos/${githubRepo}/git/refs/tags/${tag}`;
const res = (await http.getJson<TagResponse>(url)).body.object;
if (res.type === 'commit') {
digest = res.sha;
} else if (res.type === 'tag') {
digest = (await 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'
);
}
if (!digest) {
return null;
}
const cacheMinutes = 120;
await globalCache.set(
cacheNamespace,
getCacheKey(githubRepo, `tag-${tag}`),
digest,
cacheMinutes
);
return digest;
}
/**
* github.getDigest
*
* The `newValue` supplied here should be a valid tag for the docker image.
*
* This function will simply return the latest commit hash for the configured repository.
*/
export async function getDigest(
{ lookupName: githubRepo }: Partial<DigestConfig>,
newValue?: string
): Promise<string | null> {
if (newValue && newValue.length) {
return getTagCommit(githubRepo, newValue);
}
const cachedResult = await globalCache.get(
cacheNamespace,
getCacheKey(githubRepo, 'commit')
);
// istanbul ignore if
if (cachedResult) {
return cachedResult;
}
let digest: string;
try {
const url = `https://api.github.com/repos/${githubRepo}/commits?per_page=1`;
const res = await http.getJson<{ sha: string }[]>(url);
digest = res.body[0].sha;
} catch (err) {
logger.debug(
{ githubRepo, err },
'Error getting latest commit from GitHub repo'
);
}
if (!digest) {
return null;
}
const cacheMinutes = 10;
await globalCache.set(
cacheNamespace,
getCacheKey(githubRepo, 'commit'),
digest,
cacheMinutes
);
return digest;
}
/**
* github.getReleases
*
* This function can be used to fetch releases with a customisable versioning (e.g. semver) and with either tags or releases.
*
* This function will:
* - Fetch all tags or releases (depending on configuration)
* - Sanitize the versions if desired (e.g. strip out leading 'v')
* - Return a dependency object containing sourceUrl string and releases array
*/
export async function getReleases({
lookupName: repo,
}: GetReleasesConfig): Promise<ReleaseResult | null> {
let versions: string[];
const cachedResult = await globalCache.get<ReleaseResult>(
cacheNamespace,
getCacheKey(repo, 'tags')
);
// istanbul ignore if
if (cachedResult) {
return cachedResult;
}
try {
// tag
const url = `https://api.github.com/repos/${repo}/tags?per_page=100`;
type GitHubTag = {
name: string;
}[];
versions = (
await http.getJson<GitHubTag>(url, {
paginate: true,
})
).body.map((o) => o.name);
} catch (err) {
logger.debug({ repo, err }, 'Error retrieving from github');
}
if (!versions) {
return null;
}
const dependency: ReleaseResult = {
sourceUrl: 'https://github.com/' + repo,
releases: null,
};
dependency.releases = versions.map((version) => ({
version,
gitRef: version,
}));
const cacheMinutes = 10;
await globalCache.set(
cacheNamespace,
getCacheKey(repo, 'tags'),
dependency,
cacheMinutes
);
return dependency;
}