Skip to content

Commit b9a3638

Browse files
pznamenskyiainlane
authored andcommitted
pendingRecheckTime in AuthenticateWith functions
1 parent 1a936d6 commit b9a3638

File tree

2 files changed

+9
-20
lines changed

2 files changed

+9
-20
lines changed

cmd/wait-for-github/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func root() *cli.App {
9595
Name: "pending-recheck-time",
9696
Usage: "Time after which to recheck the pending status on GitHub.",
9797
EnvVars: []string{"PENDING_RECHECK_TIME"},
98-
Value: 5 * time.Second, // default value
98+
Value: 5 * time.Second,
9999
},
100100
&cli.DurationFlag{
101101
Name: "timeout",

internal/github/github.go

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -83,27 +83,16 @@ type GHClient struct {
8383
}
8484

8585
func NewGithubClient(ctx context.Context, authInfo AuthInfo, pendingRecheckTime time.Duration) (GHClient, error) {
86-
var (
87-
githubClient GHClient
88-
err error
89-
)
90-
9186
// If a GitHub token is provided, use it to authenticate in preference to
9287
// App authentication
9388
if authInfo.GithubToken != "" {
9489
log.Debug("Using GitHub token for authentication")
95-
githubClient = AuthenticateWithToken(ctx, authInfo.GithubToken)
96-
} else {
97-
// Otherwise, use the App authentication flow
98-
log.Debug("Using GitHub App for authentication")
99-
githubClient, err = AuthenticateWithApp(ctx, authInfo.PrivateKey, authInfo.AppID, authInfo.InstallationID)
100-
if err != nil {
101-
return GHClient{}, err
102-
}
90+
return AuthenticateWithToken(ctx, authInfo.GithubToken, pendingRecheckTime), nil
10391
}
10492

105-
githubClient.pendingRecheckTime = pendingRecheckTime
106-
return githubClient, nil
93+
// Otherwise, use the App authentication flow
94+
log.Debug("Using GitHub App for authentication")
95+
return AuthenticateWithApp(ctx, authInfo.PrivateKey, authInfo.AppID, authInfo.InstallationID, pendingRecheckTime)
10796
}
10897

10998
func cachingRetryableTransport() http.RoundTripper {
@@ -117,27 +106,27 @@ func cachingRetryableTransport() http.RoundTripper {
117106
}
118107

119108
// AuthenticateWithToken authenticates with a GitHub token
120-
func AuthenticateWithToken(ctx context.Context, token string) GHClient {
109+
func AuthenticateWithToken(ctx context.Context, token string, pendingRecheckTime time.Duration) GHClient {
121110
src := oauth2.StaticTokenSource(
122111
&oauth2.Token{AccessToken: token},
123112
)
124113
ctx = context.WithValue(ctx, oauth2.HTTPClient, &http.Client{Transport: cachingRetryableTransport()})
125114
httpClient := oauth2.NewClient(ctx, src)
126115
githubClient := github.NewClient(httpClient)
127116

128-
return GHClient{client: githubClient}
117+
return GHClient{client: githubClient, pendingRecheckTime: pendingRecheckTime}
129118
}
130119

131120
// AuthenticateWithApp authenticates with a GitHub App
132-
func AuthenticateWithApp(ctx context.Context, privateKey []byte, appID, installationID int64) (GHClient, error) {
121+
func AuthenticateWithApp(ctx context.Context, privateKey []byte, appID, installationID int64, pendingRecheckTime time.Duration) (GHClient, error) {
133122
itr, err := ghinstallation.New(cachingRetryableTransport(), appID, installationID, privateKey)
134123
if err != nil {
135124
return GHClient{}, fmt.Errorf("failed to create transport: %w", err)
136125
}
137126

138127
githubClient := github.NewClient(&http.Client{Transport: itr})
139128

140-
return GHClient{client: githubClient}, nil
129+
return GHClient{client: githubClient, pendingRecheckTime: pendingRecheckTime }, nil
141130
}
142131

143132
func (c GHClient) IsPRMergedOrClosed(ctx context.Context, owner, repo string, prNumber int) (string, bool, int64, error) {

0 commit comments

Comments
 (0)