79 lines
1.9 KiB
Go
79 lines
1.9 KiB
Go
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,
|
|
})
|
|
}
|