-
Notifications
You must be signed in to change notification settings - Fork 2
/
hook.js
256 lines (202 loc) · 6.72 KB
/
hook.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
const { json, send } = require('micro')
const GitHubApi = require('github')
const pino = require('pino')
const config = require('./config')
const pretty = pino.pretty()
pretty.pipe(process.stdout)
const logger = pino({
level: 'debug',
}, pretty)
const github = new GitHubApi({
debug: config.debug,
headers: {
'user-agent': 'gold-miner-robot',
},
})
github.authenticate({
type: 'token',
token: config.github.token,
})
const addComment = function addComment(number, body) {
return github.issues.createComment({
owner: config.github.owner,
repo: config.github.repo,
number,
body,
})
}
const removeLabel = function removeLabel(number, label) {
return github.issues.removeLabel({
owner: config.github.owner,
repo: config.github.repo,
number,
name: label,
})
}
const addLabels = function addLabels(number, labels) {
return github.issues.addLabels({
owner: config.github.owner,
repo: config.github.repo,
number,
labels,
})
}
const includes = function (target, keywords) {
const words = Array.isArray(keywords) ? keywords : [ keywords ]
for (let i = 0; i < words.length; i += 1) {
if (target.includes(words[i])) {
return true
}
}
return false
}
const handleApplyForTranslation = async function (payload) {
const { issue, comment, sender } = payload
let awaitTranslation = false
for (let i = 0; i < issue.labels.length; i += 1) {
if (issue.labels[i].name === '翻译认领') {
awaitTranslation = true
break
}
}
if (!awaitTranslation) return
if (includes(comment.body, ['认领翻译', '申请翻译', '翻译认领'])) {
try {
await Promise.all([
addComment(issue.number, `@${comment.user.login} 棒极啦 :tada:`),
removeLabel(issue.number, '翻译认领'),
addLabels(issue.number, ['正在翻译'])
])
} catch(err) {
return logger.error(err)
}
logger.debug(`Handle request of ${sender.login} from #${issue.number} with message ${comment.body} successfully.`)
} else {
logger.debug(`Can not handle request of ${sender.login} from #${issue.number} with message ${comment.body}.`)
}
}
const handleApplyForReview = async function (payload) {
const { issue, comment, sender } = payload
let awaitReview = false
for (let i = 0; i < issue.labels.length; i += 1) {
const label = issue.labels[i]
if (label.name === '校对认领') {
awaitReview = true
break
}
}
if (!awaitReview) return
if (includes(comment.body, ['认领校对', '申请校对', '校对认领'])) {
let hasAtLeastOneReviewer = false
for (let i = 0; i < issue.labels.length; i += 1) {
const label = issue.labels[i]
if (label.name === '正在校对') {
hasAtLeastOneReviewer = true
break
}
}
try {
logger.debug(`Trying to update PR #${issue.number} status.`)
if (hasAtLeastOneReviewer) {
await Promise.all([
addComment(issue.number, `@${comment.user.login} 妥妥哒 :beers:`),
removeLabel(issue.number, '校对认领')
])
} else {
await Promise.all([
addComment(issue.number, `@${comment.user.login} 好的呢 :beer:`),
addLabels(issue.number, ['正在校对'])
])
}
logger.debug(`Update PR #${issue.number} status successfully.`)
} catch(err) {
return logger.error(err)
}
const matchedIssueNumber = /#\d+/.exec(issue.body)
if (matchedIssueNumber) {
const issueNumber = Number(matchedIssueNumber[0].substring(1))
if (typeof issueNumber === 'number') {
try {
logger.debug(`Trying to update reference issue #${issue.number} status.`)
if (hasAtLeastOneReviewer) {
await removeLabel(issueNumber, '请到对应的 PR 下认领校对')
} else {
await addLabels(issueNumber, ['正在校对'])
}
logger.debug(`Update reference issue #${issue.number} status successfully.`)
} catch (err) {
logger.error(err)
}
}
}
logger.debug(`Handle request of ${sender.login} from #${issue.number} with message ${comment.body} successfully .`)
} else {
logger.debug(`Can not handle request of ${sender.login} from #${issue.number} with message ${comment.body}.`)
}
}
const handleNewPull = async function (payload) {
const { pull_request: pull, sender } = payload
logger.debug(`Trying to add label "校对认领" to pull ${pull.number}.`)
await Promise.all([
addLabels(pull.number, ['校对认领'])
])
logger.debug(`Add label "校对认领" to pull ${pull.number} successfully.`)
const matchedIssueNumber = /#\d+/.exec(pull.body)
if (matchedIssueNumber) {
const issueNumber = Number(matchedIssueNumber[0].substring(1))
if (typeof issueNumber === 'number') {
logger.debug(`Got the reference #${issueNumber} from pull #${pull.number}.`)
try {
logger.debug(`Trying to update issue #${issueNumber} status.`)
await Promise.all([
addComment(issueNumber, `PR 地址:#${pull.number}`),
removeLabel(issueNumber, '正在翻译'),
addLabels(issueNumber, ['请到对应的 PR 下认领校对'])
])
logger.debug(`Update issue #${issueNumber} status successfully.`)
} catch(err) {
logger.error(err)
}
}
}
}
const handleNewIssue = async (payload) => {
const { issue, sender } = payload
if (issue.title.includes('推荐优秀英文文章')) {
await addComment(issue.number, `:heart: 感谢有你 ♪(*´▽`*)ノ`)
logger.debug(`Reply issue #${issue.number} with title ${issue.title} successfully.`)
}
}
module.exports = async (req, res) => {
const eventName = req.headers['x-github-event']
let payload
logger.info(`<-- ${req.method} ${req.url}`)
try {
payload = await json(req)
} catch(err) {
logger.error(`URL: ${req.url}, Message: ${err.message}`)
logger.info(`--> ${req.method} ${req.url} 400`)
return send(res, 400)
}
if (payload.sender.login === 'leviding') {
logger.info(`--> payload.sender.login === leviding`)
logger.info(`--> ${req.method} ${req.url} 200`)
return send(res, 200)
}
logger.debug(`Received GitHub event ${eventName} ${payload.action} from ${payload.sender.login}`)
if (eventName === 'issue_comment' && payload.action === 'created') {
if (payload.issue.html_url.includes('pull')) {
handleApplyForReview(payload)
} else {
handleApplyForTranslation(payload)
}
}
if (eventName === 'pull_request' && payload.action === 'opened') {
handleNewPull(payload)
}
if (eventName === 'issues' && payload.action === 'opened') {
handleNewIssue(payload)
}
send(res, 200)
logger.info(`--> ${req.method} ${req.url} 200`)
}