-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
index.ts
103 lines (94 loc) · 3.45 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
import is from '@sindresorhus/is';
import { cache } from '../../../util/cache/package/decorator';
import { regEx } from '../../../util/regex';
import { addSecretForSanitizing } from '../../../util/sanitize';
import { parseUrl } from '../../../util/url';
import { BitBucketTagsDatasource } from '../bitbucket-tags';
import { Datasource } from '../datasource';
import { GitTagsDatasource } from '../git-tags';
import { GithubTagsDatasource } from '../github-tags';
import { GitlabTagsDatasource } from '../gitlab-tags';
import type { DigestConfig, GetReleasesConfig, ReleaseResult } from '../types';
import { BaseGoDatasource } from './base';
import { GoDirectDatasource } from './releases-direct';
import { GoProxyDatasource } from './releases-goproxy';
export class GoDatasource extends Datasource {
static readonly id = 'go';
constructor() {
super(GoDatasource.id);
}
override readonly customRegistrySupport = false;
readonly goproxy = new GoProxyDatasource();
readonly direct = new GoDirectDatasource();
// Pseudo versions https://go.dev/ref/mod#pseudo-versions
static readonly pversionRegexp = regEx(
/v\d+\.\d+\.\d+-(?:\w+\.)?(?:0\.)?\d{14}-(?<digest>[a-f0-9]{12})/
);
@cache({
namespace: `datasource-${GoDatasource.id}`,
// TODO: types (#7154)
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
key: ({ packageName }: Partial<DigestConfig>) => `${packageName}-digest`,
})
getReleases(config: GetReleasesConfig): Promise<ReleaseResult | null> {
return process.env.GOPROXY
? this.goproxy.getReleases(config)
: this.direct.getReleases(config);
}
/**
* go.getDigest
*
* This datasource resolves a go module URL into its source repository
* and then fetches the digest it if it is on GitHub.
*
* This function will:
* - Determine the source URL for the module
* - Call the respective getDigest in github to retrieve the commit hash
*/
@cache({
namespace: GoDatasource.id,
key: ({ packageName }: DigestConfig) => `${packageName}-digest`,
})
override async getDigest(
{ packageName }: DigestConfig,
value?: string | null
): Promise<string | null> {
const source = await BaseGoDatasource.getDatasource(packageName);
if (!source) {
return null;
}
// ignore vX.Y.Z-(0.)? pseudo versions that are used Go Modules - look up default branch instead
// ignore v0.0.0 versions to fetch the digest of default branch, not the commit of non-existing tag `v0.0.0`
const tag =
value && !GoDatasource.pversionRegexp.test(value) && value !== 'v0.0.0'
? value
: undefined;
switch (source.datasource) {
case GitTagsDatasource.id: {
return this.direct.git.getDigest?.(source, tag) ?? null;
}
case GithubTagsDatasource.id: {
return this.direct.github.getDigest(source, tag);
}
case BitBucketTagsDatasource.id: {
return this.direct.bitbucket.getDigest?.(source, tag) ?? null;
}
case GitlabTagsDatasource.id: {
return this.direct.gitlab.getDigest?.(source, tag) ?? null;
}
/* istanbul ignore next: can never happen, makes lint happy */
default: {
return null;
}
}
}
}
// istanbul ignore if
if (is.string(process.env.GOPROXY)) {
const uri = parseUrl(process.env.GOPROXY);
if (uri?.password) {
addSecretForSanitizing(uri.password, 'global');
} else if (uri?.username) {
addSecretForSanitizing(uri.username, 'global');
}
}