Skip to content

Commit

Permalink
Add user agent for http requests (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
jradtilbrook authored Sep 10, 2024
1 parent d57bc94 commit de618c9
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pkg/cmd/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import (

"github.com/Khan/genqlient/graphql"
"github.com/buildkite/cli/v3/internal/config"
"github.com/buildkite/cli/v3/internal/version"
"github.com/buildkite/go-buildkite/v3/buildkite"
"github.com/go-git/go-git/v5"
"github.com/sashabaranov/go-openai"
)

var userAgent string

type Factory struct {
Config *config.Config
GitRepository *git.Repository
Expand All @@ -25,15 +28,23 @@ type gqlHTTPClient struct {
token string
}

func init() {
userAgent = fmt.Sprintf("%s buildkite-cli/%s", buildkite.DefaultUserAgent, version.Version)
}

func (a *gqlHTTPClient) Do(req *http.Request) (*http.Response, error) {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", a.token))
req.Header.Set("User-Agent", userAgent)
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)
buildkiteClient, err := buildkite.NewOpts(buildkite.WithTokenAuth(conf.APIToken()))
buildkiteClient, err := buildkite.NewOpts(
buildkite.WithTokenAuth(conf.APIToken()),
buildkite.WithUserAgent(userAgent),
)
if err != nil {
return nil, fmt.Errorf("creating buildkite client: %w", err)
}
Expand Down

0 comments on commit de618c9

Please sign in to comment.