123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989 |
- package github
- import (
- "bytes"
- "context"
- "encoding/json"
- "errors"
- "fmt"
- "io"
- "io/ioutil"
- "net/http"
- "net/url"
- "reflect"
- "strconv"
- "strings"
- "sync"
- "time"
- "github.com/google/go-querystring/query"
- )
- const (
- defaultBaseURL = "https://api.github.com/"
- uploadBaseURL = "https://uploads.github.com/"
- userAgent = "go-github"
- headerRateLimit = "X-RateLimit-Limit"
- headerRateRemaining = "X-RateLimit-Remaining"
- headerRateReset = "X-RateLimit-Reset"
- headerOTP = "X-GitHub-OTP"
- mediaTypeV3 = "application/vnd.github.v3+json"
- defaultMediaType = "application/octet-stream"
- mediaTypeV3SHA = "application/vnd.github.v3.sha"
- mediaTypeV3Diff = "application/vnd.github.v3.diff"
- mediaTypeV3Patch = "application/vnd.github.v3.patch"
- mediaTypeOrgPermissionRepo = "application/vnd.github.v3.repository+json"
-
-
- mediaTypeLicensesPreview = "application/vnd.github.drax-preview+json"
-
- mediaTypeStarringPreview = "application/vnd.github.v3.star+json"
-
- mediaTypeProtectedBranchesPreview = "application/vnd.github.loki-preview+json"
-
- mediaTypeMigrationsPreview = "application/vnd.github.wyandotte-preview+json"
-
- mediaTypeDeploymentStatusPreview = "application/vnd.github.ant-man-preview+json"
-
- mediaTypeImportPreview = "application/vnd.github.barred-rock-preview"
-
- mediaTypeReactionsPreview = "application/vnd.github.squirrel-girl-preview"
-
- mediaTypeGitSigningPreview = "application/vnd.github.cryptographer-preview+json"
-
- mediaTypeTimelinePreview = "application/vnd.github.mockingbird-preview+json"
-
- mediaTypeRepositoryInvitationsPreview = "application/vnd.github.swamp-thing-preview+json"
-
- mediaTypePagesPreview = "application/vnd.github.mister-fantastic-preview+json"
-
- mediaTypeProjectsPreview = "application/vnd.github.inertia-preview+json"
-
- mediaTypeIntegrationPreview = "application/vnd.github.machine-man-preview+json"
-
- mediaTypeCommitSearchPreview = "application/vnd.github.cloak-preview+json"
-
- mediaTypeBlockUsersPreview = "application/vnd.github.giant-sentry-fist-preview+json"
-
- mediaTypeRepositoryCommunityHealthMetricsPreview = "application/vnd.github.black-panther-preview+json"
-
- mediaTypeCodesOfConductPreview = "application/vnd.github.scarlet-witch-preview+json"
-
- mediaTypeTopicsPreview = "application/vnd.github.mercy-preview+json"
-
- mediaTypeMarketplacePreview = "application/vnd.github.valkyrie-preview+json"
-
- mediaTypeNestedTeamsPreview = "application/vnd.github.hellcat-preview+json"
-
- mediaTypeRepositoryTransferPreview = "application/vnd.github.nightshade-preview+json"
-
- mediaTypeGraphQLNodeIDPreview = "application/vnd.github.jean-grey-preview+json"
-
- mediaTypeOrganizationInvitationPreview = "application/vnd.github.dazzler-preview+json"
-
- mediaTypeLabelDescriptionSearchPreview = "application/vnd.github.symmetra-preview+json"
-
- mediaTypeTeamDiscussionsPreview = "application/vnd.github.echo-preview+json"
- )
- type Client struct {
- clientMu sync.Mutex
- client *http.Client
-
-
-
- BaseURL *url.URL
-
- UploadURL *url.URL
-
- UserAgent string
- rateMu sync.Mutex
- rateLimits [categories]Rate
- common service
-
- Activity *ActivityService
- Admin *AdminService
- Apps *AppsService
- Authorizations *AuthorizationsService
- Gists *GistsService
- Git *GitService
- Gitignores *GitignoresService
- Issues *IssuesService
- Licenses *LicensesService
- Marketplace *MarketplaceService
- Migrations *MigrationService
- Organizations *OrganizationsService
- Projects *ProjectsService
- PullRequests *PullRequestsService
- Reactions *ReactionsService
- Repositories *RepositoriesService
- Search *SearchService
- Teams *TeamsService
- Users *UsersService
- }
- type service struct {
- client *Client
- }
- type ListOptions struct {
-
- Page int `url:"page,omitempty"`
-
- PerPage int `url:"per_page,omitempty"`
- }
- type UploadOptions struct {
- Name string `url:"name,omitempty"`
- }
- type RawType uint8
- const (
-
- Diff RawType = 1 + iota
-
- Patch
- )
- type RawOptions struct {
- Type RawType
- }
- func addOptions(s string, opt interface{}) (string, error) {
- v := reflect.ValueOf(opt)
- if v.Kind() == reflect.Ptr && v.IsNil() {
- return s, nil
- }
- u, err := url.Parse(s)
- if err != nil {
- return s, err
- }
- qs, err := query.Values(opt)
- if err != nil {
- return s, err
- }
- u.RawQuery = qs.Encode()
- return u.String(), nil
- }
- func NewClient(httpClient *http.Client) *Client {
- if httpClient == nil {
- httpClient = http.DefaultClient
- }
- baseURL, _ := url.Parse(defaultBaseURL)
- uploadURL, _ := url.Parse(uploadBaseURL)
- c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: userAgent, UploadURL: uploadURL}
- c.common.client = c
- c.Activity = (*ActivityService)(&c.common)
- c.Admin = (*AdminService)(&c.common)
- c.Apps = (*AppsService)(&c.common)
- c.Authorizations = (*AuthorizationsService)(&c.common)
- c.Gists = (*GistsService)(&c.common)
- c.Git = (*GitService)(&c.common)
- c.Gitignores = (*GitignoresService)(&c.common)
- c.Issues = (*IssuesService)(&c.common)
- c.Licenses = (*LicensesService)(&c.common)
- c.Marketplace = &MarketplaceService{client: c}
- c.Migrations = (*MigrationService)(&c.common)
- c.Organizations = (*OrganizationsService)(&c.common)
- c.Projects = (*ProjectsService)(&c.common)
- c.PullRequests = (*PullRequestsService)(&c.common)
- c.Reactions = (*ReactionsService)(&c.common)
- c.Repositories = (*RepositoriesService)(&c.common)
- c.Search = (*SearchService)(&c.common)
- c.Teams = (*TeamsService)(&c.common)
- c.Users = (*UsersService)(&c.common)
- return c
- }
- func NewEnterpriseClient(baseURL, uploadURL string, httpClient *http.Client) (*Client, error) {
- baseEndpoint, err := url.Parse(baseURL)
- if err != nil {
- return nil, err
- }
- if !strings.HasSuffix(baseEndpoint.Path, "/") {
- baseEndpoint.Path += "/"
- }
- uploadEndpoint, err := url.Parse(uploadURL)
- if err != nil {
- return nil, err
- }
- if !strings.HasSuffix(uploadEndpoint.Path, "/") {
- uploadEndpoint.Path += "/"
- }
- c := NewClient(httpClient)
- c.BaseURL = baseEndpoint
- c.UploadURL = uploadEndpoint
- return c, nil
- }
- func (c *Client) NewRequest(method, urlStr string, body interface{}) (*http.Request, error) {
- if !strings.HasSuffix(c.BaseURL.Path, "/") {
- return nil, fmt.Errorf("BaseURL must have a trailing slash, but %q does not", c.BaseURL)
- }
- u, err := c.BaseURL.Parse(urlStr)
- if err != nil {
- return nil, err
- }
- var buf io.ReadWriter
- if body != nil {
- buf = new(bytes.Buffer)
- enc := json.NewEncoder(buf)
- enc.SetEscapeHTML(false)
- err := enc.Encode(body)
- if err != nil {
- return nil, err
- }
- }
- req, err := http.NewRequest(method, u.String(), buf)
- if err != nil {
- return nil, err
- }
- if body != nil {
- req.Header.Set("Content-Type", "application/json")
- }
- req.Header.Set("Accept", mediaTypeV3)
- if c.UserAgent != "" {
- req.Header.Set("User-Agent", c.UserAgent)
- }
- return req, nil
- }
- func (c *Client) NewUploadRequest(urlStr string, reader io.Reader, size int64, mediaType string) (*http.Request, error) {
- if !strings.HasSuffix(c.UploadURL.Path, "/") {
- return nil, fmt.Errorf("UploadURL must have a trailing slash, but %q does not", c.UploadURL)
- }
- u, err := c.UploadURL.Parse(urlStr)
- if err != nil {
- return nil, err
- }
- req, err := http.NewRequest("POST", u.String(), reader)
- if err != nil {
- return nil, err
- }
- req.ContentLength = size
- if mediaType == "" {
- mediaType = defaultMediaType
- }
- req.Header.Set("Content-Type", mediaType)
- req.Header.Set("Accept", mediaTypeV3)
- req.Header.Set("User-Agent", c.UserAgent)
- return req, nil
- }
- type Response struct {
- *http.Response
-
-
-
-
- NextPage int
- PrevPage int
- FirstPage int
- LastPage int
- Rate
- }
- func newResponse(r *http.Response) *Response {
- response := &Response{Response: r}
- response.populatePageValues()
- response.Rate = parseRate(r)
- return response
- }
- func (r *Response) populatePageValues() {
- if links, ok := r.Response.Header["Link"]; ok && len(links) > 0 {
- for _, link := range strings.Split(links[0], ",") {
- segments := strings.Split(strings.TrimSpace(link), ";")
-
- if len(segments) < 2 {
- continue
- }
-
- if !strings.HasPrefix(segments[0], "<") || !strings.HasSuffix(segments[0], ">") {
- continue
- }
-
- url, err := url.Parse(segments[0][1 : len(segments[0])-1])
- if err != nil {
- continue
- }
- page := url.Query().Get("page")
- if page == "" {
- continue
- }
- for _, segment := range segments[1:] {
- switch strings.TrimSpace(segment) {
- case `rel="next"`:
- r.NextPage, _ = strconv.Atoi(page)
- case `rel="prev"`:
- r.PrevPage, _ = strconv.Atoi(page)
- case `rel="first"`:
- r.FirstPage, _ = strconv.Atoi(page)
- case `rel="last"`:
- r.LastPage, _ = strconv.Atoi(page)
- }
- }
- }
- }
- }
- func parseRate(r *http.Response) Rate {
- var rate Rate
- if limit := r.Header.Get(headerRateLimit); limit != "" {
- rate.Limit, _ = strconv.Atoi(limit)
- }
- if remaining := r.Header.Get(headerRateRemaining); remaining != "" {
- rate.Remaining, _ = strconv.Atoi(remaining)
- }
- if reset := r.Header.Get(headerRateReset); reset != "" {
- if v, _ := strconv.ParseInt(reset, 10, 64); v != 0 {
- rate.Reset = Timestamp{time.Unix(v, 0)}
- }
- }
- return rate
- }
- func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Response, error) {
- req = withContext(ctx, req)
- rateLimitCategory := category(req.URL.Path)
-
- if err := c.checkRateLimitBeforeDo(req, rateLimitCategory); err != nil {
- return &Response{
- Response: err.Response,
- Rate: err.Rate,
- }, err
- }
- resp, err := c.client.Do(req)
- if err != nil {
-
-
- select {
- case <-ctx.Done():
- return nil, ctx.Err()
- default:
- }
-
- if e, ok := err.(*url.Error); ok {
- if url, err := url.Parse(e.URL); err == nil {
- e.URL = sanitizeURL(url).String()
- return nil, e
- }
- }
- return nil, err
- }
- defer resp.Body.Close()
- response := newResponse(resp)
- c.rateMu.Lock()
- c.rateLimits[rateLimitCategory] = response.Rate
- c.rateMu.Unlock()
- err = CheckResponse(resp)
- if err != nil {
-
-
-
-
- if _, ok := err.(*AcceptedError); !ok {
- return response, err
- }
- }
- if v != nil {
- if w, ok := v.(io.Writer); ok {
- io.Copy(w, resp.Body)
- } else {
- decErr := json.NewDecoder(resp.Body).Decode(v)
- if decErr == io.EOF {
- decErr = nil
- }
- if decErr != nil {
- err = decErr
- }
- }
- }
- return response, err
- }
- func (c *Client) checkRateLimitBeforeDo(req *http.Request, rateLimitCategory rateLimitCategory) *RateLimitError {
- c.rateMu.Lock()
- rate := c.rateLimits[rateLimitCategory]
- c.rateMu.Unlock()
- if !rate.Reset.Time.IsZero() && rate.Remaining == 0 && time.Now().Before(rate.Reset.Time) {
-
- resp := &http.Response{
- Status: http.StatusText(http.StatusForbidden),
- StatusCode: http.StatusForbidden,
- Request: req,
- Header: make(http.Header),
- Body: ioutil.NopCloser(strings.NewReader("")),
- }
- return &RateLimitError{
- Rate: rate,
- Response: resp,
- Message: fmt.Sprintf("API rate limit of %v still exceeded until %v, not making remote request.", rate.Limit, rate.Reset.Time),
- }
- }
- return nil
- }
- type ErrorResponse struct {
- Response *http.Response
- Message string `json:"message"`
- Errors []Error `json:"errors"`
-
-
-
- Block *struct {
- Reason string `json:"reason,omitempty"`
- CreatedAt *Timestamp `json:"created_at,omitempty"`
- } `json:"block,omitempty"`
-
-
-
- DocumentationURL string `json:"documentation_url,omitempty"`
- }
- func (r *ErrorResponse) Error() string {
- return fmt.Sprintf("%v %v: %d %v %+v",
- r.Response.Request.Method, sanitizeURL(r.Response.Request.URL),
- r.Response.StatusCode, r.Message, r.Errors)
- }
- type TwoFactorAuthError ErrorResponse
- func (r *TwoFactorAuthError) Error() string { return (*ErrorResponse)(r).Error() }
- type RateLimitError struct {
- Rate Rate
- Response *http.Response
- Message string `json:"message"`
- }
- func (r *RateLimitError) Error() string {
- return fmt.Sprintf("%v %v: %d %v %v",
- r.Response.Request.Method, sanitizeURL(r.Response.Request.URL),
- r.Response.StatusCode, r.Message, formatRateReset(r.Rate.Reset.Time.Sub(time.Now())))
- }
- type AcceptedError struct{}
- func (*AcceptedError) Error() string {
- return "job scheduled on GitHub side; try again later"
- }
- type AbuseRateLimitError struct {
- Response *http.Response
- Message string `json:"message"`
-
-
-
- RetryAfter *time.Duration
- }
- func (r *AbuseRateLimitError) Error() string {
- return fmt.Sprintf("%v %v: %d %v",
- r.Response.Request.Method, sanitizeURL(r.Response.Request.URL),
- r.Response.StatusCode, r.Message)
- }
- func sanitizeURL(uri *url.URL) *url.URL {
- if uri == nil {
- return nil
- }
- params := uri.Query()
- if len(params.Get("client_secret")) > 0 {
- params.Set("client_secret", "REDACTED")
- uri.RawQuery = params.Encode()
- }
- return uri
- }
- type Error struct {
- Resource string `json:"resource"`
- Field string `json:"field"`
- Code string `json:"code"`
- Message string `json:"message"`
- }
- func (e *Error) Error() string {
- return fmt.Sprintf("%v error caused by %v field on %v resource",
- e.Code, e.Field, e.Resource)
- }
- func CheckResponse(r *http.Response) error {
- if r.StatusCode == http.StatusAccepted {
- return &AcceptedError{}
- }
- if c := r.StatusCode; 200 <= c && c <= 299 {
- return nil
- }
- errorResponse := &ErrorResponse{Response: r}
- data, err := ioutil.ReadAll(r.Body)
- if err == nil && data != nil {
- json.Unmarshal(data, errorResponse)
- }
- switch {
- case r.StatusCode == http.StatusUnauthorized && strings.HasPrefix(r.Header.Get(headerOTP), "required"):
- return (*TwoFactorAuthError)(errorResponse)
- case r.StatusCode == http.StatusForbidden && r.Header.Get(headerRateRemaining) == "0" && strings.HasPrefix(errorResponse.Message, "API rate limit exceeded for "):
- return &RateLimitError{
- Rate: parseRate(r),
- Response: errorResponse.Response,
- Message: errorResponse.Message,
- }
- case r.StatusCode == http.StatusForbidden && strings.HasSuffix(errorResponse.DocumentationURL, "/v3/#abuse-rate-limits"):
- abuseRateLimitError := &AbuseRateLimitError{
- Response: errorResponse.Response,
- Message: errorResponse.Message,
- }
- if v := r.Header["Retry-After"]; len(v) > 0 {
-
-
-
- retryAfterSeconds, _ := strconv.ParseInt(v[0], 10, 64)
- retryAfter := time.Duration(retryAfterSeconds) * time.Second
- abuseRateLimitError.RetryAfter = &retryAfter
- }
- return abuseRateLimitError
- default:
- return errorResponse
- }
- }
- func parseBoolResponse(err error) (bool, error) {
- if err == nil {
- return true, nil
- }
- if err, ok := err.(*ErrorResponse); ok && err.Response.StatusCode == http.StatusNotFound {
-
- return false, nil
- }
-
- return false, err
- }
- type Rate struct {
-
- Limit int `json:"limit"`
-
- Remaining int `json:"remaining"`
-
- Reset Timestamp `json:"reset"`
- }
- func (r Rate) String() string {
- return Stringify(r)
- }
- type RateLimits struct {
-
-
-
-
-
- Core *Rate `json:"core"`
-
-
-
-
-
- Search *Rate `json:"search"`
- }
- func (r RateLimits) String() string {
- return Stringify(r)
- }
- type rateLimitCategory uint8
- const (
- coreCategory rateLimitCategory = iota
- searchCategory
- categories
- )
- func category(path string) rateLimitCategory {
- switch {
- default:
- return coreCategory
- case strings.HasPrefix(path, "/search/"):
- return searchCategory
- }
- }
- func (c *Client) RateLimits(ctx context.Context) (*RateLimits, *Response, error) {
- req, err := c.NewRequest("GET", "rate_limit", nil)
- if err != nil {
- return nil, nil, err
- }
- response := new(struct {
- Resources *RateLimits `json:"resources"`
- })
- resp, err := c.Do(ctx, req, response)
- if err != nil {
- return nil, nil, err
- }
- if response.Resources != nil {
- c.rateMu.Lock()
- if response.Resources.Core != nil {
- c.rateLimits[coreCategory] = *response.Resources.Core
- }
- if response.Resources.Search != nil {
- c.rateLimits[searchCategory] = *response.Resources.Search
- }
- c.rateMu.Unlock()
- }
- return response.Resources, resp, nil
- }
- type UnauthenticatedRateLimitedTransport struct {
-
-
-
- ClientID string
-
-
- ClientSecret string
-
-
- Transport http.RoundTripper
- }
- func (t *UnauthenticatedRateLimitedTransport) RoundTrip(req *http.Request) (*http.Response, error) {
- if t.ClientID == "" {
- return nil, errors.New("t.ClientID is empty")
- }
- if t.ClientSecret == "" {
- return nil, errors.New("t.ClientSecret is empty")
- }
-
-
-
-
-
-
- req2 := new(http.Request)
- *req2 = *req
- req2.URL = new(url.URL)
- *req2.URL = *req.URL
- q := req2.URL.Query()
- q.Set("client_id", t.ClientID)
- q.Set("client_secret", t.ClientSecret)
- req2.URL.RawQuery = q.Encode()
-
- return t.transport().RoundTrip(req2)
- }
- func (t *UnauthenticatedRateLimitedTransport) Client() *http.Client {
- return &http.Client{Transport: t}
- }
- func (t *UnauthenticatedRateLimitedTransport) transport() http.RoundTripper {
- if t.Transport != nil {
- return t.Transport
- }
- return http.DefaultTransport
- }
- type BasicAuthTransport struct {
- Username string
- Password string
- OTP string
-
-
- Transport http.RoundTripper
- }
- func (t *BasicAuthTransport) RoundTrip(req *http.Request) (*http.Response, error) {
-
-
-
-
-
-
- req2 := new(http.Request)
- *req2 = *req
- req2.Header = make(http.Header, len(req.Header))
- for k, s := range req.Header {
- req2.Header[k] = append([]string(nil), s...)
- }
- req2.SetBasicAuth(t.Username, t.Password)
- if t.OTP != "" {
- req2.Header.Set(headerOTP, t.OTP)
- }
- return t.transport().RoundTrip(req2)
- }
- func (t *BasicAuthTransport) Client() *http.Client {
- return &http.Client{Transport: t}
- }
- func (t *BasicAuthTransport) transport() http.RoundTripper {
- if t.Transport != nil {
- return t.Transport
- }
- return http.DefaultTransport
- }
- func formatRateReset(d time.Duration) string {
- isNegative := d < 0
- if isNegative {
- d *= -1
- }
- secondsTotal := int(0.5 + d.Seconds())
- minutes := secondsTotal / 60
- seconds := secondsTotal - minutes*60
- var timeString string
- if minutes > 0 {
- timeString = fmt.Sprintf("%dm%02ds", minutes, seconds)
- } else {
- timeString = fmt.Sprintf("%ds", seconds)
- }
- if isNegative {
- return fmt.Sprintf("[rate limit was reset %v ago]", timeString)
- }
- return fmt.Sprintf("[rate reset in %v]", timeString)
- }
- func Bool(v bool) *bool { return &v }
- func Int(v int) *int { return &v }
- func Int64(v int64) *int64 { return &v }
- func String(v string) *string { return &v }
|