-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
60 lines (53 loc) · 1.49 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
/** 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.synchronized', check);
async function check(context) {
const {github} = context;
const pr = context.payload.pull_request;
setStatus({
state: 'pending',
description: 'Checking if repo has a LICENSE',
});
async function hasLicense() {
try {
const res = await context.github.repos.getContent(
context.repo({
path: 'LICENSE',
ref: pr.head.sha,
}),
);
const content = Buffer.from(res.data.content, 'base64').toString();
return content && content.length > 0;
} catch (e) {
return false;
}
}
const status = (await hasLicense())
? {
state: 'success',
description: 'Repo has a LICENSE file',
}
: {
state: 'failure',
description: 'Repo does not have a LICENSE file',
};
setStatus(status);
function setStatus(status) {
const params = Object.assign(
{
sha: pr.head.sha,
context: 'probot/pr-license',
},
status,
);
return github.repos.createStatus(context.repo(params));
}
}
};