-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriage.js
70 lines (61 loc) · 2.01 KB
/
triage.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
const github = require('@actions/github')
const core = require('@actions/core')
const YAML = require('yaml')
const fs = require('fs').promises
const context = github.context
const token = core.getInput('token')
const octokit = github.getOctokit(token)
const duplicateLabel = core.getInput('duplicateLabel')
const triageLabel = core.getInput('triageLabel')
const triagedLabels = core.getInput('triagedLabels').split('\n')
const sponsorsFile = core.getInput('sponsorsFile')
const duplicateRegexp = /duplicate(?:d| of)? #(\d+)/gi
;(async function run() {
const labelsToAdd = new Set()
const labelsToRemove = new Set()
const issueLabels = context.payload.issue.labels.map(v => v.name)
if (!issueLabels.some(v => triagedLabels.includes(v))) {
labelsToAdd.add(triageLabel)
} else {
labelsToRemove.add(triageLabel)
}
if (
context.payload.action === 'closed' &&
(
context.payload.issue.state_reason === 'duplicate' || (
context.payload.issue.state_reason === 'not_planned' &&
context.payload.issue.comments
))
) {
const comments = await octokit.rest.issues.listComments({
...context.repo,
issue_number: context.payload.issue.number,
}).then(r => r.data)
if (duplicateRegexp.test(comments.at(-1)?.body)) {
labelsToAdd.add(duplicateLabel)
labelsToRemove.add(triageLabel)
}
}
const sponsors = YAML.parse(await fs.readFile(sponsorsFile, 'utf8'))
const issueAuthor = context.payload.issue.user.login
for (const { label, members } of sponsors) {
if (members && ~members.findIndex(v => v.toLowerCase() === issueAuthor.toLowerCase())) {
labelsToAdd.add(label)
break
}
}
if (labelsToAdd.size) {
await octokit.rest.issues.addLabels({
...context.repo,
issue_number: context.payload.issue.number,
labels: [...labelsToAdd],
})
}
for (const label of labelsToRemove) {
await octokit.rest.issues.removeLabel({
...context.repo,
issue_number: context.payload.issue.number,
name: label,
})
}
})()