Skip to content

Commit

Permalink
chore: update go-github to use token (#21292)
Browse files Browse the repository at this point in the history
* minor change to use WithAuthToken

Signed-off-by: [email protected] <[email protected]>

* minor change to use WithAuthToken

Signed-off-by: [email protected] <[email protected]>

* Update applicationset/services/scm_provider/github.go

Co-authored-by: Nitish Kumar <[email protected]>
Signed-off-by: Adam Buran <[email protected]>

* remove ctx from NewGithubProvider

Signed-off-by: aburan28 <[email protected]>

* remove ctx from NewGithubProvider

Signed-off-by: aburan28 <[email protected]>

* remove unused parameter

Signed-off-by: aburan28 <[email protected]>

* update readthedogs.yml to allow for builds

Signed-off-by: aburan28 <[email protected]>

---------

Signed-off-by: [email protected] <[email protected]>
Signed-off-by: Adam Buran <[email protected]>
Signed-off-by: aburan28 <[email protected]>
Co-authored-by: Nitish Kumar <[email protected]>
  • Loading branch information
aburan28 and nitishfy authored Jan 16, 2025
1 parent f548fd7 commit 9b17495
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 25 deletions.
2 changes: 1 addition & 1 deletion applicationset/generators/pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,5 @@ func (g *PullRequestGenerator) github(ctx context.Context, cfg *argoprojiov1alph
if err != nil {
return nil, fmt.Errorf("error fetching Secret token: %w", err)
}
return pullrequest.NewGithubService(ctx, token, cfg.API, cfg.Owner, cfg.Repo, cfg.Labels)
return pullrequest.NewGithubService(token, cfg.API, cfg.Owner, cfg.Repo, cfg.Labels)
}
2 changes: 1 addition & 1 deletion applicationset/generators/scm_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,5 +289,5 @@ func (g *SCMProviderGenerator) githubProvider(ctx context.Context, github *argop
if err != nil {
return nil, fmt.Errorf("error fetching Github token: %w", err)
}
return scm_provider.NewGithubProvider(ctx, github.Organization, token, github.API, github.AllBranches)
return scm_provider.NewGithubProvider(github.Organization, token, github.API, github.AllBranches)
}
14 changes: 4 additions & 10 deletions applicationset/services/pull_request/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package pull_request
import (
"context"
"fmt"
"net/http"
"os"

"github.com/google/go-github/v66/github"
"golang.org/x/oauth2"
)

type GithubService struct {
Expand All @@ -18,21 +18,15 @@ type GithubService struct {

var _ PullRequestService = (*GithubService)(nil)

func NewGithubService(ctx context.Context, token, url, owner, repo string, labels []string) (PullRequestService, error) {
var ts oauth2.TokenSource
func NewGithubService(token, url, owner, repo string, labels []string) (PullRequestService, error) {
// Undocumented environment variable to set a default token, to be used in testing to dodge anonymous rate limits.
if token == "" {
token = os.Getenv("GITHUB_TOKEN")
}
if token != "" {
ts = oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
}
httpClient := oauth2.NewClient(ctx, ts)
httpClient := &http.Client{}
var client *github.Client
if url == "" {
client = github.NewClient(httpClient)
client = github.NewClient(httpClient).WithAuthToken(token)
} else {
var err error
client, err = github.NewClient(httpClient).WithEnterpriseURLs(url, url)
Expand Down
13 changes: 3 additions & 10 deletions applicationset/services/scm_provider/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"

"github.com/google/go-github/v66/github"
"golang.org/x/oauth2"
)

type GithubProvider struct {
Expand All @@ -18,21 +17,15 @@ type GithubProvider struct {

var _ SCMProviderService = &GithubProvider{}

func NewGithubProvider(ctx context.Context, organization string, token string, url string, allBranches bool) (*GithubProvider, error) {
var ts oauth2.TokenSource
func NewGithubProvider(organization string, token string, url string, allBranches bool) (*GithubProvider, error) {
// Undocumented environment variable to set a default token, to be used in testing to dodge anonymous rate limits.
if token == "" {
token = os.Getenv("GITHUB_TOKEN")
}
if token != "" {
ts = oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
}
httpClient := oauth2.NewClient(ctx, ts)
httpClient := &http.Client{}
var client *github.Client
if url == "" {
client = github.NewClient(httpClient)
client = github.NewClient(httpClient).WithAuthToken(token)
} else {
var err error
client, err = github.NewClient(httpClient).WithEnterpriseURLs(url, url)
Expand Down
6 changes: 3 additions & 3 deletions applicationset/services/scm_provider/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func TestGithubListRepos(t *testing.T) {
defer ts.Close()
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
provider, _ := NewGithubProvider(context.Background(), "argoproj", "", ts.URL, c.allBranches)
provider, _ := NewGithubProvider("argoproj", "", ts.URL, c.allBranches)
rawRepos, err := ListRepos(context.Background(), provider, c.filters, c.proto)
if c.hasError {
require.Error(t, err)
Expand Down Expand Up @@ -273,7 +273,7 @@ func TestGithubHasPath(t *testing.T) {
githubMockHandler(t)(w, r)
}))
defer ts.Close()
host, _ := NewGithubProvider(context.Background(), "argoproj", "", ts.URL, false)
host, _ := NewGithubProvider("argoproj", "", ts.URL, false)
repo := &Repository{
Organization: "argoproj",
Repository: "argo-cd",
Expand All @@ -293,7 +293,7 @@ func TestGithubGetBranches(t *testing.T) {
githubMockHandler(t)(w, r)
}))
defer ts.Close()
host, _ := NewGithubProvider(context.Background(), "argoproj", "", ts.URL, false)
host, _ := NewGithubProvider("argoproj", "", ts.URL, false)
repo := &Repository{
Organization: "argoproj",
Repository: "argo-cd",
Expand Down

0 comments on commit 9b17495

Please sign in to comment.