...

Source file src/github.com/google/go-github/v33/github/doc.go

Documentation: github.com/google/go-github/v33/github

     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/v33/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/free-pro-team@latest/rest/reference/.
    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  
    77  	import "github.com/bradleyfalzon/ghinstallation"
    78  
    79  	func main() {
    80  		// Wrap the shared transport for use with the integration ID 1 authenticating with installation ID 99.
    81  		itr, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, 1, 99, "2016-10-19.private-key.pem")
    82  		if err != nil {
    83  			// Handle error.
    84  		}
    85  
    86  		// Use installation transport with client
    87  		client := github.NewClient(&http.Client{Transport: itr})
    88  
    89  		// Use client...
    90  	}
    91  
    92  Rate Limiting
    93  
    94  GitHub imposes a rate limit on all API clients. Unauthenticated clients are
    95  limited to 60 requests per hour, while authenticated clients can make up to
    96  5,000 requests per hour. The Search API has a custom rate limit. Unauthenticated
    97  clients are limited to 10 requests per minute, while authenticated clients
    98  can make up to 30 requests per minute. To receive the higher rate limit when
    99  making calls that are not issued on behalf of a user,
   100  use UnauthenticatedRateLimitedTransport.
   101  
   102  The returned Response.Rate value contains the rate limit information
   103  from the most recent API call. If a recent enough response isn't
   104  available, you can use RateLimits to fetch the most up-to-date rate
   105  limit data for the client.
   106  
   107  To detect an API rate limit error, you can check if its type is *github.RateLimitError:
   108  
   109  	repos, _, err := client.Repositories.List(ctx, "", nil)
   110  	if _, ok := err.(*github.RateLimitError); ok {
   111  		log.Println("hit rate limit")
   112  	}
   113  
   114  Learn more about GitHub rate limiting at
   115  https://docs.github.com/en/free-pro-team@latest/rest/reference/#rate-limiting.
   116  
   117  Accepted Status
   118  
   119  Some endpoints may return a 202 Accepted status code, meaning that the
   120  information required is not yet ready and was scheduled to be gathered on
   121  the GitHub side. Methods known to behave like this are documented specifying
   122  this behavior.
   123  
   124  To detect this condition of error, you can check if its type is
   125  *github.AcceptedError:
   126  
   127  	stats, _, err := client.Repositories.ListContributorsStats(ctx, org, repo)
   128  	if _, ok := err.(*github.AcceptedError); ok {
   129  		log.Println("scheduled on GitHub side")
   130  	}
   131  
   132  Conditional Requests
   133  
   134  The GitHub API has good support for conditional requests which will help
   135  prevent you from burning through your rate limit, as well as help speed up your
   136  application. go-github does not handle conditional requests directly, but is
   137  instead designed to work with a caching http.Transport. We recommend using
   138  https://github.com/gregjones/httpcache for that.
   139  
   140  Learn more about GitHub conditional requests at
   141  https://docs.github.com/en/free-pro-team@latest/rest/reference/#conditional-requests.
   142  
   143  Creating and Updating Resources
   144  
   145  All structs for GitHub resources use pointer values for all non-repeated fields.
   146  This allows distinguishing between unset fields and those set to a zero-value.
   147  Helper functions have been provided to easily create these pointers for string,
   148  bool, and int values. For example:
   149  
   150  	// create a new private repository named "foo"
   151  	repo := &github.Repository{
   152  		Name:    github.String("foo"),
   153  		Private: github.Bool(true),
   154  	}
   155  	client.Repositories.Create(ctx, "", repo)
   156  
   157  Users who have worked with protocol buffers should find this pattern familiar.
   158  
   159  Pagination
   160  
   161  All requests for resource collections (repos, pull requests, issues, etc.)
   162  support pagination. Pagination options are described in the
   163  github.ListOptions struct and passed to the list methods directly or as an
   164  embedded type of a more specific list options struct (for example
   165  github.PullRequestListOptions). Pages information is available via the
   166  github.Response struct.
   167  
   168  	client := github.NewClient(nil)
   169  
   170  	opt := &github.RepositoryListByOrgOptions{
   171  		ListOptions: github.ListOptions{PerPage: 10},
   172  	}
   173  	// get all pages of results
   174  	var allRepos []*github.Repository
   175  	for {
   176  		repos, resp, err := client.Repositories.ListByOrg(ctx, "github", opt)
   177  		if err != nil {
   178  			return err
   179  		}
   180  		allRepos = append(allRepos, repos...)
   181  		if resp.NextPage == 0 {
   182  			break
   183  		}
   184  		opt.Page = resp.NextPage
   185  	}
   186  
   187  */
   188  package github
   189  

View as plain text