Skip to content

Commit

Permalink
pendingRecheckTime in AuthenticateWith functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pznamensky authored and iainlane committed Aug 30, 2024
1 parent 1a936d6 commit b9a3638
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cmd/wait-for-github/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func root() *cli.App {
Name: "pending-recheck-time",
Usage: "Time after which to recheck the pending status on GitHub.",
EnvVars: []string{"PENDING_RECHECK_TIME"},
Value: 5 * time.Second, // default value
Value: 5 * time.Second,
},
&cli.DurationFlag{
Name: "timeout",
Expand Down
27 changes: 8 additions & 19 deletions internal/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,16 @@ type GHClient struct {
}

func NewGithubClient(ctx context.Context, authInfo AuthInfo, pendingRecheckTime time.Duration) (GHClient, error) {
var (
githubClient GHClient
err error
)

// If a GitHub token is provided, use it to authenticate in preference to
// App authentication
if authInfo.GithubToken != "" {
log.Debug("Using GitHub token for authentication")
githubClient = AuthenticateWithToken(ctx, authInfo.GithubToken)
} else {
// Otherwise, use the App authentication flow
log.Debug("Using GitHub App for authentication")
githubClient, err = AuthenticateWithApp(ctx, authInfo.PrivateKey, authInfo.AppID, authInfo.InstallationID)
if err != nil {
return GHClient{}, err
}
return AuthenticateWithToken(ctx, authInfo.GithubToken, pendingRecheckTime), nil
}

githubClient.pendingRecheckTime = pendingRecheckTime
return githubClient, nil
// Otherwise, use the App authentication flow
log.Debug("Using GitHub App for authentication")
return AuthenticateWithApp(ctx, authInfo.PrivateKey, authInfo.AppID, authInfo.InstallationID, pendingRecheckTime)
}

func cachingRetryableTransport() http.RoundTripper {
Expand All @@ -117,27 +106,27 @@ func cachingRetryableTransport() http.RoundTripper {
}

// AuthenticateWithToken authenticates with a GitHub token
func AuthenticateWithToken(ctx context.Context, token string) GHClient {
func AuthenticateWithToken(ctx context.Context, token string, pendingRecheckTime time.Duration) GHClient {
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
ctx = context.WithValue(ctx, oauth2.HTTPClient, &http.Client{Transport: cachingRetryableTransport()})
httpClient := oauth2.NewClient(ctx, src)
githubClient := github.NewClient(httpClient)

return GHClient{client: githubClient}
return GHClient{client: githubClient, pendingRecheckTime: pendingRecheckTime}
}

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

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

return GHClient{client: githubClient}, nil
return GHClient{client: githubClient, pendingRecheckTime: pendingRecheckTime }, nil
}

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

0 comments on commit b9a3638

Please sign in to comment.