feat(gitea): IssueTracker client + inject into brain server (#52)
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>
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
// 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
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
package gitea_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/mathiasbq/hyperguild/ingestion/internal/gitea"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
const testToken = "super-secret-token-value"
|
||||
|
||||
func TestNewNilWhenUnconfigured(t *testing.T) {
|
||||
assert.Nil(t, gitea.New("", testToken))
|
||||
assert.Nil(t, gitea.New("https://git.example", ""))
|
||||
}
|
||||
|
||||
func TestCreateIssueForcesOwnerAndAuth(t *testing.T) {
|
||||
var gotPath, gotAuth, gotBody string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gotPath = r.URL.Path
|
||||
gotAuth = r.Header.Get("Authorization")
|
||||
b, _ := io.ReadAll(r.Body)
|
||||
gotBody = string(b)
|
||||
assert.Equal(t, http.MethodPost, r.Method)
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"number": 42, "html_url": "https://git.d-ma.be/mathias/hyperguild/issues/42"})
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
c := gitea.New(srv.URL, testToken)
|
||||
require.NotNil(t, c)
|
||||
ref, err := c.CreateIssue(context.Background(), "hyperguild", "Do the thing", "details")
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "/api/v1/repos/mathias/hyperguild/issues", gotPath, "owner forced to mathias")
|
||||
assert.Equal(t, "token "+testToken, gotAuth)
|
||||
assert.Contains(t, gotBody, "Do the thing")
|
||||
assert.Equal(t, "hyperguild", ref.Repo)
|
||||
assert.Equal(t, 42, ref.Number)
|
||||
assert.Contains(t, ref.URL, "/issues/42")
|
||||
}
|
||||
|
||||
func TestCommentIssue(t *testing.T) {
|
||||
var gotPath string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gotPath = r.URL.Path
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"html_url": "https://git/c/1"})
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
ref, err := gitea.New(srv.URL, testToken).CommentIssue(context.Background(), "hyperguild", 7, "a comment")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "/api/v1/repos/mathias/hyperguild/issues/7/comments", gotPath)
|
||||
assert.Equal(t, 7, ref.Number)
|
||||
}
|
||||
|
||||
func TestCloseIssueWithComment(t *testing.T) {
|
||||
var paths []string
|
||||
var states []string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
paths = append(paths, r.Method+" "+r.URL.Path)
|
||||
if r.Method == http.MethodPatch {
|
||||
var body map[string]any
|
||||
b, _ := io.ReadAll(r.Body)
|
||||
_ = json.Unmarshal(b, &body)
|
||||
states = append(states, body["state"].(string))
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"number": 9, "html_url": "https://git/i/9"})
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
ref, err := gitea.New(srv.URL, testToken).CloseIssue(context.Background(), "hyperguild", 9, "closing because done")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 9, ref.Number)
|
||||
// Comment posted first, then state PATCHed to closed.
|
||||
assert.Contains(t, paths, "POST /api/v1/repos/mathias/hyperguild/issues/9/comments")
|
||||
assert.Contains(t, paths, "PATCH /api/v1/repos/mathias/hyperguild/issues/9")
|
||||
assert.Equal(t, []string{"closed"}, states)
|
||||
}
|
||||
|
||||
func TestCloseIssueNoComment(t *testing.T) {
|
||||
var commented bool
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if strings.HasSuffix(r.URL.Path, "/comments") {
|
||||
commented = true
|
||||
}
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"number": 3, "html_url": "https://git/i/3"})
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
_, err := gitea.New(srv.URL, testToken).CloseIssue(context.Background(), "hyperguild", 3, "")
|
||||
require.NoError(t, err)
|
||||
assert.False(t, commented, "empty comment ⇒ no comment POST")
|
||||
}
|
||||
|
||||
func TestErrorPathDoesNotLeakToken(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
_, _ = w.Write([]byte("boom"))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
_, err := gitea.New(srv.URL, testToken).CreateIssue(context.Background(), "hyperguild", "t", "b")
|
||||
require.Error(t, err)
|
||||
assert.NotContains(t, err.Error(), testToken, "token must never appear in an error message")
|
||||
assert.Contains(t, err.Error(), "500")
|
||||
}
|
||||
Reference in New Issue
Block a user