-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
index.ts
51 lines (45 loc) · 1.19 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
import { Gitlab } from '@gitbeaker/rest'
import type { ProxyAgentConfigurationType } from 'global-agent'
import { bootstrap } from 'global-agent'
const PROXY_PROPS = ['http_proxy', 'https_proxy', 'no_proxy'] as const
declare global {
const GLOBAL_AGENT: {
HTTP_PROXY: string | null
HTTPS_PROXY: string | null
NO_PROXY: string | null
}
}
export const createApi = (gitlabToken?: string) => {
bootstrap()
for (const prop of PROXY_PROPS) {
const uProp = prop.toUpperCase() as keyof ProxyAgentConfigurationType
const value = process.env[uProp] || process.env[prop]
if (value) {
GLOBAL_AGENT[uProp] = value
}
}
const token = gitlabToken || process.env.GITLAB_TOKEN!
const host = process.env.GITLAB_HOST ?? process.env.CI_SERVER_URL
// we cannot use { [tokenType]: token } now
// because it will break the type of the Gitlab constructor
switch (process.env.GITLAB_TOKEN_TYPE) {
case 'job': {
return new Gitlab({
host,
jobToken: token,
})
}
case 'oauth': {
return new Gitlab({
host,
oauthToken: token,
})
}
default: {
return new Gitlab({
host,
token,
})
}
}
}