| 1 |
// Copyright 2013 The go-github AUTHORS. All rights reserved. |
| 2 |
// |
| 3 |
// Use of this source code is governed by a BSD-style |
| 4 |
// license that can be found in the LICENSE file. |
| 5 |
|
| 6 |
/* |
| 7 |
Package github provides a client for using the GitHub API. |
| 8 |
|
| 9 |
Usage: |
| 10 |
|
| 11 |
import "github.com/google/go-github/v60/github" // with go modules enabled (GO111MODULE=on or outside GOPATH) |
| 12 |
import "github.com/google/go-github/github" // with go modules disabled |
| 13 |
|
| 14 |
Construct a new GitHub client, then use the various services on the client to |
| 15 |
access different parts of the GitHub API. For example: |
| 16 |
|
| 17 |
client := github.NewClient(nil) |
| 18 |
|
| 19 |
// list all organizations for user "willnorris" |
| 20 |
orgs, _, err := client.Organizations.List(ctx, "willnorris", nil) |
| 21 |
|
| 22 |
Some API methods have optional parameters that can be passed. For example: |
| 23 |
|
| 24 |
client := github.NewClient(nil) |
| 25 |
|
| 26 |
// list public repositories for org "github" |
| 27 |
opt := &github.RepositoryListByOrgOptions{Type: "public"} |
| 28 |
repos, _, err := client.Repositories.ListByOrg(ctx, "github", opt) |
| 29 |
|
| 30 |
The services of a client divide the API into logical chunks and correspond to |
| 31 |
the structure of the GitHub API documentation at |
| 32 |
https://docs.github.com/rest . |
| 33 |
|
| 34 |
NOTE: Using the https://godoc.org/context package, one can easily |
| 35 |
pass cancelation signals and deadlines to various services of the client for |
| 36 |
handling a request. In case there is no context available, then context.Background() |
| 37 |
can be used as a starting point. |
| 38 |
|
| 39 |
For more sample code snippets, head over to the https://github.com/google/go-github/tree/master/example directory. |
| 40 |
|
| 41 |
# Authentication |
| 42 |
|
| 43 |
Use Client.WithAuthToken to configure your client to authenticate using an Oauth token |
| 44 |
(for example, a personal access token). This is what is needed for a majority of use cases |
| 45 |
aside from GitHub Apps. |
| 46 |
|
| 47 |
client := github.NewClient(nil).WithAuthToken("... your access token ...") |
| 48 |
|
| 49 |
Note that when using an authenticated Client, all calls made by the client will |
| 50 |
include the specified OAuth token. Therefore, authenticated clients should |
| 51 |
almost never be shared between different users. |
| 52 |
|
| 53 |
For API methods that require HTTP Basic Authentication, use the |
| 54 |
BasicAuthTransport. |
| 55 |
|
| 56 |
GitHub Apps authentication can be provided by the |
| 57 |
https://github.com/bradleyfalzon/ghinstallation package. |
| 58 |
It supports both authentication as an installation, using an installation access token, |
| 59 |
and as an app, using a JWT. |
| 60 |
|
| 61 |
To authenticate as an installation: |
| 62 |
|
| 63 |
import "github.com/bradleyfalzon/ghinstallation" |
| 64 |
|
| 65 |
func main() { |
| 66 |
// Wrap the shared transport for use with the integration ID 1 authenticating with installation ID 99. |
| 67 |
itr, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, 1, 99, "2016-10-19.private-key.pem") |
| 68 |
if err != nil { |
| 69 |
// Handle error. |
| 70 |
} |
| 71 |
|
| 72 |
// Use installation transport with client |
| 73 |
client := github.NewClient(&http.Client{Transport: itr}) |
| 74 |
|
| 75 |
// Use client... |
| 76 |
} |
| 77 |
|
| 78 |
To authenticate as an app, using a JWT: |
| 79 |
|
| 80 |
import "github.com/bradleyfalzon/ghinstallation" |
| 81 |
|
| 82 |
func main() { |
| 83 |
// Wrap the shared transport for use with the application ID 1. |
| 84 |
atr, err := ghinstallation.NewAppsTransportKeyFromFile(http.DefaultTransport, 1, "2016-10-19.private-key.pem") |
| 85 |
if err != nil { |
| 86 |
// Handle error. |
| 87 |
} |
| 88 |
|
| 89 |
// Use app transport with client |
| 90 |
client := github.NewClient(&http.Client{Transport: atr}) |
| 91 |
|
| 92 |
// Use client... |
| 93 |
} |
| 94 |
|
| 95 |
# Rate Limiting |
| 96 |
|
| 97 |
GitHub imposes a rate limit on all API clients. Unauthenticated clients are |
| 98 |
limited to 60 requests per hour, while authenticated clients can make up to |
| 99 |
5,000 requests per hour. The Search API has a custom rate limit. Unauthenticated |
| 100 |
clients are limited to 10 requests per minute, while authenticated clients |
| 101 |
can make up to 30 requests per minute. To receive the higher rate limit when |
| 102 |
making calls that are not issued on behalf of a user, |
| 103 |
use UnauthenticatedRateLimitedTransport. |
| 104 |
|
| 105 |
The returned Response.Rate value contains the rate limit information |
| 106 |
from the most recent API call. If a recent enough response isn't |
| 107 |
available, you can use RateLimits to fetch the most up-to-date rate |
| 108 |
limit data for the client. |
| 109 |
|
| 110 |
To detect an API rate limit error, you can check if its type is *github.RateLimitError. |
| 111 |
For secondary rate limits, you can check if its type is *github.AbuseRateLimitError: |
| 112 |
|
| 113 |
repos, _, err := client.Repositories.List(ctx, "", nil) |
| 114 |
if _, ok := err.(*github.RateLimitError); ok { |
| 115 |
log.Println("hit rate limit") |
| 116 |
} |
| 117 |
if _, ok := err.(*github.AbuseRateLimitError); ok { |
| 118 |
log.Println("hit secondary rate limit") |
| 119 |
} |
| 120 |
|
| 121 |
Learn more about GitHub rate limiting at |
| 122 |
https://docs.github.com/rest/rate-limit . |
| 123 |
|
| 124 |
# Accepted Status |
| 125 |
|
| 126 |
Some endpoints may return a 202 Accepted status code, meaning that the |
| 127 |
information required is not yet ready and was scheduled to be gathered on |
| 128 |
the GitHub side. Methods known to behave like this are documented specifying |
| 129 |
this behavior. |
| 130 |
|
| 131 |
To detect this condition of error, you can check if its type is |
| 132 |
*github.AcceptedError: |
| 133 |
|
| 134 |
stats, _, err := client.Repositories.ListContributorsStats(ctx, org, repo) |
| 135 |
if _, ok := err.(*github.AcceptedError); ok { |
| 136 |
log.Println("scheduled on GitHub side") |
| 137 |
} |
| 138 |
|
| 139 |
# Conditional Requests |
| 140 |
|
| 141 |
The GitHub API has good support for conditional requests which will help |
| 142 |
prevent you from burning through your rate limit, as well as help speed up your |
| 143 |
application. go-github does not handle conditional requests directly, but is |
| 144 |
instead designed to work with a caching http.Transport. We recommend using |
| 145 |
https://github.com/gregjones/httpcache for that. |
| 146 |
|
| 147 |
Learn more about GitHub conditional requests at |
| 148 |
https://docs.github.com/rest/overview/resources-in-the-rest-api#conditional-requests. |
| 149 |
|
| 150 |
# Creating and Updating Resources |
| 151 |
|
| 152 |
All structs for GitHub resources use pointer values for all non-repeated fields. |
| 153 |
This allows distinguishing between unset fields and those set to a zero-value. |
| 154 |
Helper functions have been provided to easily create these pointers for string, |
| 155 |
bool, and int values. For example: |
| 156 |
|
| 157 |
// create a new private repository named "foo" |
| 158 |
repo := &github.Repository{ |
| 159 |
Name: github.String("foo"), |
| 160 |
Private: github.Bool(true), |
| 161 |
} |
| 162 |
client.Repositories.Create(ctx, "", repo) |
| 163 |
|
| 164 |
Users who have worked with protocol buffers should find this pattern familiar. |
| 165 |
|
| 166 |
# Pagination |
| 167 |
|
| 168 |
All requests for resource collections (repos, pull requests, issues, etc.) |
| 169 |
support pagination. Pagination options are described in the |
| 170 |
github.ListOptions struct and passed to the list methods directly or as an |
| 171 |
embedded type of a more specific list options struct (for example |
| 172 |
github.PullRequestListOptions). Pages information is available via the |
| 173 |
github.Response struct. |
| 174 |
|
| 175 |
client := github.NewClient(nil) |
| 176 |
|
| 177 |
opt := &github.RepositoryListByOrgOptions{ |
| 178 |
ListOptions: github.ListOptions{PerPage: 10}, |
| 179 |
} |
| 180 |
// get all pages of results |
| 181 |
var allRepos []*github.Repository |
| 182 |
for { |
| 183 |
repos, resp, err := client.Repositories.ListByOrg(ctx, "github", opt) |
| 184 |
if err != nil { |
| 185 |
return err |
| 186 |
} |
| 187 |
allRepos = append(allRepos, repos...) |
| 188 |
if resp.NextPage == 0 { |
| 189 |
break |
| 190 |
} |
| 191 |
opt.Page = resp.NextPage |
| 192 |
} |
| 193 |
*/ |
| 194 |
package github |