-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
88 lines (77 loc) · 2.38 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
/** Copyright (c) 2019 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('issue_comment.created', comment);
robot.on('issue_comment.edited', comment);
async function comment(context) {
const {issue, comment} = context.payload;
const {pull_request, state} = issue;
const {user} = comment;
if (!pull_request || state !== 'open' || user.type !== 'User') {
return;
}
const {github} = context;
const permissions = await github.repos.getCollaboratorPermissionLevel(
context.repo({
username: user.login,
}),
);
const level = permissions.data.permission;
if (level !== 'admin' && level !== 'write') {
return;
}
const {command, merge_method} = await context.config('merge-pr.yml', {
command: '!merge',
merge_method: 'squash',
});
if (comment.body !== command) {
return;
}
try {
let commit_message = issue.html_url;
// add all PR commiters as co-authors
// https://help.github.com/articles/creating-a-commit-with-multiple-authors/
if (merge_method === 'squash') {
const authorTrailerSet = await github.pullRequests
.listCommits(
context.repo({
pull_number: issue.number,
}),
)
.then(res =>
res.data.reduce((result, item) => {
// exclude PR author from list
if (item.author.login !== user.login) {
const {email, name} = item.commit.author;
result.add(`Co-authored-by: ${name} <${email}>`);
}
return result;
}, new Set()),
);
if (authorTrailerSet.size) {
commit_message += '\n\n' + [...authorTrailerSet].join('\n');
}
}
await github.pullRequests.merge(
context.repo({
pull_number: issue.number,
commit_title: `${issue.title} (#${issue.number})`,
commit_message,
merge_method,
}),
);
} catch (err) {
if (err.code === 405) {
const message = JSON.parse(err.message).message;
github.issues.createComment(
context.issue({
body: `Failed to merge PR: ${message}`,
}),
);
}
}
}
};