diff --git a/cmd/gitea-mcp/main.go b/cmd/gitea-mcp/main.go index c70c0a0..2d33939 100644 --- a/cmd/gitea-mcp/main.go +++ b/cmd/gitea-mcp/main.go @@ -37,6 +37,7 @@ func main() { reg.Register(tools.NewWorkflowRunStatus(giteaClient, ownerAllow)) reg.Register(tools.NewRepoSearch(giteaClient, ownerAllow)) reg.Register(tools.NewCodeSearch(giteaClient, ownerAllow)) + reg.Register(tools.NewIssueCreate(giteaClient, ownerAllow)) mcpSrv := mcp.NewServer(mcp.ServerOptions{ Registry: reg, diff --git a/internal/gitea/issues.go b/internal/gitea/issues.go new file mode 100644 index 0000000..54743d1 --- /dev/null +++ b/internal/gitea/issues.go @@ -0,0 +1,71 @@ +package gitea + +import ( + "context" + "encoding/json" + "fmt" +) + +type Issue struct { + Number int `json:"number"` + Title string `json:"title"` + Body string `json:"body"` + HTMLURL string `json:"html_url"` + State string `json:"state"` +} + +type CreateIssueArgs struct { + Title string `json:"title"` + Body string `json:"body"` + Labels []int64 `json:"labels,omitempty"` + Assignees []string `json:"assignees,omitempty"` + Milestone int64 `json:"milestone,omitempty"` +} + +func (c *Client) CreateIssue(ctx context.Context, owner, repo string, args CreateIssueArgs) (*Issue, error) { + p := fmt.Sprintf("/api/v1/repos/%s/%s/issues", owner, repo) + payload, err := json.Marshal(args) + if err != nil { + return nil, err + } + body, status, err := c.PostJSON(ctx, p, payload) + if err != nil { + return nil, err + } + if err := MapStatus(status, body); err != nil { + return nil, err + } + var iss Issue + if err := json.Unmarshal(body, &iss); err != nil { + return nil, err + } + return &iss, nil +} + +type IssueComment struct { + ID int64 `json:"id"` + Body string `json:"body"` + HTMLURL string `json:"html_url"` +} + +// CreateIssueComment posts to /issues/{index}/comments. Per Gitea, this same endpoint +// works for both issues and pull requests (PRs share index space with issues). +func (c *Client) CreateIssueComment(ctx context.Context, owner, repo string, index int, body string) (*IssueComment, error) { + p := fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/comments", owner, repo, index) + payload, err := json.Marshal(map[string]string{"body": body}) + if err != nil { + return nil, err + } + respBody, status, err := c.PostJSON(ctx, p, payload) + if err != nil { + return nil, err + } + if err := MapStatus(status, respBody); err != nil { + return nil, err + } + var c2 IssueComment + if err := json.Unmarshal(respBody, &c2); err != nil { + return nil, err + } + return &c2, nil +} diff --git a/internal/gitea/issues_test.go b/internal/gitea/issues_test.go new file mode 100644 index 0000000..47c4540 --- /dev/null +++ b/internal/gitea/issues_test.go @@ -0,0 +1,72 @@ +package gitea_test + +import ( + "context" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "testing" + + "gitea.d-ma.be/mathias/gitea-mcp/internal/gitea" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestCreateIssue(t *testing.T) { + var captured []byte + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/repos/o/r/issues", r.URL.Path) + assert.Equal(t, http.MethodPost, r.Method) + var err error + captured, err = io.ReadAll(r.Body) + require.NoError(t, err) + w.WriteHeader(http.StatusCreated) + _, _ = w.Write([]byte(`{"number":42,"title":"x","body":"y","html_url":"http://example.com/issues/42","state":"open"}`)) + })) + defer srv.Close() + + c := gitea.NewClient(srv.URL, "tok") + iss, err := c.CreateIssue(context.Background(), "o", "r", gitea.CreateIssueArgs{ + Title: "x", + Body: "y", + }) + require.NoError(t, err) + + var payload map[string]any + require.NoError(t, json.Unmarshal(captured, &payload)) + assert.Equal(t, "x", payload["title"]) + assert.Equal(t, "y", payload["body"]) + + assert.Equal(t, 42, iss.Number) + assert.Equal(t, "x", iss.Title) + assert.Equal(t, "y", iss.Body) + assert.Equal(t, "http://example.com/issues/42", iss.HTMLURL) + assert.Equal(t, "open", iss.State) +} + +func TestCreateIssueComment(t *testing.T) { + var captured []byte + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/repos/o/r/issues/42/comments", r.URL.Path) + assert.Equal(t, http.MethodPost, r.Method) + var err error + captured, err = io.ReadAll(r.Body) + require.NoError(t, err) + w.WriteHeader(http.StatusCreated) + _, _ = w.Write([]byte(`{"id":7,"body":"hello","html_url":"http://example.com/issues/42#comment-7"}`)) + })) + defer srv.Close() + + c := gitea.NewClient(srv.URL, "tok") + comment, err := c.CreateIssueComment(context.Background(), "o", "r", 42, "hello") + require.NoError(t, err) + + var payload map[string]any + require.NoError(t, json.Unmarshal(captured, &payload)) + assert.Equal(t, "hello", payload["body"]) + + assert.Equal(t, int64(7), comment.ID) + assert.Equal(t, "hello", comment.Body) + assert.Equal(t, "http://example.com/issues/42#comment-7", comment.HTMLURL) +} diff --git a/internal/tools/issue_create.go b/internal/tools/issue_create.go new file mode 100644 index 0000000..a58b0d3 --- /dev/null +++ b/internal/tools/issue_create.go @@ -0,0 +1,84 @@ +package tools + +import ( + "context" + "encoding/json" + "fmt" + + "gitea.d-ma.be/mathias/gitea-mcp/internal/allowlist" + "gitea.d-ma.be/mathias/gitea-mcp/internal/auth" + "gitea.d-ma.be/mathias/gitea-mcp/internal/gitea" + "gitea.d-ma.be/mathias/gitea-mcp/internal/identity" + "gitea.d-ma.be/mathias/gitea-mcp/internal/registry" +) + +type IssueCreate struct { + c *gitea.Client + a *allowlist.Allowlist +} + +func NewIssueCreate(c *gitea.Client, a *allowlist.Allowlist) *IssueCreate { + return &IssueCreate{c: c, a: a} +} + +func (t *IssueCreate) Descriptor() registry.ToolDescriptor { + return registry.ToolDescriptor{ + Name: "issue_create", + Description: "Create an issue. Applies identity footer to body.", + InputSchema: json.RawMessage(`{ + "type":"object", + "properties":{ + "owner":{"type":"string"}, + "name":{"type":"string"}, + "title":{"type":"string"}, + "body":{"type":"string"}, + "labels":{"type":"array","items":{"type":"integer"}}, + "assignees":{"type":"array","items":{"type":"string"}}, + "milestone":{"type":"integer"} + }, + "required":["owner","name","title"] + }`), + } +} + +type issueCreateArgs struct { + Owner string `json:"owner"` + Name string `json:"name"` + Title string `json:"title"` + Body string `json:"body"` + Labels []int64 `json:"labels"` + Assignees []string `json:"assignees"` + Milestone int64 `json:"milestone"` +} + +func (t *IssueCreate) Call(ctx context.Context, raw json.RawMessage) (json.RawMessage, error) { + var args issueCreateArgs + if err := parseArgs(raw, &args); err != nil { + return nil, err + } + if err := t.a.Check(args.Owner); err != nil { + return nil, err + } + if args.Title == "" { + return nil, fmt.Errorf("title is required: %w", gitea.ErrValidation) + } + body := identity.ApplyFooter(args.Body, auth.Caller(ctx)) + + iss, err := t.c.CreateIssue(ctx, args.Owner, args.Name, gitea.CreateIssueArgs{ + Title: args.Title, + Body: body, + Labels: args.Labels, + Assignees: args.Assignees, + Milestone: args.Milestone, + }) + if err != nil { + return nil, err + } + + return textOK(map[string]any{ + "number": iss.Number, + "title": iss.Title, + "html_url": iss.HTMLURL, + "state": iss.State, + }) +} diff --git a/internal/tools/issue_create_test.go b/internal/tools/issue_create_test.go new file mode 100644 index 0000000..3e080ad --- /dev/null +++ b/internal/tools/issue_create_test.go @@ -0,0 +1,81 @@ +package tools_test + +import ( + "context" + "encoding/json" + "io" + "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" +) + +const issueFixture = `{ + "number": 42, + "title": "x", + "body": "y", + "html_url": "http://example.com/issues/42", + "state": "open" +}` + +func TestIssueCreateAppliesIdentityFooter(t *testing.T) { + var captured []byte + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assert.Equal(t, "/api/v1/repos/o/r/issues", r.URL.Path) + var err error + captured, err = io.ReadAll(r.Body) + require.NoError(t, err) + w.WriteHeader(http.StatusCreated) + _, _ = w.Write([]byte(issueFixture)) + })) + defer srv.Close() + + tool := tools.NewIssueCreate(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"o"})) + ctx := callerContext("mathiasbq") + _, err := tool.Call(ctx, json.RawMessage(`{"owner":"o","name":"r","title":"x","body":"y"}`)) + require.NoError(t, err) + + var payload map[string]any + require.NoError(t, json.Unmarshal(captured, &payload)) + body, _ := payload["body"].(string) + assert.Contains(t, body, "_Created via git-mcp on behalf of @mathiasbq_") +} + +func TestIssueCreateNoFooterWhenCallerEmpty(t *testing.T) { + var captured []byte + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var err error + captured, err = io.ReadAll(r.Body) + require.NoError(t, err) + w.WriteHeader(http.StatusCreated) + _, _ = w.Write([]byte(issueFixture)) + })) + defer srv.Close() + + tool := tools.NewIssueCreate(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"o"})) + _, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"o","name":"r","title":"x","body":"y"}`)) + require.NoError(t, err) + + var payload map[string]any + require.NoError(t, json.Unmarshal(captured, &payload)) + body, _ := payload["body"].(string) + assert.NotContains(t, body, "_Created via git-mcp on behalf of") +} + +func TestIssueCreateAllowlistRejects(t *testing.T) { + tool := tools.NewIssueCreate(gitea.NewClient("http://unused", ""), allowlist.New([]string{"allowed"})) + _, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"evil","name":"r","title":"T"}`)) + require.Error(t, err) +} + +func TestIssueCreateRequiresTitle(t *testing.T) { + tool := tools.NewIssueCreate(gitea.NewClient("http://unused", ""), allowlist.New([]string{"o"})) + _, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"o","name":"r","title":""}`)) + require.Error(t, err) + assert.ErrorIs(t, err, gitea.ErrValidation) +}