Skip to content
Closed
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
26 changes: 26 additions & 0 deletions internal/wrappers/github-http.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
"fmt"
"io"
"net/http"
"strconv"
"strings"
"time"

"github.com/checkmarx/ast-cli/internal/logger"
"github.com/checkmarx/ast-cli/internal/params"
Expand Down Expand Up @@ -248,6 +250,10 @@
if err != nil {
return nil, err
}
resp, err = handleRateLimit(resp, client, req, url, token, tokenFormat, queryParams)
if err != nil {
return nil, err
}
defer func() {
if err == nil {
_ = resp.Body.Close()
Expand Down Expand Up @@ -276,3 +282,23 @@
}
return resp, nil
}

func handleRateLimit(resp *http.Response, client *http.Client, req *http.Request, url, token, authFormat string, queryParams map[string]string) (*http.Response, error) {

Check failure on line 286 in internal/wrappers/github-http.go

View workflow job for this annotation

GitHub Actions / lint

`handleRateLimit` - `authFormat` is unused (unparam)

Check failure on line 286 in internal/wrappers/github-http.go

View workflow job for this annotation

GitHub Actions / lint

`handleRateLimit` - `authFormat` is unused (unparam)
if resp.StatusCode == http.StatusForbidden {
remaining := resp.Header.Get("X-RateLimit-Remaining")
reset := resp.Header.Get("X-RateLimit-Reset")
if remaining == "0" && reset != "" {
resetUnix, err := strconv.ParseInt(reset, 10, 64)
if err == nil {
waitDuration := time.Until(time.Unix(resetUnix, 0))
if waitDuration > 0 {
time.Sleep(waitDuration)
return GetWithQueryParamsAndCustomRequest(client, req, url, token, tokenFormat, queryParams) // Indicate to retry
}
} else {
return resp, err
}
}
}
return resp, nil //Not rate limited

Check failure on line 303 in internal/wrappers/github-http.go

View workflow job for this annotation

GitHub Actions / lint

commentFormatting: put a space between `//` and comment text (gocritic)

Check failure on line 303 in internal/wrappers/github-http.go

View workflow job for this annotation

GitHub Actions / lint

commentFormatting: put a space between `//` and comment text (gocritic)
}
Loading