Skip to content

Commit efa12d7

Browse files
Merge remote-tracking branch 'upstream/master' into add-agent-tasks-api
2 parents 8d3c4df + 40ed57f commit efa12d7

84 files changed

Lines changed: 3616 additions & 2075 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.codecov.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
ignore:
2+
# ignore examples
3+
- "example"
24
# ignore auto-generated code
35
- "github/github-accessors.go"
46
# ignore experimental scrape package
57
- "scrape"
8+
# ignore test
9+
- "test"
610
# ignore tools
711
- "tools"

README.md

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ go-github will require the N-1 major release of Go by default.
2020

2121
[support-policy]: https://golang.org/doc/devel/release.html#policy
2222

23-
## Development
23+
## Development ##
2424

2525
If you're interested in using the [GraphQL API v4][], the recommended library is
2626
[shurcooL/githubv4][].
@@ -66,7 +66,10 @@ Construct a new GitHub client, then use the various services on the client to
6666
access different parts of the GitHub API. For example:
6767

6868
```go
69-
client := github.NewClient(nil)
69+
client, err := github.NewClient()
70+
if err != nil {
71+
// Handle error.
72+
}
7073

7174
// list all organizations for user "willnorris"
7275
orgs, _, err := client.Organizations.List(context.Background(), "willnorris", nil)
@@ -75,7 +78,10 @@ orgs, _, err := client.Organizations.List(context.Background(), "willnorris", ni
7578
Some API methods have optional parameters that can be passed. For example:
7679

7780
```go
78-
client := github.NewClient(nil)
81+
client, err := github.NewClient()
82+
if err != nil {
83+
// Handle error.
84+
}
7985

8086
// list public repositories for org "github"
8187
opt := &github.RepositoryListByOrgOptions{Type: "public"}
@@ -95,14 +101,23 @@ For more sample code snippets, head over to the
95101

96102
### Authentication ###
97103

98-
Use the `WithAuthToken` method to configure your client to authenticate using an
104+
Use the `github.WithAuthToken` options method to configure your client to authenticate using an
99105
OAuth token (for example, a [personal access token][]). This is what is needed
100106
for a majority of use cases aside from GitHub Apps.
101107

102108
```go
103-
client := github.NewClient(nil).WithAuthToken("... your access token ...")
109+
client, err := github.NewClient(github.WithAuthToken("... your access token ..."))
110+
if err != nil {
111+
// Handle error.
112+
}
104113
```
105114

115+
To support more advanced use cases; you can use the `github.WithTransport` option to provide a
116+
custom `http.RoundTripper` that handles authentication for you, or the `github.WithHTTPClient`
117+
option to provide a custom `http.Client`. As an example; you can use the `oauth2.Transport`
118+
from the [golang.org/x/oauth2](https://pkg.go.dev/golang.org/x/oauth2) package to handle OAuth
119+
token refreshing for you.
120+
106121
Note that when using an authenticated Client, all calls made by the client will
107122
include the specified OAuth token. Therefore, authenticated clients should
108123
almost never be shared between different users.
@@ -146,7 +161,10 @@ func main() {
146161
}
147162

148163
// Use installation transport with client.
149-
client := github.NewClient(&http.Client{Transport: itr})
164+
client, err := github.NewClient(github.WithTransport(itr))
165+
if err != nil {
166+
// Handle error.
167+
}
150168

151169
// Use client...
152170
}
@@ -186,11 +204,14 @@ func main() {
186204
// InstallationTokenSource has the mechanism to refresh the token when it expires.
187205
httpClient := oauth2.NewClient(context.Background(), installationTokenSource)
188206

189-
client := github.NewClient(httpClient)
207+
client, err := github.NewClient(github.WithHTTPClient(httpClient))
208+
if err != nil {
209+
// Handle error.
210+
}
190211
}
191212
```
192213

193-
*Note*: In order to interact with certain APIs, for example writing a file to a repo, one must generate an installation token
214+
_Note_: In order to interact with certain APIs, for example writing a file to a repo, one must generate an installation token
194215
using the installation ID of the GitHub app and authenticate with the OAuth method mentioned above. See the examples.
195216

196217
### Rate Limiting ###
@@ -296,9 +317,10 @@ import (
296317
_ "github.com/bartventer/httpcache/store/memcache" // Register the in-memory backend
297318
)
298319

299-
client := github.NewClient(
300-
httpcache.NewClient("memcache://"),
301-
).WithAuthToken(os.Getenv("GITHUB_TOKEN"))
320+
client, err := github.NewClient(github.WithHTTPClient(httpcache.NewClient("memcache://")), github.WithAuthToken(os.Getenv("GITHUB_TOKEN")))
321+
if err != nil {
322+
// Handle error.
323+
}
302324
```
303325

