diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-11 18:28:41 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-11 18:28:41 +0200 |
| commit | 9706e53620045c20bf732054a286183c70f77581 (patch) | |
| tree | 166e822e9d41b0cc980d4481c5c175ec7164a107 /internal/github/github.go | |
| parent | 1c12325c64bb734dd0ae95a0c803de1ff45d2b4c (diff) | |
fix(api): add timed shared HTTP client
Diffstat (limited to 'internal/github/github.go')
| -rw-r--r-- | internal/github/github.go | 77 |
1 files changed, 47 insertions, 30 deletions
diff --git a/internal/github/github.go b/internal/github/github.go index c886e43..2667658 100644 --- a/internal/github/github.go +++ b/internal/github/github.go @@ -9,6 +9,8 @@ import ( "os" "path/filepath" "strings" + + "codeberg.org/snonux/gitsyncer/internal/httpclient" ) // Client handles GitHub API operations @@ -85,15 +87,16 @@ func (c *Client) RepoExists(repoName string) (bool, error) { url := fmt.Sprintf("https://api.github.com/repos/%s/%s", c.org, repoName) fmt.Printf(" Checking URL: %s\n", url) - req, err := http.NewRequest("GET", url, nil) + req, cancel, err := httpclient.NewRequest(http.MethodGet, url, nil) if err != nil { return false, err } + defer cancel() req.Header.Set("Authorization", "Bearer "+c.token) req.Header.Set("Accept", "application/vnd.github.v3+json") - resp, err := http.DefaultClient.Do(req) + resp, err := httpclient.Do(req) if err != nil { return false, err } @@ -145,16 +148,17 @@ func (c *Client) CreateRepo(repoName, description string, private bool) error { return err } - req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody)) + req, cancel, err := httpclient.NewRequest(http.MethodPost, url, bytes.NewBuffer(jsonBody)) if err != nil { return err } + defer cancel() req.Header.Set("Authorization", "Bearer "+c.token) req.Header.Set("Accept", "application/vnd.github.v3+json") req.Header.Set("Content-Type", "application/json") - resp, err := http.DefaultClient.Do(req) + resp, err := httpclient.Do(req) if err != nil { return err } @@ -196,14 +200,15 @@ func (c *Client) GetRepo(repoName string) (Repository, bool, error) { } url := fmt.Sprintf("https://api.github.com/repos/%s/%s", c.org, repoName) - req, err := http.NewRequest("GET", url, nil) + req, cancel, err := httpclient.NewRequest(http.MethodGet, url, nil) if err != nil { return repo, false, err } + defer cancel() req.Header.Set("Authorization", "Bearer "+c.token) req.Header.Set("Accept", "application/vnd.github.v3+json") - resp, err := http.DefaultClient.Do(req) + resp, err := httpclient.Do(req) if err != nil { return repo, false, err } @@ -238,15 +243,16 @@ func (c *Client) UpdateRepoDescription(repoName, description string) error { return err } - req, err := http.NewRequest("PATCH", url, bytes.NewBuffer(body)) + req, cancel, err := httpclient.NewRequest(http.MethodPatch, url, bytes.NewBuffer(body)) if err != nil { return err } + defer cancel() req.Header.Set("Authorization", "Bearer "+c.token) req.Header.Set("Accept", "application/vnd.github.v3+json") req.Header.Set("Content-Type", "application/json") - resp, err := http.DefaultClient.Do(req) + resp, err := httpclient.Do(req) if err != nil { return err } @@ -284,30 +290,11 @@ func (c *Client) ListPublicRepos() ([]Repository, error) { url := fmt.Sprintf("https://api.github.com/users/%s/repos?page=%d&per_page=%d&type=owner", c.org, page, perPage) fmt.Printf(" Fetching page %d...\n", page) - req, err := http.NewRequest("GET", url, nil) + repos, err := c.listPublicReposPage(url) if err != nil { return nil, err } - req.Header.Set("Authorization", "Bearer "+c.token) - req.Header.Set("Accept", "application/vnd.github.v3+json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - if resp.StatusCode != 200 { - body, _ := io.ReadAll(resp.Body) - return nil, fmt.Errorf("failed to list repos: status %d: %s", resp.StatusCode, string(body)) - } - - var repos []Repository - if err := json.NewDecoder(resp.Body).Decode(&repos); err != nil { - return nil, fmt.Errorf("failed to decode response: %w", err) - } - // Filter for public, non-fork, non-archived for _, repo := range repos { if !repo.Private && !repo.Fork && !repo.Archived && !repo.Disabled { @@ -325,6 +312,35 @@ func (c *Client) ListPublicRepos() ([]Repository, error) { return allRepos, nil } +func (c *Client) listPublicReposPage(url string) ([]Repository, error) { + req, cancel, err := httpclient.NewRequest(http.MethodGet, url, nil) + if err != nil { + return nil, err + } + defer cancel() + + req.Header.Set("Authorization", "Bearer "+c.token) + req.Header.Set("Accept", "application/vnd.github.v3+json") + + resp, err := httpclient.Do(req) + if err != nil { + return nil, err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(resp.Body) + return nil, fmt.Errorf("failed to list repos: status %d: %s", resp.StatusCode, string(body)) + } + + var repos []Repository + if err := json.NewDecoder(resp.Body).Decode(&repos); err != nil { + return nil, fmt.Errorf("failed to decode response: %w", err) + } + + return repos, nil +} + // GetRepoNames extracts repository names from a list of repos func GetRepoNames(repos []Repository) []string { names := make([]string, len(repos)) @@ -352,15 +368,16 @@ func (c *Client) DeleteRepo(repoName string) error { url := fmt.Sprintf("https://api.github.com/repos/%s/%s", c.org, repoName) - req, err := http.NewRequest("DELETE", url, nil) + req, cancel, err := httpclient.NewRequest(http.MethodDelete, url, nil) if err != nil { return err } + defer cancel() req.Header.Set("Authorization", "Bearer "+c.token) req.Header.Set("Accept", "application/vnd.github.v3+json") - resp, err := http.DefaultClient.Do(req) + resp, err := httpclient.Do(req) if err != nil { return err } |
