forked from mithro/autolabeler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (45 loc) · 1.36 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
const yaml = require('js-yaml');
const determineLabels = require('./src/determine-labels');
const mergeLabels = require('./src/merge-labels');
module.exports = robot => {
robot.on('pull_request.opened', sizelabel);
robot.on('pull_request.reopened', sizelabel);
robot.on('pull_request.synchronize', sizelabel);
async function sizelabel (context) {
const content = await context.github.repos.getContents(context.repo({
path: '.github/sizelabeler.yml'
}));
const config = yaml.safeLoad(
Buffer.from(content.data.content, 'base64').toString()
);
const { action, pull_request } = context.payload;
const {
additions,
deletions,
number: issue_number,
labels: prLabels,
} = pull_request;
const existingLabels = new Set(prLabels.map(({ name }) => name));
const { labelsToAdd, labelsToRemove } = determineLabels({
config,
action,
additions,
deletions,
existingLabels,
logger: (...args) => robot.log(...args),
});
const { hasChanges, finalLabelsList } = mergeLabels({
existingLabels,
labelsToAdd,
labelsToRemove,
robot,
});
if (hasChanges) {
// only send the request if we need to
await context.github.issues.replaceLabels(context.repo({
issue_number,
labels: Array.from(finalLabelsList),
}));
}
}
}