Skip to content

Commit

Permalink
Update factory to use new functional constructor for buildkite client
Browse files Browse the repository at this point in the history
  • Loading branch information
moskyb committed Aug 18, 2024
1 parent dda3ba6 commit 74530db
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
6 changes: 5 additions & 1 deletion cmd/bk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ func main() {

func mainRun() int {
ctx := context.Background()
f := factory.New(version.Version)
f, err := factory.New(version.Version)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to create factory: %s\n", err)
return 1
}

rootCmd, err := root.NewCmdRoot(f)
if err != nil {
Expand Down
32 changes: 20 additions & 12 deletions pkg/cmd/factory/factory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package factory

import (
"fmt"
"net/http"

"github.com/Khan/genqlient/graphql"
Expand All @@ -20,26 +21,33 @@ type Factory struct {
Version string
}

func New(version string) *Factory {
type gqlHTTPClient struct {
client *http.Client
token string
}

func (a *gqlHTTPClient) Do(req *http.Request) (*http.Response, error) {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", a.token))
return a.client.Do(req)
}

func New(version string) (*Factory, error) {
repo, _ := git.PlainOpenWithOptions(".", &git.PlainOpenOptions{DetectDotGit: true, EnableDotGitCommonDir: true})
conf := config.New(nil, repo)
// we use separate clients for now until we upgrade the go-buildkite package to the 3.11+
tk, err := buildkite.NewTokenConfig(conf.APIToken(), false)
gtk, _ := buildkite.NewTokenConfig(conf.APIToken(), false)
var httpClient, graphqlClient *http.Client
if err == nil {
// separate clients until we upgrade go-buildkite library
httpClient = tk.Client()
buildkiteClient, err := buildkite.NewOpts(buildkite.WithTokenAuth(conf.APIToken()))
if err != nil {
return nil, fmt.Errorf("creating buildkite client: %w", err)
}
graphqlClient = &http.Client{Transport: gtk}

graphqlHTTPClient := &gqlHTTPClient{client: http.DefaultClient, token: conf.APIToken()}

return &Factory{
Config: conf,
GitRepository: repo,
GraphQLClient: graphql.NewClient(config.DefaultGraphQLEndpoint, graphqlClient),
HttpClient: httpClient,
GraphQLClient: graphql.NewClient(config.DefaultGraphQLEndpoint, graphqlHTTPClient),
OpenAIClient: openai.NewClient(conf.GetOpenAIToken()),
RestAPIClient: buildkite.NewClient(httpClient),
RestAPIClient: buildkiteClient,
Version: version,
}
}, nil
}

0 comments on commit 74530db

Please sign in to comment.