feat(tools): pr_list

This commit is contained in:
Mathias Bergqvist
2026-05-06 22:46:11 +02:00
parent ddfcc32afd
commit 388131c8cd
4 changed files with 187 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/url"
)
type PullRequest struct {
@@ -101,3 +102,29 @@ func (c *Client) GetPullRequestDiff(ctx context.Context, owner, repo string, ind
}
return resp.Body, nil
}
func (c *Client) ListPullRequests(ctx context.Context, owner, repo, state, head string, page, limit int) ([]PullRequest, error) {
if page < 1 {
page = 1
}
if limit < 1 {
limit = 30
}
p := fmt.Sprintf("/api/v1/repos/%s/%s/pulls?state=%s&page=%d&limit=%d",
owner, repo, url.QueryEscape(state), page, limit)
if head != "" {
p += "&head=" + url.QueryEscape(head)
}
body, status, err := c.GetJSON(ctx, p)
if err != nil {
return nil, err
}
if err := MapStatus(status, body); err != nil {
return nil, err
}
var prs []PullRequest
if err := json.Unmarshal(body, &prs); err != nil {
return nil, err
}
return prs, nil
}

View File

@@ -136,3 +136,21 @@ func TestGetPullRequestDiff(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, []byte(rawDiff), diff)
}
func TestListPullRequests(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "/api/v1/repos/o/r/pulls", r.URL.Path)
assert.Equal(t, "open", r.URL.Query().Get("state"))
assert.Equal(t, "feat/x", r.URL.Query().Get("head"))
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`[` + pullFixture + `]`))
}))
defer srv.Close()
c := gitea.NewClient(srv.URL, "tok")
prs, err := c.ListPullRequests(context.Background(), "o", "r", "open", "feat/x", 0, 0)
require.NoError(t, err)
require.Len(t, prs, 1)
assert.Equal(t, 7, prs[0].Number)
assert.Equal(t, "feat/x", prs[0].Head.Ref)
}