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-14936-lane-merge-refusal-not-applied.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Serialized merge lane: a definite GitHub refusal (403, 405, 422) of the front's merge call is now recorded as not applied, so the pull request leaves the front and the next waiter is promoted within one round instead of waiting out the front timeout; 5xx and transport errors stay uncertain ([#14936](https://github.com/hivecommons/hive/issues/14936)).
7 changes: 6 additions & 1 deletion src/docs/operator-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,12 @@ default, restarted on every branch update, and extended up to 4x while the
required checks are only queued or in progress) is the fallback for a front
whose checks never finish; its reason says what the lane last knew, for
example that the head after the branch update was never evaluated, or the
last recorded waiting reason.
last recorded waiting reason. A front whose merge call GitHub definitively
refuses (HTTP 403, 405 or 422, for example a merge method the repository
does not allow) also leaves at once with the reason `merge refused by
GitHub: ...`, since nothing was applied; a 5xx or transport error on the
merge call is not definitive, so that front keeps waiting and re-validates
until the front timeout.

A front that GitHub still reports as `blocked` after its head contains the
current target tip and all required checks pass also leaves immediately. The
Expand Down
2 changes: 1 addition & 1 deletion src/pkg/github/automerge/automerge_contributor_lane.go
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,7 @@ func (c *Engine) trySweepContributorLanePR(ctx context.Context, displayRepo, own
var apiErr error
mergeResult, _, apiErr = c.gh.PullRequests.Merge(ctx, owner, repo, number, mergeMessage, mergeOpts)
if apiErr != nil {
return effects.Result{}, mergeAPIErrorNotApplied(apiErr)
return effects.Result{}, hgithub.MergeAPIErrorNotApplied(apiErr)
}
return effects.Result{Provenance: mergeResult.GetSHA()}, nil
})
Expand Down
21 changes: 3 additions & 18 deletions src/pkg/github/automerge/automerge_sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -1886,7 +1886,7 @@ func (c *Engine) trySweepSelfAuthoredPR(ctx context.Context, displayRepo, owner,
var apiErr error
mergeResult, _, apiErr = c.gh.PullRequests.Merge(ctx, owner, repo, number, mergeMessage, mergeOpts)
if apiErr != nil {
return effects.Result{}, mergeAPIErrorNotApplied(apiErr)
return effects.Result{}, hgithub.MergeAPIErrorNotApplied(apiErr)
}
return effects.Result{Provenance: mergeResult.GetSHA()}, nil
})
Expand Down Expand Up @@ -2404,7 +2404,7 @@ func (c *Engine) trySweepTrustedAuthorPR(ctx context.Context, displayRepo, owner
var apiErr error
mergeResult, _, apiErr = c.gh.PullRequests.Merge(ctx, owner, repo, number, mergeMessage, mergeOpts)
if apiErr != nil {
return effects.Result{}, mergeAPIErrorNotApplied(apiErr)
return effects.Result{}, hgithub.MergeAPIErrorNotApplied(apiErr)
}
return effects.Result{Provenance: mergeResult.GetSHA()}, nil
})
Expand Down Expand Up @@ -2759,7 +2759,7 @@ func (c *Engine) trySweepQueuedPR(ctx context.Context, displayRepo, owner, repo
var apiErr error
mergeResult, _, apiErr = c.gh.PullRequests.Merge(ctx, owner, repo, number, mergeMessage, mergeOpts)
if apiErr != nil {
return effects.Result{}, mergeAPIErrorNotApplied(apiErr)
return effects.Result{}, hgithub.MergeAPIErrorNotApplied(apiErr)
}
return effects.Result{Provenance: mergeResult.GetSHA()}, nil
})
Expand Down Expand Up @@ -3152,21 +3152,6 @@ func (c *Engine) mergeQueueBaseKnown(owner, repo, base string) bool {
return c.mergeQueueMemory().Required(owner+"/"+repo, base, c.clock())
}

func mergeAPIErrorNotApplied(err error) error {
if err == nil {
return nil
}
var ghErr *gh.ErrorResponse
if !errors.As(err, &ghErr) || ghErr.Response == nil {
return err
}
switch ghErr.Response.StatusCode {
case http.StatusMethodNotAllowed, http.StatusConflict, http.StatusUnprocessableEntity, http.StatusForbidden:
return effects.NotApplied(err)
}
return err
}

