-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmergeOnGreen.ts
42 lines (35 loc) · 1.72 KB
/
mergeOnGreen.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
import { schedule, danger, markdown } from "danger"
import { Status } from "github-webhook-event-types"
import { LabelLabel } from "github-webhook-event-types/source/Label"
export default async (status: Status) => {
const api = danger.github.api
if (status.state !== "success") {
return console.error(
`Not a successful state (note that you can define state in the settings.json) - got ${status.state}`
)
}
// Check to see if all other statuses on the same commit are also green. E.g. is this the last green.
const owner = status.repository.owner.login
const repo = status.repository.name
const allGreen = await api.repos.getCombinedStatusForRef({ owner, repo, ref: status.commit.sha })
if (allGreen.data.state !== "success") {
return console.error("Not all statuses are green")
}
// See https://github.com/maintainers/early-access-feedback/issues/114 for more context on getting a PR from a SHA
const repoString = status.repository.full_name
const searchResponse = await api.search.issues({ q: `${status.commit.sha} type:pr is:open repo:${repoString}` })
// https://developer.github.com/v3/search/#search-issues
const prsWithCommit = searchResponse.data.items.map((i: any) => i.number) as number[]
for (const number of prsWithCommit) {
// Get the PR labels
const issue = await api.issues.get({ owner, repo, number })
// Get the PR combined status
const mergeLabel = issue.data.labels.find((l: LabelLabel) => l.name === "Merge On Green")
if (!mergeLabel) {
return console.error("PR does not have Merge on Green")
}
// Merge the PR
await api.pulls.merge({ owner, repo, number, commit_title: "Merged by Peril" })
console.log(`Merged Pull Request ${number}`)
}
}