Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add error capture for GitHub rate limiting #288

Merged
merged 2 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions pkg/apimsg/apimsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const (
)

var (
ErrAsset = errors.New("searched asset not found")
ErrReturn = errors.New("unexpected value returned by API")
ErrAsset = errors.New("searched asset not found")
ErrReturn = errors.New("unexpected value returned by API")
ErrRateLimit = errors.New("you are rate-limited by GitHub. Consider using a token by setting the TENV_GITHUB_TOKEN env variable to increase the rate limit")
)
45 changes: 43 additions & 2 deletions pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ package github

import (
"context"
"encoding/json"
"errors"
"net/http"
"net/url"
"strconv"

"github.com/tofuutils/tenv/v3/pkg/apimsg"
"github.com/tofuutils/tenv/v3/pkg/download"
versionfinder "github.com/tofuutils/tenv/v3/versionmanager/semantic/finder"
)

Expand Down Expand Up @@ -113,13 +113,54 @@ func ListReleases(ctx context.Context, githubReleaseURL string, githubToken stri
}

func apiGetRequest(ctx context.Context, callURL string, authorizationHeader string) (any, error) {
return download.JSON(ctx, callURL, download.NoDisplay, func(request *http.Request) {
resp, err := downloadWithHeaders(ctx, callURL, func(request *http.Request) {
request.Header.Set("Accept", "application/vnd.github+json")
if authorizationHeader != "" {
request.Header.Set("Authorization", authorizationHeader)
}
request.Header.Set("X-GitHub-Api-Version", "2022-11-28") //nolint
})
if err != nil {
return nil, err
}
defer resp.Body.Close()

if err := checkRateLimit(resp); err != nil {
return nil, err
}

var result any
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, apimsg.ErrReturn
}

return result, nil
}

func checkRateLimit(resp *http.Response) error {
rateLimitRemaining := resp.Header.Get("X-Ratelimit-Remaining")
if rateLimitRemaining == "0" {
return apimsg.ErrRateLimit
}

return nil
}

func downloadWithHeaders(ctx context.Context, url string, modifyRequest func(*http.Request)) (*http.Response, error) {
client := &http.Client{}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return nil, err
}
if modifyRequest != nil {
modifyRequest(req)
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}

return resp, nil
}

func buildAuthorizationHeader(token string) string {
Expand Down