func (c *Engine) executeMergeEffect(ctx context.Context, claim effects.Claim, owner, repo string, number int, merge func(context.Context) (effects.Result, error)) (effects.Result, error) {
out, err := effects.Execute(ctx, c.mutation, claim, merge)
if !errors.Is(err, effects.ErrNeedsReconciliation) {
Expand Down
28 changes: 28 additions & 0 deletions src/pkg/github/merge_effects.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package github

import (
"errors"
"net/http"

gh "github.com/google/go-github/v72/github"
"github.com/hivecommons/hive/pkg/effects"
)

// MergeAPIErrorNotApplied wraps a definite GitHub refusal of a pull request
// merge (403, 405, 409, 422) in effects.NotApplied so the mutation journal
// records NotApplied instead of an uncertain outcome. Other errors (5xx,
// transport failures) are returned unchanged and stay uncertain.
func MergeAPIErrorNotApplied(err error) error {
if err == nil {
return nil
}
var ghErr *gh.ErrorResponse
if !errors.As(err, &ghErr) || ghErr.Response == nil {
return err
}
switch ghErr.Response.StatusCode {
case http.StatusMethodNotAllowed, http.StatusConflict, http.StatusUnprocessableEntity, http.StatusForbidden:
return effects.NotApplied(err)
}
return err
}
2 changes: 1 addition & 1 deletion src/pkg/mergelane/gate.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func (g *RESTGitHub) Merge(ctx context.Context, repo string, number int, head st
}, func(ctx context.Context) (effects.Result, error) {
res, _, apiErr = api.PullRequests.Merge(ctx, owner, name, number, "", &gh.PullRequestOptions{SHA: head, MergeMethod: used})
if apiErr != nil {
return effects.Result{}, apiErr
return effects.Result{}, ghub.MergeAPIErrorNotApplied(apiErr)
}
return effects.Result{Provenance: res.GetSHA()}, nil
})
Expand Down
55 changes: 55 additions & 0 deletions src/pkg/mergelane/gate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

gh "github.com/google/go-github/v72/github"
"github.com/hivecommons/hive/pkg/config"
"github.com/hivecommons/hive/pkg/effects"
ghub "github.com/hivecommons/hive/pkg/github"
)

Expand Down Expand Up @@ -486,3 +487,57 @@ func TestGatePreservesMergeError(t *testing.T) {
t.Fatal("failed merge must retain the unmerged front")
}
}

// A definite GitHub refusal of the merge call is recorded as not applied; a
// 5xx stays uncertain, and a 409 is still a moved head.
func TestRESTGitHubMergeRefusalClassification(t *testing.T) {
status := map[string]int{}
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method != http.MethodPut || !strings.HasSuffix(r.URL.Path, "/merge") {
w.WriteHeader(http.StatusNotFound)
_, _ = w.Write([]byte(`{"message":"Not Found"}`))
return
}
w.WriteHeader(status[r.URL.Path])
_, _ = w.Write([]byte(`{"message":"refused by policy"}`))
}))
defer api.Close()
client := ghub.NewClient("token", "acme", []string{"widgets"}, nil, api.URL)
g := &RESTGitHub{Client: func() *ghub.Client { return client }}
ctx := context.Background()

for _, tc := range []struct {
code int
notApplied bool
headMoved bool
uncertain bool
}{
{code: http.StatusForbidden, notApplied: true},
{code: http.StatusUnprocessableEntity, notApplied: true},
{code: http.StatusMethodNotAllowed, notApplied: true},
{code: http.StatusConflict, headMoved: true},
{code: http.StatusInternalServerError, uncertain: true},
} {
// A distinct PR per case keeps each effect a new logical operation.
n := 100 + tc.code
status[fmt.Sprintf("/repos/acme/widgets/pulls/%d/merge", n)] = tc.code
_, err := g.Merge(ctx, testRepo, n, "h1")
switch {
case tc.headMoved:
if !errors.Is(err, ErrHeadMoved) {
t.Fatalf("%d: err = %v, want ErrHeadMoved", tc.code, err)
}
case tc.notApplied:
if !errors.Is(err, effects.ErrNotApplied) || !strings.Contains(err.Error(), fmt.Sprint(tc.code)) || !strings.Contains(err.Error(), "refused by policy") {
t.Fatalf("%d: err = %v, want not-applied with status and message", tc.code, err)
}
case tc.uncertain:
// Uncertain means neither a definite refusal nor a moved head: the
// raw API error comes back so the caller holds the front.
if err == nil || errors.Is(err, effects.ErrNotApplied) || errors.Is(err, ErrHeadMoved) || !strings.Contains(err.Error(), fmt.Sprint(tc.code)) {
t.Fatalf("%d: err = %v, want an uncertain effect", tc.code, err)
}
}
}
}
18 changes: 16 additions & 2 deletions src/pkg/mergelane/lane.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"time"

