Skip to content

Commit fa50465

Browse files
committed
Merge branch 'master' into add-interfaces-to-embed
2 parents 48623de + 94d2fbb commit fa50465

File tree

4 files changed

+502
-11
lines changed

4 files changed

+502
-11
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030

3131
steps:
3232
- name: Cancel previous
33-
uses: styfle/cancel-workflow-action@0.8.0
33+
uses: styfle/cancel-workflow-action@89f242ee29e10c53a841bfe71cc0ce7b2f065abc #0.9.0
3434
with:
3535
access_token: ${{ github.token }}
3636

@@ -72,5 +72,3 @@ jobs:
7272
- name: Upload coverage to Codecov
7373
if: ${{ matrix.update-coverage }}
7474
uses: codecov/codecov-action@v1
75-
with:
76-
token: ${{ secrets.CODECOV_TOKEN }}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
go-github is a Go client library for accessing the [GitHub API v3][].
1111

12-
Currently, **go-github requires Go version 1.9 or greater**. go-github tracks
12+
Currently, **go-github requires Go version 1.13 or greater**. go-github tracks
1313
[Go's version support policy][support-policy]. We do our best not to break
1414
older versions of Go if we don't have to, but due to tooling constraints, we
1515
don't always test older versions.

github/github.go

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ const (
133133
mediaTypeContentAttachmentsPreview = "application/vnd.github.corsair-preview+json"
134134
)
135135

136+
var errNonNilContext = errors.New("context must be non-nil")
137+
136138
// A Client manages communication with the GitHub API.
137139
type Client struct {
138140
clientMu sync.Mutex // clientMu protects the client during calls that modify the CheckRedirect func.
@@ -531,7 +533,7 @@ func parseRate(r *http.Response) Rate {
531533
// canceled or times out, ctx.Err() will be returned.
532534
func (c *Client) BareDo(ctx context.Context, req *http.Request) (*Response, error) {
533535
if ctx == nil {
534-
return nil, errors.New("context must be non-nil")
536+
return nil, errNonNilContext
535537
}
536538
req = withContext(ctx, req)
537539

@@ -654,6 +656,20 @@ func (c *Client) checkRateLimitBeforeDo(req *http.Request, rateLimitCategory rat
654656
return nil
655657
}
656658

659+
// compareHttpResponse returns whether two http.Response objects are equal or not.
660+
// Currently, only StatusCode is checked. This function is used when implementing the
661+
// Is(error) bool interface for the custom error types in this package.
662+
func compareHttpResponse(r1, r2 *http.Response) bool {
663+
if r1 == nil && r2 == nil {
664+
return true
665+
}
666+
667+
if r1 != nil && r2 != nil {
668+
return r1.StatusCode == r2.StatusCode
669+
}
670+
return false
671+
}
672+
657673
/*
658674
An ErrorResponse reports one or more errors caused by an API request.
659675
@@ -682,6 +698,50 @@ func (r *ErrorResponse) Error() string {
682698
r.Response.StatusCode, r.Message, r.Errors)
683699
}
684700

701+
// Is returns whether the provided error equals this error.
702+
func (r *ErrorResponse) Is(target error) bool {
703+
v, ok := target.(*ErrorResponse)
704+
if !ok {
705+
return false
706+
}
707+
708+
if r.Message != v.Message || (r.DocumentationURL != v.DocumentationURL) ||
709+
!compareHttpResponse(r.Response, v.Response) {
710+
return false
711+
}
712+
713+
// Compare Errors.
714+
if len(r.Errors) != len(v.Errors) {
715+
return false
716+
}
717+
for idx := range r.Errors {
718+
if r.Errors[idx] != v.Errors[idx] {
719+
return false
720+
}
721+
}
722+
723+
// Compare Block.
724+
if (r.Block != nil && v.Block == nil) || (r.Block == nil && v.Block != nil) {
725+
return false
726+
}
727+
if r.Block != nil && v.Block != nil {
728+
if r.Block.Reason != v.Block.Reason {
729+
return false
730+
}
731+
if (r.Block.CreatedAt != nil && v.Block.CreatedAt == nil) || (r.Block.CreatedAt ==
732+
nil && v.Block.CreatedAt != nil) {
733+
return false
734+
}
735+
if r.Block.CreatedAt != nil && v.Block.CreatedAt != nil {
736+
if *(r.Block.CreatedAt) != *(v.Block.CreatedAt) {
737+
return false
738+
}
739+
}
740+
}
741+
742+
return true
743+
}
744+
685745
// TwoFactorAuthError occurs when using HTTP Basic Authentication for a user
686746
// that has two-factor authentication enabled. The request can be reattempted
687747
// by providing a one-time password in the request.
@@ -703,6 +763,18 @@ func (r *RateLimitError) Error() string {
703763
r.Response.StatusCode, r.Message, formatRateReset(time.Until(r.Rate.Reset.Time)))
704764
}
705765

766+
// Is returns whether the provided error equals this error.
767+
func (r *RateLimitError) Is(target error) bool {
768+
v, ok := target.(*RateLimitError)
769+
if !ok {
770+
return false
771+
}
772+
773+
return r.Rate == v.Rate &&
774+
r.Message == v.Message &&
775+
compareHttpResponse(r.Response, v.Response)
776+
}
777+
706778
// AcceptedError occurs when GitHub returns 202 Accepted response with an
707779
// empty body, which means a job was scheduled on the GitHub side to process
708780
// the information needed and cache it.
@@ -718,6 +790,15 @@ func (*AcceptedError) Error() string {
718790
return "job scheduled on GitHub side; try again later"
719791
}
720792

793+
// Is returns whether the provided error equals this error.
794+
func (ae *AcceptedError) Is(target error) bool {
795+
v, ok := target.(*AcceptedError)
796+
if !ok {
797+
return false
798+
}
799+
return bytes.Compare(ae.Raw, v.Raw) == 0
800+
}
801+
721802
// AbuseRateLimitError occurs when GitHub returns 403 Forbidden response with the
722803
// "documentation_url" field value equal to "https://docs.github.com/en/free-pro-team@latest/rest/reference/#abuse-rate-limits".
723804
type AbuseRateLimitError struct {
@@ -736,6 +817,18 @@ func (r *AbuseRateLimitError) Error() string {
736817
r.Response.StatusCode, r.Message)
737818
}
738819

820+
// Is returns whether the provided error equals this error.
821+
func (r *AbuseRateLimitError) Is(target error) bool {
822+
v, ok := target.(*AbuseRateLimitError)
823+
if !ok {
824+
return false
825+
}
826+
827+
return r.Message == v.Message &&
828+
r.RetryAfter == v.RetryAfter &&
829+
compareHttpResponse(r.Response, v.Response)
830+
}
831+
739832
// sanitizeURL redacts the client_secret parameter from the URL which may be
740833
// exposed to the user.
741834
func sanitizeURL(uri *url.URL) *url.URL {

0 commit comments

Comments
 (0)