Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/fixed-15419-queued-required-checks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- The queued auto-merge lane now remembers GitHub's "required status checks are expected" merge refusal per head, like the other lanes, instead of re-PUTting the merge every sweep pass and logging a false merge-failed ([#15419](https://github.com/hivecommons/hive/issues/15419)).
21 changes: 14 additions & 7 deletions src/pkg/github/automerge/automerge_merge_refusal.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,14 @@ const autoMergeReasonChecksExpected = "check-pending"

// requiredChecksExpected reports whether err is GitHub's 405 "N of M
// required status checks are expected" and, when it is, remembers the head
// so knownMergeRefusal skips the merge call on the following passes while
// GitHub still reports the PR blocked (hivecommons/docs#512: the sweep put
// /merge on every pass, each refusal lighting "Merge failed" on the tracker
// for a PR whose checks had simply not started). A check run completing
// flips mergeable_state off "blocked", which re-arms the call.
// and the mergeable_state it was refused in so knownMergeRefusal skips the
// merge call on the following passes while GitHub still reports that state
// (hivecommons/docs#512: the sweep put /merge on every pass, each refusal
// lighting "Merge failed" on the tracker for a PR whose checks had simply
// not started). A check run reporting moves mergeable_state (blocked →
// clean, or clean → unstable/blocked when GitHub had called the head clean
// before its required checks were even created, hivecommons/hive#15419),
// which re-arms the call, as does a new head or the recheck window.
func (c *Engine) requiredChecksExpected(a mergeAttempt, err error) bool {
if !requiredChecksExpectedMergeError(err) {
return false
Expand All @@ -123,13 +126,17 @@ func (c *Engine) requiredChecksExpected(a mergeAttempt, err error) bool {
}

// checksExpectedKnown reports whether the head of a is remembered as
// waiting on required checks and GitHub still reports it blocked.
// waiting on required checks and GitHub still reports the mergeable_state
// it was refused in. It does not require that state to be "blocked": the
// queued lane only reaches the merge call for a head GitHub already calls
// mergeable, so a "blocked"-only memory was never consulted there and the
// merge was re-PUT every pass (hivecommons/hive#15419).
func (c *Engine) checksExpectedKnown(a mergeAttempt, now time.Time) bool {
r, ok := c.mergeRefusals[mergeRefusalChecksKey(a)]
if !ok {
return false
}
if now.Sub(r.at) >= mergeRefusalRecheck || !strings.EqualFold(r.head, strings.TrimSpace(a.head)) || comparableMergeableState(a.state) != "blocked" {
if now.Sub(r.at) >= mergeRefusalRecheck || !strings.EqualFold(r.head, strings.TrimSpace(a.head)) || comparableMergeableState(a.state) != r.state {
delete(c.mergeRefusals, mergeRefusalChecksKey(a))
return false
}
Expand Down
5 changes: 3 additions & 2 deletions src/pkg/github/automerge/automerge_merge_refusal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,9 @@ func TestContributorLanePermanentMergeRefusalOneMergeCall(t *testing.T) {
// TestRequiredChecksExpectedRemembered: GitHub's 405 "N of M required status
// checks are expected" (hivecommons/docs#512, 2026-10-29) is remembered per
// head, and the next pass skips the merge call as check-pending while the
// PR is still blocked — a new head, an unblocked state or the recheck
// window re-arms it; other errors are not remembered.
// PR still reports the mergeable_state it was refused in — a new head, a
// changed state or the recheck window re-arms it; other errors are not
// remembered.
func TestRequiredChecksExpectedRemembered(t *testing.T) {
expected := &gh.ErrorResponse{Response: &http.Response{StatusCode: http.StatusMethodNotAllowed}, Message: "4 of 4 required status checks are expected."}
base := mergeAttempt{owner: "hivecommons", repo: "docs", base: "main", number: 512, head: "sha512", state: "blocked", method: "squash"}
Expand Down
5 changes: 4 additions & 1 deletion src/pkg/github/automerge/automerge_sweep.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package automerge

import (
Expand Down Expand Up @@ -1950,7 +1950,7 @@
if err != nil {
if c.requiredChecksExpected(attempt, err) {
if !branchUpdateAllowed {
return AutoMergeSweepEvent{}, "check-pending", nil
return AutoMergeSweepEvent{}, autoMergeReasonChecksExpected, nil
}
return c.updateBranchAndSkip(ctx, displayRepo, owner, repo, number, evaluatedHeadSHA, "required-checks-expected", c.isForkPR(pr))
}
Expand Down Expand Up @@ -2694,6 +2694,9 @@
return effects.Result{Provenance: mergeResult.GetSHA()}, nil
})
if err != nil {
if c.requiredChecksExpected(attempt, err) {
return AutoMergeSweepEvent{}, autoMergeReasonChecksExpected, nil
}
if refused := c.refusedMerge(attempt, err); refused != "" {
return AutoMergeSweepEvent{}, refused, nil
}
Expand Down
49 changes: 49 additions & 0 deletions src/pkg/github/automerge/automerge_sweep_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,55 @@ func TestTrySweepQueuedPRMergeError(t *testing.T) {
}
}

func TestTrySweepQueuedPRRequiredChecksExpectedIsRemembered(t *testing.T) {
var merges int
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/pulls/7":
json.NewEncoder(w).Encode(map[string]any{
"number": 7, "state": "open", "mergeable_state": "clean", "mergeable": true,
"user": map[string]string{"login": "alice"},
"head": map[string]string{"sha": "sha7"},
"labels": []map[string]string{{"name": hgithub.AutoMergeQueuedLabel}},
})
case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/pulls/7/reviews":
json.NewEncoder(w).Encode([]map[string]any{{"state": "APPROVED", "body": "Approved by @bob for Hive auto-merge on green CI.", "commit_id": "sha7", "user": map[string]string{"login": testHiveAppBotLogin}}})
case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/commits/sha7/status":
json.NewEncoder(w).Encode(map[string]any{"state": "success", "total_count": 1, "statuses": []map[string]string{{"context": "ci/build", "state": "success"}}})
case r.Method == http.MethodGet && r.URL.Path == "/repos/acme/widget/commits/sha7/check-runs":
json.NewEncoder(w).Encode(map[string]any{"total_count": 1, "check_runs": []map[string]string{{"name": "build", "status": "completed", "conclusion": "success"}}})
case r.Method == http.MethodPut && r.URL.Path == "/repos/acme/widget/pulls/7/merge":
merges++
w.WriteHeader(http.StatusMethodNotAllowed)
json.NewEncoder(w).Encode(map[string]string{"message": "4 of 4 required status checks are expected."})
default:
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
}
}))
defer api.Close()
c := newAutoMergeSweepClient(api.URL)
// Two passes against the same head: the first is refused and remembered,
// the second must skip the merge call on the memory alone.
for pass := 1; pass <= 2; pass++ {
_, reason, err := c.trySweepQueuedPR(context.Background(), "widget", "acme", "widget", 7, hgithub.AutoMergeQueuedLabel)
if err != nil || reason != autoMergeReasonChecksExpected {
t.Fatalf("pass %d: trySweepQueuedPR reason=%q err=%v, want %q without error", pass, reason, err, autoMergeReasonChecksExpected)
}
}
if merges != 1 {
t.Fatalf("merges = %d, want 1 (the remembered refusal must not be re-PUT)", merges)
}
attempt := mergeAttempt{owner: "acme", repo: "widget", number: 7, head: "sha7", state: "clean"}
if got := c.knownMergeRefusal(attempt); got != autoMergeReasonChecksExpected {
t.Fatalf("knownMergeRefusal = %q, want %q for the queued head", got, autoMergeReasonChecksExpected)
}
// A check reporting moves mergeable_state, which re-arms the merge call.
attempt.state = "unstable"
if got := c.knownMergeRefusal(attempt); got != "" {
t.Fatalf("knownMergeRefusal = %q after mergeable_state changed, want re-armed", got)
}
}

func TestLatestHiveQueueApprovalDoesNotTrustBodyWhenReviewerIsNotApp(t *testing.T) {
var logs bytes.Buffer
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
Loading