-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathutils.ts
60 lines (53 loc) · 1.88 KB
/
utils.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
import type { Sha1Value } from '#shared/interfaces/ehtag';
import * as actionsCore from '@actions/core';
import * as actionsExec from '@actions/exec';
export function ensureEnv<T>(name: string, parser: (s: string) => T): T;
export function ensureEnv(name: string): string;
export function ensureEnv<T>(name: string, parser?: (s: string) => T): T {
const env = process.env[name];
if (env == null) throw new Error(`Environment variable '${name}' required.`);
if (!parser) return env as unknown as T;
return parser(env);
}
class _GithubAction {
/** 触发工作流程的提交 SHA。 例如 ffac537e6cbbf934b08745a378932722df287a53。 */
get sha(): Sha1Value {
return ensureEnv('GITHUB_SHA') as Sha1Value;
}
/** 发起工作流程的个人或应用程序的名称。 例如 octocat。 */
get actor(): string {
return ensureEnv('GITHUB_ACTOR');
}
/** 所有者和仓库名称。 例如 octocat/Hello-World。 */
get repository(): string {
return ensureEnv('GITHUB_REPOSITORY');
}
get owner(): string {
return this.repository.split('/')[0];
}
get repo(): string {
return this.repository.split('/')[1];
}
get token(): string {
return ensureEnv('GITHUB_TOKEN');
}
isAction(): boolean {
return !!process.env['GITHUB_ACTIONS'];
}
ensureAction(): void {
if (!this.isAction()) throw new Error(`Must run in github action.`);
}
}
Object.defineProperties(_GithubAction.prototype, {
...Object.getOwnPropertyDescriptors(actionsCore),
...Object.getOwnPropertyDescriptors(actionsExec),
defaults: {
get() {
return this as unknown;
},
},
});
type ActionsCore = typeof actionsCore;
type ActionsExec = typeof actionsExec;
type GithubAction = _GithubAction & ActionsCore & ActionsExec;
export const action = new _GithubAction() as GithubAction;