feat(gitea): branch list and commit-status endpoints
This commit is contained in:
+148
@@ -228,3 +228,151 @@ func TestGetRepoMissing(t *testing.T) {
|
||||
t.Fatalf("err = %v, want ErrNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
// --- branches ---------------------------------------------------------------
|
||||
|
||||
func TestListBranches(t *testing.T) {
|
||||
c, srv := newClient(t)
|
||||
srv.AddRepo("ukrrs", "MOPAC", "main", "main", "feat/quota", "fix/lint")
|
||||
|
||||
branches, err := c.ListBranches(context.Background(), "ukrrs", "MOPAC")
|
||||
if err != nil {
|
||||
t.Fatalf("ListBranches: %v", err)
|
||||
}
|
||||
if len(branches) != 3 {
|
||||
t.Fatalf("got %d branches, want 3: %+v", len(branches), branches)
|
||||
}
|
||||
if branches[0].Name != "main" || branches[2].Name != "fix/lint" {
|
||||
t.Fatalf("order = %+v", branches)
|
||||
}
|
||||
for _, b := range branches {
|
||||
if len(b.Commit.ID) != 40 {
|
||||
t.Errorf("branch %s tip sha = %q, want 40-hex", b.Name, b.Commit.ID)
|
||||
}
|
||||
}
|
||||
if reqs := srv.Requests(); reqs[0].Path != "/api/v1/repos/ukrrs/MOPAC/branches" {
|
||||
t.Fatalf("path = %s", reqs[0].Path)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListBranchesMissingRepo(t *testing.T) {
|
||||
c, _ := newClient(t)
|
||||
_, err := c.ListBranches(context.Background(), "ukrrs", "absent")
|
||||
if !errors.Is(err, gitea.ErrNotFound) {
|
||||
t.Fatalf("err = %v, want ErrNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
// --- commit statuses ----------------------------------------------------------
|
||||
|
||||
func TestCommitStatusFlow(t *testing.T) {
|
||||
c, srv := newClient(t)
|
||||
srv.AddRepo("ukrrs", "MOPAC", "main", "main")
|
||||
sha := srv.AddBranch("ukrrs", "MOPAC", "feat/quota")
|
||||
|
||||
st, err := c.CreateCommitStatus(context.Background(), "ukrrs", "MOPAC", sha, gitea.CommitStatusParams{
|
||||
State: gitea.StatusPending,
|
||||
Context: "ci/lint",
|
||||
Description: "lint queued",
|
||||
TargetURL: "https://ci.example/run/1",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("CreateCommitStatus: %v", err)
|
||||
}
|
||||
if st.ID == 0 || st.State != "pending" || st.Context != "ci/lint" {
|
||||
t.Fatalf("status = %+v", st)
|
||||
}
|
||||
if reqs := srv.Requests(); reqs[0].Path != "/api/v1/repos/ukrrs/MOPAC/statuses/"+sha {
|
||||
t.Fatalf("path = %s", reqs[0].Path)
|
||||
}
|
||||
|
||||
// Same context flips to success; a second context reports failure.
|
||||
if _, err := c.CreateCommitStatus(context.Background(), "ukrrs", "MOPAC", sha, gitea.CommitStatusParams{
|
||||
State: gitea.StatusSuccess,
|
||||
Context: "ci/lint",
|
||||
}); err != nil {
|
||||
t.Fatalf("CreateCommitStatus flip: %v", err)
|
||||
}
|
||||
if _, err := c.CreateCommitStatus(context.Background(), "ukrrs", "MOPAC", sha, gitea.CommitStatusParams{
|
||||
State: gitea.StatusFailure,
|
||||
Context: "ci/test",
|
||||
}); err != nil {
|
||||
t.Fatalf("CreateCommitStatus second context: %v", err)
|
||||
}
|
||||
|
||||
// Listing by branch NAME resolves to the tip sha and folds to the
|
||||
// latest status per context.
|
||||
listed, err := c.ListCommitStatuses(context.Background(), "ukrrs", "MOPAC", "feat/quota")
|
||||
if err != nil {
|
||||
t.Fatalf("ListCommitStatuses: %v", err)
|
||||
}
|
||||
if len(listed) != 2 {
|
||||
t.Fatalf("got %d statuses, want 2 (latest per context): %+v", len(listed), listed)
|
||||
}
|
||||
byCtx := map[string]string{}
|
||||
for _, s := range listed {
|
||||
byCtx[s.Context] = s.State
|
||||
}
|
||||
if byCtx["ci/lint"] != "success" || byCtx["ci/test"] != "failure" {
|
||||
t.Fatalf("folded = %v", byCtx)
|
||||
}
|
||||
|
||||
// Combined status is the worst of the latest per context.
|
||||
comb, err := c.GetCombinedStatus(context.Background(), "ukrrs", "MOPAC", "feat/quota")
|
||||
if err != nil {
|
||||
t.Fatalf("GetCombinedStatus: %v", err)
|
||||
}
|
||||
if comb.State != "failure" || comb.SHA != sha || comb.TotalCount != 2 {
|
||||
t.Fatalf("combined = %+v", comb)
|
||||
}
|
||||
|
||||
// Short sha (>= 7 chars) resolves too.
|
||||
short := sha[:7]
|
||||
if _, err := c.GetCombinedStatus(context.Background(), "ukrrs", "MOPAC", short); err != nil {
|
||||
t.Fatalf("short sha: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommitStatusErrors(t *testing.T) {
|
||||
c, srv := newClient(t)
|
||||
srv.AddRepo("ukrrs", "MOPAC", "main", "main")
|
||||
sha := srv.AddBranch("ukrrs", "MOPAC", "feat/quota")
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
run func() error
|
||||
want error
|
||||
}{
|
||||
{
|
||||
name: "unknown sha",
|
||||
run: func() error {
|
||||
_, err := c.CreateCommitStatus(context.Background(), "ukrrs", "MOPAC", strings.Repeat("d", 40), gitea.CommitStatusParams{State: gitea.StatusSuccess})
|
||||
return err
|
||||
},
|
||||
want: gitea.ErrNotFound,
|
||||
},
|
||||
{
|
||||
name: "invalid state",
|
||||
run: func() error {
|
||||
_, err := c.CreateCommitStatus(context.Background(), "ukrrs", "MOPAC", sha, gitea.CommitStatusParams{State: "green"})
|
||||
return err
|
||||
},
|
||||
want: gitea.ErrValidation,
|
||||
},
|
||||
{
|
||||
name: "combined with no statuses",
|
||||
run: func() error {
|
||||
_, err := c.GetCombinedStatus(context.Background(), "ukrrs", "MOPAC", "main")
|
||||
return err
|
||||
},
|
||||
want: gitea.ErrNotFound,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := tt.run(); !errors.Is(err, tt.want) {
|
||||
t.Fatalf("err = %v, want %v", err, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user