feat(tools): tag_create
This commit is contained in:
42
internal/gitea/tags.go
Normal file
42
internal/gitea/tags.go
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package gitea
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CreateTagArgs struct {
|
||||||
|
TagName string `json:"tag_name"`
|
||||||
|
Target string `json:"target"`
|
||||||
|
Message string `json:"message,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Tag struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
ID string `json:"id"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Commit struct {
|
||||||
|
Sha string `json:"sha"`
|
||||||
|
} `json:"commit"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) CreateTag(ctx context.Context, owner, repo string, args CreateTagArgs) (*Tag, error) {
|
||||||
|
p := fmt.Sprintf("/api/v1/repos/%s/%s/tags", 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 tag Tag
|
||||||
|
if err := json.Unmarshal(body, &tag); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &tag, nil
|
||||||
|
}
|
||||||
49
internal/gitea/tags_test.go
Normal file
49
internal/gitea/tags_test.go
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
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 TestCreateTag(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/tags", 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(`{
|
||||||
|
"name":"v1.0.0",
|
||||||
|
"id":"tagsha",
|
||||||
|
"message":"release",
|
||||||
|
"commit":{"sha":"cmt1","url":"http://example.com/commit/cmt1"}
|
||||||
|
}`))
|
||||||
|
}))
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
c := gitea.NewClient(srv.URL, "tok")
|
||||||
|
tag, err := c.CreateTag(context.Background(), "o", "r", gitea.CreateTagArgs{
|
||||||
|
TagName: "v1.0.0",
|
||||||
|
Target: "main",
|
||||||
|
Message: "release",
|
||||||
|
})
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "v1.0.0", tag.Name)
|
||||||
|
assert.Equal(t, "cmt1", tag.Commit.Sha)
|
||||||
|
|
||||||
|
var payload map[string]string
|
||||||
|
require.NoError(t, json.Unmarshal(captured, &payload))
|
||||||
|
assert.Equal(t, "v1.0.0", payload["tag_name"])
|
||||||
|
assert.Equal(t, "main", payload["target"])
|
||||||
|
assert.Equal(t, "release", payload["message"])
|
||||||
|
}
|
||||||
76
internal/tools/tag_create.go
Normal file
76
internal/tools/tag_create.go
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
package tools
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"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 TagCreate struct {
|
||||||
|
c *gitea.Client
|
||||||
|
a *allowlist.Allowlist
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewTagCreate(c *gitea.Client, a *allowlist.Allowlist) *TagCreate {
|
||||||
|
return &TagCreate{c: c, a: a}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TagCreate) Descriptor() registry.ToolDescriptor {
|
||||||
|
return registry.ToolDescriptor{
|
||||||
|
Name: "tag_create",
|
||||||
|
Description: "Create a tag pointing at a branch or commit SHA. Add a message to create an annotated tag.",
|
||||||
|
InputSchema: json.RawMessage(`{
|
||||||
|
"type":"object",
|
||||||
|
"properties":{
|
||||||
|
"owner":{"type":"string"},
|
||||||
|
"name":{"type":"string"},
|
||||||
|
"tag":{"type":"string"},
|
||||||
|
"target":{"type":"string"},
|
||||||
|
"message":{"type":"string"}
|
||||||
|
},
|
||||||
|
"required":["owner","name","tag","target"]
|
||||||
|
}`),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type tagCreateArgs struct {
|
||||||
|
Owner string `json:"owner"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
Tag string `json:"tag"`
|
||||||
|
Target string `json:"target"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *TagCreate) Call(ctx context.Context, raw json.RawMessage) (json.RawMessage, error) {
|
||||||
|
var args tagCreateArgs
|
||||||
|
if err := parseArgs(raw, &args); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err := t.a.Check(args.Owner); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if args.Tag == "" {
|
||||||
|
return nil, fmt.Errorf("tag is required: %w", gitea.ErrValidation)
|
||||||
|
}
|
||||||
|
if args.Target == "" {
|
||||||
|
return nil, fmt.Errorf("target is required: %w", gitea.ErrValidation)
|
||||||
|
}
|
||||||
|
|
||||||
|
tag, err := t.c.CreateTag(ctx, args.Owner, args.Name, gitea.CreateTagArgs{
|
||||||
|
TagName: args.Tag,
|
||||||
|
Target: args.Target,
|
||||||
|
Message: args.Message,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return textOK(map[string]any{
|
||||||
|
"tag": tag.Name,
|
||||||
|
"commit_sha": tag.Commit.Sha,
|
||||||
|
})
|
||||||
|
}
|
||||||
52
internal/tools/tag_create_test.go
Normal file
52
internal/tools/tag_create_test.go
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
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 TestTagCreateSuccess(t *testing.T) {
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
assert.Equal(t, "/api/v1/repos/owner/repo/tags", r.URL.Path)
|
||||||
|
assert.Equal(t, http.MethodPost, r.Method)
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
_, _ = w.Write([]byte(`{
|
||||||
|
"name":"v2.0.0","id":"tagsha",
|
||||||
|
"commit":{"sha":"cmt1","url":""}
|
||||||
|
}`))
|
||||||
|
}))
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
tool := tools.NewTagCreate(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"owner"}))
|
||||||
|
out, err := tool.Call(context.Background(), json.RawMessage(`{
|
||||||
|
"owner":"owner","name":"repo","tag":"v2.0.0","target":"main"
|
||||||
|
}`))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
var result map[string]any
|
||||||
|
require.NoError(t, json.Unmarshal(out, &result))
|
||||||
|
assert.Equal(t, "v2.0.0", result["tag"])
|
||||||
|
assert.Equal(t, "cmt1", result["commit_sha"])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTagCreateRequiresTag(t *testing.T) {
|
||||||
|
tool := tools.NewTagCreate(gitea.NewClient("http://unused", ""), allowlist.New([]string{"owner"}))
|
||||||
|
_, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"owner","name":"repo","target":"main"}`))
|
||||||
|
require.Error(t, err)
|
||||||
|
assert.ErrorIs(t, err, gitea.ErrValidation)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTagCreateAllowlistRejects(t *testing.T) {
|
||||||
|
tool := tools.NewTagCreate(gitea.NewClient("http://unused", ""), allowlist.New([]string{"allowed"}))
|
||||||
|
_, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"evil","name":"repo","tag":"v1.0.0","target":"main"}`))
|
||||||
|
require.Error(t, err)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user