Implements the IssueTracker port as a real Gitea REST client (#49c) — the new outbound dependency the brain server gains for capture. - CreateIssue / CommentIssue / CloseIssue(+optional closing comment) over the Gitea API. Owner is the const "mathias", never caller-supplied, so a caller cannot redirect a write to another owner's repo. - Token read once at construction (BRAIN_GITEA_TOKEN), held in the struct, travels only in the Authorization header — never logged or in argv. Error messages carry status + truncated body, never the token (regression-tested). gitea.New returns nil when URL or token is unset, so missing config = tracker disabled via one nil check. - Injected into the MCP server behind the capture.IssueTracker interface via WithIssueTracker (constructor injection, swappable/testable); main wires it from BRAIN_GITEA_URL (default https://git.d-ma.be) + BRAIN_GITEA_TOKEN. Consumed by the capture use-case in #53. Tests use httptest transports: create (owner+auth header asserted), comment, close with/without comment, error path that proves the token never leaks into an error string. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
130 lines
4.4 KiB
Go
130 lines
4.4 KiB
Go
// Package gitea implements capture.IssueTracker against a Gitea instance
|
|
// over its REST API. It is the new outbound dependency the brain server
|
|
// gains for the capture capability (#49c/#52): the server otherwise does
|
|
// brain-local file ops only.
|
|
//
|
|
// Owner is hard-coded to the operator and never taken from caller input.
|
|
// The API token is read once at construction, held in the struct, and
|
|
// never logged or placed in argv — it travels only in the Authorization
|
|
// header of outbound requests (AGENTS.md secret-handling).
|
|
package gitea
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/mathiasbq/hyperguild/ingestion/internal/capture"
|
|
)
|
|
|
|
// owner is the fixed repository owner for every ticket operation. It is a
|
|
// constant, not a parameter, so a caller can never redirect a write to
|
|
// another owner's repo.
|
|
const owner = "mathias"
|
|
|
|
// Client is a Gitea REST API IssueTracker.
|
|
type Client struct {
|
|
baseURL string
|
|
token string
|
|
http *http.Client
|
|
}
|
|
|
|
// New constructs a Client. It returns nil when either baseURL or token is
|
|
// empty, so callers can treat missing config as "tracker disabled" with a
|
|
// single nil check (mirrors embed.New).
|
|
func New(baseURL, token string) *Client {
|
|
if baseURL == "" || token == "" {
|
|
return nil
|
|
}
|
|
return &Client{
|
|
baseURL: strings.TrimRight(baseURL, "/"),
|
|
token: token,
|
|
http: &http.Client{Timeout: 15 * time.Second},
|
|
}
|
|
}
|
|
|
|
// issueResponse is the subset of a Gitea issue/comment payload we read.
|
|
type issueResponse struct {
|
|
Number int `json:"number"`
|
|
HTMLURL string `json:"html_url"`
|
|
}
|
|
|
|
// CreateIssue opens a new issue under the fixed owner.
|
|
func (c *Client) CreateIssue(ctx context.Context, repo, title, body string) (capture.IssueRef, error) {
|
|
var out issueResponse
|
|
if err := c.do(ctx, http.MethodPost,
|
|
fmt.Sprintf("/api/v1/repos/%s/%s/issues", owner, repo),
|
|
map[string]any{"title": title, "body": body}, &out); err != nil {
|
|
return capture.IssueRef{}, err
|
|
}
|
|
return capture.IssueRef{Repo: repo, Number: out.Number, URL: out.HTMLURL}, nil
|
|
}
|
|
|
|
// CommentIssue posts a comment on an existing issue.
|
|
func (c *Client) CommentIssue(ctx context.Context, repo string, number int, body string) (capture.IssueRef, error) {
|
|
var out issueResponse
|
|
if err := c.do(ctx, http.MethodPost,
|
|
fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/comments", owner, repo, number),
|
|
map[string]any{"body": body}, &out); err != nil {
|
|
return capture.IssueRef{}, err
|
|
}
|
|
return capture.IssueRef{Repo: repo, Number: number, URL: out.HTMLURL}, nil
|
|
}
|
|
|
|
// CloseIssue closes an issue, first posting a closing comment when one is
|
|
// given (empty comment ⇒ close only).
|
|
func (c *Client) CloseIssue(ctx context.Context, repo string, number int, comment string) (capture.IssueRef, error) {
|
|
if strings.TrimSpace(comment) != "" {
|
|
if _, err := c.CommentIssue(ctx, repo, number, comment); err != nil {
|
|
return capture.IssueRef{}, err
|
|
}
|
|
}
|
|
var out issueResponse
|
|
if err := c.do(ctx, http.MethodPatch,
|
|
fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d", owner, repo, number),
|
|
map[string]any{"state": "closed"}, &out); err != nil {
|
|
return capture.IssueRef{}, err
|
|
}
|
|
return capture.IssueRef{Repo: repo, Number: number, URL: out.HTMLURL}, nil
|
|
}
|
|
|
|
// do performs a JSON request against the Gitea API and decodes the
|
|
// response into out. Errors carry the status and a truncated body for
|
|
// diagnosis but never the token.
|
|
func (c *Client) do(ctx context.Context, method, path string, payload any, out *issueResponse) error {
|
|
reqBody, err := json.Marshal(payload)
|
|
if err != nil {
|
|
return fmt.Errorf("marshal request: %w", err)
|
|
}
|
|
req, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, bytes.NewReader(reqBody))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Accept", "application/json")
|
|
// Gitea's token scheme. Held here only; never logged.
|
|
req.Header.Set("Authorization", "token "+c.token)
|
|
|
|
resp, err := c.http.Do(req)
|
|
if err != nil {
|
|
return fmt.Errorf("gitea %s %s: %w", method, path, err)
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
respBody, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
return fmt.Errorf("gitea %s %s: status %d: %s", method, path, resp.StatusCode, strings.TrimSpace(string(respBody)))
|
|
}
|
|
if out != nil && len(respBody) > 0 {
|
|
if err := json.Unmarshal(respBody, out); err != nil {
|
|
return fmt.Errorf("gitea %s %s: decode response: %w", method, path, err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|