Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
09a7fad6ba | ||
|
|
02af7ee71c | ||
|
|
240c3ec081 | ||
|
|
51b823ae79 |
@@ -0,0 +1,200 @@
|
||||
# How gitea-mcp works
|
||||
|
||||
`gitea-mcp` is a custom **MCP (Model Context Protocol) front door for Gitea** — a
|
||||
small Go HTTP service that exposes ~60 Gitea operations as MCP tools over
|
||||
Streamable HTTP, so a claude.ai connector (or any MCP client) can drive the
|
||||
homelab's Gitea (`https://git.d-ma.be`) with proper auth, an owner allowlist,
|
||||
caller attribution, and defensive input handling.
|
||||
|
||||
It is **not** a generic Gitea proxy. It is an opinionated, allowlisted, single-
|
||||
tenant front door built on the shared **`mcp-chassis`** auth library.
|
||||
|
||||
---
|
||||
|
||||
## 1. Request lifecycle
|
||||
|
||||
Everything is wired in `cmd/gitea-mcp/main.go` on a plain `http.ServeMux`:
|
||||
|
||||
```
|
||||
POST /mcp
|
||||
→ OriginAllowlist (internal/mcp/origin.go) browser Origin gate
|
||||
→ BearerMiddleware (mcp-chassis auth) authN: static token OR JWT
|
||||
→ CallerMiddleware (internal/auth/caller.go) extract caller identity
|
||||
→ MCP server (internal/mcp/server.go) JSON-RPC dispatch → tools
|
||||
|
||||
GET /healthz unauthenticated health JSON
|
||||
GET /.well-known/oauth-protected-resource RFC 9728 metadata (when Dex set)
|
||||
```
|
||||
|
||||
The middleware order is deliberate: Origin first (cheap reject), then authN,
|
||||
then identity extraction, then the MCP handler. A tool call is a JSON-RPC
|
||||
`tools/call` that the registry dispatches to a `Tool` handler.
|
||||
|
||||
---
|
||||
|
||||
## 2. Authentication (`mcp-chassis` `BearerMiddleware`)
|
||||
|
||||
`Authorization: Bearer <token>` is checked with this precedence (`auth/bearer.go`
|
||||
in `mcp-chassis`):
|
||||
|
||||
1. **Static bearer** — constant-time compare against `GITEA_MCP_STATIC_TOKEN`.
|
||||
Wins immediately, never emits a `WWW-Authenticate` challenge. This is the
|
||||
service-to-service path (repo `.mcp.json` files carrying `GITEA_MCP_TOKEN`).
|
||||
2. **JWT validation** — against a **list of trusted OIDC issuers**
|
||||
(`NewMultiJWTValidator`, ADR-0011):
|
||||
- **Authentik** (`DEX_ISSUER_URL` + `MCP_AUDIENCE=claude-ai`) — the claude.ai
|
||||
web connector after its OAuth handshake.
|
||||
- **k3s cluster OIDC** (`K8S_ISSUER_URL` + `K8S_MCP_AUDIENCE=gitea-mcp`) — lets
|
||||
in-cluster pods authenticate with audience-bound projected **ServiceAccount
|
||||
tokens** instead of a static bearer. The k8s issuer is additive and
|
||||
best-effort: if the in-cluster OIDC can't be reached at startup it is
|
||||
dropped so Authentik auth is never taken down (see `cmd/gitea-mcp/main.go`
|
||||
and `cmd/gitea-mcp/k8soidc.go`).
|
||||
3. Otherwise **401**. If `MCP_RESOURCE_URL` + `DEX_ISSUER_URL` are set, the 401
|
||||
carries a `WWW-Authenticate: … resource_metadata=…` header (RFC 9728) so
|
||||
claude.ai's OAuth discovery can find `/.well-known/oauth-protected-resource`.
|
||||
|
||||
A JWKS/issuer outage yields **503** (`ErrUnavailable`), distinct from a
|
||||
present-but-invalid token's **401**, so a transient IdP blip is retried rather
|
||||
than treated as a hard auth failure.
|
||||
|
||||
### The k8s SA-token wrinkle (ADR-0011)
|
||||
|
||||
k3s serves its OIDC discovery/JWKS over the **cluster CA** and requires an
|
||||
**authenticated** request (anonymous → 401). So `k8sOIDCClient()` builds an HTTP
|
||||
client that trusts `/var/run/secrets/kubernetes.io/serviceaccount/ca.crt` and
|
||||
carries the pod's own SA bearer, passed to the chassis via
|
||||
`IssuerConfig.HTTPClient`. Proven live: a real `aud=gitea-mcp` SA token → the
|
||||
running server → HTTP 200.
|
||||
|
||||
### Origin allowlist
|
||||
|
||||
`OriginAllowlist` (`internal/mcp/origin.go`) rejects any request whose `Origin`
|
||||
header is not in `GITEA_MCP_ORIGIN_ALLOWLIST` (`https://claude.ai`,
|
||||
`https://api.anthropic.com`). An **empty** Origin (server-side callers) is
|
||||
allowed — Origin is a browser-only header.
|
||||
|
||||
---
|
||||
|
||||
## 3. Authorization, identity & attribution
|
||||
|
||||
- **Owner allowlist** (`internal/allowlist`) — tools only operate on owners in
|
||||
`GITEA_MCP_ALLOWED_OWNERS` (default `mathias`). A call for any other owner is
|
||||
rejected before it reaches Gitea.
|
||||
- **Caller identity** (`internal/auth/caller.go`) — the authenticated username is
|
||||
read from reverse-proxy identity headers, `X-Auth-Request-User` (the verified
|
||||
OIDC identity, authoritative) preferred over `X-Forwarded-User`. Conflicts are
|
||||
logged, not silently resolved.
|
||||
- **Identity footer** (`internal/identity/footer.go`) — mutating tools that write
|
||||
a body (issue/PR comments, creates) append
|
||||
`_Created via git-mcp on behalf of @<caller>_`, so actions taken through the
|
||||
front door are attributable even though all upstream calls use one service PAT.
|
||||
|
||||
---
|
||||
|
||||
## 4. Upstream Gitea access
|
||||
|
||||
`internal/gitea/Client` is a thin REST client over `GITEA_BASE_URL`
|
||||
(`https://git.d-ma.be`). Every upstream call carries a **single service PAT** —
|
||||
`GITEA_MCP_DEFAULT_TOKEN` — as `Authorization: token <PAT>` (`client.go`). So
|
||||
gitea-mcp is a *front door*, not a credential pass-through: the caller's identity
|
||||
is captured for attribution, but Gitea sees one service account. A 60s branch
|
||||
cache and a 30s HTTP timeout round it out.
|
||||
|
||||
Defensive guard: paths with an empty owner/repo segment (`//`) are rejected
|
||||
locally (`hasEmptySegment`) so callers get a typed validation error instead of
|
||||
Gitea's opaque `/api/swagger` 404.
|
||||
|
||||
---
|
||||
|
||||
## 5. Tools
|
||||
|
||||
Tools are registered in `internal/tools/registry.go` into a `registry.Registry`;
|
||||
each implements the `Tool` interface (name, JSON schema, handler). ~60 tools,
|
||||
by area:
|
||||
|
||||
| Area | Tools |
|
||||
|------|-------|
|
||||
| Repos | list, get, search, status, create, delete, update, tree, topics_update, mirror_push |
|
||||
| Files | file_read, file_write_branch, file_delete, dir_list |
|
||||
| Branches | branch_list, branch_delete, branch_protection_get |
|
||||
| Issues | list, get, create, edit, close, reopen, comment, label, list_comments |
|
||||
| PRs | list, get, create, comment, merge, files_diff |
|
||||
| Labels / Releases / Tags | label_list, release_create, tag_create |
|
||||
| Workflows (Actions) | run_list, run_status, run_trigger |
|
||||
| Search / Templates | code_search, create_project_from_template |
|
||||
| Composite | tbd_ship (trunk-based ship helper) |
|
||||
|
||||
Shared tool-layer hygiene (`internal/tools/tool.go`):
|
||||
- **Alias normalization** — `repo`↔`name` and `number`↔`index` are reconciled so
|
||||
a tool works whichever spelling a caller sends (an explicit canonical wins).
|
||||
- **Required-identifier validation** — an empty `repo`/`name` is rejected at the
|
||||
tool layer (avoids the bare-404 leak); `owner` is covered by the allowlist.
|
||||
- **Page-size cap** — limits are clamped to 50.
|
||||
|
||||
---
|
||||
|
||||
## 6. Configuration (env)
|
||||
|
||||
| Var | Purpose | Example |
|
||||
|-----|---------|---------|
|
||||
| `GITEA_MCP_PORT` | listen port | `8080` |
|
||||
| `GITEA_BASE_URL` | upstream Gitea | `https://git.d-ma.be` |
|
||||
| `GITEA_MCP_DEFAULT_TOKEN` | **upstream** service PAT (all Gitea calls) | *(secret)* |
|
||||
| `GITEA_MCP_STATIC_TOKEN` | static bearer for service-to-service auth | *(secret)* |
|
||||
| `GITEA_MCP_ALLOWED_OWNERS` | owner allowlist | `mathias` |
|
||||
| `GITEA_MCP_ORIGIN_ALLOWLIST` | permitted browser Origins | `https://claude.ai,…` |
|
||||
| `DEX_ISSUER_URL` / `MCP_AUDIENCE` | Authentik issuer + audience | `…/o/claude-ai/`, `claude-ai` |
|
||||
| `K8S_ISSUER_URL` / `K8S_MCP_AUDIENCE` | k8s OIDC issuer + audience (SA tokens) | `https://kubernetes.default.svc.cluster.local`, `gitea-mcp` |
|
||||
| `MCP_RESOURCE_URL` | this server's public URL for `.well-known` metadata | `https://git-mcp.d-ma.be` |
|
||||
|
||||
Secrets come from k8s Secrets (`gitea-mcp-secrets`, `gitea-mcp-static-token`);
|
||||
non-secret values are inlined in the Deployment.
|
||||
|
||||
---
|
||||
|
||||
## 7. Health & observability
|
||||
|
||||
- `GET /healthz` → `{"ok":true,"jwt":{"status":"disabled|enabled|degraded","last_error":"…"}}`.
|
||||
`degraded` = a JWT issuer was configured but init failed (static-token auth
|
||||
still works) — distinguishes "IdP unreachable" from "JWT not configured".
|
||||
- Every auth rejection is audit-logged (`mcp-chassis` `deny`) with the reason,
|
||||
client IP, token *type*, and a truncated SHA-256 **fingerprint** — never the
|
||||
raw token, so nothing secret lands in logs.
|
||||
|
||||
---
|
||||
|
||||
## 8. Deployment & CI/CD
|
||||
|
||||
- **Runs in** the `gitea-mcp` k3s namespace (`git.d-ma.be/mathias/infra`
|
||||
`k3s/apps/gitea-mcp/`): a Deployment on `localhost:5000/gitea-mcp:<sha>`, an
|
||||
ExternalSecret for tokens, and a **default-deny NetworkPolicy** + an
|
||||
`allow-ingress-nginx` rule (this namespace is the P6.1 netpol pilot — the only
|
||||
ingress allowed is from `ingress-nginx`).
|
||||
- **Public endpoint** `https://git-mcp.d-ma.be` (NPM → ingress-nginx → svc:8080).
|
||||
- **Pipeline** (`.gitea/workflows/cd.yml`, self-hosted koala runner): quality gate
|
||||
(test/vet) → `buildah` image → `localhost:5000` → smoke test → the deploy job
|
||||
clones infra over SSH, `sed`s the image tag in `k3s/apps/gitea-mcp/deployment.yaml`,
|
||||
commits, and triggers a **Flux** reconcile. Push to `main` ⇒ new image ⇒ rolled.
|
||||
|
||||
---
|
||||
|
||||
## 9. Operational notes
|
||||
|
||||
- **Rollback** = revert the image tag (or the env change) in the infra Deployment;
|
||||
Flux rolls back. Auth changes are additive, so enabling/disabling the k8s issuer
|
||||
never affects the Authentik/static paths.
|
||||
- **NetworkPolicy kill switch** — delete the `gitea-mcp` NetworkPolicies to
|
||||
restore open ingress if the pilot ever locks something out.
|
||||
- **claude.ai connector flakiness** — the claude.ai→gitea-mcp MCP path
|
||||
intermittently hits a Cloudflare "you have been blocked" page (the error names
|
||||
`anthropic.com`). On the Claude Code CLI, prefer the direct Gitea REST API for
|
||||
reliability; the MCP connector remains the claude.ai-web path. (brain:
|
||||
`prefer-rest-api-skills-over-mcp-on-cli-cloudflare-waf`.)
|
||||
|
||||
## References
|
||||
- Auth library: `git.d-ma.be/mathias/mcp-chassis` (`auth` package) — shared
|
||||
`BearerMiddleware` + multi-issuer `JWTValidator` + RFC 9728 handler.
|
||||
- Multi-issuer / SA-token design: infra `docs/decisions/ADR-0011-service-to-service-auth.md`.
|
||||
- Source map: `cmd/gitea-mcp/{main,healthz,k8soidc}.go`,
|
||||
`internal/{mcp,auth,gitea,tools,allowlist,identity,registry,config}`.
|
||||
@@ -0,0 +1,55 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
saCAFile = "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt"
|
||||
saTokenFile = "/var/run/secrets/kubernetes.io/serviceaccount/token" //nolint:gosec // path, not a secret
|
||||
)
|
||||
|
||||
// k8sBearerRT adds this pod's ServiceAccount bearer to every request. k3s serves
|
||||
// its OIDC discovery/JWKS over the cluster CA and requires an AUTHENTICATED
|
||||
// request (anonymous is 401), so the JWKS fetch must carry a token — ADR-0011.
|
||||
type k8sBearerRT struct {
|
||||
token string
|
||||
base http.RoundTripper
|
||||
}
|
||||
|
||||
func (b k8sBearerRT) RoundTrip(r *http.Request) (*http.Response, error) {
|
||||
r.Header.Set("Authorization", "Bearer "+b.token)
|
||||
return b.base.RoundTrip(r)
|
||||
}
|
||||
|
||||
// k8sOIDCClient builds an HTTP client that can reach the in-cluster k8s OIDC
|
||||
// discovery + JWKS: it trusts the cluster CA and carries this pod's SA bearer.
|
||||
// Returns an error (not running in a pod, files unreadable) so the caller can
|
||||
// skip the k8s issuer without disabling other auth.
|
||||
func k8sOIDCClient() (*http.Client, error) {
|
||||
ca, err := os.ReadFile(saCAFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read cluster CA: %w", err)
|
||||
}
|
||||
pool := x509.NewCertPool()
|
||||
if !pool.AppendCertsFromPEM(ca) {
|
||||
return nil, fmt.Errorf("parse cluster CA: no certs in %s", saCAFile)
|
||||
}
|
||||
tok, err := os.ReadFile(saTokenFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read SA token: %w", err)
|
||||
}
|
||||
return &http.Client{
|
||||
Timeout: 15 * time.Second,
|
||||
Transport: k8sBearerRT{
|
||||
token: strings.TrimSpace(string(tok)),
|
||||
base: &http.Transport{TLSClientConfig: &tls.Config{RootCAs: pool, MinVersion: tls.VersionTLS12}},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
+30
-1
@@ -37,7 +37,36 @@ func main() {
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
jwtValidator, jwtInitErr := chassisauth.NewJWTValidator(ctx, cfg.DexIssuerURL, cfg.MCPAudience)
|
||||
// Build the trusted-issuer list. Authentik (DEX_ISSUER_URL) for interactive/
|
||||
// web clients; the in-cluster k8s OIDC issuer for ServiceAccount tokens
|
||||
// (ADR-0011 D1). The k8s issuer is ADDITIVE and best-effort: if its client
|
||||
// can't be built (not in a pod) or its discovery is unreachable at startup,
|
||||
// it is dropped so existing Authentik/static auth is never taken down.
|
||||
var issuers []chassisauth.IssuerConfig
|
||||
if cfg.DexIssuerURL != "" {
|
||||
issuers = append(issuers, chassisauth.IssuerConfig{IssuerURL: cfg.DexIssuerURL, Audience: cfg.MCPAudience})
|
||||
}
|
||||
if cfg.K8sIssuerURL != "" {
|
||||
if client, cerr := k8sOIDCClient(); cerr != nil {
|
||||
logger.Warn("k8s OIDC client init failed; SA-token auth disabled (other auth unaffected)", "err", cerr)
|
||||
} else {
|
||||
issuers = append(issuers, chassisauth.IssuerConfig{IssuerURL: cfg.K8sIssuerURL, Audience: cfg.K8sAudience, HTTPClient: client})
|
||||
}
|
||||
}
|
||||
|
||||
jwtValidator, jwtInitErr := chassisauth.NewMultiJWTValidator(ctx, issuers)
|
||||
if jwtInitErr != nil && cfg.K8sIssuerURL != "" && len(issuers) > 1 {
|
||||
// A configured issuer (likely the in-cluster k8s OIDC) was unreachable at
|
||||
// startup. Don't let it take down Authentik JWT auth — retry without it.
|
||||
logger.Warn("multi-issuer init failed; retrying without the k8s issuer", "err", jwtInitErr)
|
||||
pub := make([]chassisauth.IssuerConfig, 0, len(issuers))
|
||||
for _, ic := range issuers {
|
||||
if ic.IssuerURL != cfg.K8sIssuerURL {
|
||||
pub = append(pub, ic)
|
||||
}
|
||||
}
|
||||
jwtValidator, jwtInitErr = chassisauth.NewMultiJWTValidator(ctx, pub)
|
||||
}
|
||||
if jwtInitErr != nil {
|
||||
logger.Warn("jwt validator init failed; JWT auth degraded", "err", jwtInitErr)
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ module git.d-ma.be/mathias/gitea-mcp
|
||||
go 1.26.2
|
||||
|
||||
require (
|
||||
git.d-ma.be/mathias/mcp-chassis v0.3.0
|
||||
git.d-ma.be/mathias/mcp-chassis v0.5.0
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7
|
||||
github.com/stretchr/testify v1.11.1
|
||||
)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
git.d-ma.be/mathias/mcp-chassis v0.3.0 h1:lV/vDsjrDeZojT7lhcwolM1lMZpsnEKEvf4kEHrxIa0=
|
||||
git.d-ma.be/mathias/mcp-chassis v0.3.0/go.mod h1:Ks7EK2UnGAN0H3rJjKUxUagX8/ZBdtLrOlcUbv0RwH8=
|
||||
git.d-ma.be/mathias/mcp-chassis v0.5.0 h1:0w3dt4t4r8OtZBHIOoZkR6p6L3L6YDiqhMh9bTI/w/8=
|
||||
git.d-ma.be/mathias/mcp-chassis v0.5.0/go.mod h1:Ks7EK2UnGAN0H3rJjKUxUagX8/ZBdtLrOlcUbv0RwH8=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
|
||||
@@ -15,6 +15,8 @@ type Config struct {
|
||||
DexIssuerURL string // DEX_ISSUER_URL, e.g. https://auth.d-ma.be; empty disables JWT auth
|
||||
MCPAudience string // MCP_AUDIENCE, JWT audience claim to validate, e.g. claude-ai
|
||||
MCPResourceURL string // MCP_RESOURCE_URL, this server's public URL for /.well-known metadata
|
||||
K8sIssuerURL string // K8S_ISSUER_URL, in-cluster OIDC issuer for ServiceAccount-token auth (ADR-0011 D1); empty disables
|
||||
K8sAudience string // K8S_MCP_AUDIENCE, required audience claim for k8s SA tokens
|
||||
}
|
||||
|
||||
func Load() (Config, error) {
|
||||
@@ -28,6 +30,8 @@ func Load() (Config, error) {
|
||||
DexIssuerURL: os.Getenv("DEX_ISSUER_URL"),
|
||||
MCPAudience: os.Getenv("MCP_AUDIENCE"),
|
||||
MCPResourceURL: os.Getenv("MCP_RESOURCE_URL"),
|
||||
K8sIssuerURL: os.Getenv("K8S_ISSUER_URL"),
|
||||
K8sAudience: os.Getenv("K8S_MCP_AUDIENCE"),
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ func (t *CreateProjectFromTemplate) Descriptor() registry.ToolDescriptor {
|
||||
"description":{"type":"string"},
|
||||
"private":{"type":"boolean"},
|
||||
"template_name":{"type":"string","description":"Template repo name to generate from. Defaults to the server-configured template. Ignored when resume=true."},
|
||||
"dispatch_allow":{"type":"boolean","description":"When true, inject a .dispatch-allow file so the project is opt-in for headless dispatch (dispatch#3). Default false. Safe to re-request on resume."},
|
||||
"dispatch_allow":{"type":"boolean","description":"When true, inject a .dispatch-allow file (dispatch#3) AND register owner/name into mathias/dispatch's git-tracked allowlist (dispatch-repos.txt, dispatch#19) — both gates are required for the watcher to actually pick the repo up; each is applied independently and idempotently. Default false. Safe to re-request on resume."},
|
||||
"resume":{"type":"boolean","description":"Resume substitution on an ALREADY-CREATED repo from a prior call that hit infra#179's branch-writability race (its partial_failure names this). Skips template lookup and repo generation entirely; the destination must already exist. Safe to call repeatedly — files/renames already correct are left untouched. Default false."}
|
||||
},
|
||||
"required":["owner","name"]
|
||||
@@ -89,8 +89,21 @@ type createProjectResult struct {
|
||||
FilesSubstituted []string `json:"files_substituted"`
|
||||
PartialFailure string `json:"partial_failure,omitempty"`
|
||||
DispatchAllowFailure string `json:"dispatch_allow_failure,omitempty"`
|
||||
DispatchAllowlisted bool `json:"dispatch_allowlisted,omitempty"`
|
||||
DispatchAllowlistFailure string `json:"dispatch_allowlist_failure,omitempty"`
|
||||
}
|
||||
|
||||
// The dispatch allowlist (dispatch#19) is a fixed integration point — a
|
||||
// specific file in a specific repo the mathias/dispatch watcher reads at the
|
||||
// start of every cycle. Hardcoded to match its own DISPATCH_ALLOWLIST_OWNER/
|
||||
// _REPO/_PATH defaults (unconfigured in the live CronJob, confirmed 2026-07-06).
|
||||
const (
|
||||
dispatchAllowlistOwner = "mathias"
|
||||
dispatchAllowlistRepo = "dispatch"
|
||||
dispatchAllowlistPath = "dispatch-repos.txt"
|
||||
dispatchAllowlistBranch = "main"
|
||||
)
|
||||
|
||||
func (t *CreateProjectFromTemplate) Call(ctx context.Context, raw json.RawMessage) (json.RawMessage, error) {
|
||||
var args createProjectArgs
|
||||
if err := parseArgs(raw, &args); err != nil {
|
||||
@@ -147,19 +160,26 @@ func (t *CreateProjectFromTemplate) Call(ctx context.Context, raw json.RawMessag
|
||||
}
|
||||
}
|
||||
|
||||
// Opt the new project into headless dispatch if asked: presence of a
|
||||
// .dispatch-allow file on the default branch marks it dispatch-eligible
|
||||
// (dispatch#3). Skip if substitution itself already stalled — don't mark an
|
||||
// incomplete repo dispatch-eligible. A failure here is reported in its OWN
|
||||
// field (gitea-mcp#51) — it must never be indistinguishable from a
|
||||
// substitution failure, since one can succeed while the other doesn't.
|
||||
// Opt the new project into headless dispatch if asked. Two INDEPENDENT gates,
|
||||
// both required (dispatch#3 + dispatch#19): the .dispatch-allow marker on this
|
||||
// repo, and this repo's owner/name listed in mathias/dispatch's git-tracked
|
||||
// allowlist. Skip both if substitution itself already stalled — don't mark an
|
||||
// incomplete repo dispatch-eligible. Each failure is reported in its OWN field
|
||||
// (gitea-mcp#51/#54) — never conflated with each other or with substitution,
|
||||
// since any one of the three can fail independently of the other two.
|
||||
if args.DispatchAllow && result.PartialFailure == "" {
|
||||
didWrite, fail := t.injectDispatchAllow(ctx, args.Owner, args.Name, branch)
|
||||
if fail != "" {
|
||||
if didWrite, fail := t.injectDispatchAllow(ctx, args.Owner, args.Name, branch); fail != "" {
|
||||
result.DispatchAllowFailure = fail
|
||||
} else if didWrite {
|
||||
result.FilesSubstituted = append(result.FilesSubstituted, ".dispatch-allow")
|
||||
}
|
||||
|
||||
ownerName := args.Owner + "/" + args.Name
|
||||
if didRegister, fail := t.registerDispatchAllowlist(ctx, ownerName); fail != "" {
|
||||
result.DispatchAllowlistFailure = fail
|
||||
} else if didRegister {
|
||||
result.DispatchAllowlisted = true
|
||||
}
|
||||
}
|
||||
|
||||
// If substitution stalled because the generated branch wasn't writable in time,
|
||||
@@ -302,6 +322,42 @@ func (t *CreateProjectFromTemplate) injectDispatchAllow(ctx context.Context, own
|
||||
return true, ""
|
||||
}
|
||||
|
||||
// registerDispatchAllowlist appends ownerName ("owner/name") to
|
||||
// mathias/dispatch's git-tracked dispatch-repos.txt if not already listed
|
||||
// (gitea-mcp#54) — idempotent, safe to call on every resume. This is the
|
||||
// SECOND of the two required dispatch gates: being listed here is necessary
|
||||
// but not sufficient on its own (the repo also needs its own .dispatch-allow
|
||||
// marker, injectDispatchAllow's job) — dispatch#3's structural trust-zone
|
||||
// design requires both independently. Returns whether a write actually
|
||||
// happened, so the caller only reports a fresh registration, not a no-op.
|
||||
func (t *CreateProjectFromTemplate) registerDispatchAllowlist(ctx context.Context, ownerName string) (didRegister bool, failure string) {
|
||||
fc, err := t.c.GetFileContents(ctx, dispatchAllowlistOwner, dispatchAllowlistRepo, dispatchAllowlistPath, dispatchAllowlistBranch)
|
||||
if err != nil {
|
||||
return false, fmt.Sprintf("read %s/%s:%s: %v", dispatchAllowlistOwner, dispatchAllowlistRepo, dispatchAllowlistPath, err)
|
||||
}
|
||||
decoded, err := base64.StdEncoding.DecodeString(fc.Content)
|
||||
if err != nil {
|
||||
return false, fmt.Sprintf("decode %s: %v", dispatchAllowlistPath, err)
|
||||
}
|
||||
content := string(decoded)
|
||||
for _, line := range strings.Split(content, "\n") {
|
||||
if strings.TrimSpace(line) == ownerName {
|
||||
return false, "" // already listed — idempotent no-op
|
||||
}
|
||||
}
|
||||
|
||||
newContent := strings.TrimRight(content, "\n") + "\n" + ownerName + "\n"
|
||||
if _, err := t.c.UpsertFile(ctx, dispatchAllowlistOwner, dispatchAllowlistRepo, dispatchAllowlistPath, gitea.UpsertFileArgs{
|
||||
Branch: dispatchAllowlistBranch,
|
||||
Content: base64.StdEncoding.EncodeToString([]byte(newContent)),
|
||||
Message: fmt.Sprintf("chore(allowlist): opt in %s for headless dispatch", ownerName),
|
||||
Sha: fc.Sha,
|
||||
}); err != nil {
|
||||
return false, fmt.Sprintf("append to %s/%s:%s: %v", dispatchAllowlistOwner, dispatchAllowlistRepo, dispatchAllowlistPath, err)
|
||||
}
|
||||
return true, ""
|
||||
}
|
||||
|
||||
// infra179FinalizeMessage explains the best-effort outcome when gitea's slow
|
||||
// async template-generate (infra#179) leaves the branch unwritable within the
|
||||
// budget. It points at the concrete recovery step — re-invoking this same tool
|
||||
@@ -374,21 +430,7 @@ func (t *CreateProjectFromTemplate) substituteEntry(ctx context.Context, owner,
|
||||
enc := base64.StdEncoding.EncodeToString([]byte(newContent))
|
||||
|
||||
if renamed {
|
||||
if err := t.upsertRetry(ctx, owner, name, newPath, gitea.UpsertFileArgs{
|
||||
Branch: branch,
|
||||
Content: enc,
|
||||
Message: fmt.Sprintf("template: substitute + rename %s -> %s", path, newPath),
|
||||
}); err != nil {
|
||||
return "", fmt.Sprintf("write %s: %v", newPath, err)
|
||||
}
|
||||
if _, err := t.c.DeleteFile(ctx, owner, name, path, gitea.DeleteFileArgs{
|
||||
Branch: branch,
|
||||
Sha: fc.Sha,
|
||||
Message: fmt.Sprintf("template: drop placeholder path %s", path),
|
||||
}); err != nil {
|
||||
return "", fmt.Sprintf("delete %s: %v", path, err)
|
||||
}
|
||||
return path + " -> " + newPath, ""
|
||||
return t.renameEntry(ctx, owner, name, branch, path, newPath, enc, newContent, fc.Sha)
|
||||
}
|
||||
|
||||
if err := t.upsertRetry(ctx, owner, name, path, gitea.UpsertFileArgs{
|
||||
@@ -401,3 +443,45 @@ func (t *CreateProjectFromTemplate) substituteEntry(ctx context.Context, owner,
|
||||
}
|
||||
return path, ""
|
||||
}
|
||||
|
||||
// renameEntry writes newPath then deletes oldPath. Both halves are idempotent
|
||||
// so a resume that hits a prior write-succeeded/delete-failed rename (the two
|
||||
// are separate, non-atomic API calls) completes cleanly instead of erroring on
|
||||
// a blind re-create at a path that already exists (gitea-mcp#53): if newPath
|
||||
// already holds the correct content, the write is skipped and only the
|
||||
// outstanding delete of oldPath runs; if oldPath is already gone, the delete
|
||||
// is a no-op too.
|
||||
func (t *CreateProjectFromTemplate) renameEntry(ctx context.Context, owner, name, branch, oldPath, newPath, enc, newContent, oldSha string) (substituted, failure string) {
|
||||
existing, err := t.c.GetFileContents(ctx, owner, name, newPath, branch)
|
||||
switch {
|
||||
case err == nil:
|
||||
decoded, derr := base64.StdEncoding.DecodeString(existing.Content)
|
||||
if derr == nil && string(decoded) == newContent {
|
||||
break // already correct from a prior partial run — skip the write
|
||||
}
|
||||
if writeErr := t.upsertRetry(ctx, owner, name, newPath, gitea.UpsertFileArgs{
|
||||
Branch: branch, Content: enc, Sha: existing.Sha,
|
||||
Message: fmt.Sprintf("template: substitute + rename %s -> %s", oldPath, newPath),
|
||||
}); writeErr != nil {
|
||||
return "", fmt.Sprintf("write %s: %v", newPath, writeErr)
|
||||
}
|
||||
case errors.Is(err, gitea.ErrNotFound):
|
||||
if writeErr := t.upsertRetry(ctx, owner, name, newPath, gitea.UpsertFileArgs{
|
||||
Branch: branch, Content: enc,
|
||||
Message: fmt.Sprintf("template: substitute + rename %s -> %s", oldPath, newPath),
|
||||
}); writeErr != nil {
|
||||
return "", fmt.Sprintf("write %s: %v", newPath, writeErr)
|
||||
}
|
||||
default:
|
||||
return "", fmt.Sprintf("read %s: %v", newPath, err)
|
||||
}
|
||||
|
||||
if _, err := t.c.DeleteFile(ctx, owner, name, oldPath, gitea.DeleteFileArgs{
|
||||
Branch: branch,
|
||||
Sha: oldSha,
|
||||
Message: fmt.Sprintf("template: drop placeholder path %s", oldPath),
|
||||
}); err != nil && !errors.Is(err, gitea.ErrNotFound) {
|
||||
return "", fmt.Sprintf("delete %s: %v", oldPath, err)
|
||||
}
|
||||
return oldPath + " -> " + newPath, ""
|
||||
}
|
||||
|
||||
@@ -38,6 +38,12 @@ type fakeTemplateServer struct {
|
||||
deletes []string
|
||||
putBodies map[string]string // path -> decoded written content
|
||||
repoGetsPost int // GET dest after generate (branch fallback)
|
||||
|
||||
// dispatchRepos simulates mathias/dispatch:dispatch-repos.txt (gitea-mcp#54).
|
||||
// "" (default) 404s the read, matching a repo that hasn't seeded the file
|
||||
// (a distinct error path); tests that care set it explicitly.
|
||||
dispatchRepos string
|
||||
dispatchReposPuts int
|
||||
}
|
||||
|
||||
func newFakeTemplateServer(files map[string]string, genBranch string) *fakeTemplateServer {
|
||||
@@ -60,7 +66,28 @@ func (f *fakeTemplateServer) handler(t *testing.T, tmpl, dest string) http.Handl
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
p := r.URL.Path
|
||||
|
||||
const dispatchReposPath = "/api/v1/repos/mathias/dispatch/contents/dispatch-repos.txt"
|
||||
switch {
|
||||
case r.Method == http.MethodGet && p == dispatchReposPath:
|
||||
if f.dispatchRepos == "" {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
_, _ = w.Write([]byte(`{"message":"not found"}`))
|
||||
return
|
||||
}
|
||||
_, _ = fmt.Fprintf(w, `{"path":"dispatch-repos.txt","sha":"repos-sha","content":%q,"encoding":"base64"}`, encb64(f.dispatchRepos))
|
||||
|
||||
case r.Method == http.MethodPut && p == dispatchReposPath:
|
||||
raw, _ := io.ReadAll(r.Body)
|
||||
var args struct {
|
||||
Content string `json:"content"`
|
||||
}
|
||||
_ = json.Unmarshal(raw, &args)
|
||||
dec, _ := base64.StdEncoding.DecodeString(args.Content)
|
||||
f.dispatchRepos = string(dec)
|
||||
f.dispatchReposPuts++
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write([]byte(`{"content":{"path":"dispatch-repos.txt","sha":"repos-sha2"},"commit":{"sha":"c"}}`))
|
||||
|
||||
case r.Method == http.MethodGet && p == "/api/v1/repos/mathias/"+tmpl:
|
||||
_, _ = w.Write([]byte(templateRepoJSON(tmpl, true)))
|
||||
|
||||
@@ -151,6 +178,8 @@ type createOut struct {
|
||||
FilesSubstituted []string `json:"files_substituted"`
|
||||
PartialFailure string `json:"partial_failure,omitempty"`
|
||||
DispatchAllowFailure string `json:"dispatch_allow_failure,omitempty"`
|
||||
DispatchAllowlisted bool `json:"dispatch_allowlisted,omitempty"`
|
||||
DispatchAllowlistFailure string `json:"dispatch_allowlist_failure,omitempty"`
|
||||
}
|
||||
|
||||
// Happy path: whole-tree substitution, content + path rename, correct module host.
|
||||
@@ -329,6 +358,29 @@ func TestCreateProject_Resume_AlreadyFullyDone_IsSuccess(t *testing.T) {
|
||||
assert.Empty(t, out.PartialFailure, "nothing left to do on resume must be success, not a loud failure")
|
||||
}
|
||||
|
||||
// A resume where a prior partial run's rename write SUCCEEDED but its paired
|
||||
// delete FAILED (both are separate, non-atomic API calls) must complete
|
||||
// cleanly: recognize the new path is already correct, skip re-writing it, and
|
||||
// just finish the outstanding delete of the stray old path (gitea-mcp#53).
|
||||
func TestCreateProject_Resume_StrayRenamedOldPath_CompletesCleanly(t *testing.T) {
|
||||
files := map[string]string{
|
||||
// stray: delete never completed in the prior run
|
||||
"cmd/__PROJECT_NAME__/main.go": "package main\nimport \"__MODULE_PATH__/pkg/litellm\"\nconst n = \"__PROJECT_NAME__\"\n",
|
||||
// already correct: the write half of the same prior rename DID complete
|
||||
"cmd/new-svc/main.go": "package main\nimport \"git.d-ma.be/mathias/new-svc/pkg/litellm\"\nconst n = \"new-svc\"\n",
|
||||
}
|
||||
f := newFakeTemplateServerResumed(files, "main")
|
||||
srv := httptest.NewServer(f.handler(t, "template-go-agent", "new-svc"))
|
||||
defer srv.Close()
|
||||
|
||||
out := callTool(t, srv.URL, "template-go-agent", `{"owner":"mathias","name":"new-svc","resume":true}`)
|
||||
|
||||
assert.Empty(t, out.PartialFailure)
|
||||
assert.Contains(t, out.FilesSubstituted, "cmd/__PROJECT_NAME__/main.go -> cmd/new-svc/main.go")
|
||||
assert.NotContains(t, f.puts, "cmd/new-svc/main.go", "already-correct new path must not be rewritten")
|
||||
assert.Contains(t, f.deletes, "cmd/__PROJECT_NAME__/main.go", "the outstanding delete must still happen")
|
||||
}
|
||||
|
||||
// dispatch_allow injection is idempotent on resume: if .dispatch-allow already
|
||||
// has the correct content (from an earlier successful injection), re-invoking
|
||||
// must not attempt another write — and must not error the way a naive
|
||||
@@ -390,6 +442,86 @@ func TestCreateProject_DispatchAllowFailure_IsDistinctField(t *testing.T) {
|
||||
assert.Contains(t, out.FilesSubstituted, "go.mod", "substitution must still be reported despite the separate dispatch failure")
|
||||
}
|
||||
|
||||
// dispatch_allow now ALSO registers the new repo into mathias/dispatch's
|
||||
// git-tracked allowlist (dispatch-repos.txt) — the second of the two required
|
||||
// dispatch gates, independent of the .dispatch-allow marker (gitea-mcp#54).
|
||||
func TestCreateProject_DispatchAllow_RegistersAllowlist(t *testing.T) {
|
||||
files := map[string]string{"go.mod": "module __MODULE_PATH__\n"}
|
||||
f := newFakeTemplateServer(files, "main")
|
||||
f.dispatchRepos = "# comment\nmathias/dispatch-sandbox\nmathias/cobalt-dingo\n"
|
||||
srv := httptest.NewServer(f.handler(t, "template-go-agent", "new-svc"))
|
||||
defer srv.Close()
|
||||
|
||||
out := callTool(t, srv.URL, "template-go-agent", `{"owner":"mathias","name":"new-svc","dispatch_allow":true}`)
|
||||
|
||||
assert.Empty(t, out.PartialFailure)
|
||||
assert.Empty(t, out.DispatchAllowlistFailure)
|
||||
assert.True(t, out.DispatchAllowlisted)
|
||||
assert.Equal(t, 1, f.dispatchReposPuts)
|
||||
assert.Contains(t, f.dispatchRepos, "mathias/new-svc")
|
||||
// existing entries preserved, not clobbered
|
||||
assert.Contains(t, f.dispatchRepos, "mathias/dispatch-sandbox")
|
||||
assert.Contains(t, f.dispatchRepos, "mathias/cobalt-dingo")
|
||||
}
|
||||
|
||||
// Registering is idempotent: a repo already listed (e.g. a resume re-running
|
||||
// with dispatch_allow:true) must not produce a duplicate line or a redundant write.
|
||||
func TestCreateProject_DispatchAllow_RegistersAllowlist_AlreadyListedIsNoop(t *testing.T) {
|
||||
files := map[string]string{"go.mod": "module git.d-ma.be/mathias/new-svc\n"} // already substituted
|
||||
f := newFakeTemplateServerResumed(files, "main")
|
||||
f.dispatchRepos = "mathias/dispatch-sandbox\nmathias/new-svc\n"
|
||||
srv := httptest.NewServer(f.handler(t, "template-go-agent", "new-svc"))
|
||||
defer srv.Close()
|
||||
|
||||
out := callTool(t, srv.URL, "template-go-agent", `{"owner":"mathias","name":"new-svc","resume":true,"dispatch_allow":true}`)
|
||||
|
||||
assert.Empty(t, out.PartialFailure)
|
||||
assert.Empty(t, out.DispatchAllowlistFailure)
|
||||
assert.False(t, out.DispatchAllowlisted, "already-listed must not be reported as a fresh registration")
|
||||
assert.Equal(t, 0, f.dispatchReposPuts, "already-listed repo must not trigger a write")
|
||||
}
|
||||
|
||||
// A failure registering the allowlist is reported in its OWN field — distinct
|
||||
// from PartialFailure (substitution) AND DispatchAllowFailure (the marker
|
||||
// file) — since all three are independent gates that can fail independently.
|
||||
func TestCreateProject_DispatchAllowlistFailure_IsDistinctField(t *testing.T) {
|
||||
files := map[string]string{"go.mod": "module __MODULE_PATH__\n"}
|
||||
f := newFakeTemplateServer(files, "main")
|
||||
f.dispatchRepos = "mathias/dispatch-sandbox\n"
|
||||
base := f.handler(t, "template-go-agent", "new-svc")
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodPut && r.URL.Path == "/api/v1/repos/mathias/dispatch/contents/dispatch-repos.txt" {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
_, _ = w.Write([]byte(`{"message":"boom"}`))
|
||||
return
|
||||
}
|
||||
base(w, r)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
out := callTool(t, srv.URL, "template-go-agent", `{"owner":"mathias","name":"new-svc","dispatch_allow":true}`)
|
||||
|
||||
assert.Empty(t, out.PartialFailure, "substitution succeeded — must not be conflated")
|
||||
assert.Empty(t, out.DispatchAllowFailure, ".dispatch-allow marker succeeded — must not be conflated")
|
||||
assert.NotEmpty(t, out.DispatchAllowlistFailure)
|
||||
assert.Contains(t, out.DispatchAllowlistFailure, "dispatch-repos.txt")
|
||||
assert.Contains(t, out.FilesSubstituted, "go.mod", "substitution must still be reported despite the separate allowlist failure")
|
||||
}
|
||||
|
||||
// dispatch_allow=false/omitted must never touch the allowlist file at all.
|
||||
func TestCreateProject_DispatchAllowFalse_DoesNotTouchAllowlist(t *testing.T) {
|
||||
files := map[string]string{"go.mod": "module __MODULE_PATH__\n"}
|
||||
f := newFakeTemplateServer(files, "main")
|
||||
srv := httptest.NewServer(f.handler(t, "template-go-agent", "new-svc"))
|
||||
defer srv.Close()
|
||||
|
||||
out := callTool(t, srv.URL, "template-go-agent", `{"owner":"mathias","name":"new-svc"}`)
|
||||
|
||||
assert.Empty(t, out.PartialFailure)
|
||||
assert.False(t, out.DispatchAllowlisted)
|
||||
assert.Equal(t, 0, f.dispatchReposPuts)
|
||||
}
|
||||
|
||||
// ── guardrails unchanged by the rewrite ──────────────────────────────────────
|
||||
|
||||
func TestCreateProject_NameRegexFailure(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user