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/v45/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/en/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 The go-github library does not directly handle authentication. Instead, when 44 creating a new client, pass an http.Client that can handle authentication for 45 you. The easiest and recommended way to do this is using the golang.org/x/oauth2 46 library, but you can always use any other library that provides an http.Client. 47 If you have an OAuth2 access token (for example, a personal API token), you can 48 use it with the oauth2 library using: 49 50 import "golang.org/x/oauth2" 51 52 func main() { 53 ctx := context.Background() 54 ts := oauth2.StaticTokenSource( 55 &oauth2.Token{AccessToken: "... your access token ..."}, 56 ) 57 tc := oauth2.NewClient(ctx, ts) 58 59 client := github.NewClient(tc) 60 61 // list all repositories for the authenticated user 62 repos, _, err := client.Repositories.List(ctx, "", nil) 63 } 64 65 Note that when using an authenticated Client, all calls made by the client will 66 include the specified OAuth token. Therefore, authenticated clients should 67 almost never be shared between different users. 68 69 See the oauth2 docs for complete instructions on using that library. 70 71 For API methods that require HTTP Basic Authentication, use the 72 BasicAuthTransport. 73 74 GitHub Apps authentication can be provided by the 75 https://github.com/bradleyfalzon/ghinstallation package. 76 It supports both authentication as an installation, using an installation access token, 77 and as an app, using a JWT. 78 79 To authenticate as an installation: 80 81 import "github.com/bradleyfalzon/ghinstallation" 82 83 func main() { 84 // Wrap the shared transport for use with the integration ID 1 authenticating with installation ID 99. 85 itr, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, 1, 99, "2016-10-19.private-key.pem") 86 if err != nil { 87 // Handle error. 88 } 89 90 // Use installation transport with client 91 client := github.NewClient(&http.Client{Transport: itr}) 92 93 // Use client... 94 } 95 96 To authenticate as an app, using a JWT: 97 98 import "github.com/bradleyfalzon/ghinstallation" 99 100 func main() { 101 // Wrap the shared transport for use with the application ID 1. 102 atr, err := ghinstallation.NewAppsTransportKeyFromFile(http.DefaultTransport, 1, "2016-10-19.private-key.pem") 103 if err != nil { 104 // Handle error. 105 } 106 107 // Use app transport with client 108 client := github.NewClient(&http.Client{Transport: atr}) 109 110 // Use client... 111 } 112 113 Rate Limiting 114 115 GitHub imposes a rate limit on all API clients. Unauthenticated clients are 116 limited to 60 requests per hour, while authenticated clients can make up to 117 5,000 requests per hour. The Search API has a custom rate limit. Unauthenticated 118 clients are limited to 10 requests per minute, while authenticated clients 119 can make up to 30 requests per minute. To receive the higher rate limit when 120 making calls that are not issued on behalf of a user, 121 use UnauthenticatedRateLimitedTransport. 122 123 The returned Response.Rate value contains the rate limit information 124 from the most recent API call. If a recent enough response isn't 125 available, you can use RateLimits to fetch the most up-to-date rate 126 limit data for the client. 127 128 To detect an API rate limit error, you can check if its type is *github.RateLimitError. 129 For secondary rate limits, you can check if its type is *github.AbuseRateLimitError: 130 131 repos, _, err := client.Repositories.List(ctx, "", nil) 132 if _, ok := err.(*github.RateLimitError); ok { 133 log.Println("hit rate limit") 134 } 135 if _, ok := err.(*github.AbuseRateLimitError); ok { 136 log.Println("hit secondary rate limit") 137 } 138 139 Learn more about GitHub rate limiting at 140 https://docs.github.com/en/rest/rate-limit . 141 142 Accepted Status 143 144 Some endpoints may return a 202 Accepted status code, meaning that the 145 information required is not yet ready and was scheduled to be gathered on 146 the GitHub side. Methods known to behave like this are documented specifying 147 this behavior. 148 149 To detect this condition of error, you can check if its type is 150 *github.AcceptedError: 151 152 stats, _, err := client.Repositories.ListContributorsStats(ctx, org, repo) 153 if _, ok := err.(*github.AcceptedError); ok { 154 log.Println("scheduled on GitHub side") 155 } 156 157 Conditional Requests 158 159 The GitHub API has good support for conditional requests which will help 160 prevent you from burning through your rate limit, as well as help speed up your 161 application. go-github does not handle conditional requests directly, but is 162 instead designed to work with a caching http.Transport. We recommend using 163 https://github.com/gregjones/httpcache for that. 164 165 Learn more about GitHub conditional requests at 166 https://docs.github.com/en/rest/overview/resources-in-the-rest-api#conditional-requests. 167 168 Creating and Updating Resources 169 170 All structs for GitHub resources use pointer values for all non-repeated fields. 171 This allows distinguishing between unset fields and those set to a zero-value. 172 Helper functions have been provided to easily create these pointers for string, 173 bool, and int values. For example: 174 175 // create a new private repository named "foo" 176 repo := &github.Repository{ 177 Name: github.String("foo"), 178 Private: github.Bool(true), 179 } 180 client.Repositories.Create(ctx, "", repo) 181 182 Users who have worked with protocol buffers should find this pattern familiar. 183 184 Pagination 185 186 All requests for resource collections (repos, pull requests, issues, etc.) 187 support pagination. Pagination options are described in the 188 github.ListOptions struct and passed to the list methods directly or as an 189 embedded type of a more specific list options struct (for example 190 github.PullRequestListOptions). Pages information is available via the 191 github.Response struct. 192 193 client := github.NewClient(nil) 194 195 opt := &github.RepositoryListByOrgOptions{ 196 ListOptions: github.ListOptions{PerPage: 10}, 197 } 198 // get all pages of results 199 var allRepos []*github.Repository 200 for { 201 repos, resp, err := client.Repositories.ListByOrg(ctx, "github", opt) 202 if err != nil { 203 return err 204 } 205 allRepos = append(allRepos, repos...) 206 if resp.NextPage == 0 { 207 break 208 } 209 opt.Page = resp.NextPage 210 } 211 212 */ 213 package github 214