package thin import ( "context" "log/slog" "os" "path/filepath" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/wire" ) // TestPushFailureKeepsTheHubAnswer: when the hub refuses a step's push // (a hook, a protected branch), the runner's log line has git's answer // and the refusal, and the step report's log tail (what the task page // shows for a stopped step) leads with them, before Engineering's own // tail; redacted and bounded in both. The reason is as before. func TestPushFailureKeepsTheHubAnswer(t *testing.T) { old := pushBackoff pushBackoff = time.Millisecond t.Cleanup(func() { pushBackoff = old }) e, f, r, _ := hubRig(t) logs := &lockedBuffer{} e.O.Logger = slog.New(slog.NewTextHandler(logs, nil)) secret := "Authorization: Bearer s3cr3t-hub-token, see https://agent:s3cr3t-pw@amadan.net/x" hook := "#!/bin/sh\necho 'GL-HOOK-ERR: you cannot push to the default branch. " + secret + "' >&2\nexit 1\n" if err := os.WriteFile(filepath.Join(f.dir, "hooks", "pre-receive"), []byte(hook), 0700); err != nil { t.Fatal(err) } // The step's private repository holds a commit on main. r.Specialist, r.StepID, r.Branch = wire.SpecialistEngineering, "s2", "equipo/tk1-another-fix" base := gitOut(t, f.dir, "rev-parse", "main") gitDir := e.tree(r, base).GitDir gitOut(t, filepath.Dir(f.dir), "clone", "-q", "--bare", f.dir, gitDir) tree := gitOut(t, filepath.Dir(f.dir), "--git-dir="+gitDir, "rev-parse", base+"^{tree}") r.Base, r.Candidate = base, gitOut(t, filepath.Dir(f.dir), "--git-dir="+gitDir, "commit-tree", tree, "-p", base, "-m", "another fix") rep := e.publish(context.Background(), r, wire.StepReport{LogTail: "Engineering's own tail"}) line := string(logs.Bytes()) if rep.State != wire.StepFailed || rep.Reason != "push_failed" || rep.Pushed { t.Fatalf("report %+v", rep) } detail, rest, _ := strings.Cut(rep.LogTail, "\n\n") if !strings.HasPrefix(detail, "refusal: refused\npush: ") || !strings.Contains(detail, "[remote rejected] (pre-receive hook declined)") || !strings.Contains(detail, "GL-HOOK-ERR: you cannot push to the default branch.") || rest != "Engineering's own tail" { t.Fatalf("log tail %q", rep.LogTail) } if !strings.Contains(line, `msg="step push"`) || !strings.Contains(line, "refusal=refused") || !strings.Contains(line, "GL-HOOK-ERR: you cannot push to the default branch.") || !strings.Contains(line, "Authorization: ***") { t.Fatalf("log %q", line) } if strings.Contains(rep.LogTail+line, "s3cr3t") || len(detail) > 500 || len(line) > 800 { t.Fatalf("not redacted or bounded: %q / %q", rep.LogTail, line) } // The default branch cannot be read: the push is never tried, and the // report and the log say why. before := logs.Len() f.dir = filepath.Join(t.TempDir(), "missing.git") rep = e.publish(context.Background(), r, wire.StepReport{}) line = string(logs.Bytes()[before:]) if rep.Reason != "push_failed" || !strings.HasPrefix(rep.LogTail, "error: reading the default branch: ls-remote: exit status 128: ") || !strings.Contains(rep.LogTail, "does not appear to be a git repository") || strings.Contains(line, "refusal=") || !strings.Contains(line, "does not appear to be a git repository") { t.Fatalf("unreadable default: %+v %q", rep, line) } // The remote cannot be resolved, in words that carry credentials. before = logs.Len() e.Clones.Forge = brokenRemote{f} rep = e.publish(context.Background(), r, wire.StepReport{}) line = string(logs.Bytes()[before:]) want := "reading the default branch: dial https://***@amadan.net/x: Authorization: *** refused" if rep.Reason != "push_failed" || rep.LogTail != "error: "+want || !strings.Contains(line, want) || strings.Contains(rep.LogTail+line, "s3cr3t") { t.Fatalf("unresolved remote: %+v %q", rep, line) } }