-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
index.ts
138 lines (116 loc) · 4.39 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
import { cache } from '../../../util/cache/package/decorator';
import { BitbucketHttp } from '../../../util/http/bitbucket';
import { ensureTrailingSlash } from '../../../util/url';
import type { PagedResult, RepoInfoBody } from '../../platform/bitbucket/types';
import { Datasource } from '../datasource';
import type { DigestConfig, GetReleasesConfig, ReleaseResult } from '../types';
import type { BitbucketCommit, BitbucketTag } from './types';
export class BitbucketTagsDatasource extends Datasource {
bitbucketHttp = new BitbucketHttp(BitbucketTagsDatasource.id);
static readonly id = 'bitbucket-tags';
static readonly customRegistrySupport = true;
static readonly registryStrategy = 'first';
static readonly defaultRegistryUrls = ['https://bitbucket.org'];
static readonly cacheNamespace = `datasource-${BitbucketTagsDatasource.id}`;
constructor() {
super(BitbucketTagsDatasource.id);
}
static getRegistryURL(registryUrl?: string): string {
// fallback to default API endpoint if custom not provided
return registryUrl ?? this.defaultRegistryUrls[0];
}
static getCacheKey(
registryUrl: string | undefined,
repo: string,
type: string
): string {
return `${BitbucketTagsDatasource.getRegistryURL(
registryUrl
)}:${repo}:${type}`;
}
static getSourceUrl(packageName: string, registryUrl?: string): string {
const url = BitbucketTagsDatasource.getRegistryURL(registryUrl);
const normalizedUrl = ensureTrailingSlash(url);
return `${normalizedUrl}${packageName}`;
}
// getReleases fetches list of tags for the repository
@cache({
namespace: BitbucketTagsDatasource.cacheNamespace,
key: ({ registryUrl, packageName }: GetReleasesConfig) =>
BitbucketTagsDatasource.getCacheKey(registryUrl, packageName, 'tags'),
})
async getReleases({
registryUrl,
packageName: repo,
}: GetReleasesConfig): Promise<ReleaseResult | null> {
const url = `/2.0/repositories/${repo}/refs/tags`;
const bitbucketTags = (
await this.bitbucketHttp.getJson<PagedResult<BitbucketTag>>(url, {
paginate: true,
})
).body.values;
const dependency: ReleaseResult = {
sourceUrl: BitbucketTagsDatasource.getSourceUrl(repo, registryUrl),
registryUrl: BitbucketTagsDatasource.getRegistryURL(registryUrl),
releases: bitbucketTags.map(({ name, target }) => ({
version: name,
gitRef: name,
releaseTimestamp: target?.date,
})),
};
return dependency;
}
// getTagCommit fetched the commit has for specified tag
@cache({
namespace: BitbucketTagsDatasource.cacheNamespace,
key: (registryUrl: string | undefined, repo: string, tag: string): string =>
BitbucketTagsDatasource.getCacheKey(registryUrl, repo, `tag-${tag}`),
})
async getTagCommit(
_registryUrl: string | undefined,
repo: string,
tag: string
): Promise<string | null> {
const url = `/2.0/repositories/${repo}/refs/tags/${tag}`;
const bitbucketTag = (await this.bitbucketHttp.getJson<BitbucketTag>(url))
.body;
return bitbucketTag.target?.hash ?? null;
}
@cache({
namespace: BitbucketTagsDatasource.cacheNamespace,
key: (registryUrl: string, repo: string) =>
BitbucketTagsDatasource.getCacheKey(registryUrl, repo, 'mainbranch'),
ttlMinutes: 60,
})
async getMainBranch(repo: string): Promise<string> {
return (
await this.bitbucketHttp.getJson<RepoInfoBody>(
`/2.0/repositories/${repo}`
)
).body.mainbranch.name;
}
// getDigest fetched the latest commit for repository main branch
// however, if newValue is provided, then getTagCommit is called
@cache({
namespace: BitbucketTagsDatasource.cacheNamespace,
key: ({ registryUrl, packageName }: DigestConfig) =>
BitbucketTagsDatasource.getCacheKey(registryUrl, packageName, 'digest'),
})
override async getDigest(
{ packageName: repo, registryUrl }: DigestConfig,
newValue?: string
): Promise<string | null> {
if (newValue?.length) {
return this.getTagCommit(registryUrl, repo, newValue);
}
const mainBranch = await this.getMainBranch(repo);
const url = `/2.0/repositories/${repo}/commits/${mainBranch}`;
const bitbucketCommits = (
await this.bitbucketHttp.getJson<PagedResult<BitbucketCommit>>(url)
).body;
if (bitbucketCommits.values.length === 0) {
return null;
}
return bitbucketCommits.values[0].hash;
}
}