From f7c8d7b7466b459ba906896ce9687b7d0fa9ba50 Mon Sep 17 00:00:00 2001 From: Virgil Date: Wed, 1 Apr 2026 23:54:57 +0000 Subject: [PATCH] Add pull request changed files endpoint Co-Authored-By: Virgil --- pulls.go | 12 +++++++++ pulls_test.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/pulls.go b/pulls.go index 9d69a96..bdab33d 100644 --- a/pulls.go +++ b/pulls.go @@ -56,6 +56,18 @@ func (s *PullService) IterReviews(ctx context.Context, owner, repo string, index return ListIter[types.PullReview](ctx, s.client, path, nil) } +// ListFiles returns all changed files on a pull request. +func (s *PullService) ListFiles(ctx context.Context, owner, repo string, index int64) ([]types.ChangedFile, error) { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/pulls/{index}/files", pathParams("owner", owner, "repo", repo, "index", int64String(index))) + return ListAll[types.ChangedFile](ctx, s.client, path, nil) +} + +// IterFiles returns an iterator over all changed files on a pull request. +func (s *PullService) IterFiles(ctx context.Context, owner, repo string, index int64) iter.Seq2[types.ChangedFile, error] { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/pulls/{index}/files", pathParams("owner", owner, "repo", repo, "index", int64String(index))) + return ListIter[types.ChangedFile](ctx, s.client, path, nil) +} + // ListReviewers returns all users who can be requested to review a pull request. func (s *PullService) ListReviewers(ctx context.Context, owner, repo string) ([]types.User, error) { path := ResolvePath("/api/v1/repos/{owner}/{repo}/reviewers", pathParams("owner", owner, "repo", repo)) diff --git a/pulls_test.go b/pulls_test.go index 762eae4..65225cf 100644 --- a/pulls_test.go +++ b/pulls_test.go @@ -121,6 +121,80 @@ func TestPullService_ListReviewers_Good(t *testing.T) { } } +func TestPullService_ListFiles_Good(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + t.Errorf("expected GET, got %s", r.Method) + } + if r.URL.Path != "/api/v1/repos/core/go-forge/pulls/7/files" { + t.Errorf("wrong path: %s", r.URL.Path) + http.NotFound(w, r) + return + } + w.Header().Set("X-Total-Count", "1") + json.NewEncoder(w).Encode([]types.ChangedFile{ + {Filename: "README.md", Status: "modified", Additions: 2, Deletions: 1}, + }) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + files, err := f.Pulls.ListFiles(context.Background(), "core", "go-forge", 7) + if err != nil { + t.Fatal(err) + } + if len(files) != 1 { + t.Fatalf("got %d files, want 1", len(files)) + } + if files[0].Filename != "README.md" || files[0].Status != "modified" { + t.Fatalf("got %#v", files[0]) + } +} + +func TestPullService_IterFiles_Good(t *testing.T) { + requests := 0 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + requests++ + if r.Method != http.MethodGet { + t.Errorf("expected GET, got %s", r.Method) + } + if r.URL.Path != "/api/v1/repos/core/go-forge/pulls/7/files" { + t.Errorf("wrong path: %s", r.URL.Path) + http.NotFound(w, r) + return + } + switch requests { + case 1: + if got := r.URL.Query().Get("page"); got != "1" { + t.Errorf("got page=%q, want %q", got, "1") + } + w.Header().Set("X-Total-Count", "2") + json.NewEncoder(w).Encode([]types.ChangedFile{{Filename: "README.md", Status: "modified"}}) + case 2: + if got := r.URL.Query().Get("page"); got != "2" { + t.Errorf("got page=%q, want %q", got, "2") + } + w.Header().Set("X-Total-Count", "2") + json.NewEncoder(w).Encode([]types.ChangedFile{{Filename: "docs/guide.md", Status: "added"}}) + default: + t.Fatalf("unexpected request %d", requests) + } + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + var got []string + for file, err := range f.Pulls.IterFiles(context.Background(), "core", "go-forge", 7) { + if err != nil { + t.Fatal(err) + } + got = append(got, file.Filename) + } + if len(got) != 2 || got[0] != "README.md" || got[1] != "docs/guide.md" { + t.Fatalf("got %#v", got) + } +} + func TestPullService_IterReviewers_Good(t *testing.T) { requests := 0 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {