diff --git a/lib/merge.js b/lib/merge.js index 597a8191..f7761be3 100644 --- a/lib/merge.js +++ b/lib/merge.js @@ -256,33 +256,51 @@ function skipPullRequest(context, pullRequest, approvalCount) { return skip; } -function waitUntilReady(pullRequest, context) { +async function waitUntilReady(pullRequest, context) { const { octokit, config: { mergeRetries, mergeRetrySleep, mergeErrorFail } } = context; + const checks = await getPullRequestChecks(octokit, pullRequest); return retry( mergeRetries, mergeRetrySleep, - () => checkReady(pullRequest, context), + () => checkReady(pullRequest, checks, context), async () => { - const pr = await getPullRequest(octokit, pullRequest); - return checkReady(pr, context); + const [{ data: pr }, { data: checks }] = await Promise.all([ + getPullRequest(octokit, pullRequest), + getPullRequestChecks(octokit, pullRequest), + ]); + return checkReady(pr, checks, context); }, () => failOrInfo(mergeRetries, mergeErrorFail) ); } -function checkReady(pullRequest, context) { +function checkReady(pullRequest, checks, context) { if (skipPullRequest(context, pullRequest)) { return "failure"; } - return mergeable(pullRequest, context); + return mergeable(pullRequest, checks, context); } -function mergeable(pullRequest, context) { - const { mergeable_state } = pullRequest; +function mergeable(pullRequest, checks, context) { + const { mergeable, mergeable_state } = pullRequest; + const areChecksCompleted = checks.check_runs.every(check => check.status === "completed"); + + if (mergeable === null) { + return "retry"; + } + + if (!mergeable) { + return "failure"; + } + + if (!areChecksCompleted) { + return "retry"; + } + const { config: { mergeReadyState } } = context; @@ -312,6 +330,19 @@ async function getPullRequest(octokit, pullRequest) { return pr; } +async function getPullRequestChecks(octokit, pullRequest) { + logger.debug("Getting PR checks..."); + const { data: checks } = await octokit.checks.listForRef({ + owner: pullRequest.base.repo.owner.login, + repo: pullRequest.base.repo.name, + ref: pullRequest.head.sha, + }); + + logger.trace("PR checks:", checks); + + return checks; +} + function tryMerge( octokit, pullRequest,