feat(tools): file_delete

This commit is contained in:
Mathias Bergqvist
2026-05-06 22:51:21 +02:00
parent 0eb9ebcafd
commit 5dac4856bd
4 changed files with 188 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
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 FileDelete struct {
c *gitea.Client
a *allowlist.Allowlist
}
func NewFileDelete(c *gitea.Client, a *allowlist.Allowlist) *FileDelete {
return &FileDelete{c: c, a: a}
}
func (t *FileDelete) Descriptor() registry.ToolDescriptor {
return registry.ToolDescriptor{
Name: "file_delete",
Description: "Delete a file from a repository branch. sha is the current blob SHA (from file_read).",
InputSchema: json.RawMessage(`{
"type":"object",
"properties":{
"owner":{"type":"string"},
"name":{"type":"string"},
"path":{"type":"string"},
"branch":{"type":"string"},
"message":{"type":"string"},
"sha":{"type":"string"}
},
"required":["owner","name","path","branch","message","sha"]
}`),
}
}
type fileDeleteArgs struct {
Owner string `json:"owner"`
Name string `json:"name"`
Path string `json:"path"`
Branch string `json:"branch"`
Message string `json:"message"`
Sha string `json:"sha"`
}
func (t *FileDelete) Call(ctx context.Context, raw json.RawMessage) (json.RawMessage, error) {
var args fileDeleteArgs
if err := parseArgs(raw, &args); err != nil {
return nil, err
}
if err := t.a.Check(args.Owner); err != nil {
return nil, err
}
if args.Sha == "" {
return nil, fmt.Errorf("sha is required: %w", gitea.ErrValidation)
}
if args.Message == "" {
return nil, fmt.Errorf("message is required: %w", gitea.ErrValidation)
}
result, err := t.c.DeleteFile(ctx, args.Owner, args.Name, args.Path, gitea.DeleteFileArgs{
Branch: args.Branch,
Message: args.Message,
Sha: args.Sha,
})
if err != nil {
return nil, err
}
return textOK(map[string]any{
"commit_sha": result.Commit.Sha,
"html_url": result.Commit.HTMLURL,
})
}