forked from squalrus/merge-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
87 lines (71 loc) · 2.41 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
const core = require('@actions/core');
const { context, getOctokit } = require('@actions/github');
const Config = require('./lib/config')
const Pull = require('./lib/pull');
const renderMessage = require('./lib/message');
async function run() {
try {
console.log(`action: ${context.payload.action}`);
console.log(`[data] payload: ${JSON.stringify(context.payload)}`);
const config = new Config(core);
console.log(`[data] config: ${JSON.stringify(config)}`);
const pull = new Pull(context.payload);
console.log(`[data] pull (payload): ${JSON.stringify(pull)}`);
const token = core.getInput('GITHUB_TOKEN');
const octokit = getOctokit(token);
console.log(`[info] get reviews`);
const reviews = await octokit.pulls.listReviews({
owner: pull.owner,
repo: pull.repo,
pull_number: pull.pull_number
});
console.log(`[info] get checks`);
const checks = await octokit.checks.listForRef({
owner: pull.owner,
repo: pull.repo,
ref: pull.branch_name
});
pull.compileReviews(reviews);
pull.compileChecks(checks);
console.log(`[data] pull (checks + reviews): ${JSON.stringify(pull)}`);
console.log(`merge: ${pull.canMerge(config)}`);
if (config.test_mode) {
// comment in test mode
await octokit.issues.createComment({
owner: pull.owner,
repo: pull.repo,
issue_number: pull.pull_number,
body: renderMessage(github.context.payload.action, config, pull)
});
} else {
if (pull.canMerge(config)) {
// merge the pull request
console.log(`[info] merge start`);
await octokit.pulls.merge({
owner: pull.owner,
repo: pull.repo,
pull_number: pull.pull_number,
merge_method: config.merge_method
});
console.log(`[info] merge complete`);
if (config.delete_source_branch) {
if (pull.headRepoId !== pull.baseRepoId) {
console.log(`[warning] unable to delete branch from fork, branch retained`);
} else {
// delete the branch
console.log(`[info] delete start`);
await octokit.git.deleteRef({
owner: pull.owner,
repo: pull.repo,
ref: pull.ref
});
console.log(`[info] delete complete`);
}
}
}
}
} catch (error) {
core.setFailed(error.message);
}
}
run();