From 3c1ca7d3db482ceed469e2796e889040a8bcb2bf Mon Sep 17 00:00:00 2001 From: Mathias Date: Wed, 27 May 2026 22:01:34 +0200 Subject: [PATCH] feat: add issue_list_comments tool (closes #32) Lists all comments on an issue or PR via GET /api/v1/repos/{owner}/{repo}/issues/{index}/comments. Read-only, allowlist-gated. Extended IssueComment struct with user/timestamps populated by the list endpoint. Co-Authored-By: Claude Opus 4.7 (1M context) --- cmd/gitea-mcp/main.go | 1 + internal/gitea/issues.go | 27 ++++++++- internal/gitea/issues_test.go | 48 ++++++++++++++++ internal/tools/issue_list_comments.go | 56 ++++++++++++++++++ internal/tools/issue_list_comments_test.go | 67 ++++++++++++++++++++++ 5 files changed, 196 insertions(+), 3 deletions(-) create mode 100644 internal/tools/issue_list_comments.go create mode 100644 internal/tools/issue_list_comments_test.go diff --git a/cmd/gitea-mcp/main.go b/cmd/gitea-mcp/main.go index e977044..cdaa926 100644 --- a/cmd/gitea-mcp/main.go +++ b/cmd/gitea-mcp/main.go @@ -69,6 +69,7 @@ func main() { reg.Register(tools.NewRepoTopicsUpdate(giteaClient, ownerAllow)) reg.Register(tools.NewIssueGet(giteaClient, ownerAllow)) reg.Register(tools.NewIssueList(giteaClient, ownerAllow)) + reg.Register(tools.NewIssueListComments(giteaClient, ownerAllow)) reg.Register(tools.NewIssueClose(giteaClient, ownerAllow)) reg.Register(tools.NewIssueReopen(giteaClient, ownerAllow)) reg.Register(tools.NewWorkflowRunList(giteaClient, ownerAllow)) diff --git a/internal/gitea/issues.go b/internal/gitea/issues.go index 9b358a5..082c80a 100644 --- a/internal/gitea/issues.go +++ b/internal/gitea/issues.go @@ -141,9 +141,30 @@ func (c *Client) SetIssueState(ctx context.Context, owner, repo string, number i } type IssueComment struct { - ID int64 `json:"id"` - Body string `json:"body"` - HTMLURL string `json:"html_url"` + ID int64 `json:"id"` + Body string `json:"body"` + HTMLURL string `json:"html_url"` + User User `json:"user,omitempty"` + CreatedAt string `json:"created_at,omitempty"` + UpdatedAt string `json:"updated_at,omitempty"` +} + +// ListIssueComments fetches all comments on an issue or pull request. +// Per Gitea, /issues/{index}/comments serves both since PRs share index space with issues. +func (c *Client) ListIssueComments(ctx context.Context, owner, repo string, index int) ([]IssueComment, error) { + p := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/comments", owner, repo, index) + body, status, err := c.GetJSON(ctx, p) + if err != nil { + return nil, err + } + if err := MapStatus(status, body); err != nil { + return nil, err + } + var comments []IssueComment + if err := json.Unmarshal(body, &comments); err != nil { + return nil, err + } + return comments, nil } // CreateIssueComment posts to /issues/{index}/comments. Per Gitea, this same endpoint diff --git a/internal/gitea/issues_test.go b/internal/gitea/issues_test.go index 8e0141d..ef4efc6 100644 --- a/internal/gitea/issues_test.go +++ b/internal/gitea/issues_test.go @@ -101,3 +101,51 @@ func TestCreateIssueComment(t *testing.T) { assert.Equal(t, "hello", comment.Body) assert.Equal(t, "http://example.com/issues/42#comment-7", comment.HTMLURL) } + +func TestListIssueComments(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodGet, r.Method) + assert.Equal(t, "/api/v1/repos/o/r/issues/42/comments", r.URL.Path) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`[ + {"id":1,"body":"first","html_url":"http://example.com/issues/42#comment-1","user":{"login":"alice"},"created_at":"2026-05-01T00:00:00Z","updated_at":"2026-05-01T00:00:00Z"}, + {"id":2,"body":"second","html_url":"http://example.com/issues/42#comment-2","user":{"login":"bob"},"created_at":"2026-05-02T00:00:00Z","updated_at":"2026-05-02T00:00:00Z"} + ]`)) + })) + defer srv.Close() + + c := gitea.NewClient(srv.URL, "tok") + comments, err := c.ListIssueComments(context.Background(), "o", "r", 42) + require.NoError(t, err) + require.Len(t, comments, 2) + assert.Equal(t, int64(1), comments[0].ID) + assert.Equal(t, "first", comments[0].Body) + assert.Equal(t, "alice", comments[0].User.Login) + assert.Equal(t, "bob", comments[1].User.Login) +} + +func TestListIssueComments_Empty(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`[]`)) + })) + defer srv.Close() + + c := gitea.NewClient(srv.URL, "tok") + comments, err := c.ListIssueComments(context.Background(), "o", "r", 42) + require.NoError(t, err) + assert.Empty(t, comments) +} + +func TestListIssueComments_NotFound(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message":"issue not found"}`)) + })) + defer srv.Close() + + c := gitea.NewClient(srv.URL, "tok") + _, err := c.ListIssueComments(context.Background(), "o", "r", 999) + require.Error(t, err) + assert.ErrorIs(t, err, gitea.ErrNotFound) +} diff --git a/internal/tools/issue_list_comments.go b/internal/tools/issue_list_comments.go new file mode 100644 index 0000000..c2d2888 --- /dev/null +++ b/internal/tools/issue_list_comments.go @@ -0,0 +1,56 @@ +package tools + +import ( + "context" + "encoding/json" + + "gitea.d-ma.be/mathias/gitea-mcp/internal/allowlist" + "gitea.d-ma.be/mathias/gitea-mcp/internal/gitea" + "gitea.d-ma.be/mathias/gitea-mcp/internal/registry" +) + +type IssueListComments struct { + c *gitea.Client + a *allowlist.Allowlist +} + +func NewIssueListComments(c *gitea.Client, a *allowlist.Allowlist) *IssueListComments { + return &IssueListComments{c: c, a: a} +} + +func (t *IssueListComments) Descriptor() registry.ToolDescriptor { + return registry.ToolDescriptor{ + Name: "issue_list_comments", + Description: "List all comments on an issue or pull request. Returns id, body, author, html_url, and timestamps for each comment.", + InputSchema: json.RawMessage(`{ + "type":"object", + "properties":{ + "owner":{"type":"string"}, + "name":{"type":"string"}, + "number":{"type":"integer","minimum":1} + }, + "required":["owner","name","number"] + }`), + } +} + +type issueListCommentsArgs struct { + Owner string `json:"owner"` + Name string `json:"name"` + Number int `json:"number"` +} + +func (t *IssueListComments) Call(ctx context.Context, raw json.RawMessage) (json.RawMessage, error) { + var args issueListCommentsArgs + if err := parseArgs(raw, &args); err != nil { + return nil, err + } + if err := t.a.Check(args.Owner); err != nil { + return nil, err + } + comments, err := t.c.ListIssueComments(ctx, args.Owner, args.Name, args.Number) + if err != nil { + return nil, err + } + return textOK(comments) +} diff --git a/internal/tools/issue_list_comments_test.go b/internal/tools/issue_list_comments_test.go new file mode 100644 index 0000000..3305339 --- /dev/null +++ b/internal/tools/issue_list_comments_test.go @@ -0,0 +1,67 @@ +package tools_test + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "gitea.d-ma.be/mathias/gitea-mcp/internal/allowlist" + "gitea.d-ma.be/mathias/gitea-mcp/internal/gitea" + "gitea.d-ma.be/mathias/gitea-mcp/internal/tools" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestIssueListCommentsTool(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, http.MethodGet, r.Method) + assert.Equal(t, "/api/v1/repos/mathias/infra/issues/42/comments", r.URL.Path) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`[ + {"id":1,"body":"first","html_url":"http://gitea.example.com/mathias/infra/issues/42#comment-1","user":{"login":"alice"},"created_at":"2026-05-01T00:00:00Z","updated_at":"2026-05-01T00:00:00Z"}, + {"id":2,"body":"second","html_url":"http://gitea.example.com/mathias/infra/issues/42#comment-2","user":{"login":"bob"},"created_at":"2026-05-02T00:00:00Z","updated_at":"2026-05-02T00:00:00Z"} + ]`)) + })) + defer srv.Close() + + tool := tools.NewIssueListComments(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"mathias"})) + out, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"mathias","name":"infra","number":42}`)) + require.NoError(t, err) + assert.Contains(t, string(out), `"id":1`) + assert.Contains(t, string(out), `"body":"first"`) + assert.Contains(t, string(out), `"login":"alice"`) + assert.Contains(t, string(out), `"login":"bob"`) +} + +func TestIssueListCommentsTool_Empty(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`[]`)) + })) + defer srv.Close() + + tool := tools.NewIssueListComments(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"mathias"})) + out, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"mathias","name":"infra","number":42}`)) + require.NoError(t, err) + assert.Contains(t, string(out), `[]`) +} + +func TestIssueListCommentsTool_NotFound(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusNotFound) + _, _ = w.Write([]byte(`{"message":"issue not found"}`)) + })) + defer srv.Close() + + tool := tools.NewIssueListComments(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"mathias"})) + _, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"mathias","name":"infra","number":999}`)) + require.Error(t, err) +} + +func TestIssueListCommentsAllowlistRejects(t *testing.T) { + tool := tools.NewIssueListComments(gitea.NewClient("http://unused", ""), allowlist.New([]string{"mathias"})) + _, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"evil","name":"x","number":1}`)) + require.Error(t, err) +}