feat(tools): issue_create with identity footer
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
84
internal/tools/issue_create.go
Normal file
84
internal/tools/issue_create.go
Normal file
@@ -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,
|
||||
})
|
||||
}
|
||||
81
internal/tools/issue_create_test.go
Normal file
81
internal/tools/issue_create_test.go
Normal file
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user