Skip to content

Commit 625ce04

Browse files
authored
Merge pull request #33 from kayahk/feat/github-issue-read-tools
feat: add GitHub repository issue listing
2 parents 749fe27 + f4a0533 commit 625ce04

7 files changed

Lines changed: 172 additions & 17 deletions

File tree

‎backend/go.mod‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ module github.com/Paca-AI/first-party/github
33
go 1.24
44

55
require github.com/Paca-AI/plugin-sdk-go v0.4.1
6+
7+
replace github.com/Paca-AI/plugin-sdk-go => github.com/kayahk/plugin-sdk-go v0.0.0-20261102180211-0a7e5c7287f9

‎backend/go.sum‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
github.com/Paca-AI/plugin-sdk-go v0.4.1 h1:Fid4T5OryhJca+9dBtM09iTipli9KouY/3G5ju2Sz5Y=
2-
github.com/Paca-AI/plugin-sdk-go v0.4.1/go.mod h1:5WeC6cSEf2wM1ovICZbDaVky9oi5id/Qpdfc5LDAQnw=
1+
github.com/kayahk/plugin-sdk-go v0.0.0-20261102180211-0a7e5c7287f9 h1:OKM/oY0vR88PGi7LnZaGId2hHYroqt9BS63hmQbRS2Y=
2+
github.com/kayahk/plugin-sdk-go v0.0.0-20261102180211-0a7e5c7287f9/go.mod h1:5WeC6cSEf2wM1ovICZbDaVky9oi5id/Qpdfc5LDAQnw=

