-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
87 lines (72 loc) · 1.93 KB
/
index.js
File metadata and controls
87 lines (72 loc) · 1.93 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
const Koa = require('koa')
const bodyParser = require('koa-bodyparser')()
const logger = require('koa-logger')
const comments = require('./consts').comment
const { addAssignees, translate, referToFaq, notifyEmptyFiddle } = require('./service')
const util = require('./utils')
const github = require('./github')
const config = {
enableAssign: false
}
const app = new Koa()
app.use(logger())
app.use(bodyParser)
app.use(async (ctx, next) => {
const { action, issue } = ctx.request.body
if (!issue) {
ctx.body = 'This is not an issue.'
return
}
if (config.enableAssign && action === 'labeled') {
await addAssignees(ctx, issue)
return
}
if (action !== 'opened') {
ctx.body = `This issue is ${ action }, so it does not need to be processed.`
return
}
if (util.isValid(issue)) {
ctx.body = 'This is a valid issue.'
if (util.isChinese(issue)) {
await translate(ctx, issue)
}
if (util.isKeyEvent(issue) || util.isIconDemand(issue)) {
await referToFaq(ctx, issue)
} else if (util.isInvalidFiddle(issue)) {
await notifyEmptyFiddle(ctx, issue)
}
return
}
ctx.body = 'This issue is invalid.'
await next()
})
app.use(async (ctx, next) => {
const { issue } = ctx.request.body
const comment = comments.close.en
try {
await github.commentIssue(issue, comment)
await next()
} catch(e) {
ctx.body += ` Error occurred when commenting: ${ e.statusCode }`
}
})
app.use(async (ctx, next) => {
const { issue } = ctx.request.body
const labels = '["invalid"]'
try {
await github.addLabels(issue, labels)
await next()
} catch(e) {
ctx.body += ` Error occurred when adding labels: ${ e.statusCode }`
}
})
app.use(async ctx => {
const { issue } = ctx.request.body
try {
await github.closeIssue(issue)
ctx.body += ' Closed successfully.'
} catch(e) {
ctx.body += ` Error occurred when closing: ${ e.statusCode }`
}
})
app.listen(3000)