feat(repos): add pinned pull request listing
Some checks are pending
Test / test (push) Waiting to run
Security Scan / security (push) Successful in 13s

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-02 02:16:09 +00:00
parent 204696915f
commit 232c1fa943
2 changed files with 87 additions and 0 deletions

View file

@ -556,6 +556,18 @@ func (s *RepoService) GetNewPinAllowed(ctx context.Context, owner, repo string)
return &out, nil
}
// ListPinnedPullRequests returns all pinned pull requests in a repository.
func (s *RepoService) ListPinnedPullRequests(ctx context.Context, owner, repo string) ([]types.PullRequest, error) {
path := ResolvePath("/api/v1/repos/{owner}/{repo}/pulls/pinned", pathParams("owner", owner, "repo", repo))
return ListAll[types.PullRequest](ctx, s.client, path, nil)
}
// IterPinnedPullRequests returns an iterator over all pinned pull requests in a repository.
func (s *RepoService) IterPinnedPullRequests(ctx context.Context, owner, repo string) iter.Seq2[types.PullRequest, error] {
path := ResolvePath("/api/v1/repos/{owner}/{repo}/pulls/pinned", pathParams("owner", owner, "repo", repo))
return ListIter[types.PullRequest](ctx, s.client, path, nil)
}
// UpdateAvatar updates a repository avatar.
func (s *RepoService) UpdateAvatar(ctx context.Context, owner, repo string, opts *types.UpdateRepoAvatarOption) error {
path := ResolvePath("/api/v1/repos/{owner}/{repo}/avatar", pathParams("owner", owner, "repo", repo))

View file

@ -1138,6 +1138,81 @@ func TestRepoService_GetSubscription_Good(t *testing.T) {
}
}
func TestRepoService_ListPinnedPullRequests_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/pinned" {
t.Errorf("wrong path: %s", r.URL.Path)
http.NotFound(w, r)
return
}
w.Header().Set("X-Total-Count", "2")
json.NewEncoder(w).Encode([]types.PullRequest{
{ID: 7, Title: "pin me"},
{ID: 8, Title: "keep me"},
})
}))
defer srv.Close()
f := NewForge(srv.URL, "tok")
pulls, err := f.Repos.ListPinnedPullRequests(context.Background(), "core", "go-forge")
if err != nil {
t.Fatal(err)
}
if got, want := len(pulls), 2; got != want {
t.Fatalf("got %d pull requests, want %d", got, want)
}
if pulls[0].Title != "pin me" {
t.Fatalf("got first title %q", pulls[0].Title)
}
}
func TestRepoService_IterPinnedPullRequests_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/pinned" {
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.PullRequest{{ID: 7, Title: "pin me"}})
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.PullRequest{{ID: 8, Title: "keep me"}})
default:
t.Fatalf("unexpected request %d", requests)
}
}))
defer srv.Close()
f := NewForge(srv.URL, "tok")
var got []string
for pr, err := range f.Repos.IterPinnedPullRequests(context.Background(), "core", "go-forge") {
if err != nil {
t.Fatal(err)
}
got = append(got, pr.Title)
}
if len(got) != 2 || got[0] != "pin me" || got[1] != "keep me" {
t.Fatalf("got %#v", got)
}
}
func TestRepoService_ListStargazers_Good(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {