Skip to content

Commit 2cc47f9

Browse files
authored
Add code_search and dependency_snapshots for RateLimits (#3019)
Fixes: #3018.
1 parent d47936f commit 2cc47f9

File tree

6 files changed

+127
-3
lines changed

6 files changed

+127
-3
lines changed

github/github-accessors.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-accessors_test.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,8 @@ const (
13031303
codeScanningUploadCategory
13041304
actionsRunnerRegistrationCategory
13051305
scimCategory
1306+
dependencySnapshotsCategory
1307+
codeSearchCategory
13061308

13071309
categories // An array of this length will be able to contain all rate limit categories.
13081310
)
@@ -1315,6 +1317,12 @@ func category(method, path string) rateLimitCategory {
13151317
// NOTE: coreCategory is returned for actionsRunnerRegistrationCategory too,
13161318
// because no API found for this category.
13171319
return coreCategory
1320+
1321+
// https://docs.github.com/en/rest/search/search#search-code
1322+
case strings.HasPrefix(path, "/search/code") &&
1323+
method == http.MethodGet:
1324+
return codeSearchCategory
1325+
13181326
case strings.HasPrefix(path, "/search/"):
13191327
return searchCategory
13201328
case path == "/graphql":
@@ -1337,6 +1345,12 @@ func category(method, path string) rateLimitCategory {
13371345
// https://docs.github.com/enterprise-cloud@latest/rest/scim
13381346
case strings.HasPrefix(path, "/scim/"):
13391347
return scimCategory
1348+
1349+
// https://docs.github.com/en/rest/dependency-graph/dependency-submission#create-a-snapshot-of-dependencies-for-a-repository
1350+
case strings.HasPrefix(path, "/repos/") &&
1351+
strings.HasSuffix(path, "/dependency-graph/snapshots") &&
1352+
method == http.MethodPost:
1353+
return dependencySnapshotsCategory
13401354
}
13411355
}
13421356

github/github_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,16 @@ func TestDo_rateLimitCategory(t *testing.T) {
11531153
url: "/scim/v2/organizations/ORG/Users",
11541154
category: scimCategory,
11551155
},
1156+
{
1157+
method: http.MethodPost,
1158+
url: "/repos/google/go-github/dependency-graph/snapshots",
1159+
category: dependencySnapshotsCategory,
1160+
},
1161+
{
1162+
method: http.MethodGet,
1163+
url: "/search/code?q=rate",
1164+
category: codeSearchCategory,
1165+
},
11561166
// missing a check for actionsRunnerRegistrationCategory: API not found
11571167
}
11581168

github/rate_limit.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ type RateLimits struct {
5252
CodeScanningUpload *Rate `json:"code_scanning_upload"`
5353
ActionsRunnerRegistration *Rate `json:"actions_runner_registration"`
5454
SCIM *Rate `json:"scim"`
55+
DependencySnapshots *Rate `json:"dependency_snapshots"`
56+
CodeSearch *Rate `json:"code_search"`
5557
}
5658

5759
func (r RateLimits) String() string {
@@ -106,6 +108,12 @@ func (s *RateLimitService) Get(ctx context.Context) (*RateLimits, *Response, err
106108
if response.Resources.SCIM != nil {
107109
s.client.rateLimits[scimCategory] = *response.Resources.SCIM
108110
}
111+
if response.Resources.DependencySnapshots != nil {
112+
s.client.rateLimits[dependencySnapshotsCategory] = *response.Resources.DependencySnapshots
113+
}
114+
if response.Resources.CodeSearch != nil {
115+
s.client.rateLimits[codeSearchCategory] = *response.Resources.CodeSearch
116+
}
109117
s.client.rateMu.Unlock()
110118
}
111119

github/rate_limit_test.go

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ func TestRateLimits_String(t *testing.T) {
2525
CodeScanningUpload: &Rate{},
2626
ActionsRunnerRegistration: &Rate{},
2727
SCIM: &Rate{},
28+
DependencySnapshots: &Rate{},
29+
CodeSearch: &Rate{},
2830
}
29-
want := `github.RateLimits{Core:github.Rate{Limit:0, Remaining:0, Reset:github.Timestamp{0001-01-01 00:00:00 +0000 UTC}}, Search:github.Rate{Limit:0, Remaining:0, Reset:github.Timestamp{0001-01-01 00:00:00 +0000 UTC}}, GraphQL:github.Rate{Limit:0, Remaining:0, Reset:github.Timestamp{0001-01-01 00:00:00 +0000 UTC}}, IntegrationManifest:github.Rate{Limit:0, Remaining:0, Reset:github.Timestamp{0001-01-01 00:00:00 +0000 UTC}}, SourceImport:github.Rate{Limit:0, Remaining:0, Reset:github.Timestamp{0001-01-01 00:00:00 +0000 UTC}}, CodeScanningUpload:github.Rate{Limit:0, Remaining:0, Reset:github.Timestamp{0001-01-01 00:00:00 +0000 UTC}}, ActionsRunnerRegistration:github.Rate{Limit:0, Remaining:0, Reset:github.Timestamp{0001-01-01 00:00:00 +0000 UTC}}, SCIM:github.Rate{Limit:0, Remaining:0, Reset:github.Timestamp{0001-01-01 00:00:00 +0000 UTC}}}`
31+
want := `github.RateLimits{Core:github.Rate{Limit:0, Remaining:0, Reset:github.Timestamp{0001-01-01 00:00:00 +0000 UTC}}, Search:github.Rate{Limit:0, Remaining:0, Reset:github.Timestamp{0001-01-01 00:00:00 +0000 UTC}}, GraphQL:github.Rate{Limit:0, Remaining:0, Reset:github.Timestamp{0001-01-01 00:00:00 +0000 UTC}}, IntegrationManifest:github.Rate{Limit:0, Remaining:0, Reset:github.Timestamp{0001-01-01 00:00:00 +0000 UTC}}, SourceImport:github.Rate{Limit:0, Remaining:0, Reset:github.Timestamp{0001-01-01 00:00:00 +0000 UTC}}, CodeScanningUpload:github.Rate{Limit:0, Remaining:0, Reset:github.Timestamp{0001-01-01 00:00:00 +0000 UTC}}, ActionsRunnerRegistration:github.Rate{Limit:0, Remaining:0, Reset:github.Timestamp{0001-01-01 00:00:00 +0000 UTC}}, SCIM:github.Rate{Limit:0, Remaining:0, Reset:github.Timestamp{0001-01-01 00:00:00 +0000 UTC}}, DependencySnapshots:github.Rate{Limit:0, Remaining:0, Reset:github.Timestamp{0001-01-01 00:00:00 +0000 UTC}}, CodeSearch:github.Rate{Limit:0, Remaining:0, Reset:github.Timestamp{0001-01-01 00:00:00 +0000 UTC}}}`
3032
if got := v.String(); got != want {
3133
t.Errorf("RateLimits.String = %v, want %v", got, want)
3234
}
@@ -46,7 +48,9 @@ func TestRateLimits(t *testing.T) {
4648
"source_import": {"limit":6,"remaining":5,"reset":1372700877},
4749
"code_scanning_upload": {"limit":7,"remaining":6,"reset":1372700878},
4850
"actions_runner_registration": {"limit":8,"remaining":7,"reset":1372700879},
49-
"scim": {"limit":9,"remaining":8,"reset":1372700880}
51+
"scim": {"limit":9,"remaining":8,"reset":1372700880},
52+
"dependency_snapshots": {"limit":10,"remaining":9,"reset":1372700881},
53+
"code_search": {"limit":11,"remaining":10,"reset":1372700882}
5054
}}`)
5155
})
5256

@@ -97,6 +101,16 @@ func TestRateLimits(t *testing.T) {
97101
Remaining: 8,
98102
Reset: Timestamp{time.Date(2013, time.July, 1, 17, 48, 00, 0, time.UTC).Local()},
99103
},
104+
DependencySnapshots: &Rate{
105+
Limit: 10,
106+
Remaining: 9,
107+
Reset: Timestamp{time.Date(2013, time.July, 1, 17, 48, 1, 0, time.UTC).Local()},
108+
},
109+
CodeSearch: &Rate{
110+
Limit: 11,
111+
Remaining: 10,
112+
Reset: Timestamp{time.Date(2013, time.July, 1, 17, 48, 2, 0, time.UTC).Local()},
113+
},
100114
}
101115
if !cmp.Equal(rate, want) {
102116
t.Errorf("RateLimits returned %+v, want %+v", rate, want)
@@ -137,6 +151,14 @@ func TestRateLimits(t *testing.T) {
137151
category: scimCategory,
138152
rate: want.SCIM,
139153
},
154+
{
155+
category: dependencySnapshotsCategory,
156+
rate: want.DependencySnapshots,
157+
},
158+
{
159+
category: codeSearchCategory,
160+
rate: want.CodeSearch,
161+
},
140162
}
141163

142164
for _, tt := range tests {
@@ -177,7 +199,9 @@ func TestRateLimits_overQuota(t *testing.T) {
177199
"source_import": {"limit":6,"remaining":5,"reset":1372700877},
178200
"code_scanning_upload": {"limit":7,"remaining":6,"reset":1372700878},
179201
"actions_runner_registration": {"limit":8,"remaining":7,"reset":1372700879},
180-
"scim": {"limit":9,"remaining":8,"reset":1372700880}
202+
"scim": {"limit":9,"remaining":8,"reset":1372700880},
203+
"dependency_snapshots": {"limit":10,"remaining":9,"reset":1372700881},
204+
"code_search": {"limit":11,"remaining":10,"reset":1372700882}
181205
}}`)
182206
})
183207

