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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 3 additions & 9 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 @@ -19,20 +19,14 @@ 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
// 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
12 changes: 3 additions & 9 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 @@ -19,20 +18,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
// 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)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit

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
Loading