Files
gitea-mcp/internal/tools/pr_create.go
T
mathiasandClaude Opus 4.8 4c1f36f9eb
CD / Lint / Test / Vet (push) Successful in 7s
CD / Build & Import (push) Successful in 23s
CD / Deploy via GitOps (push) Has been skipped
chore(rename): module gitea.d-ma.be → git.d-ma.be/mathias/gitea-mcp (#39)
Gitea host renamed (infra ADR-0004); the mcp-chassis dep already migrated
(3329ff3), so the sequencing gate is clear. `go mod edit -module` + bulk import
rewrite across all .go files. gitea-mcp is a server binary (not an imported
library), so no downstream consumers break. build + task check green.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-03 22:48:26 +02:00

92 lines
2.2 KiB
Go

package tools
import (
"context"
"encoding/json"
"fmt"
"git.d-ma.be/mathias/gitea-mcp/internal/allowlist"
"git.d-ma.be/mathias/gitea-mcp/internal/auth"
"git.d-ma.be/mathias/gitea-mcp/internal/gitea"
"git.d-ma.be/mathias/gitea-mcp/internal/identity"
"git.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"},
"repo":{"type":"string"},
"title":{"type":"string"},
"body":{"type":"string"},
"head":{"type":"string"},
"base":{"type":"string"},
"draft":{"type":"boolean"}
},
"required":["owner","repo","title","head","base"]
}`),
}
}
type prCreateArgs struct {
Owner string `json:"owner"`
Repo string `json:"repo"`
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.Repo, 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,
})
}