/
index.ts
85 lines (74 loc) · 2 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
import URL from 'url';
import is from '@sindresorhus/is';
import { logger } from '../../logger';
import * as globalCache from '../../util/cache/global';
import { GitlabHttp } from '../../util/http/gitlab';
import { GetReleasesConfig, ReleaseResult } from '../common';
const gitlabApi = new GitlabHttp();
export const id = 'gitlab-tags';
const cacheNamespace = 'datasource-gitlab';
function getCacheKey(depHost: string, repo: string): string {
const type = 'tags';
return `${depHost}:${repo}:${type}`;
}
type GitlabTag = {
name: string;
commit?: {
created_at?: string;
};
};
export async function getReleases({
registryUrls,
lookupName: repo,
}: GetReleasesConfig): Promise<ReleaseResult | null> {
// Use registryUrls if present, otherwise default to publid gitlab.com
const depHost = is.nonEmptyArray(registryUrls)
? registryUrls[0].replace(/\/$/, '')
: 'https://gitlab.com';
let gitlabTags: GitlabTag[];
const cachedResult = await globalCache.get<ReleaseResult>(
cacheNamespace,
getCacheKey(depHost, repo)
);
// istanbul ignore if
if (cachedResult) {
return cachedResult;
}
const urlEncodedRepo = encodeURIComponent(repo);
try {
// tag
const url = URL.resolve(
depHost,
`/api/v4/projects/${urlEncodedRepo}/repository/tags?per_page=100`
);
gitlabTags = (
await gitlabApi.getJson<GitlabTag[]>(url, {
paginate: true,
})
).body;
} catch (err) {
// istanbul ignore next
logger.debug({ repo, err }, 'Error retrieving from Gitlab');
}
// istanbul ignore if
if (!gitlabTags) {
return null;
}
const dependency: ReleaseResult = {
sourceUrl: `${depHost}/${repo}`,
releases: null,
};
dependency.releases = gitlabTags.map(({ name, commit }) => ({
version: name,
gitRef: name,
releaseTimestamp: commit?.created_at,
}));
const cacheMinutes = 10;
await globalCache.set(
cacheNamespace,
getCacheKey(depHost, repo),
dependency,
cacheMinutes
);
return dependency;
}