-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
89 lines (74 loc) · 2.42 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
/** 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 issueRegex = require('issue-regex');
module.exports = robot => {
robot.on('pull_request.opened', check);
robot.on('pull_request.edited', check);
robot.on('pull_request.labeled', check);
robot.on('pull_request.unlabeled', check);
robot.on('pull_request.synchronize', check);
async function check(context) {
const {github} = context;
const pr = context.payload.pull_request;
setStatus({
state: 'pending',
description: 'Checking if PR correctly references an issue',
});
const config = await context.config('pr-issue.yml', {
ignore: ['release', 'docs'],
});
const labels = await github.issues.getIssueLabels(context.issue());
let shouldIgnore = false;
// PRs with whitelisted labels don't need to reference an issue
shouldIgnore =
shouldIgnore ||
labels.data.some(label => {
let name = label.name.toLowerCase();
return config.ignore.some(ignored => ignored === name);
});
// Bots don't need to reference an issue
shouldIgnore = shouldIgnore || pr.user.type === 'Bot';
if (shouldIgnore) {
setStatus({
state: 'success',
description: 'PR does not need to reference an issue',
});
return;
}
function hasIssue() {
const issues = matchMaybe(pr.body, issueRegex());
if (issues !== null) {
return true;
}
const repo = context.payload.repository.full_name;
const urlRegex = new RegExp(`${repo}/issues/\\d+`, 'g');
return matchMaybe(pr.body, urlRegex) !== null;
}
const status = hasIssue()
? {
state: 'success',
description: 'PR references an issue',
}
: {
state: 'failure',
description: 'PR does not reference an issue',
};
setStatus(status);
function setStatus(status) {
const params = Object.assign(
{
sha: pr.head.sha,
context: 'probot/pr-issue',
},
status,
);
return github.repos.createStatus(context.repo(params));
}
}
};
function matchMaybe(maybeString, pattern) {
return typeof maybeString === 'string' ? maybeString.match(pattern) : null;
}