Skip to content

Commit

Permalink
Merge pull request #326 from cpanato/add-custom
Browse files Browse the repository at this point in the history
add NewWithTokenWithClient
  • Loading branch information
k8s-ci-robot committed Mar 26, 2024
2 parents 3515ccc + d6594be commit cb59713
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const (
TokenEnvKey = "GITHUB_TOKEN"
// GitHubURL Prefix for github URLs
GitHubURL = "https://github.com/"

unauthenticated = "unauthenticated"
)

// GitHub is a wrapper around GitHub related functionality
Expand Down Expand Up @@ -201,7 +203,29 @@ func New() *GitHub {
func NewWithToken(token string) (*GitHub, error) {
ctx := context.Background()
client := http.DefaultClient
state := "unauthenticated"
state := unauthenticated
if token != "" {
state = strings.TrimPrefix(state, "un")
client = oauth2.NewClient(ctx, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
))
}

logrus.Debugf("Using %s GitHub client", state)
return &GitHub{
client: &githubClient{github.NewClient(client)},
options: DefaultOptions(),
}, nil
}

// NewWithTokenWithClient can be used to specify a GitHub token through parameters and
// set an custom HTTP Client.
// Empty string will result in unauthenticated client, which makes
// unauthenticated requests.
func NewWithTokenWithClient(token string, httpClient *http.Client) (*GitHub, error) {
ctx := context.Background()
client := httpClient
state := unauthenticated
if token != "" {
state = strings.TrimPrefix(state, "un")
client = oauth2.NewClient(ctx, oauth2.StaticTokenSource(
Expand All @@ -224,7 +248,7 @@ func NewEnterprise(baseURL, uploadURL string) (*GitHub, error) {
func NewEnterpriseWithToken(baseURL, uploadURL, token string) (*GitHub, error) {
ctx := context.Background()
client := http.DefaultClient
state := "unauthenticated"
state := unauthenticated
if token != "" {
state = strings.TrimPrefix(state, "un")
client = oauth2.NewClient(ctx, oauth2.StaticTokenSource(
Expand Down

0 comments on commit cb59713

Please sign in to comment.