Skip to content

Commit aa2c143

Browse files
authored
Use NewTokenClient in tests and examples (#2644)
1 parent cdecead commit aa2c143

12 files changed

Lines changed: 14 additions & 84 deletions

File tree

example/actionpermissions/main.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"os"
1616

1717
"github.com/google/go-github/v49/github"
18-
"golang.org/x/oauth2"
1918
)
2019

2120
var (
@@ -36,9 +35,7 @@ func main() {
3635
log.Fatal("No owner: owner of repo must be given")
3736
}
3837
ctx := context.Background()
39-
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
40-
tc := oauth2.NewClient(ctx, ts)
41-
client := github.NewClient(tc)
38+
client := github.NewTokenClient(ctx, token)
4239

4340
actionsPermissionsRepository, _, err := client.Repositories.GetActionsPermissions(ctx, *owner, *name)
4441
if err != nil {

example/appengine/app.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"os"
1414

1515
"github.com/google/go-github/v49/github"
16-
"golang.org/x/oauth2"
1716
"google.golang.org/appengine"
1817
"google.golang.org/appengine/log"
1918
)
@@ -29,11 +28,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
2928
}
3029

3130
ctx := appengine.NewContext(r)
32-
ts := oauth2.StaticTokenSource(
33-
&oauth2.Token{AccessToken: os.Getenv("GITHUB_AUTH_TOKEN")},
34-
)
35-
tc := oauth2.NewClient(ctx, ts)
36-
client := github.NewClient(tc)
31+
client := github.NewTokenClient(ctx, os.Getenv("GITHUB_AUTH_TOKEN"))
3732

3833
commits, _, err := client.Repositories.ListCommits(ctx, "google", "go-github", nil)
3934
if err != nil {

example/commitpr/main.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"time"
3232

3333
"github.com/google/go-github/v49/github"
34-
"golang.org/x/oauth2"
3534
)
3635