‎backend/spi_assert.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var (
99
_ gp.Provider = (*ghProvider)(nil)
1010
_ gp.ThreadResolver = (*ghProvider)(nil)
1111
_ gp.FileReader = (*ghProvider)(nil)
12+
_ gp.IssueLister = (*ghProvider)(nil)
1213
_ gp.LabelManager = (*ghProvider)(nil)
1314
_ gp.ReviewerRequester = (*ghProvider)(nil)
1415
_ gp.AssigneeSetter = (*ghProvider)(nil)

‎backend/spi_fakegithub_test.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,12 @@ func (g *fakeGitHub) repoSub(method, sub string, q url.Values, headers map[strin
478478
return resp(201, map[string]any{"ref": str(payload, "ref")})
479479

480480
// ── issues and comments ──
481+
case sub == "issues" && method == "GET":
482+
items := []map[string]any{}
483+
if q.Get("state") != "closed" && q.Get("page") == "1" {
484+
items = append(items, map[string]any{"number": 7, "title": "Bug report", "body": "it broke", "state": "open", "html_url": "https://github.com/acme/web/issues/7", "user": map[string]string{"login": "reader"}})
485+
}
486+
return resp(200, items)
481487
case seg[0] == "issues" && len(seg) == 2 && method == "GET":
482488
n, _ := strconv.Atoi(seg[1])
483489
if n != 7 && g.prs[n] == nil {

‎backend/spi_issues_test.go‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"net/url"
6+
"testing"
7+
8+
plugin "github.com/Paca-AI/plugin-sdk-go"
9+
gp "github.com/Paca-AI/plugin-sdk-go/gitprovider"
10+
)
11+
12+
func TestListIssuesFiltersPullRequestsWithoutLosingPages(t *testing.T) {
13+
p, g, _ := seededProvider(t)
14+
fetch = func(method, rawURL string, headers map[string]string, body string) (*plugin.FetchResponse, error) {
15+
u, err := url.Parse(rawURL)
16+
if err != nil {
17+
t.Fatal(err)
18+
}
19+
if u.Path != "/repos/acme/web/issues" {
20+
return g.fetch(method, rawURL, headers, body)
21+
}
22+
if method != "GET" || u.Query().Get("state") != "closed" || u.Query().Get("per_page") != "1" {
23+
t.Fatalf("unexpected request: %s %s", method, rawURL)
24+
}
25+
switch u.Query().Get("page") {
26+
case "1":
27+
return resp(200, []map[string]any{{"number": 1, "pull_request": map[string]any{"url": "x"}}})
28+
case "2":
29+
return resp(200, []map[string]any{{"number": 101, "title": "Bug", "body": "Details", "state": "closed", "html_url": "https://github.com/acme/web/issues/101", "user": map[string]string{"login": "alice"}, "labels": []map[string]string{{"name": "bug"}}, "assignees": []map[string]string{{"login": "bob"}}}})
30+
case "3":
31+
return resp(200, []map[string]any{})
32+
default:
33+
t.Fatalf("unexpected page: %s", rawURL)
34+
return nil, nil
35+
}
36+
}
37+
first, err := p.ListIssues(bg, gp.ListIssuesRequest{Target: webTarget(), State: "closed"})
38+
if err != nil || len(first.Items) != 0 || first.NextCursor != "2" {
39+
t.Fatalf("first page: %+v, %v", first, err)
40+
}
41+
second, err := p.ListIssues(bg, gp.ListIssuesRequest{Target: webTarget(), State: "closed", Cursor: first.NextCursor})
42+
if err != nil || len(second.Items) != 1 || second.NextCursor != "3" {
43+
t.Fatalf("second page: %+v, %v", second, err)
44+
}
45+
third, err := p.ListIssues(bg, gp.ListIssuesRequest{Target: webTarget(), State: "closed", Cursor: second.NextCursor})
46+
if err != nil || len(third.Items) != 0 || third.NextCursor != "" {
47+
t.Fatalf("third page: %+v, %v", third, err)
48+
}
49+
i := second.Items[0]
50+
if i.Number != "101" || i.Title != "Bug" || i.Body != "Details" || i.State != "closed" || i.Author != "alice" || i.URL != "https://github.com/acme/web/issues/101" || len(i.Labels) != 1 || i.Labels[0] != "bug" || len(i.Assignees) != 1 || i.Assignees[0] != "bob" {
51+
t.Fatalf("issue fields: %+v", i)
52+
}
53+
}
54+
55+
func TestListIssuesValidatesAndPropagatesErrors(t *testing.T) {
56+
p, g, _ := seededProvider(t)
57+
for _, req := range []gp.ListIssuesRequest{
58+
{Target: webTarget(), State: "invalid"},
59+
{Target: webTarget(), Cursor: "-1"},
60+
} {
61+
_, err := p.ListIssues(bg, req)
62+
if codeOf(t, err) != gp.CodeValidation {
63+
t.Fatalf("request %+v: %v", req, err)
64+
}
65+
}
66+
g.forceError["GET /repos/acme/web/issues"] = fakeResponse{403, `{"message":"Resource not accessible by integration"}`, nil}
67+
_, err := p.ListIssues(bg, gp.ListIssuesRequest{Target: webTarget()})
68+
if codeOf(t, err) != gp.CodeForbidden {
69+
t.Fatalf("GitHub error: %v", err)
70+
}
71+
}
72+
73+
func TestListIssuesWireAndDefaultState(t *testing.T) {
74+
p, g, _ := seededProvider(t)
75+
fetch = func(method, rawURL string, headers map[string]string, body string) (*plugin.FetchResponse, error) {
76+
u, _ := url.Parse(rawURL)
77+
if u.Path == "/repos/acme/web/issues" {
78+
if u.Query().Get("state") != "open" {
79+
t.Fatalf("default state: %s", rawURL)
80+
}
81+
return resp(200, []any{})
82+
}
83+
return g.fetch(method, rawURL, headers, body)
84+
}
85+
if !p.Describe().Capabilities.IssueList {
86+
t.Fatal("issue listing must be declared")
87+
}
88+
body, err := json.Marshal(gp.ListIssuesRequest{Target: webTarget()})
89+
if err != nil {
90+
t.Fatal(err)
91+
}
92+
status, out := gp.Dispatch(bg, p, "list_issues", body)
93+
if status != 200 {
94+
t.Fatalf("wire status %d: %s", status, out)
95+
}
96+
var wire struct {
97+
OK bool `json:"ok"`
98+
Result gp.IssuePage `json:"result"`
99+
}
100+
if err := json.Unmarshal(out, &wire); err != nil || !wire.OK || len(wire.Result.Items) != 0 {
101+
t.Fatalf("wire result: %s (%v)", out, err)
102+
}
103+
}

‎backend/spi_provider.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func (p *ghProvider) Describe() gp.Info {
6565

6666
ResolveThreads: true,
6767
FileRead: true,
68+
IssueList: true,
6869
Labels: true,
6970
ReviewerRequests: true,
7071
Assignees: true,

‎backend/spi_reads.go‎

Lines changed: 57 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"encoding/base64"
6+
"encoding/json"
67
"net/url"
78
"strconv"
89
"strings"
@@ -360,6 +361,29 @@ func (p *ghProvider) ListComments(_ context.Context, req gp.ListCommentsRequest)
360361
return out, nil
361362
}
362363

364+
type ghIssue struct {
365+
Number int `json:"number"`
366+
Title string `json:"title"`
367+
Body string `json:"body"`
368+
State string `json:"state"`
369+
HTMLURL string `json:"html_url"`
370+
User ghUser `json:"user"`
371+
Labels []struct{ Name string } `json:"labels"`
372+
Assignees []ghUser `json:"assignees"`
373+
PullRequest json.RawMessage `json:"pull_request"`
374+
}
375+
376+
func (is ghIssue) toIssue() gp.Issue {
377+
out := gp.Issue{Number: strconv.Itoa(is.Number), Title: is.Title, Body: is.Body, State: is.State, Author: is.User.Login, URL: is.HTMLURL}
378+
for _, l := range is.Labels {
379+
out.Labels = append(out.Labels, l.Name)
380+
}
381+
for _, a := range is.Assignees {
382+
out.Assignees = append(out.Assignees, a.Login)
383+
}
384+
return out
385+
}
386+
363387
// GetIssue implements gp.Provider.
364388
func (p *ghProvider) GetIssue(_ context.Context, req gp.IssueRequest) (gp.Issue, error) {
365389
num, err := number(req.Number)
@@ -370,25 +394,43 @@ func (p *ghProvider) GetIssue(_ context.Context, req gp.IssueRequest) (gp.Issue,
370394
if err != nil {
371395
return gp.Issue{}, err
372396
}
373-
var is struct {
374-
Number int `json:"number"`
375-
Title string `json:"title"`
376-
Body string `json:"body"`
377-
State string `json:"state"`
378-
HTMLURL string `json:"html_url"`
379-
User ghUser `json:"user"`
380-
Labels []struct{ Name string } `json:"labels"`
381-
Assignees []ghUser `json:"assignees"`
382-
}
397+
var is ghIssue
383398
if err := api.get(repo+"/issues/"+num, &is); err != nil {
384399
return gp.Issue{}, err
385400
}
386-
out := gp.Issue{Number: strconv.Itoa(is.Number), Title: is.Title, Body: is.Body, State: is.State, Author: is.User.Login, URL: is.HTMLURL}
387-
for _, l := range is.Labels {
388-
out.Labels = append(out.Labels, l.Name)
401+
return is.toIssue(), nil
402+
}
403+
404+
// ListIssues implements gp.IssueLister.
405+
func (p *ghProvider) ListIssues(_ context.Context, req gp.ListIssuesRequest) (gp.IssuePage, error) {
406+
state := req.State
407+
if state == "" {
408+
state = "open"
389409
}
390-
for _, a := range is.Assignees {
391-
out.Assignees = append(out.Assignees, a.Login)
410+
if state != "open" && state != "closed" && state != "all" {
411+
return gp.IssuePage{}, gp.Errorf(gp.CodeValidation, "invalid issue state")
412+
}
413+
pg, err := page(req.Cursor)
414+
if err != nil {
415+
return gp.IssuePage{}, err
416+
}
417+
api, repo, err := p.apiFor(req.Target)
418+
if err != nil {
419+
return gp.IssuePage{}, err
420+
}
421+
var issues []ghIssue
422+
// shortcut: one large response can still exceed the WASM arena; upgrade the SDK transport limit handling.
423+
if err := api.get(repo+"/issues?state="+state+"&per_page=1&page="+strconv.Itoa(pg), &issues); err != nil {
424+
return gp.IssuePage{}, err
425+
}
426+
out := gp.IssuePage{Items: []gp.Issue{}}
427+
if len(issues) > 0 {
428+
out.NextCursor = strconv.Itoa(pg + 1)
429+
}
430+
for _, issue := range issues {
431+
if len(issue.PullRequest) == 0 || string(issue.PullRequest) == "null" {
432+
out.Items = append(out.Items, issue.toIssue())
433+
}
392434
}
393435
return out, nil
394436
}

0 commit comments

Comments
 (0)