Skip to content

Commit

Permalink
Simplify client creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Iofel committed Jun 7, 2022
1 parent d57eeea commit 724c03c
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 16 deletions.
5 changes: 3 additions & 2 deletions lib/githubPRStatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"time"

"github.com/google/go-github/v35/github"
"github.com/hasura/go-graphql-client"
)

Expand All @@ -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
}
Expand Down
13 changes: 2 additions & 11 deletions lib/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
2 changes: 1 addition & 1 deletion merge/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion push/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion sync/syncGithub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 724c03c

Please sign in to comment.