|
1 | 1 | const core = require('@actions/core');
|
2 |
| -const github = require('@actions/github'); |
| 2 | +const { context, getOctokit } = require('@actions/github'); |
3 | 3 |
|
4 | 4 | const Config = require('./lib/config')
|
5 | 5 | const Pull = require('./lib/pull');
|
6 | 6 | const renderMessage = require('./lib/message');
|
7 | 7 |
|
8 | 8 | async function run() {
|
9 |
| - try { |
10 |
| - console.log(`action: ${github.context.payload.action}`); |
11 |
| - console.log(`[data] payload: ${JSON.stringify(github.context.payload)}`); |
| 9 | + try { |
| 10 | + console.log(`action: ${context.payload.action}`); |
| 11 | + console.log(`[data] payload: ${JSON.stringify(context.payload)}`); |
12 | 12 |
|
13 |
| - const config = new Config(core); |
14 |
| - console.log(`[data] config: ${JSON.stringify(config)}`); |
| 13 | + const config = new Config(core); |
| 14 | + console.log(`[data] config: ${JSON.stringify(config)}`); |
15 | 15 |
|
16 |
| - const pull = new Pull(github.context.payload); |
17 |
| - console.log(`[data] pull (payload): ${JSON.stringify(pull)}`); |
| 16 | + const pull = new Pull(context.payload); |
| 17 | + console.log(`[data] pull (payload): ${JSON.stringify(pull)}`); |
18 | 18 |
|
19 |
| - const token = core.getInput('GITHUB_TOKEN'); |
20 |
| - const octokit = new github.getOctokit(token); |
21 | 19 |
|
22 |
| - console.log(`[info] get reviews`); |
23 |
| - const reviews = await octokit.pulls.listReviews({ |
24 |
| - owner: pull.owner, |
25 |
| - repo: pull.repo, |
26 |
| - pull_number: pull.pull_number |
27 |
| - }); |
| 20 | + const token = core.getInput('GITHUB_TOKEN'); |
| 21 | + const octokit = getOctokit(token); |
28 | 22 |
|
29 |
| - console.log(`[info] get checks`); |
30 |
| - const checks = await octokit.checks.listForRef({ |
31 |
| - owner: pull.owner, |
32 |
| - repo: pull.repo, |
33 |
| - ref: pull.branch_name |
34 |
| - }); |
| 23 | + console.log(`[info] get reviews`); |
| 24 | + const reviews = await octokit.pulls.listReviews({ |
| 25 | + owner: pull.owner, |
| 26 | + repo: pull.repo, |
| 27 | + pull_number: pull.pull_number |
| 28 | + }); |
35 | 29 |
|
36 |
| - pull.compileReviews(reviews); |
37 |
| - pull.compileChecks(checks); |
38 |
| - console.log(`[data] pull (checks + reviews): ${JSON.stringify(pull)}`); |
| 30 | + console.log(`[info] get checks`); |
| 31 | + const checks = await octokit.checks.listForRef({ |
| 32 | + owner: pull.owner, |
| 33 | + repo: pull.repo, |
| 34 | + ref: pull.branch_name |
| 35 | + }); |
39 | 36 |
|
40 |
| - console.log(`merge: ${pull.canMerge(config)}`); |
| 37 | + pull.compileReviews(reviews); |
| 38 | + pull.compileChecks(checks); |
| 39 | + console.log(`[data] pull (checks + reviews): ${JSON.stringify(pull)}`); |
41 | 40 |
|
42 |
| - if (config.test_mode) { |
| 41 | + console.log(`merge: ${pull.canMerge(config)}`); |
43 | 42 |
|
44 |
| - // comment in test mode |
45 |
| - await octokit.issues.createComment({ |
46 |
| - owner: pull.owner, |
47 |
| - repo: pull.repo, |
48 |
| - issue_number: pull.pull_number, |
49 |
| - body: renderMessage(github.context.payload.action, config, pull) |
50 |
| - }); |
| 43 | + if (config.test_mode) { |
| 44 | + |
| 45 | + // comment in test mode |
| 46 | + await octokit.issues.createComment({ |
| 47 | + owner: pull.owner, |
| 48 | + repo: pull.repo, |
| 49 | + issue_number: pull.pull_number, |
| 50 | + body: renderMessage(github.context.payload.action, config, pull) |
| 51 | + }); |
51 | 52 |
|
52 |
| - } else { |
53 |
| - if (pull.canMerge(config)) { |
54 |
| - |
55 |
| - // merge the pull request |
56 |
| - console.log(`[info] merge start`); |
57 |
| - await octokit.pulls.merge({ |
58 |
| - owner: pull.owner, |
59 |
| - repo: pull.repo, |
60 |
| - pull_number: pull.pull_number, |
61 |
| - merge_method: config.merge_method |
62 |
| - }); |
63 |
| - console.log(`[info] merge complete`); |
64 |
| - |
65 |
| - if (config.delete_source_branch) { |
66 |
| - if (pull.headRepoId !== pull.baseRepoId) { |
67 |
| - console.log(`[warning] unable to delete branch from fork, branch retained`); |
68 |
| - } else { |
69 |
| - // delete the branch |
70 |
| - console.log(`[info] delete start`); |
71 |
| - await octokit.git.deleteRef({ |
72 |
| - owner: pull.owner, |
73 |
| - repo: pull.repo, |
74 |
| - ref: pull.ref |
75 |
| - }); |
76 |
| - console.log(`[info] delete complete`); |
77 |
| - } |
78 |
| - } |
79 |
| - } |
| 53 | + } else { |
| 54 | + if (pull.canMerge(config)) { |
| 55 | + |
| 56 | + // merge the pull request |
| 57 | + console.log(`[info] merge start`); |
| 58 | + await octokit.pulls.merge({ |
| 59 | + owner: pull.owner, |
| 60 | + repo: pull.repo, |
| 61 | + pull_number: pull.pull_number, |
| 62 | + merge_method: config.merge_method |
| 63 | + }); |
| 64 | + console.log(`[info] merge complete`); |
| 65 | + |
| 66 | + if (config.delete_source_branch) { |
| 67 | + if (pull.headRepoId !== pull.baseRepoId) { |
| 68 | + console.log(`[warning] unable to delete branch from fork, branch retained`); |
| 69 | + } else { |
| 70 | + // delete the branch |
| 71 | + console.log(`[info] delete start`); |
| 72 | + await octokit.git.deleteRef({ |
| 73 | + owner: pull.owner, |
| 74 | + repo: pull.repo, |
| 75 | + ref: pull.ref |
| 76 | + }); |
| 77 | + console.log(`[info] delete complete`); |
| 78 | + } |
80 | 79 | }
|
81 |
| - } catch (error) { |
82 |
| - core.setFailed(error.message); |
| 80 | + } |
83 | 81 | }
|
| 82 | + } catch (error) { |
| 83 | + core.setFailed(error.message); |
| 84 | + } |
84 | 85 | }
|
85 | 86 |
|
86 | 87 | run();
|
0 commit comments