92 lines
2.2 KiB
Go
92 lines
2.2 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/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 PRCreate struct {
|
|
c *gitea.Client
|
|
a *allowlist.Allowlist
|
|
}
|
|
|
|
func NewPRCreate(c *gitea.Client, a *allowlist.Allowlist) *PRCreate {
|
|
return &PRCreate{c: c, a: a}
|
|
}
|
|
|
|
func (t *PRCreate) Descriptor() registry.ToolDescriptor {
|
|
return registry.ToolDescriptor{
|
|
Name: "pr_create",
|
|
Description: "Create a pull request. Applies an identity footer to the PR body.",
|
|
InputSchema: json.RawMessage(`{
|
|
"type":"object",
|
|
"properties":{
|
|
"owner":{"type":"string"},
|
|
"name":{"type":"string"},
|
|
"title":{"type":"string"},
|
|
"body":{"type":"string"},
|
|
"head":{"type":"string"},
|
|
"base":{"type":"string"},
|
|
"draft":{"type":"boolean"}
|
|
},
|
|
"required":["owner","name","title","head","base"]
|
|
}`),
|
|
}
|
|
}
|
|
|
|
type prCreateArgs struct {
|
|
Owner string `json:"owner"`
|
|
Name string `json:"name"`
|
|
Title string `json:"title"`
|
|
Body string `json:"body"`
|
|
Head string `json:"head"`
|
|
Base string `json:"base"`
|
|
Draft bool `json:"draft"`
|
|
}
|
|
|
|
func (t *PRCreate) Call(ctx context.Context, raw json.RawMessage) (json.RawMessage, error) {
|
|
var args prCreateArgs
|
|
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)
|
|
}
|
|
if args.Head == "" || args.Base == "" {
|
|
return nil, fmt.Errorf("head and base are required: %w", gitea.ErrValidation)
|
|
}
|
|
|
|
body := identity.ApplyFooter(args.Body, auth.Caller(ctx))
|
|
|
|
pr, err := t.c.CreatePullRequest(ctx, args.Owner, args.Name, gitea.CreatePullRequestArgs{
|
|
Title: args.Title,
|
|
Body: body,
|
|
Head: args.Head,
|
|
Base: args.Base,
|
|
Draft: args.Draft,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return textOK(map[string]any{
|
|
"number": pr.Number,
|
|
"title": pr.Title,
|
|
"html_url": pr.HTMLURL,
|
|
"head": pr.Head.Ref,
|
|
"base": pr.Base.Ref,
|
|
"state": pr.State,
|
|
"draft": pr.Draft,
|
|
})
|
|
}
|