|
3 | 3 | const { context, getOctokit } = require('@actions/github')
|
4 | 4 | const core = require('@actions/core');
|
5 | 5 | const Codeowners = require('codeowners');
|
6 |
| -const {readFileSync} = require("fs") |
| 6 | +const {readFileSync} = require("fs"); |
7 | 7 |
|
8 | 8 | // Effectively the main function
|
9 | 9 | async function run() {
|
@@ -112,15 +112,37 @@ async function mergeIfLGTMAndHasAccess() {
|
112 | 112 | if (filesWhichArentOwned.length !== 0) {
|
113 | 113 | console.log(`@${sender} does not have access to merge \n - ${filesWhichArentOwned.join("\n - ")}\n`)
|
114 | 114 | listFilesWithOwners(changedFiles, cwd)
|
115 |
| - process.exit(0) |
| 115 | + await octokit.issues.createComment({ ...thisRepo, issue_number: issue.number, body: `Sorry @${sender}, you don't have access to merge: ${filesWhichArentOwned.join(", ")}.` }); |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + // Don't try merge unmergable stuff |
| 120 | + const prInfo = await octokit.pulls.get({ ...thisRepo, pull_number: issue.number }) |
| 121 | + if (!prInfo.data.mergeable) { |
| 122 | + await octokit.issues.createComment({ ...thisRepo, issue_number: issue.number, body: `Sorry @${sender}, this PR has merge conflicts. They'll need to be fixed before this can be merged.` }); |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + if (prInfo.data.state !== "OPEN") { |
| 127 | + await octokit.issues.createComment({ ...thisRepo, issue_number: issue.number, body: `Sorry @${sender}, this PR isn't open.` }); |
| 128 | + return |
| 129 | + } |
| 130 | + |
| 131 | + // Don't merge red PRs |
| 132 | + const statusInfo = await octokit.repos.listCommitStatusesForRef({ ...thisRepo, ref: prInfo.data.head.sha }) |
| 133 | + const failedStatus = statusInfo.data.find(s => s.state !== "success") |
| 134 | + |
| 135 | + if (failedStatus) { |
| 136 | + await octokit.issues.createComment({ ...thisRepo, issue_number: issue.number, body: `Sorry @${sender}, this PR could not be merged because it wasn't green. Blocked by [${failedStatus.context}](${failedStatus.target_url}): '${failedStatus.description}'.` }); |
| 137 | + return |
116 | 138 | }
|
117 | 139 |
|
118 | 140 | core.info(`Creating comments and merging`)
|
119 | 141 | try {
|
120 | 142 | await octokit.pulls.merge({ ...thisRepo, pull_number: issue.number });
|
121 | 143 | await octokit.issues.createComment({ ...thisRepo, issue_number: issue.number, body: `Merging because @${sender} is a code-owner of all the changes - thanks!` });
|
122 | 144 | } catch (error) {
|
123 |
| - await octokit.issues.createComment({ ...thisRepo, issue_number: issue.number, body: `Looks good to merge, thanks ${sender}.` }); |
| 145 | + await octokit.issues.createComment({ ...thisRepo, issue_number: issue.number, body: `There was an issue merging, maybe try again ${sender}.` }); |
124 | 146 | }
|
125 | 147 | }
|
126 | 148 |
|
|
0 commit comments