/
github.ts
73 lines (61 loc) · 1.71 KB
/
github.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
// https://developer.github.com/actions/creating-github-actions/accessing-the-runtime-environment/#environment-variables
import type { DetectProvider } from "../types.ts";
function readJsonSync(path: string) {
const decoder = new TextDecoder("utf-8");
const content = decoder.decode(Deno.readFileSync(path));
return JSON.parse(content);
}
function parseBranch(branch: string): string {
return (/refs\/heads\/(.*)/i.exec(branch) || [])[1];
}
type PullRequest = {
base?: {
ref: string;
};
number: string;
};
type GitHubEvent = {
pull_request: PullRequest;
};
function getPrEvent(env: {
[index: string]: string;
}): { branch?: string; pr?: string } {
try {
const event = env.GITHUB_EVENT_PATH
? (readJsonSync(env.GITHUB_EVENT_PATH) as GitHubEvent)
: undefined;
if (event && event.pull_request) {
return {
branch: event.pull_request.base
? parseBranch(event.pull_request.base.ref)
: undefined,
pr: event.pull_request.number,
};
}
} catch {
return { pr: undefined, branch: undefined };
}
return { pr: undefined, branch: undefined };
}
const githubProvider: DetectProvider = {
detect(env) {
return Boolean(env.GITHUB_ACTION);
},
// deno-lint-ignore require-await
async configuration(env) {
const isPr = env.GITHUB_EVENT_NAME === "pull_request";
const branch = parseBranch(env.GITHUB_REF);
return {
name: "GitHub Actions",
service: "github",
commit: env.GITHUB_SHA,
isPr,
branch,
prBranch: isPr ? branch : undefined,
slug: env.GITHUB_REPOSITORY,
root: env.GITHUB_WORKSPACE,
...(isPr ? getPrEvent(env) : undefined),
};
},
};
export default githubProvider;