-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (73 loc) · 2.97 KB
/
index.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
const parser = require('./lib/parser.js')
const defaultConfig = require('./lib/defaultConfig.js')
async function getClosedIssues(context, keywords) {
const issueInfo = context.issue()
const commits = await context.github.pullRequests.getCommits(issueInfo)
const body = context.payload.pull_request.body.toLowerCase()
const filteredMessages = commits.data
.map(({ commit }) => commit.message.toLowerCase())
.concat([body])
.join('\n')
return parser.extractIssueNumbers(filteredMessages, keywords)
}
async function createLabelIfNecessary(context, robot, labelName, labelColor) {
const repoInfo = context.repo()
const labels = await context.github.issues.getLabels({ ...repoInfo, per_page: 100 })
if (!labels.data.some(({ name }) => name === labelName)) {
robot.log.debug(`No label with name ${labelName} found. Creating a new label...`)
await context.github.issues.createLabel({
...repoInfo,
name: labelName,
color: labelColor,
})
}
}
async function getUserConfig(context, robot) {
const userConfig = await context.config('config.yml')
robot.log.debug('User defined config values: ', userConfig)
return userConfig && userConfig.labelPullRequests ? userConfig.labelPullRequests : {}
}
async function getConfig(context, robot) {
const userConfig = await getUserConfig(context, robot)
const config = {
...defaultConfig,
...userConfig,
}
robot.log.debug('Using config: ', config)
return config
}
async function handleOpenedPr(robot, context) {
const { labelName, labelColor, keywords } = await getConfig(context, robot)
const closedIssues = await getClosedIssues(context, keywords)
await createLabelIfNecessary(context, robot, labelName, labelColor)
const repoInfo = context.repo()
robot.log.debug('[PR OPENED] Issues that will be closed by this PR: ', closedIssues)
closedIssues.forEach((issue) => {
robot.log.debug(`[PR OPENED] Adding label ${labelName} to issue ${issue}`)
context.github.issues.addLabels({
...repoInfo,
number: issue,
labels: [labelName],
})
})
}
module.exports = (robot) => {
handleOpenedPr = handleOpenedPr.bind(null, robot)
robot.on('pull_request.opened', handleOpenedPr)
robot.on('pull_request.reopened', handleOpenedPr)
robot.on('pull_request.edited', handleOpenedPr)
robot.on('pull_request.closed', async context => {
const { labelName, keywords } = await getConfig(context, robot)
const closedIssues = await getClosedIssues(context, keywords)
robot.log.debug('[PR CLOSED] Issues closed by this PR: ', closedIssues)
closedIssues.forEach((issue) => {
robot.log.debug(`[PR CLOSED] Removing label ${labelName} from ${issue}`)
// I don't know why I can't do await with a try/catch here.
context.github.issues.removeLabel(context.repo({
number: issue,
name: labelName,
}))
.catch(robot.error)
})
})
}