Skip to content
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
65 changes: 46 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ go-github will require the N-1 major release of Go by default.

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

## Development
## Development ##

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

```go
client := github.NewClient(nil)
client, err := github.NewClient()
if err != nil {
// Handle error.
}

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

```go
client := github.NewClient(nil)
client, err := github.NewClient()
if err != nil {
// Handle error.
}

// list public repositories for org "github"
opt := &github.RepositoryListByOrgOptions{Type: "public"}
Expand All @@ -95,12 +101,15 @@ For more sample code snippets, head over to the

### Authentication ###

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

```go
client := github.NewClient(nil).WithAuthToken("... your access token ...")
client, err := github.NewClient(github.WithAuthToken("... your access token ..."))
if err != nil {
// Handle error.
}
```

Note that when using an authenticated Client, all calls made by the client will
Expand Down Expand Up @@ -146,7 +155,10 @@ func main() {
}

// Use installation transport with client.
client := github.NewClient(&http.Client{Transport: itr})
client, err := github.NewClient(github.WithTransport(itr))
if err != nil {
// Handle error.
}

// Use client...
}
Expand Down Expand Up @@ -186,11 +198,14 @@ func main() {
// InstallationTokenSource has the mechanism to refresh the token when it expires.
httpClient := oauth2.NewClient(context.Background(), installationTokenSource)

client := github.NewClient(httpClient)
client, err := github.NewClient(github.WithHTTPClient(httpClient))
if err != nil {
// Handle error.
}
}
```

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

### Rate Limiting ###
Expand Down Expand Up @@ -296,9 +311,10 @@ import (
_ "github.com/bartventer/httpcache/store/memcache" // Register the in-memory backend
)

client := github.NewClient(
httpcache.NewClient("memcache://"),
).WithAuthToken(os.Getenv("GITHUB_TOKEN"))
client, err := github.NewClient(github.WithHTTPClient(httpcache.NewClient("memcache://")), github.WithAuthToken(os.Getenv("GITHUB_TOKEN")))
if err != nil {
// Handle error.
}
```

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

```go
client := github.NewClient(nil)
client, err := github.NewClient()
if err != nil {
// Handle error.
}

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

```go
client := github.NewClient(nil)
client, err := github.NewClient()
if err != nil {
// Handle error.
}
var allRepos []*github.Repository

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

```go
client := github.NewClient(nil)
client, err := github.NewClient()
if err != nil {
// Handle error.
}
var allRepos []*github.Repository

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

* We increment the **major version** with any incompatible change to
non-preview functionality, including changes to the exported Go API surface
or behavior of the API.
non-preview functionality, including changes to the exported Go API surface
or behavior of the API.

* We increment the **minor version** with any backwards-compatible changes to
functionality, as well as any changes to preview functionality in the GitHub
API. GitHub makes no guarantee about the stability of preview functionality,
so neither do we consider it a stable part of the go-github API.
functionality, as well as any changes to preview functionality in the GitHub
API. GitHub makes no guarantee about the stability of preview functionality,
so neither do we consider it a stable part of the go-github API.

* We increment the **patch version** with any backwards-compatible bug fixes.

Preview functionality may take the form of entire methods or simply additional
Expand Down
5 changes: 4 additions & 1 deletion example/actionpermissions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ func main() {
log.Fatal("No owner: owner of repo must be given")
}
ctx := context.Background()
client := github.NewClient(nil).WithAuthToken(token)
client, err := github.NewClient(github.WithAuthToken(token))
if err != nil {
log.Fatal(err)
}

actionsPermissionsRepository, _, err := client.Repositories.GetActionsPermissions(ctx, *owner, *name)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion example/auditlogstream/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func runDelete(args []string) {
}

func newClient(token, apiURL string) *github.Client {
client, err := github.NewClient(nil).WithAuthToken(token).WithEnterpriseURLs(apiURL, apiURL)
client, err := github.NewClient(github.WithAuthToken(token), github.WithEnterpriseURLs(apiURL, apiURL))
if err != nil {
log.Fatalf("Error creating GitHub client: %v", err)
}
Expand Down
6 changes: 5 additions & 1 deletion example/basicauth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ func main() {
Password: strings.TrimSpace(string(password)),
}

client := github.NewClient(tp.Client())
client, err := github.NewClient(github.WithHTTPClient(tp.Client()))
if err != nil {
fmt.Printf("\nerror: %v\n", err)
return
Comment on lines +44 to +45
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
fmt.Printf("\nerror: %v\n", err)
return
log.Fatalf("error: %v", err)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I intentionally kept the existing pattern for the individual examples rather than standardising them all.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

No, let's please clean them all up and standardize them since you are touching every one of these in this PR.

}
ctx := context.Background()
user, _, err := client.Users.Get(ctx, "")

Expand Down
5 changes: 4 additions & 1 deletion example/codespaces/newreposecretwithxcrypto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ func main() {
}

ctx := context.Background()
client := github.NewClient(nil).WithAuthToken(token)
client, err := github.NewClient(github.WithAuthToken(token))
if err != nil {
log.Fatal(err)
}

if err := addRepoSecret(ctx, client, *owner, *repo, secretName, secretValue); err != nil {
log.Fatal(err)
Expand Down
5 changes: 4 additions & 1 deletion example/codespaces/newusersecretwithxcrypto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ func main() {
}

ctx := context.Background()
client := github.NewClient(nil).WithAuthToken(token)
client, err := github.NewClient(github.WithAuthToken(token))
if err != nil {
log.Fatal(err)
}

if err := addUserSecret(ctx, client, secretName, secretValue, *owner, *repo); err != nil {
log.Fatal(err)
Expand Down
6 changes: 5 additions & 1 deletion example/commitpr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,11 @@ 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`")
}
client = github.NewClient(nil).WithAuthToken(token)
c, err := github.NewClient(github.WithAuthToken(token))
if err != nil {
log.Fatal(err)
}
client = c

