-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
main.js
259 lines (213 loc) Β· 7.95 KB
/
main.js
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
const core = require('@actions/core')
const github = require('@actions/github')
const fs = require('fs')
async function main() {
try {
const token = core.getInput("token", { required: true })
const def = core.getInput("def", { required: true })
// Parse definition
const constraints = JSON.parse(def)
// Lint constraints
for (const constraint of constraints) {
if (!constraint.label) {
console.log(constraint)
core.setFailed("Missing label in above constraint")
return
}
}
const client = github.getOctokit(token)
// Parse event
const json = fs.readFileSync(process.env.GITHUB_EVENT_PATH, { encoding: "utf-8" })
const event = JSON.parse(json)
const pull = event.pull_request
// Fetch PR files
const files = await client.pulls.listFiles({
...github.context.repo,
pull_number: pull.number
})
// Fetch PR body
const { data: { body: body } } = await client.pulls.get({
...github.context.repo,
pull_number: pull.number
})
// Extend every file object with its content
for (const file of files.data) {
// File object could have this field set as 'null'
if (!file.sha) {
file.content = ""
continue
}
// Fetch file content
const blob = await client.git.getBlob({
...github.context.repo,
file_sha: file.sha
})
// Decode received base64-encoded file content
const buffer = Buffer.from(blob.data.content, blob.data.encoding)
const content = buffer.toString('ascii')
file.content = content
}
// Get existing labels on PR
let existingLabels = await client.issues.listLabelsOnIssue({
...github.context.repo,
issue_number: pull.number
})
existingLabels = existingLabels.data.map(label => label.name)
// Map constraint to an array of matching files objects
const constraintToMatchingFiles = new Map()
// Match files with given constraints
for (const file of files.data) {
for (const constraint of constraints) {
// Constraints that check the PR body are checked separately
if (constraint.pr_body_content) {
continue
}
let constraintApplies = false
let labelExists = false
// Possible unwanted label
if (existingLabels.includes(constraint.label)) {
labelExists = true
}
// Continue if the label exists and we aren't going to remove
// it regardless of whether it applies or not
if (labelExists && constraint.keep_if_no_match) {
continue
}
// Check constraints
constraintApplies = doesConstraintApply(constraint, file)
if (labelExists && constraintApplies) {
continue
}
if (!labelExists && !constraintApplies) {
continue
}
// Unwanted label
if (labelExists && !constraintApplies) {
constraint.wanted = false
}
// Wanted label
if (!labelExists && constraintApplies) {
constraint.wanted = true
}
// Init map key if not found
if (!constraintToMatchingFiles.has(constraint)) {
constraintToMatchingFiles.set(constraint, [])
}
// Append file to array
constraintToMatchingFiles.get(constraint).push(file)
}
}
// Only consider labelling when all PR files match a constraint
for (const constraintAndMatchingFiles of constraintToMatchingFiles) {
const constraint = constraintAndMatchingFiles[0]
const matchingFiles = constraintAndMatchingFiles[1]
if (matchingFiles.length == files.data.length) {
continue
}
constraintToMatchingFiles.delete(constraint)
}
for (const constraint of constraints) {
// Only check constraints that check the PR body here
if (!constraint.pr_body_content) {
continue
}
let constraintApplies = false
let labelExists = false
// Possible unwanted label
if (existingLabels.includes(constraint.label)) {
labelExists = true
}
if (body != null) {
constraintApplies = body.match(constraint.pr_body_content)
} else {
constraintApplies = true
}
if (labelExists && constraintApplies) {
continue
}
if (!labelExists && !constraintApplies) {
continue
}
// Unwanted label
if (labelExists && !constraintApplies) {
constraint.wanted = false
}
// Wanted label
if (!labelExists && constraintApplies) {
constraint.wanted = true
}
// Init map key if not found
if (!constraintToMatchingFiles.has(constraint)) {
constraintToMatchingFiles.set(constraint, [])
}
}
// Copy labels into new Array
const updatedLabels = existingLabels.slice()
// Determine which labels should be added or deleted
for (const constraint of constraintToMatchingFiles.keys()) {
if (constraint.wanted && !updatedLabels.includes(constraint.label)) {
updatedLabels.push(constraint.label)
}
if (!constraint.wanted) {
const index = updatedLabels.indexOf(constraint.label);
if (index > -1) {
updatedLabels.splice(index, 1);
}
}
}
// No constraints matched, just return
if (existingLabels.length == updatedLabels.length && existingLabels.every((label, i) => label == updatedLabels[i])) {
return
}
console.log(`#${pull.number}: [${existingLabels}] => [${updatedLabels}]`)
// Update PR labels
await client.issues.update({
...github.context.repo,
issue_number: pull.number,
labels: updatedLabels
})
} catch (error) {
core.setFailed(error.message)
}
}
function doesConstraintApply(constraint, file) {
if (constraint.status && file.status != constraint.status) {
return false
}
if (constraint.path && !file.filename.match(constraint.path)) {
return false
}
if (constraint.except) {
if (Array.isArray(constraint.except)) {
if (constraint.except.includes(file.filename)) {
return false
}
} else if (file.filename == constraint.except) {
return false
}
}
if (constraint.content) {
if (Array.isArray(constraint.content)) {
for (const content of constraint.content) {
if (!file.content.match(content)) {
return false
}
}
} else if (!file.content.match(constraint.content)) {
return false
}
}
if (constraint.missing_content) {
if (Array.isArray(constraint.missing_content)) {
for (const content of constraint.missing_content) {
if (file.content.match(content)) {
return false
}
}
} else if (file.content.match(constraint.missing_content)) {
return false
}
}
return true
}
main()