"github.com/hivecommons/hive/pkg/config"
"github.com/hivecommons/hive/pkg/effects"
ghub "github.com/hivecommons/hive/pkg/github"
)

Expand Down Expand Up @@ -98,7 +99,11 @@ type GitHub interface {
IssueCommentsContain(ctx context.Context, repo string, number int, needle string) (bool, error)
CreateIssueComment(ctx context.Context, repo string, number int, body string) error
// Merge is the REST merge with head pinned, no admin bypass; it returns
// the merge commit SHA.
// the merge commit SHA. On a 409 head mismatch it returns ErrHeadMoved.
// When GitHub definitively refuses the merge (403/405/422) it returns an
// error wrapping effects.ErrNotApplied so the lane can let the front
// leave at once; any other error (5xx, transport) is reported as-is and
// the front keeps waiting.
Merge(ctx context.Context, repo string, number int, head string) (string, error)
CommitParents(ctx context.Context, repo, sha string) ([]string, error)
PullRequestsForCommit(ctx context.Context, repo, sha string) ([]int, error)
Expand Down Expand Up @@ -175,7 +180,8 @@ type Decision struct {
Reason string
Position int
MergeSHA string
// MergeError is the underlying failure when a merge call holds the front.
// MergeError is the underlying failure when a merge call holds the front
// or GitHub definitively refuses it.
MergeError error
}

Expand Down Expand Up @@ -802,6 +808,14 @@ func (r *round) merge(head, tip string) (Decision, error) {
r.front.Stage = StageValidating
return r.hold(head, tip, "merge refused: head moved past the pinned SHA; validation restarts")
}
if errors.Is(err, effects.ErrNotApplied) {
// GitHub definitively refused the merge, so nothing happened and
// the front need not wait out its timeout. The typed refusal still
// travels with the decision so callers can explain it.
dec, leaveErr := r.leave(head, tip, "merge refused by GitHub: "+err.Error())
dec.MergeError = err
return dec, leaveErr
}
if err != nil {
r.front.Stage = StageValidating
dec, holdErr := r.hold(head, tip, "merge call failed; no merge assumed this round: "+err.Error())
Expand Down
27 changes: 27 additions & 0 deletions src/pkg/mergelane/scenario_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"
"time"

"github.com/hivecommons/hive/pkg/effects"
ghub "github.com/hivecommons/hive/pkg/github"
)

Expand Down Expand Up @@ -822,3 +823,29 @@ func TestExtendedFrontMergesWhenChecksFinish(t *testing.T) {
t.Fatalf("extensions = %d, want 1", got)
}
}

// A definite GitHub refusal of the front's merge (403/405/422) leaves the
// front at once, before the front timeout, and the next waiter is promoted.
func TestFrontWhoseMergeIsRefusedLeavesAndNextWaiterIsPromoted(t *testing.T) {
h := newHarness(t)
p := h.f.addPR(1, "h1")
h.f.addPR(2, "h2")
expectOutcome(t, h.acquire(1), OutcomeFront, "")
h.now = h.now.Add(time.Minute)
expectOutcome(t, h.acquire(2), OutcomeDeferred, ReasonNotAtFront)

refusal := effects.NotApplied(errors.New("PUT /pulls/1/merge: 403 Resource not accessible by integration []"))
h.f.errs["Merge"] = refusal
dec := h.advance(1)
expectOutcome(t, dec, OutcomeLeft, "merge refused by GitHub: ")
if !errors.Is(dec.MergeError, refusal) {
t.Fatalf("merge error = %v, want the typed GitHub refusal carried out of the lane", dec.MergeError)
}
if p.Merged || h.frontPR() != 2 {
t.Fatalf("merged=%v front=#%d, want #1 unmerged and #2 promoted", p.Merged, h.frontPR())
}
exit := h.record().LastExit
if exit == nil || exit.PR != 1 || !strings.Contains(exit.Reason, "403") || strings.Contains(exit.Reason, "front timeout") {
t.Fatalf("last exit = %+v, want #1 leaving with GitHub's refusal", exit)
}
}
Loading