@@ -228,6 +252,16 @@ func TestRateLimits_overQuota(t *testing.T) {
228252
Remaining: 8,
229253
Reset: Timestamp{time.Date(2013, time.July, 1, 17, 48, 00, 0, time.UTC).Local()},
230254
},
255+
DependencySnapshots: &Rate{
256+
Limit: 10,
257+
Remaining: 9,
258+
Reset: Timestamp{time.Date(2013, time.July, 1, 17, 48, 1, 0, time.UTC).Local()},
259+
},
260+
CodeSearch: &Rate{
261+
Limit: 11,
262+
Remaining: 10,
263+
Reset: Timestamp{time.Date(2013, time.July, 1, 17, 48, 2, 0, time.UTC).Local()},
264+
},
231265
}
232266
if !cmp.Equal(rate, want) {
233267
t.Errorf("RateLimits returned %+v, want %+v", rate, want)
@@ -269,6 +303,14 @@ func TestRateLimits_overQuota(t *testing.T) {
269303
category: scimCategory,
270304
rate: want.SCIM,
271305
},
306+
{
307+
category: dependencySnapshotsCategory,
308+
rate: want.DependencySnapshots,
309+
},
310+
{
311+
category: codeSearchCategory,
312+
rate: want.CodeSearch,
313+
},
272314
}
273315
for _, tt := range tests {
274316
if got, want := client.rateLimits[tt.category], *tt.rate; got != want {
@@ -321,6 +363,16 @@ func TestRateLimits_Marshal(t *testing.T) {
321363
Remaining: 1,
322364
Reset: Timestamp{referenceTime},
323365
},
366+
DependencySnapshots: &Rate{
367+
Limit: 1,
368+
Remaining: 1,
369+
Reset: Timestamp{referenceTime},
370+
},
371+
CodeSearch: &Rate{
372+
Limit: 1,
373+
Remaining: 1,
374+
Reset: Timestamp{referenceTime},
375+
},
324376
}
325377

326378
want := `{
@@ -363,6 +415,16 @@ func TestRateLimits_Marshal(t *testing.T) {
363415
"limit": 1,
364416
"remaining": 1,
365417
"reset": ` + referenceTimeStr + `
418+
},
419+
"dependency_snapshots": {
420+
"limit": 1,
421+
"remaining": 1,
422+
"reset": ` + referenceTimeStr + `
423+
},
424+
"code_search": {
425+
"limit": 1,
426+
"remaining": 1,
427+
"reset": ` + referenceTimeStr + `
366428
}
367429
}`
368430

0 commit comments

Comments
 (0)