ref, err := getRef()
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion example/contents/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ func main() {

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

client := github.NewClient(nil)
client, err := github.NewClient()
if err != nil {
fmt.Printf("Error creating GitHub client: %v\n", err)
os.Exit(1)
Comment on lines +55 to +56
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
fmt.Printf("Error creating GitHub client: %v\n", err)
os.Exit(1)
log.Fatalf("Error creating GitHub client: %v", err)

}

rc, _, err := client.Repositories.DownloadContents(context.Background(), owner, repo, repoPath, &github.RepositoryContentGetOptions{Ref: ref})
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion example/listenvironments/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ func main() {
}

ctx := context.Background()
client := github.NewClient(nil).WithAuthToken(token)
client, err := github.NewClient(github.WithAuthToken(token))
if err != nil {
log.Fatal(err)
}

expectedPageSize := 2

Expand Down
5 changes: 4 additions & 1 deletion example/migrations/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import (

func fetchAllUserMigrations() ([]*github.UserMigration, error) {
ctx := context.Background()
client := github.NewClient(nil).WithAuthToken("<GITHUB_AUTH_TOKEN>")
client, err := github.NewClient(github.WithAuthToken("<GITHUB_AUTH_TOKEN>"))
if err != nil {
return nil, err
}

migrations, _, err := client.Migrations.ListUserMigrations(ctx, &github.ListOptions{Page: 1})
return migrations, err
Expand Down
14 changes: 5 additions & 9 deletions example/newfilewithappauth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ func main() {
itr.BaseURL = gitHost

// create git client with app transport
client, err := github.NewClient(
&http.Client{
Transport: itr,
Timeout: time.Second * 30,
},
).WithEnterpriseURLs(gitHost, gitHost)
client, err := github.NewClient(github.WithHTTPClient(&http.Client{
Transport: itr,
Timeout: time.Second * 30,
}), github.WithEnterpriseURLs(gitHost, gitHost))
if err != nil {
log.Fatalf("failed to create git client for app: %v\n", err)
}
Expand All @@ -64,9 +62,7 @@ func main() {
log.Fatalf("failed to create installation token: %v\n", err)
}

apiClient, err := github.NewClient(nil).WithAuthToken(
token.GetToken(),
).WithEnterpriseURLs(gitHost, gitHost)
apiClient, err := github.NewClient(github.WithAuthToken(token.GetToken()), github.WithEnterpriseURLs(gitHost, gitHost))
if err != nil {
log.Fatalf("failed to create new git client with token: %v\n", err)
}
Expand Down
5 changes: 4 additions & 1 deletion example/newrepo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ func main() {
log.Fatal("No name: New repos must be given a name")
}
ctx := context.Background()
client := github.NewClient(nil).WithAuthToken(token)
client, err := github.NewClient(github.WithAuthToken(token))
if err != nil {
log.Fatal(err)
}

r := &github.Repository{Name: name, Private: private, Description: description, AutoInit: autoInit}
repo, _, err := client.Repositories.Create(ctx, "", r)
Expand Down
5 changes: 4 additions & 1 deletion example/newreposecretwithxcrypto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ func main() {
}

ctx := context.Background()
client := github.NewClient(nil).WithAuthToken(token)
client, err := github.NewClient(github.WithAuthToken(token))
if err != nil {
log.Fatal(err)
}

if err := addRepoSecret(ctx, client, *owner, *repo, secretName, secretValue); err != nil {
log.Fatal(err)
Expand Down
17 changes: 9 additions & 8 deletions example/otel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ func main() {
}
}()

// Configure HTTP client with OTel transport
httpClient := &http.Client{
Transport: otel.NewTransport(
http.DefaultTransport,
otel.WithTracerProvider(tp),
),
}
// Configure OTel transport
t := otel.NewTransport(
http.DefaultTransport,
otel.WithTracerProvider(tp),
)

client := github.NewClient(httpClient)
client, err := github.NewClient(github.WithTransport(t))
if err != nil {
log.Fatalf("Error creating GitHub client: %v", err)
}

// Make a request (Get Rate Limits is public and cheap)
limits, resp, err := client.RateLimit.Get(context.Background())
Expand Down
6 changes: 5 additions & 1 deletion example/ratelimit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ func main() {
paginator := githubpagination.NewClient(rateLimiter,
githubpagination.WithPerPage(100), // default to 100 results per page
)
client := github.NewClient(paginator)
client, err := github.NewClient(github.WithHTTPClient(paginator))
if err != nil {
fmt.Printf("Error creating GitHub client: %v\n", err)
return
Comment on lines +42 to +43
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
fmt.Printf("Error creating GitHub client: %v\n", err)
return
log.Fatalf("Error creating GitHub client: %v", err)

}

// Example usage of the client
repos, _, err := client.Repositories.ListByUser(context.Background(), username, nil)
Expand Down
5 changes: 4 additions & 1 deletion example/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ import (

// Fetch all the public organizations' membership of a user.
func fetchOrganizations(username string) ([]*github.Organization, error) {
client := github.NewClient(nil)
client, err := github.NewClient()
if err != nil {
return nil, err
}
orgs, _, err := client.Organizations.List(context.Background(), username, nil)
return orgs, err
}
Expand Down
5 changes: 4 additions & 1 deletion example/tokenauth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ func main() {
fmt.Println()

ctx := context.Background()
client := github.NewClient(nil).WithAuthToken(string(token))
client, err := github.NewClient(github.WithAuthToken(string(token)))
if err != nil {
log.Fatalf("Error creating GitHub client: %v\n", err)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
log.Fatalf("Error creating GitHub client: %v\n", err)
log.Fatalf("Error creating GitHub client: %v", err)

}

user, resp, err := client.Users.Get(ctx, "")
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion example/topics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import (

// Fetch and lists all the public topics associated with the specified GitHub topic.
func fetchTopics(topic string) (*github.TopicsSearchResult, error) {
client := github.NewClient(nil)
client, err := github.NewClient()
if err != nil {
return nil, err
}

topics, _, err := client.Search.Topics(context.Background(), topic, nil)
return topics, err
}
Expand Down
5 changes: 4 additions & 1 deletion example/uploadreleaseassetfromrelease/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ func main() {
}

ctx := context.Background()
client := github.NewClient(nil).WithAuthToken(token)
client, err := github.NewClient(github.WithAuthToken(token))
if err != nil {
log.Fatalf("Error creating GitHub client: %v\n", err)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
log.Fatalf("Error creating GitHub client: %v\n", err)
log.Fatalf("Error creating GitHub client: %v", err)

}

owner := "OWNER"
repo := "REPO"
Expand Down
Loading
Loading