Allowlist.Check now takes ctx: when the caller authenticated with their own Gitea PAT (pass-through, v0.12.0), it skips the static GITEA_MCP_ALLOWED_OWNERS check entirely — Gitea's own permission model already gates that caller's access more precisely than a coarse owner-name list can. The static list still applies unchanged for the shared static-token/JWT path, where it's the only defense against the service token's blast radius. Mechanical: every tool call site already had ctx in scope, so this is a signature-only change at 41 call sites, no other tool behavior changes. Closes the "Deferred" item from #59. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
88 lines
2.5 KiB
Go
88 lines
2.5 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/gitea"
|
|
"git.d-ma.be/mathias/gitea-mcp/internal/registry"
|
|
)
|
|
|
|
type IssueLabel struct {
|
|
c *gitea.Client
|
|
a *allowlist.Allowlist
|
|
}
|
|
|
|
func NewIssueLabel(c *gitea.Client, a *allowlist.Allowlist) *IssueLabel {
|
|
return &IssueLabel{c: c, a: a}
|
|
}
|
|
|
|
func (t *IssueLabel) Descriptor() registry.ToolDescriptor {
|
|
return registry.ToolDescriptor{
|
|
Name: "issue_label",
|
|
Description: "Add labels to an issue or pull request. Resolves label names to IDs via the repo's label list. Additive — existing labels are left in place.",
|
|
InputSchema: json.RawMessage(`{
|
|
"type":"object",
|
|
"properties":{
|
|
"owner":{"type":"string"},
|
|
"repo":{"type":"string"},
|
|
"number":{"type":"integer","minimum":1},
|
|
"labels":{"type":"array","items":{"type":"string"},"description":"Label names to resolve and apply. Either labels or label_ids is required."},
|
|
"label_ids":{"type":"array","items":{"type":"integer"},"description":"Label IDs to apply directly, skipping name resolution. Either labels or label_ids is required."}
|
|
},
|
|
"required":["owner","repo","number"]
|
|
}`),
|
|
}
|
|
}
|
|
|
|
type issueLabelArgs struct {
|
|
Owner string `json:"owner"`
|
|
Repo string `json:"repo"`
|
|
Number int `json:"number"`
|
|
Labels []string `json:"labels,omitempty"`
|
|
LabelIDs []int64 `json:"label_ids,omitempty"`
|
|
}
|
|
|
|
func (t *IssueLabel) Call(ctx context.Context, raw json.RawMessage) (json.RawMessage, error) {
|
|
var args issueLabelArgs
|
|
if err := parseArgs(raw, &args); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := t.a.Check(ctx, args.Owner); err != nil {
|
|
return nil, err
|
|
}
|
|
if args.Number < 1 {
|
|
return nil, fmt.Errorf("number must be >= 1: %w", gitea.ErrValidation)
|
|
}
|
|
if len(args.Labels) == 0 && len(args.LabelIDs) == 0 {
|
|
return nil, fmt.Errorf("labels is required: %w", gitea.ErrValidation)
|
|
}
|
|
|
|
ids := append([]int64{}, args.LabelIDs...)
|
|
if len(args.Labels) > 0 {
|
|
existing, err := t.c.ListLabels(ctx, args.Owner, args.Repo)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
byName := make(map[string]int64, len(existing))
|
|
for _, l := range existing {
|
|
byName[l.Name] = l.ID
|
|
}
|
|
for _, name := range args.Labels {
|
|
id, ok := byName[name]
|
|
if !ok {
|
|
return nil, fmt.Errorf("label %q not found in %s/%s: %w", name, args.Owner, args.Repo, gitea.ErrValidation)
|
|
}
|
|
ids = append(ids, id)
|
|
}
|
|
}
|
|
|
|
labels, err := t.c.AddIssueLabels(ctx, args.Owner, args.Repo, args.Number, ids)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return textOK(labels)
|
|
}
|