3736
var (
@@ -188,9 +187,7 @@ func main() {
188187
if *sourceOwner == "" || *sourceRepo == "" || *commitBranch == "" || *sourceFiles == "" || *authorName == "" || *authorEmail == "" {
189188
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`")
190189
}
191-
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
192-
tc := oauth2.NewClient(ctx, ts)
193-
client = github.NewClient(tc)
190+
client = github.NewTokenClient(ctx, token)
194191

195192
ref, err := getRef()
196193
if err != nil {

example/listenvironments/main.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"os"
2020

2121
"github.com/google/go-github/v49/github"
22-
"golang.org/x/oauth2"
2322
)
2423

2524
func main() {
@@ -31,11 +30,8 @@ func main() {
3130
log.Fatal("Unauthorized: No token present")
3231
}
3332

34-
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
35-
3633
ctx := context.Background()
37-
tc := oauth2.NewClient(ctx, ts)
38-
client := github.NewClient(tc)
34+
client := github.NewTokenClient(ctx, token)
3935

4036
expectedPageSize := 2
4137

example/migrations/main.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,11 @@ import (
1313
"fmt"
1414

1515
"github.com/google/go-github/v49/github"
16-
"golang.org/x/oauth2"
1716
)
1817

1918
func fetchAllUserMigrations() ([]*github.UserMigration, error) {
2019
ctx := context.Background()
21-
ts := oauth2.StaticTokenSource(
22-
&oauth2.Token{AccessToken: "<GITHUB_AUTH_TOKEN>"},
23-
)
24-
tc := oauth2.NewClient(ctx, ts)
25-
client := github.NewClient(tc)
20+
client := github.NewTokenClient(ctx, "<GITHUB_AUTH_TOKEN>")
2621

2722
migrations, _, err := client.Migrations.ListUserMigrations(ctx, &github.ListOptions{Page: 1})
2823
return migrations, err

example/newrepo/main.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"os"
1818

1919
"github.com/google/go-github/v49/github"
20-
"golang.org/x/oauth2"
2120
)
2221

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

4441
r := &github.Repository{Name: name, Private: private, Description: description, AutoInit: autoInit}
4542
repo, _, err := client.Repositories.Create(ctx, "", r)

example/newreposecretwithlibsodium/main.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737

3838
sodium "github.com/GoKillers/libsodium-go/cryptobox"
3939
"github.com/google/go-github/v49/github"
40-
"golang.org/x/oauth2"
4140
)
4241

4342
var (
@@ -71,7 +70,8 @@ func main() {
7170
log.Fatal(err)
7271
}
7372

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

102-
// githubAuth returns a GitHub client and context.
103-
func githubAuth(token string) (context.Context, *github.Client, error) {
104-
ctx := context.Background()
105-
ts := oauth2.StaticTokenSource(
106-
&oauth2.Token{AccessToken: token},
107-
)
108-
tc := oauth2.NewClient(ctx, ts)
109-
110-
client := github.NewClient(tc)
111-
return ctx, client, nil
112-
}
113-
114102
// addRepoSecret will add a secret to a GitHub repo for use in GitHub Actions.
115103
//
116104
// Finally, the secretName and secretValue will determine the name of the secret added and it's corresponding value.

example/newreposecretwithxcrypto/main.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import (
3838

3939
"github.com/google/go-github/v49/github"
4040
"golang.org/x/crypto/nacl/box"
41-
"golang.org/x/oauth2"
4241
)
4342

4443
var (
@@ -72,10 +71,8 @@ func main() {
7271
log.Fatal(err)
7372
}
7473

75-
ctx, client, err := githubAuth(token)
76-
if err != nil {
77-
log.Fatalf("unable to authorize using env GITHUB_AUTH_TOKEN: %v", err)
78-
}
74+
ctx := context.Background()
75+
client := github.NewTokenClient(ctx, token)
7976

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

103-
// githubAuth returns a GitHub client and context.
104-
func githubAuth(token string) (context.Context, *github.Client, error) {
105-
ctx := context.Background()
106-
ts := oauth2.StaticTokenSource(
107-
&oauth2.Token{AccessToken: token},
108-
)
109-
tc := oauth2.NewClient(ctx, ts)
110-
111-
client := github.NewClient(tc)
112-
return ctx, client, nil
113-
}
114-
115100
// addRepoSecret will add a secret to a GitHub repo for use in GitHub Actions.
116101
//
117102
// Finally, the secretName and secretValue will determine the name of the secret added and it's corresponding value.

example/tagprotection/main.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121

2222
"github.com/google/go-github/v49/github"
2323
"golang.org/x/crypto/ssh/terminal"
24-
"golang.org/x/oauth2"
2524
)
2625

2726
func main() {
@@ -45,12 +44,7 @@ func main() {
4544
token := string(byteToken)
4645

4746
ctx := context.Background()
48-
ts := oauth2.StaticTokenSource(
49-
&oauth2.Token{AccessToken: token},
50-
)
51-
tc := oauth2.NewClient(ctx, ts)
52-
53-
client := github.NewClient(tc)
47+
client := github.NewTokenClient(ctx, token)
5448

5549
// create new tag protection
5650
if pattern != "" {

example/tokenauth/main.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616

1717
"github.com/google/go-github/v49/github"
1818
"golang.org/x/crypto/ssh/terminal"
19-
"golang.org/x/oauth2"
2019
)
2120

2221
func main() {
@@ -26,12 +25,7 @@ func main() {
2625
token := string(byteToken)
2726

2827
ctx := context.Background()
29-
ts := oauth2.StaticTokenSource(
30-
&oauth2.Token{AccessToken: token},
31-
)
32-
tc := oauth2.NewClient(ctx, ts)
33-
34-
client := github.NewClient(tc)
28+
client := github.NewTokenClient(ctx, token)
3529

3630
user, resp, err := client.Users.Get(ctx, "")
3731
if err != nil {

0 commit comments

Comments
 (0)