|
| 1 | +package gitea |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + forge "github.com/git-pkgs/forge" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestGiteaListCommitStatuses(t *testing.T) { |
| 13 | + mux := http.NewServeMux() |
| 14 | + mux.HandleFunc("GET /api/v1/version", giteaVersionHandler) |
| 15 | + mux.HandleFunc("GET /api/v1/repos/testorg/testrepo/commits/abc123/statuses", func(w http.ResponseWriter, r *http.Request) { |
| 16 | + _ = json.NewEncoder(w).Encode([]map[string]any{ |
| 17 | + { |
| 18 | + "id": 1, |
| 19 | + "status": "success", |
| 20 | + "context": "ci/tests", |
| 21 | + "description": "All tests passed", |
| 22 | + "target_url": "https://ci.example.com/1", |
| 23 | + "creator": map[string]any{"login": "bot"}, |
| 24 | + }, |
| 25 | + }) |
| 26 | + }) |
| 27 | + |
| 28 | + srv := httptest.NewServer(mux) |
| 29 | + defer srv.Close() |
| 30 | + |
| 31 | + f := New(srv.URL, "test-token", nil) |
| 32 | + statuses, err := f.CommitStatuses().List(context.Background(), "testorg", "testrepo", "abc123") |
| 33 | + if err != nil { |
| 34 | + t.Fatalf("unexpected error: %v", err) |
| 35 | + } |
| 36 | + if len(statuses) != 1 { |
| 37 | + t.Fatalf("expected 1 status, got %d", len(statuses)) |
| 38 | + } |
| 39 | + assertEqual(t, "statuses[0].State", "success", statuses[0].State) |
| 40 | + assertEqual(t, "statuses[0].Context", "ci/tests", statuses[0].Context) |
| 41 | + assertEqual(t, "statuses[0].Creator", "bot", statuses[0].Creator) |
| 42 | +} |
| 43 | + |
| 44 | +func TestGiteaSetCommitStatus(t *testing.T) { |
| 45 | + mux := http.NewServeMux() |
| 46 | + mux.HandleFunc("GET /api/v1/version", giteaVersionHandler) |
| 47 | + mux.HandleFunc("POST /api/v1/repos/testorg/testrepo/statuses/abc123", func(w http.ResponseWriter, r *http.Request) { |
| 48 | + w.WriteHeader(http.StatusCreated) |
| 49 | + _ = json.NewEncoder(w).Encode(map[string]any{ |
| 50 | + "id": 1, |
| 51 | + "status": "success", |
| 52 | + "context": "my-check", |
| 53 | + "description": "Everything passed", |
| 54 | + "target_url": "https://example.com", |
| 55 | + "creator": map[string]any{"login": "bot"}, |
| 56 | + }) |
| 57 | + }) |
| 58 | + |
| 59 | + srv := httptest.NewServer(mux) |
| 60 | + defer srv.Close() |
| 61 | + |
| 62 | + f := New(srv.URL, "test-token", nil) |
| 63 | + status, err := f.CommitStatuses().Set(context.Background(), "testorg", "testrepo", "abc123", forge.SetCommitStatusOpts{ |
| 64 | + State: "success", |
| 65 | + Context: "my-check", |
| 66 | + Description: "Everything passed", |
| 67 | + TargetURL: "https://example.com", |
| 68 | + }) |
| 69 | + if err != nil { |
| 70 | + t.Fatalf("unexpected error: %v", err) |
| 71 | + } |
| 72 | + assertEqual(t, "State", "success", status.State) |
| 73 | + assertEqual(t, "Context", "my-check", status.Context) |
| 74 | +} |
| 75 | + |
| 76 | +func TestGiteaListCommitStatusesNotFound(t *testing.T) { |
| 77 | + mux := http.NewServeMux() |
| 78 | + mux.HandleFunc("GET /api/v1/version", giteaVersionHandler) |
| 79 | + mux.HandleFunc("GET /api/v1/repos/testorg/nope/commits/abc123/statuses", func(w http.ResponseWriter, r *http.Request) { |
| 80 | + w.WriteHeader(http.StatusNotFound) |
| 81 | + }) |
| 82 | + |
| 83 | + srv := httptest.NewServer(mux) |
| 84 | + defer srv.Close() |
| 85 | + |
| 86 | + f := New(srv.URL, "test-token", nil) |
| 87 | + _, err := f.CommitStatuses().List(context.Background(), "testorg", "nope", "abc123") |
| 88 | + if err != forge.ErrNotFound { |
| 89 | + t.Fatalf("expected forge.ErrNotFound, got %v", err) |
| 90 | + } |
| 91 | +} |
0 commit comments