304326
Alternatively, the [bored-engineer/github-conditional-http-transport](https://github.com/bored-engineer/github-conditional-http-transport)
@@ -334,7 +356,10 @@ embedded type of a more specific list options struct (for example
334356
`github.Response` struct.
335357

336358
```go
337-
client := github.NewClient(nil)
359+
client, err := github.NewClient()
360+
if err != nil {
361+
// Handle error.
362+
}
338363

339364
opt := &github.RepositoryListByOrgOptions{
340365
ListOptions: github.ListOptions{PerPage: 10},
@@ -372,7 +397,10 @@ To handle rate limiting issues, make sure to use a rate-limiting transport.
372397
To use these methods, simply create an iterator and then range over it, for example:
373398

374399
```go
375-
client := github.NewClient(nil)
400+
client, err := github.NewClient()
401+
if err != nil {
402+
// Handle error.
403+
}
376404
var allRepos []*github.Repository
377405

378406
// create an iterator and start looping through all the results
@@ -389,7 +417,10 @@ Alternatively, if you wish to use an external package, there is `enrichman/gh-it
389417
Its iterator will handle pagination for you, looping through all the available results.
390418

391419
```go
392-
client := github.NewClient(nil)
420+
client, err := github.NewClient()
421+
if err != nil {
422+
// Handle error.
423+
}
393424
var allRepos []*github.Repository
394425

395426
// create an iterator and start looping through all the results
@@ -465,12 +496,14 @@ implementing preview features of the GitHub API, we've adopted the following
465496
versioning policy:
466497

467498
* We increment the **major version** with any incompatible change to
468-
non-preview functionality, including changes to the exported Go API surface
469-
or behavior of the API.
499+
non-preview functionality, including changes to the exported Go API surface
500+
or behavior of the API.
501+
470502
* We increment the **minor version** with any backwards-compatible changes to
471-
functionality, as well as any changes to preview functionality in the GitHub
472-
API. GitHub makes no guarantee about the stability of preview functionality,
473-
so neither do we consider it a stable part of the go-github API.
503+
functionality, as well as any changes to preview functionality in the GitHub
504+
API. GitHub makes no guarantee about the stability of preview functionality,
505+
so neither do we consider it a stable part of the go-github API.
506+
474507
* We increment the **patch version** with any backwards-compatible bug fixes.
475508

476509
Preview functionality may take the form of entire methods or simply additional

example/actionpermissions/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ func main() {
3535
log.Fatal("No owner: owner of repo must be given")
3636
}
3737
ctx := context.Background()
38-
client := github.NewClient(nil).WithAuthToken(token)
38+
client, err := github.NewClient(github.WithAuthToken(token))
39+
if err != nil {
40+
log.Fatal(err)
41+
}
3942

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

example/auditlogstream/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func runDelete(args []string) {
157157
}
158158

159159
func newClient(token, apiURL string) *github.Client {
160-
client, err := github.NewClient(nil).WithAuthToken(token).WithEnterpriseURLs(apiURL, apiURL)
160+
client, err := github.NewClient(github.WithAuthToken(token), github.WithEnterpriseURLs(apiURL, apiURL))
161161
if err != nil {
162162
log.Fatalf("Error creating GitHub client: %v", err)
163163
}

example/basicauth/main.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"context"
2020
"errors"
2121
"fmt"
22+
"log"
2223
"os"
2324
"strings"
2425

@@ -39,7 +40,10 @@ func main() {
3940
Password: strings.TrimSpace(string(password)),
4041
}
4142

42-
client := github.NewClient(tp.Client())
43+
client, err := github.NewClient(github.WithHTTPClient(tp.Client()))
44+
if err != nil {
45+
log.Fatalf("error: %v", err)
46+
}
4347
ctx := context.Background()
4448
user, _, err := client.Users.Get(ctx, "")
4549

@@ -52,8 +56,7 @@ func main() {
5256
}
5357

5458
if err != nil {
55-
fmt.Printf("\nerror: %v\n", err)
56-
return
59+
log.Fatalf("error: %v", err)
5760
}
5861

5962
fmt.Printf("\n%v\n", github.Stringify(user))

example/codespaces/newreposecretwithxcrypto/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ func main() {
7373
}
7474

7575
ctx := context.Background()
76-
client := github.NewClient(nil).WithAuthToken(token)
76+
client, err := github.NewClient(github.WithAuthToken(token))
77+
if err != nil {
78+
log.Fatal(err)
79+
}
7780

7881
if err := addRepoSecret(ctx, client, *owner, *repo, secretName, secretValue); err != nil {
7982
log.Fatal(err)

example/codespaces/newusersecretwithxcrypto/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ func main() {
6666
}
6767

6868
ctx := context.Background()
69-
client := github.NewClient(nil).WithAuthToken(token)
69+
client, err := github.NewClient(github.WithAuthToken(token))
70+
if err != nil {
71+
log.Fatal(err)
72+
}
7073

7174
if err := addUserSecret(ctx, client, secretName, secretValue, *owner, *repo); err != nil {
7275
log.Fatal(err)

example/commitpr/main.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,23 +221,27 @@ func main() {
221221
if *sourceOwner == "" || *sourceRepo == "" || *commitBranch == "" || *sourceFiles == "" || *authorName == "" || *authorEmail == "" {
222222
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`")
223223
}
224-
client = github.NewClient(nil).WithAuthToken(token)
224+
c, err := github.NewClient(github.WithAuthToken(token))
225+
if err != nil {
226+
log.Fatal(err)
227+
}
228+
client = c
225229

226230
ref, err := getRef()
227231
if err != nil {
228-
log.Fatalf("Unable to get/create the commit reference: %v\n", err)
232+
log.Fatalf("Unable to get/create the commit reference: %v", err)
229233
}
230234
if ref == nil {
231235
log.Fatal("No error where returned but the reference is nil")
232236
}
233237

234238
tree, err := getTree(ref)
235239
if err != nil {
236-
log.Fatalf("Unable to create the tree based on the provided files: %v\n", err)
240+
log.Fatalf("Unable to create the tree based on the provided files: %v", err)
237241
}
238242

239243
if err := pushCommit(ref, tree); err != nil {
240-
log.Fatalf("Unable to create the commit: %v\n", err)
244+
log.Fatalf("Unable to create the commit: %v", err)
241245
}
242246

243247
if err := createPR(); err != nil {

example/contents/main.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"context"
1717
"fmt"
1818
"io"
19+
"log"
1920
"os"
2021
"path/filepath"
2122
"strings"
@@ -50,25 +51,25 @@ func main() {
5051

5152
fmt.Printf("\nDownloading %v/%v/%v at ref %v to %v...\n", owner, repo, repoPath, ref, outputPath)
5253

53-
client := github.NewClient(nil)
54+
client, err := github.NewClient()
55+
if err != nil {
56+
log.Fatalf("Error creating GitHub client: %v", err)
57+
}
5458

5559
rc, _, err := client.Repositories.DownloadContents(context.Background(), owner, repo, repoPath, &github.RepositoryContentGetOptions{Ref: ref})
5660
if err != nil {
57-
fmt.Printf("Error: %v\n", err)
58-
os.Exit(1)
61+
log.Fatalf("Error downloading contents: %v", err)
5962
}
6063
defer rc.Close()
6164

6265
f, err := os.Create(outputPath) //#nosec G703 -- path is validated above
6366
if err != nil {
64-
fmt.Printf("Error: %v\n", err)
65-
os.Exit(1)
67+
log.Fatalf("Error creating output file: %v", err)
6668
}
6769
defer f.Close()
6870

6971
if _, err := io.Copy(f, rc); err != nil {
70-
fmt.Printf("Error: %v\n", err)
71-
os.Exit(1)
72+
log.Fatalf("Error writing to output file: %v", err)
7273
}
7374

7475
fmt.Println("Download completed.")

example/go.mod

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ require (
1212
github.com/sigstore/sigstore-go v1.1.4
1313
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.43.0
1414
go.opentelemetry.io/otel/sdk v1.43.0
15-
golang.org/x/crypto v0.50.0
16-
golang.org/x/term v0.42.0
15+
golang.org/x/crypto v0.51.0
16+
golang.org/x/term v0.43.0
1717
)
1818

1919
require (
@@ -76,11 +76,11 @@ require (
7676
go.opentelemetry.io/otel/metric v1.43.0 // indirect
7777
go.opentelemetry.io/otel/trace v1.43.0 // indirect
7878
go.yaml.in/yaml/v3 v3.0.4 // indirect
79-
golang.org/x/mod v0.34.0 // indirect
80-
golang.org/x/net v0.53.0 // indirect
79+
golang.org/x/mod v0.35.0 // indirect
80+
golang.org/x/net v0.54.0 // indirect
8181
golang.org/x/sync v0.20.0 // indirect
82-
golang.org/x/sys v0.43.0 // indirect
83-
golang.org/x/text v0.36.0 // indirect
82+
golang.org/x/sys v0.44.0 // indirect
83+
golang.org/x/text v0.37.0 // indirect
8484
google.golang.org/genproto/googleapis/api v0.0.0-20260316180232-0b37fe3546d5 // indirect
8585
google.golang.org/genproto/googleapis/rpc v0.0.0-20260316180232-0b37fe3546d5 // indirect
8686
google.golang.org/grpc v1.79.3 // indirect

0 commit comments

Comments
 (0)