Files
gitea-mcp/internal/gitea/client.go
Mathias Bergqvist 64559f0250
Some checks failed
CD / Lint / Test / Vet (push) Failing after 2s
CD / Build & Import (push) Has been skipped
CD / Deploy via GitOps (push) Has been skipped
fix(lint): check Body.Close error return in http client
2026-05-05 08:55:31 +02:00

129 lines
3.3 KiB
Go

package gitea
import (
"bytes"
"context"
"io"
"net/http"
"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
}
func (c *Client) doOnce(ctx context.Context, method, path string, body []byte) ([]byte, int, error) {
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
}
if c.token != "" {
req.Header.Set("Authorization", "token "+c.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)
}
type rawResponse struct {
Body []byte
Status int
Headers http.Header
}
func (c *Client) doRaw(ctx context.Context, method, path string, body []byte) (*rawResponse, error) {
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
}
if c.token != "" {
req.Header.Set("Authorization", "token "+c.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
}