doc.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. Package github provides a client for using the GitHub API.
  7. Usage:
  8. import "github.com/google/go-github/github"
  9. Construct a new GitHub client, then use the various services on the client to
  10. access different parts of the GitHub API. For example:
  11. client := github.NewClient(nil)
  12. // list all organizations for user "willnorris"
  13. orgs, _, err := client.Organizations.List(ctx, "willnorris", nil)
  14. Some API methods have optional parameters that can be passed. For example:
  15. client := github.NewClient(nil)
  16. // list public repositories for org "github"
  17. opt := &github.RepositoryListByOrgOptions{Type: "public"}
  18. repos, _, err := client.Repositories.ListByOrg(ctx, "github", opt)
  19. The services of a client divide the API into logical chunks and correspond to
  20. the structure of the GitHub API documentation at
  21. https://developer.github.com/v3/.
  22. Authentication
  23. The go-github library does not directly handle authentication. Instead, when
  24. creating a new client, pass an http.Client that can handle authentication for
  25. you. The easiest and recommended way to do this is using the golang.org/x/oauth2
  26. library, but you can always use any other library that provides an http.Client.
  27. If you have an OAuth2 access token (for example, a personal API token), you can
  28. use it with the oauth2 library using:
  29. import "golang.org/x/oauth2"
  30. func main() {
  31. ctx := context.Background()
  32. ts := oauth2.StaticTokenSource(
  33. &oauth2.Token{AccessToken: "... your access token ..."},
  34. )
  35. tc := oauth2.NewClient(ctx, ts)
  36. client := github.NewClient(tc)
  37. // list all repositories for the authenticated user
  38. repos, _, err := client.Repositories.List(ctx, "", nil)
  39. }
  40. Note that when using an authenticated Client, all calls made by the client will
  41. include the specified OAuth token. Therefore, authenticated clients should
  42. almost never be shared between different users.
  43. See the oauth2 docs for complete instructions on using that library.
  44. For API methods that require HTTP Basic Authentication, use the
  45. BasicAuthTransport.
  46. GitHub Apps authentication can be provided by the
  47. https://github.com/bradleyfalzon/ghinstallation package.
  48. import "github.com/bradleyfalzon/ghinstallation"
  49. func main() {
  50. // Wrap the shared transport for use with the integration ID 1 authenticating with installation ID 99.
  51. itr, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, 1, 99, "2016-10-19.private-key.pem")
  52. if err != nil {
  53. // Handle error.
  54. }
  55. // Use installation transport with client
  56. client := github.NewClient(&http.Client{Transport: itr})
  57. // Use client...
  58. }
  59. Rate Limiting
  60. GitHub imposes a rate limit on all API clients. Unauthenticated clients are
  61. limited to 60 requests per hour, while authenticated clients can make up to
  62. 5,000 requests per hour. The Search API has a custom rate limit. Unauthenticated
  63. clients are limited to 10 requests per minute, while authenticated clients
  64. can make up to 30 requests per minute. To receive the higher rate limit when
  65. making calls that are not issued on behalf of a user,
  66. use UnauthenticatedRateLimitedTransport.
  67. The returned Response.Rate value contains the rate limit information
  68. from the most recent API call. If a recent enough response isn't
  69. available, you can use RateLimits to fetch the most up-to-date rate
  70. limit data for the client.
  71. To detect an API rate limit error, you can check if its type is *github.RateLimitError:
  72. repos, _, err := client.Repositories.List(ctx, "", nil)
  73. if _, ok := err.(*github.RateLimitError); ok {
  74. log.Println("hit rate limit")
  75. }
  76. Learn more about GitHub rate limiting at
  77. https://developer.github.com/v3/#rate-limiting.
  78. Accepted Status
  79. Some endpoints may return a 202 Accepted status code, meaning that the
  80. information required is not yet ready and was scheduled to be gathered on
  81. the GitHub side. Methods known to behave like this are documented specifying
  82. this behavior.
  83. To detect this condition of error, you can check if its type is
  84. *github.AcceptedError:
  85. stats, _, err := client.Repositories.ListContributorsStats(ctx, org, repo)
  86. if _, ok := err.(*github.AcceptedError); ok {
  87. log.Println("scheduled on GitHub side")
  88. }
  89. Conditional Requests
  90. The GitHub API has good support for conditional requests which will help
  91. prevent you from burning through your rate limit, as well as help speed up your
  92. application. go-github does not handle conditional requests directly, but is
  93. instead designed to work with a caching http.Transport. We recommend using
  94. https://github.com/gregjones/httpcache for that.
  95. Learn more about GitHub conditional requests at
  96. https://developer.github.com/v3/#conditional-requests.
  97. Creating and Updating Resources
  98. All structs for GitHub resources use pointer values for all non-repeated fields.
  99. This allows distinguishing between unset fields and those set to a zero-value.
  100. Helper functions have been provided to easily create these pointers for string,
  101. bool, and int values. For example:
  102. // create a new private repository named "foo"
  103. repo := &github.Repository{
  104. Name: github.String("foo"),
  105. Private: github.Bool(true),
  106. }
  107. client.Repositories.Create(ctx, "", repo)
  108. Users who have worked with protocol buffers should find this pattern familiar.
  109. Pagination
  110. All requests for resource collections (repos, pull requests, issues, etc.)
  111. support pagination. Pagination options are described in the
  112. github.ListOptions struct and passed to the list methods directly or as an
  113. embedded type of a more specific list options struct (for example
  114. github.PullRequestListOptions). Pages information is available via the
  115. github.Response struct.
  116. client := github.NewClient(nil)
  117. opt := &github.RepositoryListByOrgOptions{
  118. ListOptions: github.ListOptions{PerPage: 10},
  119. }
  120. // get all pages of results
  121. var allRepos []*github.Repository
  122. for {
  123. repos, resp, err := client.Repositories.ListByOrg(ctx, "github", opt)
  124. if err != nil {
  125. return err
  126. }
  127. allRepos = append(allRepos, repos...)
  128. if resp.NextPage == 0 {
  129. break
  130. }
  131. opt.Page = resp.NextPage
  132. }
  133. Google App Engine
  134. Go on App Engine Classic (which as of this writing uses Go 1.6) can not use
  135. the "context" import and still relies on "golang.org/x/net/context".
  136. As a result, if you wish to continue to use "go-github" on App Engine Classic,
  137. you will need to rewrite all the "context" imports using the following command:
  138. gofmt -w -r '"context" -> "golang.org/x/net/context"' *.go
  139. See "with_appengine.go" for more details.
  140. */
  141. package github