Skip to content

Commit 8119995

Browse files
committed
chore(scripts): check PR mergeable state before merging
The release manager merge bot was previously failing with a 405 error when a PR's combined status was "success" but required checks (like GitHub Actions check runs) or reviews were still missing.
1 parent 76cbf2b commit 8119995

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed
4.68 KB
Binary file not shown.

.github/scripts/release_manager_merge_bot.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,16 @@ func main() {
213213
}
214214

215215
state = *status.State
216-
log.Printf("Overall status: %s", state)
216+
log.Printf("Overall commit status: %s", state)
217+
if pr.MergeableState != nil {
218+
log.Printf("PR mergeable state: %s", *pr.MergeableState)
219+
if *pr.MergeableState == "dirty" {
220+
fatalError("PR #%d has merge conflicts (dirty).", prNumber)
221+
}
222+
if *pr.MergeableState == "draft" {
223+
fatalError("PR #%d is a draft.", prNumber)
224+
}
225+
}
217226

218227
switch state {
219228
case "failure":
@@ -230,7 +239,19 @@ func main() {
230239
}
231240
retryCount++
232241
case "success":
233-
log.Println("All checks have passed. Merging the pull request...")
242+
// Check if the PR is truly mergeable. MergeableState can be "blocked" if required
243+
// check runs (e.g. GitHub Actions) or reviews are missing, even if CombinedStatus is "success".
244+
// If it's nil, GitHub is still calculating mergeability.
245+
if pr.MergeableState == nil || *pr.MergeableState == "blocked" || *pr.MergeableState == "behind" {
246+
stateStr := "nil"
247+
if pr.MergeableState != nil {
248+
stateStr = *pr.MergeableState
249+
}
250+
log.Printf("Commit status is success, but PR mergeable state is %q. Waiting...", stateStr)
251+
goto wait
252+
}
253+
254+
log.Println("All checks have passed and PR is mergeable. Merging the pull request...")
234255
commitMessage := fmt.Sprintf("Merge pull request #%d from %s/%s", prNumber, owner, repo)
235256
mergeResult, _, err := client.PullRequests.Merge(ctx, owner, repo, prNumber, commitMessage, &github.PullRequestOptions{
236257
MergeMethod: "squash",

0 commit comments

Comments
 (0)