| 1 | package forge |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | // The hub's answer to a merge it did not make keeps its status, its |
| 14 | // machine-readable reason and its sentence, read from its JSON (error, |
| 15 | // reason) or, for a page, the page's text. |
| 16 | func TestHubMergeKeepsTheAnswer(t *testing.T) { |
| 17 | status, body := 0, "" |
| 18 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 19 | w.WriteHeader(status) |
| 20 | w.Write([]byte(body)) |
| 21 | })) |
| 22 | defer srv.Close() |
| 23 | cred := Credential{Agent: "equipo~p@example.com", Bearer: "token-1"} |
| 24 | req := MergeRequest{ExpectSHA: strings.Repeat("a", 40), Message: "Fix typo", Strategy: "merge", Agent: AgentMeta{Session: "equipo-tk1", Harness: "equipo", Host: "test"}} |
| 25 | for _, c := range []struct { |
| 26 | status int |
| 27 | body string |
| 28 | err error |
| 29 | reason, message string |
| 30 | }{ |
| 31 | {409, `{"error":"this branch is archived; unarchive it before merging","reason":"archived"}`, nil, "archived", "this branch is archived; unarchive it before merging"}, |
| 32 | {403, `{"error":"you must be a writer on this repo to do that"}`, nil, "", "you must be a writer on this repo to do that"}, |
| 33 | {500, "<html><head><title>500</title></head><body><h1>Internal Server Error</h1><p>upstream & down</p></body></html>", ErrMergeUncertain, "", "500 Internal Server Error upstream & down"}, |
| 34 | } { |
| 35 | status, body = c.status, c.body |
| 36 | res, err := HubMerge(context.Background(), srv.Client(), srv.URL, cred, "amadan.net/oficina/sample", "equipo/tk1-fix-typo", req) |
| 37 | if !errors.Is(err, c.err) || res.Status != c.status || res.Reason != c.reason || res.Message != c.message { |
| 38 | t.Fatalf("%d %s: %+v %v", c.status, c.body, res, err) |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | // A refusal or a failure carries the hub's answer (redacted, bounded) |
| 44 | // and still says which outcome it is; a cause that is not the hub's |
| 45 | // answer is kept in words only, never as an outcome of its own. |
| 46 | func TestMergeBranchKeepsTheHubAnswer(t *testing.T) { |
| 47 | secret := "Bearer s3cr3t-hub-token at https://agent:s3cr3t-pw@amadan.net/x" |
| 48 | for _, c := range []struct { |
| 49 | res MergeResult |
| 50 | want error |
| 51 | }{ |
| 52 | {MergeResult{Status: 409, Reason: "archived", Message: "archived; " + secret}, ErrMergeFailed}, |
| 53 | {MergeResult{Status: 409, Reason: "conflicts", Message: "conflicts in README"}, ErrMergeConflict}, |
| 54 | {MergeResult{Status: 502, Reason: "internal", Message: "could not reach the repo instance; " + secret}, ErrMergeUncertain}, |
| 55 | {MergeResult{Status: 409, Reason: "archived", Message: strings.Repeat("long ", 400)}, ErrMergeFailed}, |
| 56 | } { |
| 57 | h := &fakeHub{head: strings.Repeat("a", 40), answer: func(*fakeHub, MergeRequest) (MergeResult, error) { return c.res, nil }} |
| 58 | _, err := MergeBranch(context.Background(), newMerge(t, h)) |
| 59 | var he *HubError |
| 60 | if !errors.Is(err, c.want) || !errors.As(err, &he) || he.Status != c.res.Status || he.Code != c.res.Reason { |
| 61 | t.Fatalf("%+v: %v", c.res, err) |
| 62 | } |
| 63 | detail := MergeDetail(err) |
| 64 | if strings.Contains(he.Message, "s3cr3t") || strings.Contains(err.Error(), "s3cr3t") || strings.Contains(detail, "s3cr3t") || len(he.Message) > 500 { |
| 65 | t.Fatalf("not redacted or bounded: %q / %q / %q", he.Message, err, detail) |
| 66 | } |
| 67 | lead, _, _ := strings.Cut(c.res.Message, ";") |
| 68 | if !strings.HasPrefix(detail, "status: "+strconv.Itoa(c.res.Status)+"\ncode: "+c.res.Reason+"\nmessage: "+lead[:min(len(lead), 20)]) { |
| 69 | t.Fatalf("detail %q", detail) |
| 70 | } |
| 71 | } |
| 72 | // No answer at all: the cause in words, redacted; its own identity |
| 73 | // (here a local refusal) never decides the outcome. |
| 74 | h := &fakeHub{head: strings.Repeat("a", 40), answer: func(*fakeHub, MergeRequest) (MergeResult, error) { |
| 75 | return MergeResult{}, errors.Join(ErrMergeRefused, errors.New(secret)) |
| 76 | }} |
| 77 | _, err := MergeBranch(context.Background(), newMerge(t, h)) |
| 78 | if !errors.Is(err, ErrMergeUncertain) || errors.Is(err, ErrMergeRefused) || !strings.Contains(err.Error(), "refused by validation") { |
| 79 | t.Fatalf("no answer: %v", err) |
| 80 | } |
| 81 | if d := MergeDetail(err); !strings.HasPrefix(d, "error: ") || strings.Contains(d, "s3cr3t") { |
| 82 | t.Fatalf("no answer detail %q", d) |
| 83 | } |
| 84 | // The remote could not be read: nothing was asked, and the reason says |
| 85 | // why. |
| 86 | m := newMerge(t, &fakeHub{head: strings.Repeat("a", 40)}) |
| 87 | m.Inspect = func(context.Context) (RemoteState, error) { |
| 88 | return RemoteState{}, errors.New("ls-remote: fatal: repository not found") |
| 89 | } |
| 90 | if _, err := MergeBranch(context.Background(), m); !errors.Is(err, ErrMergeUncertain) || !strings.Contains(MergeDetail(err), "repository not found") { |
| 91 | t.Fatalf("inspect: %v", err) |
| 92 | } |
| 93 | } |
| 94 | |