feat(gitea): support extra outbound HTTP headers via GITEA_EXTRA_HEADERS

Add a GITEA_EXTRA_HEADERS environment variable that accepts a JSON
object of header name/value pairs (e.g. Cloudflare Access
credentials) and applies them to every outbound request to Gitea,
both the raw pkg/gitea.DoJSON/DoBytes path and the SDK-backed
pkg/gitea.NewClient path, without overriding Authorization,
Content-Type, or Accept.

Co-Authored-By: Codet <codet@commitgo.dev> (GPT-5-Codex)
This commit is contained in:
Lunny Xiao
2026-08-23 23:33:36 -07:00
parent 815a0e26aa
commit 6744cab627
8 changed files with 197 additions and 3 deletions
+27 -1
View File
@@ -30,6 +30,32 @@ func sharedTransport() *http.Transport {
return sharedTrans
}
// extraHeaderTransport injects flag.ExtraHeaders into every request, without
// overriding headers the caller already set (e.g. Authorization, Content-Type,
// Accept). It reads flag.ExtraHeaders on each round trip rather than caching
// it, so tests can change it between requests.
type extraHeaderTransport struct {
base http.RoundTripper
}
func (t *extraHeaderTransport) RoundTrip(req *http.Request) (*http.Response, error) {
headers := flag.ExtraHeaders
if len(headers) == 0 {
return t.base.RoundTrip(req)
}
cloned := req.Clone(req.Context())
for name, values := range headers {
if cloned.Header.Get(name) == "" {
cloned.Header[name] = values
}
}
return t.base.RoundTrip(cloned)
}
func giteaTransport() http.RoundTripper {
return &extraHeaderTransport{base: sharedTransport()}
}
// NewClient returns a cached *gitea.Client keyed by host+token. The SDK's per-client
// version cache and the shared transport let us reuse keep-alive connections
// and avoid the SDK's /api/v1/version preflight on every tool call.
@@ -40,7 +66,7 @@ func NewClient(token string) (*gitea.Client, error) {
}
httpClient := &http.Client{
Transport: sharedTransport(),
Transport: giteaTransport(),
CheckRedirect: checkRedirect,
}
opts := []gitea.ClientOption{