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

..03-May-2022-

example/H10-Aug-2018-

github/H10-Aug-2018-

test/H10-Aug-2018-

.gitignoreH A D10-Aug-201820

.travis.ymlH A D10-Aug-20181.3 KiB

AUTHORSH A D10-Aug-20186.3 KiB

CONTRIBUTING.mdH A D10-Aug-20185.5 KiB

LICENSEH A D10-Aug-20181.5 KiB

README.mdH A D10-Aug-20189.7 KiB

README.md

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