github.com/google/go-github/v70@v70.0.0/github/doc.go (about)

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