Skip to content

Commit

Permalink
use new method for auth token
Browse files Browse the repository at this point in the history
  • Loading branch information
aburan28 committed Dec 22, 2024
1 parent ba01ae8 commit 9ae6758
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions applicationset/services/scm_provider/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/google/go-github/v63/github"
"github.com/gregjones/httpcache"
"golang.org/x/oauth2"
)

type contextKey struct{}
Expand All @@ -28,34 +27,31 @@ type GithubProvider struct {
var _ SCMProviderService = &GithubProvider{}

func NewGithubProvider(ctx context.Context, organization string, token string, url string, allBranches bool, cacheEnabled bool) (*GithubProvider, error) {
var ts oauth2.TokenSource
// Undocumented environment variable to set a default token, to be used in testing to dodge anonymous rate limits.
var client *github.Client

if token == "" {
token = os.Getenv("GITHUB_TOKEN")
}
if token != "" {
ts = oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
if cache, ok := ctx.Value(cacheContextKey).(httpcache.Cache); ok {
ctx = context.WithValue(ctx, oauth2.HTTPClient, &http.Client{
Transport: &httpcache.Transport{
Cache: cache,
},
})
}

oauth2.NewClient(ctx, ts)
httpClient := &http.Client{}
if cacheEnabled {
cache := httpcache.NewMemoryCache()

httpClient = &http.Client{
Transport: &httpcache.Transport{
Cache: cache,
},
}
} else {
httpClient = &http.Client{}
}

if cacheEnabled {
if token != "" {
client.WithAuthToken(token)

}

httpClient := oauth2.NewClient(ctx, ts)

var client *github.Client
if url == "" {
client = github.NewClient(httpClient)
} else {
Expand Down

0 comments on commit 9ae6758

Please sign in to comment.