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(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) }