This repository has been archived by the owner on Nov 10, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
github.js
72 lines (61 loc) · 2.15 KB
/
github.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
const stripMargin = require('./lib/util/stringHelpers').stripMargin
const config = require('./lib/model/config').config
module.exports = app => {
app.on('issues.opened', doLabel)
app.on('pull_request.opened', doLabel)
async function doLabel (context) {
if (!context.isBot) {
const configObj = await context.config('auto-labeler.yml')
const c = config(configObj)
// noinspection JSUnresolvedVariable
const isPr = !!context.payload.pull_request
const target = isPr ? (c.enabled.prs ? context.payload.pull_request : null) : c.enabled.issues && context.payload.issue
if (target) {
let comment = isPr ? c.prComment : c.issueComment
if ((comment || '').length > 0) {
// noinspection JSUnresolvedVariable
comment = stripMargin`${comment}`
} else {
comment = stripMargin`
| Thanks for opening this issue! I have applied any relevant labels.
`
}
const body = `${target.title} ${target.body}`
let addLabels = new Set()
if (body.length > 3) {
for (const v of c.labels) {
let matches = v.include.some(i => {
let match = i.test(body)
i.lastIndex = 0
return match
})
if (matches) {
let excludes = v.exclude.some(i => {
let match = i.test(body)
i.lastIndex = 0
return match
})
if (!excludes) {
addLabels.add(v.label)
}
}
}
}
if (addLabels.size > 0) {
let params = context.issue({labels: Array.from(addLabels)})
await context.github.issues.addLabels(params)
if (comment && comment.length > 0) {
let issueComment = context.issue({
body: comment
})
return context.github.issues.createComment(issueComment)
}
}
}
}
}
// For more information on building apps:
// https://probot.github.io/docs/
// To get your app running against GitHub, see:
// https://probot.github.io/docs/development/
}