feat(labels): add label_list + issue_label tools (#52)
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>
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package tools_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"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/tools"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
const labelListFixture = `[
|
||||
{"id":1,"name":"bug","color":"ee0701"},
|
||||
{"id":2,"name":"enhancement","color":"84b6eb"}
|
||||
]`
|
||||
|
||||
func TestIssueLabelAppliesByName(t *testing.T) {
|
||||
var captured []byte
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case r.Method == http.MethodGet && r.URL.Path == "/api/v1/repos/o/r/labels":
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(labelListFixture))
|
||||
case r.Method == http.MethodPost && r.URL.Path == "/api/v1/repos/o/r/issues/42/labels":
|
||||
var err error
|
||||
captured, err = io.ReadAll(r.Body)
|
||||
require.NoError(t, err)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(labelListFixture))
|
||||
default:
|
||||
t.Fatalf("unexpected request: %s %s", r.Method, r.URL.Path)
|
||||
}
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
tool := tools.NewIssueLabel(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"o"}))
|
||||
out, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"o","repo":"r","number":42,"labels":["bug","enhancement"]}`))
|
||||
require.NoError(t, err)
|
||||
|
||||
var payload map[string]any
|
||||
require.NoError(t, json.Unmarshal(captured, &payload))
|
||||
ids, ok := payload["labels"].([]any)
|
||||
require.True(t, ok)
|
||||
assert.ElementsMatch(t, []any{float64(1), float64(2)}, ids)
|
||||
|
||||
assert.Contains(t, string(out), `"name":"bug"`)
|
||||
assert.Contains(t, string(out), `"name":"enhancement"`)
|
||||
}
|
||||
|
||||
func TestIssueLabelUnknownNameNamesTheMissingLabel(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(labelListFixture))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
tool := tools.NewIssueLabel(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"o"}))
|
||||
_, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"o","repo":"r","number":42,"labels":["bug","does-not-exist"]}`))
|
||||
require.Error(t, err)
|
||||
assert.ErrorIs(t, err, gitea.ErrValidation)
|
||||
assert.Contains(t, err.Error(), `"does-not-exist"`)
|
||||
assert.Contains(t, err.Error(), "o/r")
|
||||
}
|
||||
|
||||
func TestIssueLabelAllowlistRejects(t *testing.T) {
|
||||
tool := tools.NewIssueLabel(gitea.NewClient("http://unused", ""), allowlist.New([]string{"allowed"}))
|
||||
_, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"evil","repo":"r","number":1,"labels":["bug"]}`))
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestIssueLabelRequiresValidNumber(t *testing.T) {
|
||||
tool := tools.NewIssueLabel(gitea.NewClient("http://unused", ""), allowlist.New([]string{"o"}))
|
||||
_, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"o","repo":"r","number":0,"labels":["bug"]}`))
|
||||
require.Error(t, err)
|
||||
assert.ErrorIs(t, err, gitea.ErrValidation)
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package tools
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"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 LabelList struct {
|
||||
c *gitea.Client
|
||||
a *allowlist.Allowlist
|
||||
}
|
||||
|
||||
func NewLabelList(c *gitea.Client, a *allowlist.Allowlist) *LabelList {
|
||||
return &LabelList{c: c, a: a}
|
||||
}
|
||||
|
||||
func (t *LabelList) Descriptor() registry.ToolDescriptor {
|
||||
return registry.ToolDescriptor{
|
||||
Name: "label_list",
|
||||
Description: "List all labels defined on a repo. Returns id, name, and color for each label.",
|
||||
InputSchema: json.RawMessage(`{
|
||||
"type":"object",
|
||||
"properties":{
|
||||
"owner":{"type":"string"},
|
||||
"repo":{"type":"string"}
|
||||
},
|
||||
"required":["owner","repo"]
|
||||
}`),
|
||||
}
|
||||
}
|
||||
|
||||
type labelListArgs struct {
|
||||
Owner string `json:"owner"`
|
||||
Repo string `json:"repo"`
|
||||
}
|
||||
|
||||
func (t *LabelList) Call(ctx context.Context, raw json.RawMessage) (json.RawMessage, error) {
|
||||
var args labelListArgs
|
||||
if err := parseArgs(raw, &args); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := t.a.Check(args.Owner); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
labels, err := t.c.ListLabels(ctx, args.Owner, args.Repo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return textOK(labels)
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package tools_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"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/tools"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestLabelListTool(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
assert.Equal(t, http.MethodGet, r.Method)
|
||||
assert.Equal(t, "/api/v1/repos/mathias/infra/labels", r.URL.Path)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`[
|
||||
{"id":1,"name":"bug","color":"ee0701"},
|
||||
{"id":2,"name":"enhancement","color":"84b6eb"}
|
||||
]`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
tool := tools.NewLabelList(gitea.NewClient(srv.URL, "tok"), allowlist.New([]string{"mathias"}))
|
||||
out, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"mathias","repo":"infra"}`))
|
||||
require.NoError(t, err)
|
||||
assert.Contains(t, string(out), `"id":1`)
|
||||
assert.Contains(t, string(out), `"name":"bug"`)
|
||||
assert.Contains(t, string(out), `"color":"ee0701"`)
|
||||
assert.Contains(t, string(out), `"name":"enhancement"`)
|
||||
}
|
||||
|
||||
func TestLabelListAllowlistRejects(t *testing.T) {
|
||||
tool := tools.NewLabelList(gitea.NewClient("http://unused", ""), allowlist.New([]string{"mathias"}))
|
||||
_, err := tool.Call(context.Background(), json.RawMessage(`{"owner":"evil","repo":"x"}`))
|
||||
require.Error(t, err)
|
||||
}
|
||||
@@ -54,6 +54,8 @@ func RegisterAll(
|
||||
reg.Register(NewIssueListComments(c, a))
|
||||
reg.Register(NewIssueClose(c, a))
|
||||
reg.Register(NewIssueReopen(c, a))
|
||||
reg.Register(NewLabelList(c, a))
|
||||
reg.Register(NewIssueLabel(c, a))
|
||||
reg.Register(NewWorkflowRunList(c, a))
|
||||
reg.Register(NewReleaseCreate(c, a))
|
||||
reg.Register(NewRepoDelete(c, a))
|
||||
|
||||
@@ -54,5 +54,5 @@ func TestEveryRegisteredToolIsDispatchable(t *testing.T) {
|
||||
// Lock the tool count so an accidental drop of a registration in RegisterAll
|
||||
// (the single source main.go and this test share) fails loudly.
|
||||
func TestRegisteredToolCount(t *testing.T) {
|
||||
assert.Len(t, buildRegistry().Tools(), 39)
|
||||
assert.Len(t, buildRegistry().Tools(), 41)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user