Skip to content

Commit 724c03c

Browse files
author
Ben Iofel
committed
Simplify client creation
1 parent d57eeea commit 724c03c

File tree

5 files changed

+8
-16
lines changed

5 files changed

+8
-16
lines changed

lib/githubPRStatus.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77
"time"
88

9+
"github.com/google/go-github/v35/github"
910
"github.com/hasura/go-graphql-client"
1011
)
1112

@@ -22,9 +23,9 @@ type GithubPRStatus struct {
2223
Statuses []GithubStatusContext
2324
}
2425

25-
func GetGithubPRStatus(ctx context.Context, repoLimiter *time.Ticker, repo Repo, prNumber int) (GithubPRStatus, error) {
26+
func GetGithubPRStatus(ctx context.Context, client *github.Client, repoLimiter *time.Ticker, repo Repo, prNumber int) (GithubPRStatus, error) {
2627
p := NewProviderFromConfig(repo.ProviderConfig)
27-
graphqlClient, err := p.GithubGraphqlClient(ctx)
28+
graphqlClient, err := p.GithubGraphqlClient(ctx, client)
2829
if err != nil {
2930
return GithubPRStatus{}, err
3031
}

lib/provider.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,13 @@ func (p *Provider) GithubClient(ctx context.Context) (*github.Client, error) {
5353
return client, nil
5454
}
5555

56-
func (p *Provider) GithubGraphqlClient(ctx context.Context) (*graphql.Client, error) {
57-
// validation
58-
if p.Backend != "github" {
59-
return nil, fmt.Errorf("cannot initialize GithubGraphqlClient: backend is not 'github', but instead is '%s'", p.Backend)
60-
}
56+
func (p *Provider) GithubGraphqlClient(ctx context.Context, rest *github.Client) (*graphql.Client, error) {
6157
token := os.Getenv("GITHUB_API_TOKEN")
6258
if token == "" {
6359
return nil, fmt.Errorf("cannot initialize GithubGraphqlClient: GITHUB_API_TOKEN is not set")
6460
}
6561

66-
url := "https://api.github.com/graphql"
67-
if p.IsEnterprise() {
68-
url = p.BackendURL
69-
}
70-
71-
client := graphql.NewClient(url, nil).
62+
client := graphql.NewClient(rest.BaseURL.String()+"graphql", nil).
7263
WithRequestModifier(func(req *http.Request) {
7364
req.Header.Add("Authorization", "Bearer "+token)
7465
})

merge/merge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func GitHubMerge(ctx context.Context, input Input, repoLimiter *time.Ticker, mer
6969
}
7070

7171
// (2) Check commit status
72-
status, err := lib.GetGithubPRStatus(ctx, repoLimiter, input.Repo, input.PRNumber)
72+
status, err := lib.GetGithubPRStatus(ctx, client, repoLimiter, input.Repo, input.PRNumber)
7373
if err != nil {
7474
return Output{Success: false}, err
7575
}

push/push.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func GithubPush(ctx context.Context, input Input, repoLimiter *time.Ticker, push
138138
}
139139
}
140140

141-
cs, err := lib.GetGithubPRStatus(ctx, repoLimiter, input.Repo, *pr.Number)
141+
cs, err := lib.GetGithubPRStatus(ctx, client, repoLimiter, input.Repo, *pr.Number)
142142
if err != nil {
143143
return Output{Success: false}, err
144144
}

sync/syncGithub.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func GithubSyncPush(ctx context.Context, r lib.Repo, po push.Output, repoLimiter
2828
return Output{}, err
2929
}
3030

31-
cs, err := lib.GetGithubPRStatus(ctx, repoLimiter, r, po.PullRequestNumber)
31+
cs, err := lib.GetGithubPRStatus(ctx, client, repoLimiter, r, po.PullRequestNumber)
3232
if err != nil {
3333
return Output{}, err
3434
}

0 commit comments

Comments
 (0)