From 8fde28eae3c00137f370b93a232a8ac6fe654ce5 Mon Sep 17 00:00:00 2001 From: Rufina Talalaeva Date: Wed, 6 Dec 2023 01:27:17 +0300 Subject: [PATCH 1/3] Add code_search and dependency_snapshots for RateLimits --- github/github.go | 14 ++++++++ github/github_test.go | 10 ++++++ github/rate_limit.go | 8 +++++ github/rate_limit_test.go | 68 +++++++++++++++++++++++++++++++++++++-- 4 files changed, 97 insertions(+), 3 deletions(-) diff --git a/github/github.go b/github/github.go index c248b256f6..e41036361a 100644 --- a/github/github.go +++ b/github/github.go @@ -1303,6 +1303,8 @@ const ( codeScanningUploadCategory actionsRunnerRegistrationCategory scimCategory + dependencySnapshotsCategory + codeSearchCategory categories // An array of this length will be able to contain all rate limit categories. ) @@ -1315,6 +1317,12 @@ func category(method, path string) rateLimitCategory { // NOTE: coreCategory is returned for actionsRunnerRegistrationCategory too, // because no API found for this category. return coreCategory + + // https://docs.github.com/en/rest/search/search#search-code + case strings.HasPrefix(path, "/search/code") && + method == http.MethodGet: + return codeSearchCategory + case strings.HasPrefix(path, "/search/"): return searchCategory case path == "/graphql": @@ -1337,6 +1345,12 @@ func category(method, path string) rateLimitCategory { // https://docs.github.com/enterprise-cloud@latest/rest/scim case strings.HasPrefix(path, "/scim/"): return scimCategory + + // https://docs.github.com/en/rest/dependency-graph/dependency-submission#create-a-snapshot-of-dependencies-for-a-repository + case strings.HasPrefix(path, "/repos/") && + strings.HasSuffix(path, "/dependency-graph/snapshots") && + method == http.MethodPost: + return dependencySnapshotsCategory } } diff --git a/github/github_test.go b/github/github_test.go index b994496cc0..5bd631bfec 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -1153,6 +1153,16 @@ func TestDo_rateLimitCategory(t *testing.T) { url: "/scim/v2/organizations/ORG/Users", category: scimCategory, }, + { + method: http.MethodGet, + url: "repos/google/go-github/dependency-graph/snapshots", + category: dependencySnapshotsCategory, // only POST requests are in the dependency graph category + }, + { + method: http.MethodGet, + url: "/search/code?q=rate", + category: codeSearchCategory, + }, // missing a check for actionsRunnerRegistrationCategory: API not found } diff --git a/github/rate_limit.go b/github/rate_limit.go index 0fc15f815e..febe5edccf 100644 --- a/github/rate_limit.go +++ b/github/rate_limit.go @@ -52,6 +52,8 @@ type RateLimits struct { CodeScanningUpload *Rate `json:"code_scanning_upload"` ActionsRunnerRegistration *Rate `json:"actions_runner_registration"` SCIM *Rate `json:"scim"` + DependencySnapshots *Rate `json:"dependency_snapshots"` + CodeSearch *Rate `json:"code_search"` } func (r RateLimits) String() string { @@ -106,6 +108,12 @@ func (s *RateLimitService) Get(ctx context.Context) (*RateLimits, *Response, err if response.Resources.SCIM != nil { s.client.rateLimits[scimCategory] = *response.Resources.SCIM } + if response.Resources.DependencySnapshots != nil { + s.client.rateLimits[dependencySnapshotsCategory] = *response.Resources.DependencySnapshots + } + if response.Resources.CodeSearch != nil { + s.client.rateLimits[codeSearchCategory] = *response.Resources.CodeSearch + } s.client.rateMu.Unlock() } diff --git a/github/rate_limit_test.go b/github/rate_limit_test.go index 167288bc88..da8a68c910 100644 --- a/github/rate_limit_test.go +++ b/github/rate_limit_test.go @@ -25,8 +25,10 @@ func TestRateLimits_String(t *testing.T) { CodeScanningUpload: &Rate{}, ActionsRunnerRegistration: &Rate{}, SCIM: &Rate{}, + DependencySnapshots: &Rate{}, + CodeSearch: &Rate{}, } - 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}}}` + 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}}}` if got := v.String(); got != want { t.Errorf("RateLimits.String = %v, want %v", got, want) } @@ -46,7 +48,9 @@ func TestRateLimits(t *testing.T) { "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} + "scim": {"limit":9,"remaining":8,"reset":1372700880}, + "dependency_snapshots": {"limit":10,"remaining":9,"reset":1372700881}, + "code_search": {"limit":11,"remaining":10,"reset":1372700882} }}`) }) @@ -97,6 +101,16 @@ func TestRateLimits(t *testing.T) { Remaining: 8, Reset: Timestamp{time.Date(2013, time.July, 1, 17, 48, 00, 0, time.UTC).Local()}, }, + DependencySnapshots: &Rate{ + Limit: 10, + Remaining: 9, + Reset: Timestamp{time.Date(2013, time.July, 1, 17, 48, 1, 0, time.UTC).Local()}, + }, + CodeSearch: &Rate{ + Limit: 11, + Remaining: 10, + Reset: Timestamp{time.Date(2013, time.July, 1, 17, 48, 2, 0, time.UTC).Local()}, + }, } if !cmp.Equal(rate, want) { t.Errorf("RateLimits returned %+v, want %+v", rate, want) @@ -137,6 +151,14 @@ func TestRateLimits(t *testing.T) { category: scimCategory, rate: want.SCIM, }, + { + category: dependencySnapshotsCategory, + rate: want.DependencySnapshots, + }, + { + category: codeSearchCategory, + rate: want.CodeSearch, + }, } for _, tt := range tests { @@ -177,7 +199,9 @@ func TestRateLimits_overQuota(t *testing.T) { "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} + "scim": {"limit":9,"remaining":8,"reset":1372700880}, + "dependency_snapshots": {"limit":10,"remaining":9,"reset":1372700881}, + "code_search": {"limit":11,"remaining":10,"reset":1372700882} }}`) }) @@ -228,6 +252,16 @@ func TestRateLimits_overQuota(t *testing.T) { Remaining: 8, Reset: Timestamp{time.Date(2013, time.July, 1, 17, 48, 00, 0, time.UTC).Local()}, }, + DependencySnapshots: &Rate{ + Limit: 10, + Remaining: 9, + Reset: Timestamp{time.Date(2013, time.July, 1, 17, 48, 1, 0, time.UTC).Local()}, + }, + CodeSearch: &Rate{ + Limit: 11, + Remaining: 10, + Reset: Timestamp{time.Date(2013, time.July, 1, 17, 48, 2, 0, time.UTC).Local()}, + }, } if !cmp.Equal(rate, want) { t.Errorf("RateLimits returned %+v, want %+v", rate, want) @@ -269,6 +303,14 @@ func TestRateLimits_overQuota(t *testing.T) { category: scimCategory, rate: want.SCIM, }, + { + category: dependencySnapshotsCategory, + rate: want.DependencySnapshots, + }, + { + category: codeSearchCategory, + rate: want.CodeSearch, + }, } for _, tt := range tests { if got, want := client.rateLimits[tt.category], *tt.rate; got != want { @@ -321,6 +363,16 @@ func TestRateLimits_Marshal(t *testing.T) { Remaining: 1, Reset: Timestamp{referenceTime}, }, + DependencySnapshots: &Rate{ + Limit: 1, + Remaining: 1, + Reset: Timestamp{referenceTime}, + }, + CodeSearch: &Rate{ + Limit: 1, + Remaining: 1, + Reset: Timestamp{referenceTime}, + }, } want := `{ @@ -363,6 +415,16 @@ func TestRateLimits_Marshal(t *testing.T) { "limit": 1, "remaining": 1, "reset": ` + referenceTimeStr + ` + }, + "dependency_snapshots": { + "limit": 1, + "remaining": 1, + "reset": ` + referenceTimeStr + ` + }, + "code_search": { + "limit": 1, + "remaining": 1, + "reset": ` + referenceTimeStr + ` } }` From 390a3baef6abeede1f3a48eae0ec6ab6cb31ecc1 Mon Sep 17 00:00:00 2001 From: Rufina Talalaeva Date: Wed, 6 Dec 2023 02:12:24 +0300 Subject: [PATCH 2/3] Add result of script/generate.sh --- github/github-accessors.go | 16 ++++++++++++++++ github/github-accessors_test.go | 14 ++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/github/github-accessors.go b/github/github-accessors.go index e15eb10204..c9d4c3bb0c 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -17766,6 +17766,14 @@ func (r *RateLimits) GetCodeScanningUpload() *Rate { return r.CodeScanningUpload } +// GetCodeSearch returns the CodeSearch field. +func (r *RateLimits) GetCodeSearch() *Rate { + if r == nil { + return nil + } + return r.CodeSearch +} + // GetCore returns the Core field. func (r *RateLimits) GetCore() *Rate { if r == nil { @@ -17774,6 +17782,14 @@ func (r *RateLimits) GetCore() *Rate { return r.Core } +// GetDependencySnapshots returns the DependencySnapshots field. +func (r *RateLimits) GetDependencySnapshots() *Rate { + if r == nil { + return nil + } + return r.DependencySnapshots +} + // GetGraphQL returns the GraphQL field. func (r *RateLimits) GetGraphQL() *Rate { if r == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 84d104f18c..0528b36793 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -20606,6 +20606,13 @@ func TestRateLimits_GetCodeScanningUpload(tt *testing.T) { r.GetCodeScanningUpload() } +func TestRateLimits_GetCodeSearch(tt *testing.T) { + r := &RateLimits{} + r.GetCodeSearch() + r = nil + r.GetCodeSearch() +} + func TestRateLimits_GetCore(tt *testing.T) { r := &RateLimits{} r.GetCore() @@ -20613,6 +20620,13 @@ func TestRateLimits_GetCore(tt *testing.T) { r.GetCore() } +func TestRateLimits_GetDependencySnapshots(tt *testing.T) { + r := &RateLimits{} + r.GetDependencySnapshots() + r = nil + r.GetDependencySnapshots() +} + func TestRateLimits_GetGraphQL(tt *testing.T) { r := &RateLimits{} r.GetGraphQL() From 3bb2ab4a6275b3bb0f48827f87daa789cc171d37 Mon Sep 17 00:00:00 2001 From: Rufina Talalaeva Date: Wed, 6 Dec 2023 02:25:35 +0300 Subject: [PATCH 3/3] Fix test case --- github/github_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/github/github_test.go b/github/github_test.go index 5bd631bfec..933d29e774 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -1154,9 +1154,9 @@ func TestDo_rateLimitCategory(t *testing.T) { category: scimCategory, }, { - method: http.MethodGet, - url: "repos/google/go-github/dependency-graph/snapshots", - category: dependencySnapshotsCategory, // only POST requests are in the dependency graph category + method: http.MethodPost, + url: "/repos/google/go-github/dependency-graph/snapshots", + category: dependencySnapshotsCategory, }, { method: http.MethodGet,