-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
116 lines (103 loc) · 2.89 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/** 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.
*/
const parseTitle = require('./parse-title');
module.exports = robot => {
robot.on('pull_request.opened', 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;
// set status to pending while checks happen
setStatus(context, {
state: 'pending',
description: 'Checking whether to apply or remove Release label',
});
async function isRelease() {
const compare = await context.github.repos.compareCommits(
context.repo({
base: pr.base.sha,
head: pr.head.sha,
}),
);
const files = compare.data.files;
if (files.length !== 1) {
return false;
}
const [file] = files;
if (file.filename !== 'package.json') {
return false;
}
if (file.status !== 'modified') {
return false;
}
const head = context.github.repos.getContent(
context.repo({
path: file.filename,
ref: pr.head.sha,
}),
);
const base = context.github.repos.getContent(
context.repo({
path: file.filename,
ref: pr.base.sha,
}),
);
const results = await Promise.all([head, base]);
const [after, before] = results.map(res =>
JSON.parse(Buffer.from(res.data.content, 'base64').toString()),
);
if (after.version === before.version) {
return false;
}
return after.version;
}
const version = await isRelease();
if (version) {
const titleVersion = parseTitle(pr.title);
if (!titleVersion || titleVersion.version !== `v${version}`) {
return setStatus(context, {
state: 'failure',
description: 'Detected release PR, but invalid PR title',
});
}
await context.github.issues.addLabels(
context.issue({
labels: ['release'],
}),
);
} else {
try {
await context.github.issues.removeLabel(
context.issue({
name: 'release',
}),
);
} catch (err) {
if (err.code !== 404) {
throw err;
}
}
}
// set status to success
setStatus(context, {
state: 'success',
description: 'Release label has been set (or unset)',
});
}
};
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/label-release-pr',
}),
);
}