-
Notifications
You must be signed in to change notification settings - Fork 3
/
markAsMergeOnGreen.ts
73 lines (63 loc) · 2.3 KB
/
markAsMergeOnGreen.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
import { danger } from "danger"
import { IssueComment } from "github-webhook-event-types"
// The shape of a label
interface Label {
id: number
url: string
name: string
description: string
color: string
default: boolean
}
/** If a comment to an issue contains "Merge on Green", apply a label for it to be merged when green. */
export default async (issueComment: IssueComment) => {
const issue = issueComment.issue
const comment = issueComment.comment
const api = danger.github.api
// Only look at PR issue comments, this isn't in the type system
if (!(issue as any).pull_request) {
console.error("Not a Pull Request")
return
}
// Don't do any work unless we have to
const keywords = ["merge on green", "merge on ci green"]
const match = keywords.find(k => comment.body.toLowerCase().includes(k))
if (!match) {
console.error("Did not find any of the phrases in the comment: ", comment.body.toLocaleLowerCase())
return
}
// Check to see if the label has already been set
if (issue.labels.find(l => l.name === "Merge On Green")) {
console.error("Already has Merge on Green")
return
}
const sender = comment.user
const username = sender.login
const org = issueComment.repository.owner.login
// Check for org access, so that some rando doesn't
// try to merge something without permission
try {
await api.orgs.checkMembership({ org, username })
} catch (error) {
// Someone does not have permission to force a merge
return console.error("Sender does not have permission to merge")
}
// Create or re-use an existing label
const owner = org
const repo = issueComment.repository.name
const existingLabels = await api.issues.listLabelsForRepo({ owner, repo })
const mergeOnGreen = existingLabels.data.find((l: Label) => l.name == "Merge On Green")
// Create the label if it doesn't exist yet
if (!mergeOnGreen) {
const newLabel = await api.issues.createLabel({
owner,
repo,
name: "Merge On Green",
color: "247A38",
description: "A label to indicate that Peril should merge this PR when all statuses are green",
} as any)
}
// Then add the label
await api.issues.addLabels({ owner, repo, number: issue.number, labels: ["Merge On Green"] })
console.log("Updated the PR with a Merge on Green label")
}