diff --git a/github/methods_test.go b/github/methods_test.go new file mode 100644 index 0000000..51510c3 --- /dev/null +++ b/github/methods_test.go @@ -0,0 +1,518 @@ +package github + +import ( + "context" + "encoding/base64" + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "testing" +) + +// newTestClient creates a Client pointed at the test server. +func newTestClient(srv *httptest.Server) *Client { + return NewClient("test-token", srv.URL, AllowInsecureHTTPForTest()) +} + +func TestGetPullRequest(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet || r.URL.Path != "/repos/owner/repo/pulls/42" { + http.Error(w, "unexpected", http.StatusNotFound) + return + } + if got := r.Header.Get("Authorization"); got != "Bearer test-token" { + http.Error(w, "unauthorized", http.StatusUnauthorized) + return + } + w.Header().Set("Content-Type", "application/json") + fmt.Fprintln(w, `{"title":"Fix bug","body":"Body text","head":{"sha":"abc1234","ref":"fix/bug"}}`) + })) + defer srv.Close() + + c := newTestClient(srv) + pr, err := c.GetPullRequest(context.Background(), "owner", "repo", 42) + if err != nil { + t.Fatalf("GetPullRequest: %v", err) + } + if pr.Title != "Fix bug" { + t.Errorf("Title = %q, want %q", pr.Title, "Fix bug") + } + if pr.Head.Sha != "abc1234" { + t.Errorf("Head.Sha = %q, want %q", pr.Head.Sha, "abc1234") + } + if pr.Head.Ref != "fix/bug" { + t.Errorf("Head.Ref = %q, want %q", pr.Head.Ref, "fix/bug") + } +} + +func TestGetPullRequest_NotFound(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + http.Error(w, `{"message":"Not Found"}`, http.StatusNotFound) + })) + defer srv.Close() + + c := newTestClient(srv) + _, err := c.GetPullRequest(context.Background(), "owner", "repo", 99) + if err == nil { + t.Fatal("expected error for 404, got nil") + } + if !IsNotFound(err) { + t.Errorf("expected IsNotFound error, got %v", err) + } +} + +func TestGetPullRequestDiff(t *testing.T) { + diffText := "diff --git a/foo.go b/foo.go\n@@ -1,1 +1,2 @@\n+added" + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/repos/owner/repo/pulls/1" { + http.Error(w, "unexpected", http.StatusNotFound) + return + } + if r.Header.Get("Accept") != "application/vnd.github.v3.diff" { + http.Error(w, "wrong accept", http.StatusNotAcceptable) + return + } + w.Header().Set("Content-Type", "text/plain") + fmt.Fprint(w, diffText) + })) + defer srv.Close() + + c := newTestClient(srv) + got, err := c.GetPullRequestDiff(context.Background(), "owner", "repo", 1) + if err != nil { + t.Fatalf("GetPullRequestDiff: %v", err) + } + if got != diffText { + t.Errorf("diff = %q, want %q", got, diffText) + } +} + +func TestGetPullRequestFiles(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/repos/owner/repo/pulls/5/files" { + http.Error(w, "unexpected", http.StatusNotFound) + return + } + w.Header().Set("Content-Type", "application/json") + fmt.Fprintln(w, `[{"filename":"foo.go","status":"added"},{"filename":"bar.go","status":"modified"}]`) + })) + defer srv.Close() + + c := newTestClient(srv) + files, err := c.GetPullRequestFiles(context.Background(), "owner", "repo", 5) + if err != nil { + t.Fatalf("GetPullRequestFiles: %v", err) + } + if len(files) != 2 { + t.Fatalf("len(files) = %d, want 2", len(files)) + } + if files[0].Filename != "foo.go" || files[0].Status != "added" { + t.Errorf("files[0] = %+v", files[0]) + } +} + +func TestGetCommitStatuses(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/repos/owner/repo/commits/deadbeef/statuses" { + http.Error(w, "unexpected", http.StatusNotFound) + return + } + w.Header().Set("Content-Type", "application/json") + fmt.Fprintln(w, `[{"state":"success","context":"ci/test","description":"Tests passed","target_url":"https://ci.example.com"}]`) + })) + defer srv.Close() + + c := newTestClient(srv) + statuses, err := c.GetCommitStatuses(context.Background(), "owner", "repo", "deadbeef") + if err != nil { + t.Fatalf("GetCommitStatuses: %v", err) + } + if len(statuses) != 1 { + t.Fatalf("len(statuses) = %d, want 1", len(statuses)) + } + if statuses[0].State != "success" { + t.Errorf("State = %q, want success", statuses[0].State) + } + if statuses[0].Context != "ci/test" { + t.Errorf("Context = %q, want ci/test", statuses[0].Context) + } +} + +func TestGetFileContent(t *testing.T) { + content := "package main\nfunc main() {}\n" + encoded := base64.StdEncoding.EncodeToString([]byte(content)) + // GitHub wraps base64 in newlines every 60 chars + var chunked string + for i := 0; i < len(encoded); i += 60 { + end := i + 60 + if end > len(encoded) { + end = len(encoded) + } + chunked += encoded[i:end] + "\n" + } + + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/repos/owner/repo/contents/main.go" { + http.Error(w, "unexpected path: "+r.URL.Path, http.StatusNotFound) + return + } + w.Header().Set("Content-Type", "application/json") + resp := map[string]string{ + "content": chunked, + "encoding": "base64", + } + if err := json.NewEncoder(w).Encode(resp); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } + })) + defer srv.Close() + + c := newTestClient(srv) + got, err := c.GetFileContent(context.Background(), "owner", "repo", "main.go") + if err != nil { + t.Fatalf("GetFileContent: %v", err) + } + if got != content { + t.Errorf("content = %q, want %q", got, content) + } +} + +func TestGetFileContentRef(t *testing.T) { + content := "hello world" + encoded := base64.StdEncoding.EncodeToString([]byte(content)) + + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/repos/owner/repo/contents/README.md" { + http.Error(w, "unexpected", http.StatusNotFound) + return + } + if r.URL.Query().Get("ref") != "abc123" { + http.Error(w, "missing ref", http.StatusBadRequest) + return + } + w.Header().Set("Content-Type", "application/json") + resp := map[string]string{"content": encoded + "\n", "encoding": "base64"} + if err := json.NewEncoder(w).Encode(resp); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } + })) + defer srv.Close() + + c := newTestClient(srv) + got, err := c.GetFileContentRef(context.Background(), "owner", "repo", "README.md", "abc123") + if err != nil { + t.Fatalf("GetFileContentRef: %v", err) + } + if got != content { + t.Errorf("content = %q, want %q", got, content) + } +} + +func TestListContents(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/repos/owner/repo/contents" { + w.Header().Set("Content-Type", "application/json") + fmt.Fprintln(w, `[{"name":"README.md","path":"README.md","type":"file"},{"name":"src","path":"src","type":"dir"}]`) + return + } + http.Error(w, "unexpected: "+r.URL.Path, http.StatusNotFound) + })) + defer srv.Close() + + c := newTestClient(srv) + entries, err := c.ListContents(context.Background(), "owner", "repo", "") + if err != nil { + t.Fatalf("ListContents: %v", err) + } + if len(entries) != 2 { + t.Fatalf("len(entries) = %d, want 2", len(entries)) + } + if entries[0].Name != "README.md" || entries[0].Type != "file" { + t.Errorf("entries[0] = %+v", entries[0]) + } + if entries[1].Name != "src" || entries[1].Type != "dir" { + t.Errorf("entries[1] = %+v", entries[1]) + } +} + +func TestListContents_Dot(t *testing.T) { + // "." should be treated as "" (root) + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/repos/owner/repo/contents" { + w.Header().Set("Content-Type", "application/json") + fmt.Fprintln(w, `[]`) + return + } + http.Error(w, "unexpected: "+r.URL.Path, http.StatusNotFound) + })) + defer srv.Close() + + c := newTestClient(srv) + entries, err := c.ListContents(context.Background(), "owner", "repo", ".") + if err != nil { + t.Fatalf("ListContents: %v", err) + } + if len(entries) != 0 { + t.Errorf("expected empty entries, got %d", len(entries)) + } +} + +func TestPostReview(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost || r.URL.Path != "/repos/owner/repo/pulls/10/reviews" { + http.Error(w, "unexpected", http.StatusNotFound) + return + } + var payload struct { + Body string `json:"body"` + Event string `json:"event"` + CommitID string `json:"commit_id"` + } + if err := json.NewDecoder(r.Body).Decode(&payload); err != nil { + http.Error(w, "bad body", http.StatusBadRequest) + return + } + // Verify APPROVED is normalized to APPROVE + if payload.Event != "APPROVE" { + http.Error(w, fmt.Sprintf("expected APPROVE, got %s", payload.Event), http.StatusBadRequest) + return + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + fmt.Fprintf(w, `{"id":99,"body":%q,"user":{"login":"bot"},"state":"APPROVED","commit_id":%q}`, payload.Body, payload.CommitID) + })) + defer srv.Close() + + c := newTestClient(srv) + // Pass "APPROVED" (Gitea-style) — should be normalized to APPROVE + review, err := c.PostReview(context.Background(), "owner", "repo", 10, "APPROVED", "Looks good", "abc123", nil) + if err != nil { + t.Fatalf("PostReview: %v", err) + } + if review.ID != 99 { + t.Errorf("review.ID = %d, want 99", review.ID) + } + if review.User.Login != "bot" { + t.Errorf("review.User.Login = %q, want bot", review.User.Login) + } +} + +func TestListReviews(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/repos/owner/repo/pulls/7/reviews" { + http.Error(w, "unexpected", http.StatusNotFound) + return + } + w.Header().Set("Content-Type", "application/json") + fmt.Fprintln(w, `[{"id":1,"body":"LGTM","user":{"login":"alice"},"state":"APPROVED","commit_id":"abc"}]`) + })) + defer srv.Close() + + c := newTestClient(srv) + reviews, err := c.ListReviews(context.Background(), "owner", "repo", 7) + if err != nil { + t.Fatalf("ListReviews: %v", err) + } + if len(reviews) != 1 { + t.Fatalf("len(reviews) = %d, want 1", len(reviews)) + } + if reviews[0].User.Login != "alice" { + t.Errorf("User.Login = %q, want alice", reviews[0].User.Login) + } +} + +func TestGetAuthenticatedUser(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/user" { + http.Error(w, "unexpected", http.StatusNotFound) + return + } + w.Header().Set("Content-Type", "application/json") + fmt.Fprintln(w, `{"login":"sonnet-review"}`) + })) + defer srv.Close() + + c := newTestClient(srv) + login, err := c.GetAuthenticatedUser(context.Background()) + if err != nil { + t.Fatalf("GetAuthenticatedUser: %v", err) + } + if login != "sonnet-review" { + t.Errorf("login = %q, want sonnet-review", login) + } +} + +func TestResolveComment_NoOp(t *testing.T) { + // ResolveComment is a no-op on GitHub — should not make any HTTP call. + callCount := 0 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + callCount++ + http.Error(w, "unexpected call", http.StatusInternalServerError) + })) + defer srv.Close() + + c := newTestClient(srv) + if err := c.ResolveComment(context.Background(), "owner", "repo", 123); err != nil { + t.Errorf("ResolveComment: %v (expected no-op)", err) + } + if callCount != 0 { + t.Errorf("expected no HTTP calls, got %d", callCount) + } +} + +func TestGetTimelineReviewCommentIDForReview(t *testing.T) { + // Should return reviewID unchanged without making HTTP calls. + callCount := 0 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + callCount++ + http.Error(w, "unexpected", http.StatusInternalServerError) + })) + defer srv.Close() + + c := newTestClient(srv) + got, err := c.GetTimelineReviewCommentIDForReview(context.Background(), "owner", "repo", 5, 42) + if err != nil { + t.Fatalf("GetTimelineReviewCommentIDForReview: %v", err) + } + if got != 42 { + t.Errorf("got %d, want 42", got) + } + if callCount != 0 { + t.Errorf("expected no HTTP calls, got %d", callCount) + } +} + +func TestRequestReviewer(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost || r.URL.Path != "/repos/owner/repo/pulls/3/requested_reviewers" { + http.Error(w, "unexpected", http.StatusNotFound) + return + } + var payload struct { + Reviewers []string `json:"reviewers"` + } + if err := json.NewDecoder(r.Body).Decode(&payload); err != nil || len(payload.Reviewers) == 0 { + http.Error(w, "bad body", http.StatusBadRequest) + return + } + if payload.Reviewers[0] != "bot-user" { + http.Error(w, fmt.Sprintf("unexpected reviewer %q", payload.Reviewers[0]), http.StatusBadRequest) + return + } + w.WriteHeader(http.StatusCreated) + fmt.Fprintln(w, `{}`) + })) + defer srv.Close() + + c := newTestClient(srv) + if err := c.RequestReviewer(context.Background(), "owner", "repo", 3, "bot-user"); err != nil { + t.Errorf("RequestReviewer: %v", err) + } +} + +func TestEditComment(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPatch || r.URL.Path != "/repos/owner/repo/pulls/comments/55" { + http.Error(w, "unexpected", http.StatusNotFound) + return + } + var payload struct { + Body string `json:"body"` + } + if err := json.NewDecoder(r.Body).Decode(&payload); err != nil { + http.Error(w, "bad body", http.StatusBadRequest) + return + } + if payload.Body != "updated body" { + http.Error(w, "wrong body", http.StatusBadRequest) + return + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + fmt.Fprintln(w, `{"id":55,"body":"updated body"}`) + })) + defer srv.Close() + + c := newTestClient(srv) + if err := c.EditComment(context.Background(), "owner", "repo", 55, "updated body"); err != nil { + t.Errorf("EditComment: %v", err) + } +} + +func TestListReviewComments(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/repos/owner/repo/pulls/9/reviews/20/comments" { + http.Error(w, "unexpected", http.StatusNotFound) + return + } + w.Header().Set("Content-Type", "application/json") + fmt.Fprintln(w, `[{"id":100,"path":"main.go","position":5,"body":"Needs fix"}]`) + })) + defer srv.Close() + + c := newTestClient(srv) + comments, err := c.ListReviewComments(context.Background(), "owner", "repo", 9, 20) + if err != nil { + t.Fatalf("ListReviewComments: %v", err) + } + if len(comments) != 1 { + t.Fatalf("len(comments) = %d, want 1", len(comments)) + } + if comments[0].Path != "main.go" { + t.Errorf("Path = %q, want main.go", comments[0].Path) + } + if comments[0].Position != 5 { + t.Errorf("Position = %d, want 5", comments[0].Position) + } +} + +func TestDeleteReview(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodDelete || r.URL.Path != "/repos/owner/repo/pulls/7/reviews/11" { + http.Error(w, "unexpected", http.StatusNotFound) + return + } + w.WriteHeader(http.StatusNoContent) + })) + defer srv.Close() + + c := newTestClient(srv) + if err := c.DeleteReview(context.Background(), "owner", "repo", 7, 11); err != nil { + t.Errorf("DeleteReview: %v", err) + } +} + +func TestGetAllFilesInPath(t *testing.T) { + content := "file content" + encoded := base64.StdEncoding.EncodeToString([]byte(content)) + + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/repos/owner/repo/contents/patterns": + w.Header().Set("Content-Type", "application/json") + fmt.Fprintln(w, `[{"name":"patterns.md","path":"patterns/patterns.md","type":"file"}]`) + case "/repos/owner/repo/contents/patterns/patterns.md": + w.Header().Set("Content-Type", "application/json") + resp := map[string]string{"content": encoded + "\n", "encoding": "base64"} + if err := json.NewEncoder(w).Encode(resp); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } + default: + http.Error(w, "unexpected: "+r.URL.Path, http.StatusNotFound) + } + })) + defer srv.Close() + + c := newTestClient(srv) + files, err := c.GetAllFilesInPath(context.Background(), "owner", "repo", "patterns") + if err != nil { + t.Fatalf("GetAllFilesInPath: %v", err) + } + if len(files) != 1 { + t.Fatalf("len(files) = %d, want 1", len(files)) + } + if files["patterns/patterns.md"] != content { + t.Errorf("content = %q, want %q", files["patterns/patterns.md"], content) + } +}