Closes #52 — unblocks parallax#3 dispatch labeling, which needs to both discover a repo's label set and attach labels to an issue by name (the CAD pipeline doesn't track Gitea's internal numeric label IDs). - gitea.Client: ListLabels, AddIssueLabels (additive POST, matches Gitea's own semantics — no delete-then-post needed); extend the existing Label struct with Color for label_list's output. - tools.LabelList (read-only, allowlisted): lists a repo's labels. - tools.IssueLabel (allowlisted): resolves label names to IDs via ListLabels, so callers pass names (the primary interface) instead of hunting for numeric IDs; also accepts label_ids for callers that already have them. An unknown name fails closed, naming exactly which label wasn't found. - Bump TestRegisteredToolCount 39 -> 41 in the same commit (this project was bitten today by a locked count going stale silently). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
88 lines
2.3 KiB
Go
88 lines
2.3 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"}},
|
|
"label_ids":{"type":"array","items":{"type":"integer"}}
|
|
},
|
|
"required":["owner","repo","number","labels"]
|
|
}`),
|
|
}
|
|
}
|
|
|
|
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(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)
|
|
}
|