Skip to content

Commit 2ee1653

Browse files
authored
Add retries to GH API clients (#320)
This commit adds a retryable http client to the github API calls, this should make the calls more resilient to temporary network failures. Signed-off-by: Adolfo Garcia Veytia (puerco) <[email protected]>
1 parent 71cfb51 commit 2ee1653

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/go-git/go-git/v6 v6.0.0-20250711134917-1f24ae85fe16
1111
github.com/google/go-github/v69 v69.2.0
1212
github.com/google/uuid v1.6.0
13+
github.com/hashicorp/go-retryablehttp v0.7.8
1314
github.com/in-toto/attestation v1.1.2
1415
github.com/maxbrunsfeld/counterfeiter/v6 v6.12.0
1516
github.com/migueleliasweb/go-github-mock v1.4.0
@@ -99,7 +100,6 @@ require (
99100
github.com/gorilla/mux v1.8.1 // indirect
100101
github.com/grpc-ecosystem/grpc-gateway/v2 v2.27.2 // indirect
101102
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
102-
github.com/hashicorp/go-retryablehttp v0.7.8 // indirect
103103
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
104104
github.com/hashicorp/hcl v1.0.1-vault-7 // indirect
105105
github.com/in-toto/in-toto-golang v0.9.0 // indirect

pkg/auth/authenticator.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/fatih/color"
1616
"github.com/google/go-github/v69/github"
17+
"github.com/hashicorp/go-retryablehttp"
1718

1819
"github.com/slsa-framework/source-tool/pkg/sourcetool/models"
1920
)
@@ -132,7 +133,12 @@ func (a *Authenticator) GetGitHubClient() (*github.Client, error) {
132133
if token == "" {
133134
return nil, errors.New("token is empty")
134135
}
135-
return github.NewClient(nil).WithAuthToken(token), nil
136+
137+
rClient := retryablehttp.NewClient()
138+
rClient.RetryMax = 3
139+
rClient.Logger = nil // Comment this line to monitor GH api calls
140+
httpClient := rClient.StandardClient()
141+
return github.NewClient(httpClient).WithAuthToken(token), nil
136142
}
137143

138144
// WhoAmI returns the user authenticated with the token

pkg/ghcontrol/connection.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010

1111
"github.com/google/go-github/v69/github"
12+
"github.com/hashicorp/go-retryablehttp"
1213
)
1314

1415
const tokenEnvVar = "GITHUB_TOKEN" //nolint:gosec // These are not credentials
@@ -21,7 +22,11 @@ type GitHubConnection struct {
2122
}
2223

2324
func NewGhConnection(owner, repo, ref string) *GitHubConnection {
24-
return NewGhConnectionWithClient(owner, repo, ref, github.NewClient(nil))
25+
opts := defaultOptions
26+
rClient := retryablehttp.NewClient()
27+
rClient.RetryMax = int(opts.ApiRetries)
28+
rClient.Logger = nil
29+
return NewGhConnectionWithClient(owner, repo, ref, github.NewClient(rClient.StandardClient()))
2530
}
2631

2732
func NewGhConnectionWithClient(owner, repo, ref string, client *github.Client) *GitHubConnection {

pkg/ghcontrol/options.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package ghcontrol
55

66
var defaultOptions = Options{
77
AllowMergeCommits: false,
8+
ApiRetries: 3,
89
}
910

1011
type Options struct {
@@ -14,4 +15,7 @@ type Options struct {
1415

1516
// accessToken is the token we will use to connect to the GitHub API
1617
accessToken string
18+
19+
// ApiRetries controls the number of time we retry calls to the GitHub API
20+
ApiRetries uint8
1721
}

0 commit comments

Comments
 (0)