Skip to content

Commit c4d7d4d

Browse files
committed
Add more error states, and always write a comment when bailing
1 parent 820c785 commit c4d7d4d

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

index.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const { context, getOctokit } = require('@actions/github')
44
const core = require('@actions/core');
55
const Codeowners = require('codeowners');
6-
const {readFileSync} = require("fs")
6+
const {readFileSync} = require("fs");
77

88
// Effectively the main function
99
async function run() {
@@ -112,15 +112,37 @@ async function mergeIfLGTMAndHasAccess() {
112112
if (filesWhichArentOwned.length !== 0) {
113113
console.log(`@${sender} does not have access to merge \n - ${filesWhichArentOwned.join("\n - ")}\n`)
114114
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
116138
}
117139

118140
core.info(`Creating comments and merging`)
119141
try {
120142
await octokit.pulls.merge({ ...thisRepo, pull_number: issue.number });
121143
await octokit.issues.createComment({ ...thisRepo, issue_number: issue.number, body: `Merging because @${sender} is a code-owner of all the changes - thanks!` });
122144
} 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}.` });
124146
}
125147
}
126148

0 commit comments

Comments
 (0)