Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update go-github to use token #21292

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading