• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

example/H25-Jan-2018-

github/H25-Jan-2018-

test/H25-Jan-2018-

.gitignoreH A D25-Jan-201820

.travis.ymlH A D25-Jan-20181.3 KiB

AUTHORSH A D25-Jan-20186.2 KiB

CONTRIBUTING.mdH A D25-Jan-20185.5 KiB

LICENSEH A D25-Jan-201820.3 KiB

README.mdH A D25-Jan-20188.5 KiB

README.md

1# go-github #
2
3go-github is a Go client library for accessing the [GitHub API v3][].
4
5**Documentation:** [![GoDoc](https://godoc.org/github.com/google/go-github/github?status.svg)](https://godoc.org/github.com/google/go-github/github)
6**Mailing List:** [go-github@googlegroups.com](https://groups.google.com/group/go-github)
7**Build Status:** [![Build Status](https://travis-ci.org/google/go-github.svg?branch=master)](https://travis-ci.org/google/go-github)
8**Test Coverage:** [![Test Coverage](https://coveralls.io/repos/google/go-github/badge.svg?branch=master)](https://coveralls.io/r/google/go-github?branch=master)
9
10go-github requires Go version 1.7 or greater.
11
12If you're interested in using the [GraphQL API v4][], the recommended library is
13[shurcooL/githubql][].
14
15## Usage ##
16
17```go
18import "github.com/google/go-github/github"
19```
20
21Construct a new GitHub client, then use the various services on the client to
22access different parts of the GitHub API. For example:
23
24```go
25client := github.NewClient(nil)
26
27// list all organizations for user "willnorris"
28orgs, _, err := client.Organizations.List(ctx, "willnorris", nil)
29```
30
31Some API methods have optional parameters that can be passed. For example:
32
33```go
34client := github.NewClient(nil)
35
36// list public repositories for org "github"
37opt := &github.RepositoryListByOrgOptions{Type: "public"}
38repos, _, err := client.Repositories.ListByOrg(ctx, "github", opt)
39```
40
41The services of a client divide the API into logical chunks and correspond to
42the structure of the GitHub API documentation at
43https://developer.github.com/v3/.
44
45### Authentication ###
46
47The go-github library does not directly handle authentication. Instead, when
48creating a new client, pass an `http.Client` that can handle authentication for
49you. The easiest and recommended way to do this is using the [oauth2][]
50library, but you can always use any other library that provides an
51`http.Client`. If you have an OAuth2 access token (for example, a [personal
52API token][]), you can use it with the oauth2 library using:
53
54```go
55import "golang.org/x/oauth2"
56
57func main() {
58	ctx := context.Background()
59	ts := oauth2.StaticTokenSource(
60		&oauth2.Token{AccessToken: "... your access token ..."},
61	)
62	tc := oauth2.NewClient(ctx, ts)
63
64	client := github.NewClient(tc)
65
66	// list all repositories for the authenticated user
67	repos, _, err := client.Repositories.List(ctx, "", nil)
68}
69```
70
71Note that when using an authenticated Client, all calls made by the client will
72include the specified OAuth token. Therefore, authenticated clients should
73almost never be shared between different users.
74
75See the [oauth2 docs][] for complete instructions on using that library.
76
77For API methods that require HTTP Basic Authentication, use the
78[`BasicAuthTransport`](https://godoc.org/github.com/google/go-github/github#BasicAuthTransport).
79
80GitHub Apps authentication can be provided by the [ghinstallation](https://github.com/bradleyfalzon/ghinstallation)
81package.
82
83```go
84import "github.com/bradleyfalzon/ghinstallation"
85
86func main() {
87	// Wrap the shared transport for use with the integration ID 1 authenticating with installation ID 99.
88	itr, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, 1, 99, "2016-10-19.private-key.pem")
89	if err != nil {
90		// Handle error.
91	}
92
93	// Use installation transport with client.
94	client := github.NewClient(&http.Client{Transport: itr})
95
96	// Use client...
97}
98```
99
100### Rate Limiting ###
101
102GitHub imposes a rate limit on all API clients. Unauthenticated clients are
103limited to 60 requests per hour, while authenticated clients can make up to
1045,000 requests per hour. The Search API has a custom rate limit. Unauthenticated
105clients are limited to 10 requests per minute, while authenticated clients
106can make up to 30 requests per minute. To receive the higher rate limit when
107making calls that are not issued on behalf of a user,
108use `UnauthenticatedRateLimitedTransport`.
109
110The returned `Response.Rate` value contains the rate limit information
111from the most recent API call. If a recent enough response isn't
112available, you can use `RateLimits` to fetch the most up-to-date rate
113limit data for the client.
114
115To detect an API rate limit error, you can check if its type is `*github.RateLimitError`:
116
117```go
118repos, _, err := client.Repositories.List(ctx, "", nil)
119if _, ok := err.(*github.RateLimitError); ok {
120	log.Println("hit rate limit")
121}
122```
123
124Learn more about GitHub rate limiting at
125https://developer.github.com/v3/#rate-limiting.
126
127### Accepted Status ###
128
129Some endpoints may return a 202 Accepted status code, meaning that the
130information required is not yet ready and was scheduled to be gathered on
131the GitHub side. Methods known to behave like this are documented specifying
132this behavior.
133
134To detect this condition of error, you can check if its type is
135`*github.AcceptedError`:
136
137```go
138stats, _, err := client.Repositories.ListContributorsStats(ctx, org, repo)
139if _, ok := err.(*github.AcceptedError); ok {
140	log.Println("scheduled on GitHub side")
141}
142```
143
144### Conditional Requests ###
145
146The GitHub API has good support for conditional requests which will help
147prevent you from burning through your rate limit, as well as help speed up your
148application. `go-github` does not handle conditional requests directly, but is
149instead designed to work with a caching `http.Transport`. We recommend using
150https://github.com/gregjones/httpcache for that.
151
152Learn more about GitHub conditional requests at
153https://developer.github.com/v3/#conditional-requests.
154
155### Creating and Updating Resources ###
156
157All structs for GitHub resources use pointer values for all non-repeated fields.
158This allows distinguishing between unset fields and those set to a zero-value.
159Helper functions have been provided to easily create these pointers for string,
160bool, and int values. For example:
161
162```go
163// create a new private repository named "foo"
164repo := &github.Repository{
165	Name:    github.String("foo"),
166	Private: github.Bool(true),
167}
168client.Repositories.Create(ctx, "", repo)
169```
170
171Users who have worked with protocol buffers should find this pattern familiar.
172
173### Pagination ###
174
175All requests for resource collections (repos, pull requests, issues, etc.)
176support pagination. Pagination options are described in the
177`github.ListOptions` struct and passed to the list methods directly or as an
178embedded type of a more specific list options struct (for example
179`github.PullRequestListOptions`). Pages information is available via the
180`github.Response` struct.
181
182```go
183client := github.NewClient(nil)
184
185opt := &github.RepositoryListByOrgOptions{
186	ListOptions: github.ListOptions{PerPage: 10},
187}
188// get all pages of results
189var allRepos []*github.Repository
190for {
191	repos, resp, err := client.Repositories.ListByOrg(ctx, "github", opt)
192	if err != nil {
193		return err
194	}
195	allRepos = append(allRepos, repos...)
196	if resp.NextPage == 0 {
197		break
198	}
199	opt.Page = resp.NextPage
200}
201```
202
203For complete usage of go-github, see the full [package docs][].
204
205[GitHub API v3]: https://developer.github.com/v3/
206[oauth2]: https://github.com/golang/oauth2
207[oauth2 docs]: https://godoc.org/golang.org/x/oauth2
208[personal API token]: https://github.com/blog/1509-personal-api-tokens
209[package docs]: https://godoc.org/github.com/google/go-github/github
210[GraphQL API v4]: https://developer.github.com/v4/
211[shurcooL/githubql]: https://github.com/shurcooL/githubql
212
213### Google App Engine ###
214
215Go on App Engine Classic (which as of this writing uses Go 1.6) can not use
216the `"context"` import and still relies on `"golang.org/x/net/context"`.
217As a result, if you wish to continue to use `go-github` on App Engine Classic,
218you will need to rewrite all the `"context"` imports using the following command:
219
220	gofmt -w -r '"context" -> "golang.org/x/net/context"' *.go
221
222See `with_appengine.go` for more details.
223
224### Integration Tests ###
225
226You can run integration tests from the `test` directory. See the integration tests [README](test/README.md).
227
228## Roadmap ##
229
230This library is being initially developed for an internal application at
231Google, so API methods will likely be implemented in the order that they are
232needed by that application. You can track the status of implementation in
233[this Google spreadsheet][roadmap]. Eventually, I would like to cover the entire
234GitHub API, so contributions are of course [always welcome][contributing]. The
235calling pattern is pretty well established, so adding new methods is relatively
236straightforward.
237
238[roadmap]: https://docs.google.com/spreadsheet/ccc?key=0ApoVX4GOiXr-dGNKN1pObFh6ek1DR2FKUjBNZ1FmaEE&usp=sharing
239[contributing]: CONTRIBUTING.md
240
241## License ##
242
243This library is distributed under the BSD-style license found in the [LICENSE](./LICENSE)
244file.
245