-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathindex.ts
More file actions
143 lines (124 loc) · 6.08 KB
/
index.ts
File metadata and controls
143 lines (124 loc) · 6.08 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { PullRequestEvent, PullRequestReviewEvent } from '@octokit/webhooks-types'
import { Context, Probot } from 'probot'
module.exports = (app: Probot) => {
app.on(['pull_request.opened', 'pull_request.reopened', 'pull_request.labeled', 'pull_request.edited', 'pull_request_review'], async (context) => {
context.log('Repo: %s', context.payload.repository.full_name)
const pr = context.payload.pull_request
context.log('PR: %s', pr.html_url)
context.log('Action: %s', context.payload.action)
// NOTE(dabrady) When a PR is first opened, it can fire several different kinds of events if the author e.g. requests
// reviewers or adds labels during creation. This triggers parallel runs of our GitHub App, so we need to filter out
// those simultaneous events and focus just on the re/open event in this scenario.
//
// These simultaneous events contain the same pull request data in their payloads, and specify the 'updated at'
// timestamp to be the same as the 'created at' timestamp for the pull request. We can use this to distinguish events
// that are fired during creation from events fired later on.
if (!['opened', 'reopened'].includes(context.payload.action) && pr.created_at === pr.updated_at) {
context.log('Ignoring additional creation event: %s', context.payload.action)
return
}
// reading configuration
const config: any = await context.config('autoapproval.yml')
context.log(config, '\n\nLoaded config')
// determine if the PR has any "blacklisted" labels
const prLabels: string[] = pr.labels.map((label: any) => label.name)
let blacklistedLabels: string[] = []
if (config.blacklisted_labels !== undefined) {
blacklistedLabels = config.blacklisted_labels
.filter((blacklistedLabel: any) => prLabels.includes(blacklistedLabel))
// if PR contains any black listed labels, do not proceed further
if (blacklistedLabels.length > 0) {
context.log('PR black listed from approving: %s', blacklistedLabels)
return
}
}
// reading pull request owner info and check it with configuration (case insensitive)
const lowerLogin = pr.user.login.toLowerCase()
const ownerSatisfied = config.from_owner.length === 0 || config.from_owner.some((owner: string) => owner.toLowerCase() === lowerLogin)
// reading pull request labels and check them with configuration
let requiredLabelsSatisfied
if (config.required_labels_mode === 'one_of') {
// one of the required_labels needs to be applied
const appliedRequiredLabels = config.required_labels
.filter((requiredLabel: any) => prLabels.includes(requiredLabel))
requiredLabelsSatisfied = appliedRequiredLabels.length > 0
} else {
// all of the required_labels need to be applied
const missingRequiredLabels = config.required_labels
.filter((requiredLabel: any) => !prLabels.includes(requiredLabel))
requiredLabelsSatisfied = missingRequiredLabels.length === 0
}
if (requiredLabelsSatisfied && ownerSatisfied) {
const reviews = await getAutoapprovalReviews(context)
if (reviews.length > 0) {
context.log('PR has already reviews')
if (context.payload.action === 'dismissed') {
await applyAutoMerge(context, prLabels, config.auto_merge_labels, config.auto_rebase_merge_labels, config.auto_squash_merge_labels)
approvePullRequest(context)
applyLabels(context, config.apply_labels as string[])
context.log('Review was dismissed, approve again')
}
} else {
await applyAutoMerge(context, prLabels, config.auto_merge_labels, config.auto_rebase_merge_labels, config.auto_squash_merge_labels)
approvePullRequest(context)
applyLabels(context, config.apply_labels as string[])
context.log('PR approved first time')
}
} else {
// one of the checks failed
context.log('Condition failed! \n - has required labels: %s\n - PR owner found: %s', requiredLabelsSatisfied, ownerSatisfied)
}
})
}
async function approvePullRequest (context: Context) {
const prParams = context.pullRequest({ event: 'APPROVE' as const, body: 'Approved :+1:' })
await context.octokit.pulls.createReview(prParams)
}
async function applyLabels (context: Context, labels: string[]) {
// if there are labels required to be added, add them
if (labels.length > 0) {
// trying to apply existing labels to PR. If labels didn't exist, this call will fail
const labelsParam = context.issue({ labels: labels })
await context.octokit.issues.addLabels(labelsParam)
}
}
function applyAutoMerge (context: Context, prLabels: string[], mergeLabels: string[], rebaseLabels: string[], squashLabels: string[]) {
if (mergeLabels && prLabels.filter((label: any) => mergeLabels.includes(label)).length > 0) {
enableAutoMerge(context, 'MERGE')
}
if (rebaseLabels && prLabels.filter((label: any) => rebaseLabels.includes(label)).length > 0) {
enableAutoMerge(context, 'REBASE')
}
if (squashLabels && prLabels.filter((label: any) => squashLabels.includes(label)).length > 0) {
enableAutoMerge(context, 'SQUASH')
}
}
const enableAutoMergeMutation = `
mutation($pullRequestId: ID!, $mergeMethod: PullRequestMergeMethod!) {
enablePullRequestAutoMerge(input:{
pullRequestId: $pullRequestId,
mergeMethod: $mergeMethod
}) {
pullRequest {
id,
autoMergeRequest {
mergeMethod
}
}
}
}
`
async function enableAutoMerge (context: Context, method: string) {
context.log('Auto merging with merge method %s', method)
const payload = context.payload as PullRequestEvent | PullRequestReviewEvent
await context.octokit.graphql(enableAutoMergeMutation, {
pullRequestId: payload.pull_request.node_id,
mergeMethod: method
})
}
async function getAutoapprovalReviews (context: Context): Promise<any> {
const pr = context.pullRequest()
const reviews = await context.octokit.pulls.listReviews(pr)
const autoapprovalReviews = (reviews.data).filter((item: any) => item.user.login === 'autoapproval[bot]')
return autoapprovalReviews
}