-
-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathGitHubActions.ts
101 lines (88 loc) · 2.45 KB
/
GitHubActions.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
import { Env, CISource } from "../ci_source"
import { ensureEnvKeysExist } from "../ci_source_helpers"
import { readFileSync, existsSync } from "fs"
// https://developer.github.com/actions/
/**
* ### CI Setup
*
* To use Danger JS with GitHub Actions, you'll need want to set up
* Danger to run on a `pull_request` webhook. To do this, you'll need
* to add/amend your repo's workflow file with something like:
*
* ```
* workflow "Dangerfile JS Eval" {
* on = "pull_request"
* resolves = "Danger JS"
* }
*
* action "Danger JS" {
* uses = "danger/danger-js"
* secrets = ["GITHUB_TOKEN"]
* }
* ```
*
* You can pass additional CLI to Danger JS in an action via:
*
* ```
* action "Danger JS" {
* [...]
* args = "--dangerfile artsy/peril-settings/org/allPRs.ts"
* }
* ```
*
* This runs the file [`org/allPRs.ts`](https://github.com/artsy/peril-settings/blob/master/org/allPRs.ts)
* from the repo [artsy/peril-settings](https://github.com/artsy/peril-settings).
*
* ### Token Setup
*
* You need to make sure that the secret `"GITHUB_TOKEN"` is
* enabled in your workspace. This is so that Danger can connect
* to GitHub.
*
* ```
* action "Danger JS" {
* uses = "danger/danger-js"
* secrets = ["GITHUB_TOKEN"]
* }
* ```
*
*/
export class GitHubActions implements CISource {
private event: any
constructor(private readonly env: Env) {
if (existsSync("/github/workflow/event.json")) {
const event = readFileSync("/github/workflow/event.json", "utf8")
this.event = JSON.parse(event)
}
}
get name(): string {
return "GitHub Actions"
}
get isCI(): boolean {
return ensureEnvKeysExist(this.env, ["GITHUB_WORKFLOW"])
}
get isPR(): boolean {
// This one is complicated, because it needs to not *just* support PRs
return true
}
get useEventDSL() {
// Support event based PR runs
return this.env.GITHUB_EVENT_NAME !== "pull_request"
}
get pullRequestID(): string {
if (this.env.GITHUB_EVENT_NAME === "pull_request") {
return this.event.pull_request.number
}
throw new Error("pullRequestID was called on GitHubActions when it wasn't a PR")
}
get repoSlug(): string {
if (this.env.GITHUB_EVENT_NAME === "pull_request") {
return this.event.pull_request.base.repo.full_name
}
throw new Error("repoSlug was called on GitHubActions when it wasn't a PR")
}
// I made a request for this
// get ciRunURL() {
// return process.env.BUILD_URL
// }
}