-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
72 lines (65 loc) · 1.88 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
/** Copyright (c) 2017 Uber Technologies, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
module.exports = robot => {
robot.on('pull_request.opened', check);
robot.on('pull_request.reopened', check);
robot.on('pull_request.edited', check);
robot.on('pull_request.synchronize', check);
robot.on('pull_request.unlabeled', check);
robot.on('pull_request.labeled', check);
async function check(context) {
const pr = context.payload.pull_request;
const requiredLabels = (await context.config('required-labels.yml', {
labels: [
'breaking',
'feature',
'bugfix',
'docs',
'refactor',
'release',
'prerelease',
'greenkeeping',
],
})).labels;
// set status to pending while checks happen
setStatus(context, {
state: 'pending',
description:
'Checking whether at least one required semver-related label exists',
});
function getRequiredLabels(pr) {
const labels = pr.labels;
const filteredLabels = labels.filter(l =>
requiredLabels.includes(l.name),
);
return filteredLabels;
}
const hasRequiredLabel = getRequiredLabels(pr).length > 0;
if (hasRequiredLabel) {
return void setStatus(context, {
state: 'success',
description: 'At least one required semver-related label exists',
});
}
return void setStatus(context, {
state: 'failure',
description: `Missing one of required labels: ${requiredLabels.join(
', ',
)}`,
});
}
};
async function setStatus(context, {state, description}) {
const {github} = context;
return github.repos.createStatus(
context.issue({
state,
description,
sha: context.payload.pull_request.head.sha,
context: 'probot/pr-label',
}),
);
}