Skip to content

Commit

Permalink
Add values to RateLimits method
Browse files Browse the repository at this point in the history
  • Loading branch information
shokada committed Apr 25, 2022
1 parent 86f3ae6 commit a6c7bcd
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 7 deletions.
30 changes: 24 additions & 6 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -1113,12 +1113,12 @@ type rateLimitCategory uint8
const (
coreCategory rateLimitCategory = iota
searchCategory
graphqlCategory //nolint:deadcode,varcheck
integrationManifestCategory //nolint:deadcode,varcheck
sourceImportCategory //nolint:deadcode,varcheck
codeScanningUploadCategory //nolint:deadcode,varcheck
actionsRunnerRegistrationCategory //nolint:deadcode,varcheck
scimCategory //nolint:deadcode,varcheck
graphqlCategory
integrationManifestCategory
sourceImportCategory
codeScanningUploadCategory
actionsRunnerRegistrationCategory
scimCategory

categories // An array of this length will be able to contain all rate limit categories.
)
Expand Down Expand Up @@ -1159,6 +1159,24 @@ func (c *Client) RateLimits(ctx context.Context) (*RateLimits, *Response, error)
if response.Resources.Search != nil {
c.rateLimits[searchCategory] = *response.Resources.Search
}
if response.Resources.GraphQL != nil {
c.rateLimits[graphqlCategory] = *response.Resources.GraphQL
}
if response.Resources.IntegrationManifest != nil {
c.rateLimits[integrationManifestCategory] = *response.Resources.IntegrationManifest
}
if response.Resources.SourceImport != nil {
c.rateLimits[sourceImportCategory] = *response.Resources.SourceImport
}
if response.Resources.CodeScanningUpload != nil {
c.rateLimits[codeScanningUploadCategory] = *response.Resources.CodeScanningUpload
}
if response.Resources.ActionsRunnerRegistration != nil {
c.rateLimits[actionsRunnerRegistrationCategory] = *response.Resources.ActionsRunnerRegistration
}
if response.Resources.SCIM != nil {
c.rateLimits[scimCategory] = *response.Resources.SCIM
}
c.rateMu.Unlock()
}

Expand Down
56 changes: 55 additions & 1 deletion github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1868,7 +1868,13 @@ func TestRateLimits(t *testing.T) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"resources":{
"core": {"limit":2,"remaining":1,"reset":1372700873},
"search": {"limit":3,"remaining":2,"reset":1372700874}
"search": {"limit":3,"remaining":2,"reset":1372700874},
"graphql": {"limit":4,"remaining":3,"reset":1372700875},
"integration_manifest": {"limit":5,"remaining":4,"reset":1372700876},
"source_import": {"limit":6,"remaining":5,"reset":1372700877},
"code_scanning_upload": {"limit":7,"remaining":6,"reset":1372700878},
"actions_runner_registration": {"limit":8,"remaining":7,"reset":1372700879},
"scim": {"limit":9,"remaining":8,"reset":1372700880}
}}`)
})

Expand All @@ -1889,6 +1895,36 @@ func TestRateLimits(t *testing.T) {
Remaining: 2,
Reset: Timestamp{time.Date(2013, time.July, 1, 17, 47, 54, 0, time.UTC).Local()},
},
GraphQL: &Rate{
Limit: 4,
Remaining: 3,
Reset: Timestamp{time.Date(2013, time.July, 1, 17, 47, 55, 0, time.UTC).Local()},
},
IntegrationManifest: &Rate{
Limit: 5,
Remaining: 4,
Reset: Timestamp{time.Date(2013, time.July, 1, 17, 47, 56, 0, time.UTC).Local()},
},
SourceImport: &Rate{
Limit: 6,
Remaining: 5,
Reset: Timestamp{time.Date(2013, time.July, 1, 17, 47, 57, 0, time.UTC).Local()},
},
CodeScanningUpload: &Rate{
Limit: 7,
Remaining: 6,
Reset: Timestamp{time.Date(2013, time.July, 1, 17, 47, 58, 0, time.UTC).Local()},
},
ActionsRunnerRegistration: &Rate{
Limit: 8,
Remaining: 7,
Reset: Timestamp{time.Date(2013, time.July, 1, 17, 47, 59, 0, time.UTC).Local()},
},
SCIM: &Rate{
Limit: 9,
Remaining: 8,
Reset: Timestamp{time.Date(2013, time.July, 1, 17, 48, 00, 0, time.UTC).Local()},
},
}
if !cmp.Equal(rate, want) {
t.Errorf("RateLimits returned %+v, want %+v", rate, want)
Expand All @@ -1900,6 +1936,24 @@ func TestRateLimits(t *testing.T) {
if got, want := client.rateLimits[searchCategory], *want.Search; got != want {
t.Errorf("client.rateLimits[searchCategory] is %+v, want %+v", got, want)
}
if got, want := client.rateLimits[graphqlCategory], *want.GraphQL; got != want {
t.Errorf("client.rateLimits[graphqlCategory] is %+v, want %+v", got, want)
}
if got, want := client.rateLimits[integrationManifestCategory], *want.IntegrationManifest; got != want {
t.Errorf("client.rateLimits[integrationManifestCategory] is %+v, want %+v", got, want)
}
if got, want := client.rateLimits[sourceImportCategory], *want.SourceImport; got != want {
t.Errorf("client.rateLimits[sourceImportCategory] is %+v, want %+v", got, want)
}
if got, want := client.rateLimits[codeScanningUploadCategory], *want.CodeScanningUpload; got != want {
t.Errorf("client.rateLimits[codeScanningUploadCategory] is %+v, want %+v", got, want)
}
if got, want := client.rateLimits[actionsRunnerRegistrationCategory], *want.ActionsRunnerRegistration; got != want {
t.Errorf("client.rateLimits[actionsRunnerRegistrationCategory] is %+v, want %+v", got, want)
}
if got, want := client.rateLimits[scimCategory], *want.SCIM; got != want {
t.Errorf("client.rateLimits[scimCategory] is %+v, want %+v", got, want)
}
}

func TestRateLimits_coverage(t *testing.T) {
Expand Down

0 comments on commit a6c7bcd

Please sign in to comment.