feat: add context.Context + unexport client fields
CI / test (pull_request) Successful in 13s
CI / review (gpt-5, sonnet, SONNET_REVIEW_TOKEN) (pull_request) Successful in 54s
CI / review (gpt-5-mini, gpt, GPT_REVIEW_TOKEN) (pull_request) Successful in 1m22s

REVIEW.md findings 1-4, 14:
- All Gitea client methods now accept context.Context as first param
- All LLM client methods now accept context.Context as first param
- Use http.NewRequestWithContext for cancellation/timeout support
- Main uses 3-minute timeout context for all operations
- Unexport Client struct fields (baseURL, token, apiKey, etc.)
- Use bytes.NewReader instead of strings.NewReader(string(...))
This commit is contained in:
Rodin
2026-05-01 12:31:41 -07:00
parent f77ea171c3
commit 27e0056f29
6 changed files with 112 additions and 99 deletions
+43 -41
View File
@@ -1,6 +1,8 @@
package gitea
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
@@ -10,17 +12,17 @@ import (
// Client interacts with the Gitea API.
type Client struct {
BaseURL string
Token string
HTTP *http.Client
baseURL string
token string
http *http.Client
}
// NewClient creates a new Gitea API client.
func NewClient(baseURL, token string) *Client {
return &Client{
BaseURL: strings.TrimRight(baseURL, "/"),
Token: token,
HTTP: &http.Client{},
baseURL: strings.TrimRight(baseURL, "/"),
token: token,
http: &http.Client{},
}
}
@@ -49,9 +51,9 @@ type ChangedFile struct {
}
// GetPullRequest fetches PR metadata.
func (c *Client) GetPullRequest(owner, repo string, number int) (*PullRequest, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/pulls/%d", c.BaseURL, owner, repo, number)
body, err := c.doGet(url)
func (c *Client) GetPullRequest(ctx context.Context, owner, repo string, number int) (*PullRequest, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/pulls/%d", c.baseURL, owner, repo, number)
body, err := c.doGet(ctx, url)
if err != nil {
return nil, fmt.Errorf("fetch PR: %w", err)
}
@@ -63,9 +65,9 @@ func (c *Client) GetPullRequest(owner, repo string, number int) (*PullRequest, e
}
// GetPullRequestDiff fetches the unified diff for a PR.
func (c *Client) GetPullRequestDiff(owner, repo string, number int) (string, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/pulls/%d.diff", c.BaseURL, owner, repo, number)
body, err := c.doGet(url)
func (c *Client) GetPullRequestDiff(ctx context.Context, owner, repo string, number int) (string, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/pulls/%d.diff", c.baseURL, owner, repo, number)
body, err := c.doGet(ctx, url)
if err != nil {
return "", fmt.Errorf("fetch diff: %w", err)
}
@@ -73,9 +75,9 @@ func (c *Client) GetPullRequestDiff(owner, repo string, number int) (string, err
}
// GetPullRequestFiles fetches the list of files changed in a PR.
func (c *Client) GetPullRequestFiles(owner, repo string, number int) ([]ChangedFile, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/pulls/%d/files", c.BaseURL, owner, repo, number)
body, err := c.doGet(url)
func (c *Client) GetPullRequestFiles(ctx context.Context, owner, repo string, number int) ([]ChangedFile, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/pulls/%d/files", c.baseURL, owner, repo, number)
body, err := c.doGet(ctx, url)
if err != nil {
return nil, fmt.Errorf("fetch PR files: %w", err)
}
@@ -87,9 +89,9 @@ func (c *Client) GetPullRequestFiles(owner, repo string, number int) ([]ChangedF
}
// GetCommitStatuses fetches CI statuses for a commit SHA.
func (c *Client) GetCommitStatuses(owner, repo, sha string) ([]CommitStatus, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/commits/%s/statuses", c.BaseURL, owner, repo, sha)
body, err := c.doGet(url)
func (c *Client) GetCommitStatuses(ctx context.Context, owner, repo, sha string) ([]CommitStatus, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/commits/%s/statuses", c.baseURL, owner, repo, sha)
body, err := c.doGet(ctx, url)
if err != nil {
return nil, fmt.Errorf("fetch commit statuses: %w", err)
}
@@ -101,9 +103,9 @@ func (c *Client) GetCommitStatuses(owner, repo, sha string) ([]CommitStatus, err
}
// GetFileContent fetches a file from the default branch of a repo.
func (c *Client) GetFileContent(owner, repo, filepath string) (string, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/raw/%s", c.BaseURL, owner, repo, filepath)
body, err := c.doGet(url)
func (c *Client) GetFileContent(ctx context.Context, owner, repo, filepath string) (string, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/raw/%s", c.baseURL, owner, repo, filepath)
body, err := c.doGet(ctx, url)
if err != nil {
return "", fmt.Errorf("fetch file %s: %w", filepath, err)
}
@@ -111,9 +113,9 @@ func (c *Client) GetFileContent(owner, repo, filepath string) (string, error) {
}
// GetFileContentRef fetches a file from a specific ref (branch/tag/sha) in a repo.
func (c *Client) GetFileContentRef(owner, repo, filepath, ref string) (string, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/raw/%s?ref=%s", c.BaseURL, owner, repo, filepath, ref)
body, err := c.doGet(url)
func (c *Client) GetFileContentRef(ctx context.Context, owner, repo, filepath, ref string) (string, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/raw/%s?ref=%s", c.baseURL, owner, repo, filepath, ref)
body, err := c.doGet(ctx, url)
if err != nil {
return "", fmt.Errorf("fetch file %s@%s: %w", filepath, ref, err)
}
@@ -122,8 +124,8 @@ func (c *Client) GetFileContentRef(owner, repo, filepath, ref string) (string, e
// PostReview submits a review to a PR.
// event should be "APPROVED" or "REQUEST_CHANGES".
func (c *Client) PostReview(owner, repo string, number int, event, body string) error {
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/pulls/%d/reviews", c.BaseURL, owner, repo, number)
func (c *Client) PostReview(ctx context.Context, owner, repo string, number int, event, body string) error {
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/pulls/%d/reviews", c.baseURL, owner, repo, number)
payload := struct {
Body string `json:"body"`
@@ -138,14 +140,14 @@ func (c *Client) PostReview(owner, repo string, number int, event, body string)
return fmt.Errorf("marshal review payload: %w", err)
}
req, err := http.NewRequest("POST", url, strings.NewReader(string(data)))
req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewReader(data))
if err != nil {
return fmt.Errorf("create review request: %w", err)
}
req.Header.Set("Authorization", "token "+c.Token)
req.Header.Set("Authorization", "token "+c.token)
req.Header.Set("Content-Type", "application/json")
resp, err := c.HTTP.Do(req)
resp, err := c.http.Do(req)
if err != nil {
return fmt.Errorf("post review: %w", err)
}
@@ -158,14 +160,14 @@ func (c *Client) PostReview(owner, repo string, number int, event, body string)
return nil
}
func (c *Client) doGet(url string) ([]byte, error) {
req, err := http.NewRequest("GET", url, nil)
func (c *Client) doGet(ctx context.Context, url string) ([]byte, error) {
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "token "+c.Token)
req.Header.Set("Authorization", "token "+c.token)
resp, err := c.HTTP.Do(req)
resp, err := c.http.Do(req)
if err != nil {
return nil, err
}
@@ -186,9 +188,9 @@ type ContentEntry struct {
}
// ListContents lists files and directories at a given path in a repo.
func (c *Client) ListContents(owner, repo, path string) ([]ContentEntry, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/contents/%s", c.BaseURL, owner, repo, path)
body, err := c.doGet(url)
func (c *Client) ListContents(ctx context.Context, owner, repo, path string) ([]ContentEntry, error) {
url := fmt.Sprintf("%s/api/v1/repos/%s/%s/contents/%s", c.baseURL, owner, repo, path)
body, err := c.doGet(ctx, url)
if err != nil {
return nil, fmt.Errorf("list contents %s: %w", path, err)
}
@@ -202,14 +204,14 @@ func (c *Client) ListContents(owner, repo, path string) ([]ContentEntry, error)
// GetAllFilesInPath recursively fetches all file contents under a path.
// If the path is a file, returns just that file's content.
// If the path is a directory, recursively fetches all files within it.
func (c *Client) GetAllFilesInPath(owner, repo, path string) (map[string]string, error) {
func (c *Client) GetAllFilesInPath(ctx context.Context, owner, repo, path string) (map[string]string, error) {
results := make(map[string]string)
// Try listing as directory first
entries, err := c.ListContents(owner, repo, path)
entries, err := c.ListContents(ctx, owner, repo, path)
if err != nil {
// Might be a file, try fetching directly
content, fileErr := c.GetFileContent(owner, repo, path)
content, fileErr := c.GetFileContent(ctx, owner, repo, path)
if fileErr != nil {
return nil, fmt.Errorf("path %q is neither a file nor directory: %w", path, err)
}
@@ -220,13 +222,13 @@ func (c *Client) GetAllFilesInPath(owner, repo, path string) (map[string]string,
for _, entry := range entries {
switch entry.Type {
case "file":
content, err := c.GetFileContent(owner, repo, entry.Path)
content, err := c.GetFileContent(ctx, owner, repo, entry.Path)
if err != nil {
continue // Skip files we can't read
}
results[entry.Path] = content
case "dir":
subResults, err := c.GetAllFilesInPath(owner, repo, entry.Path)
subResults, err := c.GetAllFilesInPath(ctx, owner, repo, entry.Path)
if err != nil {
continue
}