-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
79 lines (71 loc) · 1.81 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
const github = require('@actions/github')
const process = require('process')
const octokit = new github.GitHub(process.env.GITHUB_TOKEN)
const command = '[/spam]'
const spamLabel = 'Spam'
async function run() {
const payload = github.context.payload
const issue = payload.issue
const repository = payload.repository
const owner = repository.owner.login
const repo = repository.name
const issueNumber = issue.number
let body = payload.comment.body
let lastCharacters = body.slice(-command.length)
if (lastCharacters == command) {
console.log(`Matched ${command} command`)
// Apply 'Spam' label
await applySpamLabel(issue, owner, repo, issueNumber)
// Lock issue
await lockIssue(owner, repo, issueNumber)
// Close issue
await closeIssue(owner, repo, issueNumber)
}
return true
}
async function applySpamLabel(issue, owner, repo, issueNumber) {
// does a 'Spam' label already exist for this issue?
const existingLabels = issue.labels
let labelExists = false
for (l in existingLabels) {
if (existingLabels[l].name == 'Spam') {
labelExists = true
return true
}
}
// apply the 'Spam' label
try {
return octokit.issues.addLabels({
owner: owner,
repo: repo,
issue_number: issueNumber,
labels: [spamLabel]
})
} catch (err) {
return console.log(err)
}
}
async function lockIssue(owner, repo, issueNumber) {
try {
return octokit.issues.lock({
owner: owner,
repo: repo,
issue_number: issueNumber
})
} catch (err) {
return console.log(err)
}
}
async function closeIssue(owner, repo, issueNumber) {
try {
return octokit.issues.update({
owner: owner,
repo: repo,
issue_number: issueNumber,
state: 'closed'
})
} catch (err) {
return console.log(err)
}
}
run()