Per-repo MCP tools 404'd while owner-level tools worked (#36). Root cause was a parameter-name contract mismatch, not a routing fault: every per-repo tool declares the repo identifier as 'name' (and issue/PR index as 'number'), but every caller — the claude.ai connector, LLMs primed on gitea's own API — sends 'repo' and 'index'. The unmatched fields zero-valued the upstream path segment, producing '/api/v1/repos/{owner}//...', which gitea answers with its generic api-404 whose body points at /api/swagger. That swagger pointer is gitea boiler- plate, not a misroute — the MCP dispatch was correct all along. Two layers of defence: - parseArgs aliases repo->name and index->number (explicit canonical wins; alias key left intact so pr_merge's real 'index' field is unaffected). Kills the recurrence by accepting the idiomatic argument names. - the gitea client rejects any path with an empty segment before the HTTP call, returning ErrValidation instead of forwarding a malformed path and surfacing gitea's opaque swagger-404. Kills the silent-misleading-error class. Closes #36 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
154 lines
4.2 KiB
Go
154 lines
4.2 KiB
Go
package gitea
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/hashicorp/golang-lru/v2/expirable"
|
|
)
|
|
|
|
type Client struct {
|
|
baseURL string
|
|
token string
|
|
hc *http.Client
|
|
branchCache *expirable.LRU[string, string]
|
|
}
|
|
|
|
func NewClient(baseURL, token string) *Client {
|
|
return &Client{
|
|
baseURL: baseURL,
|
|
token: token,
|
|
hc: &http.Client{Timeout: 30 * time.Second},
|
|
branchCache: expirable.NewLRU[string, string](64, nil, 60*time.Second),
|
|
}
|
|
}
|
|
|
|
// DefaultBranch returns the default branch for a repo. Cached for 60s.
|
|
func (c *Client) DefaultBranch(ctx context.Context, owner, name string) (string, error) {
|
|
key := owner + "/" + name
|
|
if v, ok := c.branchCache.Get(key); ok {
|
|
return v, nil
|
|
}
|
|
repo, err := c.GetRepo(ctx, owner, name)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
c.branchCache.Add(key, repo.DefaultBranch)
|
|
return repo.DefaultBranch, nil
|
|
}
|
|
|
|
// hasEmptySegment reports whether the path portion (before any query string)
|
|
// contains an empty segment ("//"), which means an owner or repo path
|
|
// parameter was empty. Forwarding it upstream yields gitea's opaque
|
|
// /api/swagger 404 (#36), so callers reject it locally instead.
|
|
func hasEmptySegment(path string) bool {
|
|
if i := strings.IndexByte(path, '?'); i >= 0 {
|
|
path = path[:i]
|
|
}
|
|
return strings.Contains(path, "//")
|
|
}
|
|
|
|
func (c *Client) doOnce(ctx context.Context, method, path string, body []byte) ([]byte, int, error) {
|
|
if hasEmptySegment(path) {
|
|
return nil, 0, fmt.Errorf("%w: upstream path %q has an empty owner or repo segment", ErrValidation, path)
|
|
}
|
|
var reader io.Reader
|
|
if body != nil {
|
|
reader = bytes.NewReader(body)
|
|
}
|
|
req, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, reader)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
token := c.token
|
|
if token != "" {
|
|
req.Header.Set("Authorization", "token "+token)
|
|
}
|
|
if body != nil {
|
|
req.Header.Set("Content-Type", "application/json")
|
|
}
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
resp, err := c.hc.Do(req)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
b, err := io.ReadAll(resp.Body)
|
|
return b, resp.StatusCode, err
|
|
}
|
|
|
|
func (c *Client) do(ctx context.Context, method, path string, body []byte) ([]byte, int, error) {
|
|
b, status, err := c.doOnce(ctx, method, path, body)
|
|
if err == nil && method == http.MethodGet && status >= 500 && status < 600 {
|
|
time.Sleep(250 * time.Millisecond)
|
|
return c.doOnce(ctx, method, path, body)
|
|
}
|
|
return b, status, err
|
|
}
|
|
|
|
func (c *Client) GetJSON(ctx context.Context, path string) ([]byte, int, error) {
|
|
return c.do(ctx, http.MethodGet, path, nil)
|
|
}
|
|
|
|
func (c *Client) PostJSON(ctx context.Context, path string, body []byte) ([]byte, int, error) {
|
|
return c.do(ctx, http.MethodPost, path, body)
|
|
}
|
|
|
|
func (c *Client) PatchJSON(ctx context.Context, path string, body []byte) ([]byte, int, error) {
|
|
return c.do(ctx, http.MethodPatch, path, body)
|
|
}
|
|
|
|
func (c *Client) PutJSON(ctx context.Context, path string, body []byte) ([]byte, int, error) {
|
|
return c.do(ctx, http.MethodPut, path, body)
|
|
}
|
|
|
|
func (c *Client) DeleteJSON(ctx context.Context, path string) ([]byte, int, error) {
|
|
return c.do(ctx, http.MethodDelete, path, nil)
|
|
}
|
|
|
|
func (c *Client) DeleteJSONBody(ctx context.Context, path string, body []byte) ([]byte, int, error) {
|
|
return c.do(ctx, http.MethodDelete, path, body)
|
|
}
|
|
|
|
type rawResponse struct {
|
|
Body []byte
|
|
Status int
|
|
Headers http.Header
|
|
}
|
|
|
|
func (c *Client) doRaw(ctx context.Context, method, path string, body []byte) (*rawResponse, error) {
|
|
if hasEmptySegment(path) {
|
|
return nil, fmt.Errorf("%w: upstream path %q has an empty owner or repo segment", ErrValidation, path)
|
|
}
|
|
var reader io.Reader
|
|
if body != nil {
|
|
reader = bytes.NewReader(body)
|
|
}
|
|
req, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, reader)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
token := c.token
|
|
if token != "" {
|
|
req.Header.Set("Authorization", "token "+token)
|
|
}
|
|
if body != nil {
|
|
req.Header.Set("Content-Type", "application/json")
|
|
}
|
|
req.Header.Set("Accept", "application/json")
|
|
|
|
resp, err := c.hc.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
b, err := io.ReadAll(resp.Body)
|
|
return &rawResponse{Body: b, Status: resp.StatusCode, Headers: resp.Header}, err
|
|
}
|