forked from mithro/autolabeler
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
38 lines (30 loc) · 1.14 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
const yaml = require('js-yaml')
const ignore = require('ignore')
module.exports = robot => {
robot.on('pull_request.opened', autolabel)
robot.on('pull_request.synchronize', autolabel)
async function autolabel (context) {
const content = await context.github.repos.getContent(context.repo({
path: '.github/autolabeler.yml'
}))
const config = yaml.safeLoad(Buffer.from(content.data.content, 'base64').toString())
const githubResponse = await context.github.pullRequests.listCommits(context.issue())
const messages = githubResponse.data.map(commit => commit.commit.message)
const labels = new Set()
// eslint-disable-next-line guard-for-in
for (const label in config) {
robot.log('looking for changes', label, config[label])
const matcher = ignore().add(config[label])
if (messages.find(message => matcher.ignores(message))) {
labels.add(label)
}
}
const labelsToAdd = Array.from(labels)
robot.log('Adding labels', labelsToAdd)
if (labelsToAdd.length > 0) {
return context.github.issues.addLabels(context.issue({
labels: labelsToAdd
}))
}
}
}