package forge import ( "context" "errors" "strings" "testing" ) // The first merge into an empty repository, whose default branch is // unborn (no tip, no advertised name). Today's hub refuses it: // unborn_default, or is_default once a rebuild pointed its HEAD at the // task branch. That is ErrMergeUnbornDefault, final for the request: // one request, no blind retry. "moved" while the default is still unborn // is the same; once someone created the default meanwhile it is an // ordinary refusal, and the next merge is an ordinary one. func TestMergeBranchUnbornDefaultBeforeTheHubUpdate(t *testing.T) { commit := strings.Repeat("a", 40) for _, c := range []MergeResult{{Status: 409, Reason: "unborn_default"}, {Status: 400, Reason: "is_default"}, {Status: 409, Reason: "moved"}} { h := &fakeHub{head: commit, unborn: true, answer: func(*fakeHub, MergeRequest) (MergeResult, error) { return c, nil }} m := newMerge(t, h) if _, err := MergeBranch(context.Background(), m); !errors.Is(err, ErrMergeUnbornDefault) || len(h.requests) != 1 { t.Fatalf("%+v: %v (%d requests)", c, err, len(h.requests)) } if rec, ok, _ := m.load(); !ok || rec.Phase != mergeRefusedPh || rec.Reason != c.Reason { t.Fatalf("%+v: record %+v", c, rec) } } h := &fakeHub{head: commit, unborn: true, answer: func(h *fakeHub, _ MergeRequest) (MergeResult, error) { h.unborn = false // someone created the default branch meanwhile return MergeResult{Status: 409, Reason: "moved"}, nil }} if _, err := MergeBranch(context.Background(), newMerge(t, h)); !errors.Is(err, ErrMergeFailed) || errors.Is(err, ErrMergeUnbornDefault) { t.Fatalf("default created meanwhile: %v", err) } } // After the hub update the first merge creates the default branch at // the reviewed tip: 200, merged, the SHA is the tip itself (no merge // commit). A second call reads the record, never asks again. func TestMergeBranchUnbornDefaultAfterTheHubUpdate(t *testing.T) { commit := strings.Repeat("a", 40) h := &fakeHub{head: commit, unborn: true, answer: func(h *fakeHub, req MergeRequest) (MergeResult, error) { if h.head != req.ExpectSHA { return MergeResult{Status: 409, Reason: "moved"}, nil } h.unborn, h.landed, h.merge = false, true, req.ExpectSHA return MergeResult{Status: 200, Merged: true, SHA: req.ExpectSHA, Into: "main"}, nil }} m := newMerge(t, h) out, err := MergeBranch(context.Background(), m) if err != nil || out.SHA != commit || out.Into != "main" || out.Source != SourceHub || len(h.requests) != 1 { t.Fatalf("first merge %+v %v (%d requests)", out, err, len(h.requests)) } if again, err := MergeBranch(context.Background(), m); err != nil || again.SHA != commit || len(h.requests) != 1 { t.Fatalf("again %+v %v", again, err) } // A branch that moved since it was judged is never merged, unborn or not. h2 := &fakeHub{head: strings.Repeat("b", 40), unborn: true, answer: h.answer} if _, err := MergeBranch(context.Background(), newMerge(t, h2)); !errors.Is(err, ErrMergeMoved) || len(h2.requests) != 0 { t.Fatalf("moved: %v", err) } } // The hub since 53d8125 keeps the source branch on the first merge and // says so (branch_closed false); an answer without "into" still lands in // the unborn default's name, main. The kept branch then reads as merged: // the next call is answered by the record, never a second request. func TestMergeBranchFirstMergeKeepsTheBranch(t *testing.T) { commit := strings.Repeat("a", 40) h := &fakeHub{head: commit, unborn: true, answer: func(h *fakeHub, req MergeRequest) (MergeResult, error) { h.unborn, h.landed, h.merge = false, true, req.ExpectSHA return MergeResult{Status: 200, Merged: true, SHA: req.ExpectSHA, BranchClosed: false}, nil }} m := newMerge(t, h) out, err := MergeBranch(context.Background(), m) if err != nil || out.SHA != commit || out.Into != "main" || out.Source != SourceHub { t.Fatalf("first merge %+v %v", out, err) } if h.head != commit { t.Fatalf("the branch was not kept: %q", h.head) } if again, err := MergeBranch(context.Background(), m); err != nil || again.SHA != commit || again.Into != "main" || len(h.requests) != 1 { t.Fatalf("again %+v %v (%d requests)", again, err, len(h.requests)) } }