本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
6.5KB

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