-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
74 lines (72 loc) · 2.17 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
const APPROVE = "APPROVE";
const AUTOMERGE_MESSAGE = "**Automerge**: Enabled";
const RENOVATE_BOT = process.env.RENOVATE_BOT_USER || "renovate[bot]";
const MEND_BOT = "mend-for-github-com[bot]";
const RENOVATE_APPROVE_BOT = process.env.RENOVATE_APPROVE_BOT_USER || "renovate-approve[bot]";
module.exports = (app) => {
app.log("App is loaded");
function isValidBot(context) {
try {
return context.payload.sender.login === RENOVATE_BOT || context.payload.sender.login === MEND_BOT;
} catch (err) {
context.log(context.payload);
context.log(err);
return false;
}
}
function isAutomerging(context) {
try {
return context.payload.pull_request.body.includes(AUTOMERGE_MESSAGE);
} catch (err) {
context.log(context.payload);
context.log(err);
return false;
}
}
function isRenovateApprover(context) {
try {
return context.payload.review.user.login === RENOVATE_APPROVE_BOT;
} catch (err) {
context.log(err);
return false;
}
}
function isRenovateUser(context) {
try {
return context.payload.pull_request.user.login === RENOVATE_BOT;
} catch (err) {
context.log(context.payload);
context.log(err);
return false;
}
}
function approvePr(context) {
try {
const params = context.pullRequest({ event: APPROVE });
return context.octokit.pulls.createReview(params);
} catch (err) {
context.log(err);
context.log(context.payload);
}
}
app.on("pull_request.opened", async context => {
context.log("Received PR open event");
if (isValidBot(context) && isAutomerging(context)) {
context.log("Approving new PR");
return approvePr(context);
}
});
app.on("pull_request_review.dismissed", async context => {
context.log("Received PR review dismiss event");
context.log(context.payload);
if (
isValidBot(context) &&
isAutomerging(context) &&
isRenovateApprover(context) &&
isRenovateUser(context)
) {
context.log("Re-approving dismissed approval");
return approvePr(context);
}
});
};