From 724c03cf3ae2b0556fbe567559458878742d71e8 Mon Sep 17 00:00:00 2001 From: Ben Iofel Date: Mon, 6 Jun 2022 22:05:20 -0400 Subject: [PATCH] Simplify client creation --- lib/githubPRStatus.go | 5 +++-- lib/provider.go | 13 ++----------- merge/merge.go | 2 +- push/push.go | 2 +- sync/syncGithub.go | 2 +- 5 files changed, 8 insertions(+), 16 deletions(-) diff --git a/lib/githubPRStatus.go b/lib/githubPRStatus.go index db1eb58..60a9810 100644 --- a/lib/githubPRStatus.go +++ b/lib/githubPRStatus.go @@ -6,6 +6,7 @@ import ( "strings" "time" + "github.com/google/go-github/v35/github" "github.com/hasura/go-graphql-client" ) @@ -22,9 +23,9 @@ type GithubPRStatus struct { Statuses []GithubStatusContext } -func GetGithubPRStatus(ctx context.Context, repoLimiter *time.Ticker, repo Repo, prNumber int) (GithubPRStatus, error) { +func GetGithubPRStatus(ctx context.Context, client *github.Client, repoLimiter *time.Ticker, repo Repo, prNumber int) (GithubPRStatus, error) { p := NewProviderFromConfig(repo.ProviderConfig) - graphqlClient, err := p.GithubGraphqlClient(ctx) + graphqlClient, err := p.GithubGraphqlClient(ctx, client) if err != nil { return GithubPRStatus{}, err } diff --git a/lib/provider.go b/lib/provider.go index 299fbde..4dd961b 100644 --- a/lib/provider.go +++ b/lib/provider.go @@ -53,22 +53,13 @@ func (p *Provider) GithubClient(ctx context.Context) (*github.Client, error) { return client, nil } -func (p *Provider) GithubGraphqlClient(ctx context.Context) (*graphql.Client, error) { - // validation - if p.Backend != "github" { - return nil, fmt.Errorf("cannot initialize GithubGraphqlClient: backend is not 'github', but instead is '%s'", p.Backend) - } +func (p *Provider) GithubGraphqlClient(ctx context.Context, rest *github.Client) (*graphql.Client, error) { token := os.Getenv("GITHUB_API_TOKEN") if token == "" { return nil, fmt.Errorf("cannot initialize GithubGraphqlClient: GITHUB_API_TOKEN is not set") } - url := "https://api.github.com/graphql" - if p.IsEnterprise() { - url = p.BackendURL - } - - client := graphql.NewClient(url, nil). + client := graphql.NewClient(rest.BaseURL.String()+"graphql", nil). WithRequestModifier(func(req *http.Request) { req.Header.Add("Authorization", "Bearer "+token) }) diff --git a/merge/merge.go b/merge/merge.go index 5ca9049..07aed9f 100644 --- a/merge/merge.go +++ b/merge/merge.go @@ -69,7 +69,7 @@ func GitHubMerge(ctx context.Context, input Input, repoLimiter *time.Ticker, mer } // (2) Check commit status - status, err := lib.GetGithubPRStatus(ctx, repoLimiter, input.Repo, input.PRNumber) + status, err := lib.GetGithubPRStatus(ctx, client, repoLimiter, input.Repo, input.PRNumber) if err != nil { return Output{Success: false}, err } diff --git a/push/push.go b/push/push.go index 874fb6d..f71ba84 100644 --- a/push/push.go +++ b/push/push.go @@ -138,7 +138,7 @@ func GithubPush(ctx context.Context, input Input, repoLimiter *time.Ticker, push } } - cs, err := lib.GetGithubPRStatus(ctx, repoLimiter, input.Repo, *pr.Number) + cs, err := lib.GetGithubPRStatus(ctx, client, repoLimiter, input.Repo, *pr.Number) if err != nil { return Output{Success: false}, err } diff --git a/sync/syncGithub.go b/sync/syncGithub.go index f97b830..b30f6b8 100644 --- a/sync/syncGithub.go +++ b/sync/syncGithub.go @@ -28,7 +28,7 @@ func GithubSyncPush(ctx context.Context, r lib.Repo, po push.Output, repoLimiter return Output{}, err } - cs, err := lib.GetGithubPRStatus(ctx, repoLimiter, r, po.PullRequestNumber) + cs, err := lib.GetGithubPRStatus(ctx, client, repoLimiter, r, po.PullRequestNumber) if err != nil { return Output{}, err }