Skip to content

Commit

Permalink
Use NewTokenClient in tests and examples (#2644)
Browse files Browse the repository at this point in the history
  • Loading branch information
icholy authored Jan 26, 2023
1 parent cdecead commit aa2c143
Show file tree
Hide file tree
Showing 12 changed files with 14 additions and 84 deletions.
5 changes: 1 addition & 4 deletions example/actionpermissions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"os"

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

var (
Expand All @@ -36,9 +35,7 @@ func main() {
log.Fatal("No owner: owner of repo must be given")
}
ctx := context.Background()
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
client := github.NewTokenClient(ctx, token)

actionsPermissionsRepository, _, err := client.Repositories.GetActionsPermissions(ctx, *owner, *name)
if err != nil {
Expand Down
7 changes: 1 addition & 6 deletions example/appengine/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"os"

"github.com/google/go-github/v49/github"
"golang.org/x/oauth2"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
)
Expand All @@ -29,11 +28,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
}

ctx := appengine.NewContext(r)
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_AUTH_TOKEN")},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
client := github.NewTokenClient(ctx, os.Getenv("GITHUB_AUTH_TOKEN"))

commits, _, err := client.Repositories.ListCommits(ctx, "google", "go-github", nil)
if err != nil {
Expand Down
5 changes: 1 addition & 4 deletions example/commitpr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"time"

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

var (
Expand Down Expand Up @@ -188,9 +187,7 @@ func main() {
if *sourceOwner == "" || *sourceRepo == "" || *commitBranch == "" || *sourceFiles == "" || *authorName == "" || *authorEmail == "" {
log.Fatal("You need to specify a non-empty value for the flags `-source-owner`, `-source-repo`, `-commit-branch`, `-files`, `-author-name` and `-author-email`")
}
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
tc := oauth2.NewClient(ctx, ts)
client = github.NewClient(tc)
client = github.NewTokenClient(ctx, token)

ref, err := getRef()
if err != nil {
Expand Down
6 changes: 1 addition & 5 deletions example/listenvironments/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"os"

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

func main() {
Expand All @@ -31,11 +30,8 @@ func main() {
log.Fatal("Unauthorized: No token present")
}

ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})

ctx := context.Background()
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
client := github.NewTokenClient(ctx, token)

expectedPageSize := 2

Expand Down
7 changes: 1 addition & 6 deletions example/migrations/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,11 @@ import (
"fmt"

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

func fetchAllUserMigrations() ([]*github.UserMigration, error) {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: "<GITHUB_AUTH_TOKEN>"},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
client := github.NewTokenClient(ctx, "<GITHUB_AUTH_TOKEN>")

migrations, _, err := client.Migrations.ListUserMigrations(ctx, &github.ListOptions{Page: 1})
return migrations, err
Expand Down
5 changes: 1 addition & 4 deletions example/newrepo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"os"

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

var (
Expand All @@ -37,9 +36,7 @@ func main() {
log.Fatal("No name: New repos must be given a name")
}
ctx := context.Background()
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
client := github.NewTokenClient(ctx, token)

r := &github.Repository{Name: name, Private: private, Description: description, AutoInit: autoInit}
repo, _, err := client.Repositories.Create(ctx, "", r)
Expand Down
16 changes: 2 additions & 14 deletions example/newreposecretwithlibsodium/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (

sodium "github.com/GoKillers/libsodium-go/cryptobox"
"github.com/google/go-github/v49/github"
"golang.org/x/oauth2"
)

var (
Expand Down Expand Up @@ -71,7 +70,8 @@ func main() {
log.Fatal(err)
}

ctx, client, err := githubAuth(token)
ctx := context.Background()
client := github.NewTokenClient(ctx, token)
if err != nil {
log.Fatalf("unable to authorize using env GITHUB_AUTH_TOKEN: %v", err)
}
Expand Down Expand Up @@ -99,18 +99,6 @@ func getSecretValue(secretName string) (string, error) {
return secretValue, nil
}

// githubAuth returns a GitHub client and context.
func githubAuth(token string) (context.Context, *github.Client, error) {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)

client := github.NewClient(tc)
return ctx, client, nil
}

// addRepoSecret will add a secret to a GitHub repo for use in GitHub Actions.
//
// Finally, the secretName and secretValue will determine the name of the secret added and it's corresponding value.
Expand Down
19 changes: 2 additions & 17 deletions example/newreposecretwithxcrypto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (

"github.com/google/go-github/v49/github"
"golang.org/x/crypto/nacl/box"
"golang.org/x/oauth2"
)

var (
Expand Down Expand Up @@ -72,10 +71,8 @@ func main() {
log.Fatal(err)
}

ctx, client, err := githubAuth(token)
if err != nil {
log.Fatalf("unable to authorize using env GITHUB_AUTH_TOKEN: %v", err)
}
ctx := context.Background()
client := github.NewTokenClient(ctx, token)

if err := addRepoSecret(ctx, client, *owner, *repo, secretName, secretValue); err != nil {
log.Fatal(err)
Expand All @@ -100,18 +97,6 @@ func getSecretValue(secretName string) (string, error) {
return secretValue, nil
}

// githubAuth returns a GitHub client and context.
func githubAuth(token string) (context.Context, *github.Client, error) {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)

client := github.NewClient(tc)
return ctx, client, nil
}

// addRepoSecret will add a secret to a GitHub repo for use in GitHub Actions.
//
// Finally, the secretName and secretValue will determine the name of the secret added and it's corresponding value.
Expand Down
8 changes: 1 addition & 7 deletions example/tagprotection/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

"github.com/google/go-github/v49/github"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/oauth2"
)

func main() {
Expand All @@ -45,12 +44,7 @@ func main() {
token := string(byteToken)

ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)

client := github.NewClient(tc)
client := github.NewTokenClient(ctx, token)

// create new tag protection
if pattern != "" {
Expand Down
8 changes: 1 addition & 7 deletions example/tokenauth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

"github.com/google/go-github/v49/github"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/oauth2"
)

func main() {
Expand All @@ -26,12 +25,7 @@ func main() {
token := string(byteToken)

ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)

client := github.NewClient(tc)
client := github.NewTokenClient(ctx, token)

user, resp, err := client.Users.Get(ctx, "")
if err != nil {
Expand Down
6 changes: 1 addition & 5 deletions test/fields/fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"strings"

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

var (
Expand All @@ -47,10 +46,7 @@ func main() {
print("!!! No OAuth token. Some tests won't run. !!!\n\n")
client = github.NewClient(nil)
} else {
tc := oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
))
client = github.NewClient(tc)
client = github.NewTokenClient(context.Background(), token)
auth = true
}

Expand Down
6 changes: 1 addition & 5 deletions test/integration/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"os"

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

var (
Expand All @@ -33,10 +32,7 @@ func init() {
print("!!! No OAuth token. Some tests won't run. !!!\n\n")
client = github.NewClient(nil)
} else {
tc := oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
))
client = github.NewClient(tc)
client = github.NewTokenClient(context.Background(), token)
auth = true
}
}
Expand Down

0 comments on commit aa2c143

Please sign in to comment.