Repository: google/go-github
Pull request #3019 (2023-12-05 22:29:28)

Add code_search and dependency_snapshots for RateLimits
resolves https://github.com/google/go-github/issues/3018

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/google/go-github/pull/3019?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`1e66201`)](https://app.codecov.io/gh/google/go-github/commit/1e66201027e8c106a1915fdaf44a63513f317409?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) 97.91% compared to head [(`3bb2ab4`)](https://app.codecov.io/gh/google/go-github/pull/3019?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) 97.92%.


<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##           master    #3019   +/-   ##
=======================================
  Coverage   97.91%   97.92%           
=======================================
  Files         150      150           
  Lines       12980    12990   +10     
=======================================
+ Hits        12710    12720   +10     
  Misses        192      192           
  Partials       78       78           
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/google/go-github/pull/3019?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google).


Patch:
diff --git a/github/github-accessors.go b/github/github-accessors.go
index 4cb34b12..02d1212c 100644
--- a/github/github-accessors.go
+++ b/github/github-accessors.go
@@ -1454,6 +1454,14 @@ func (a *AuditEntry) GetReadOnly() string {
 	return *a.ReadOnly
 }
 
+// GetReferrer returns the Referrer field if it's non-nil, zero value otherwise.
+func (a *AuditEntry) GetReferrer() string {
+	if a == nil || a.Referrer == nil {
+		return ""
+	}
+	return *a.Referrer
+}
+
 // GetRepo returns the Repo field if it's non-nil, zero value otherwise.
 func (a *AuditEntry) GetRepo() string {
 	if a == nil || a.Repo == nil {
@@ -17782,6 +17790,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 {
@@ -17790,6 +17806,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.go b/github/github.go
index c248b256..e4103636 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/orgs_audit_log.go b/github/orgs_audit_log.go
index e3afd311..aa359135 100644
--- a/github/orgs_audit_log.go
+++ b/github/orgs_audit_log.go
@@ -95,6 +95,7 @@ type AuditEntry struct {
 	PullRequestURL           *string                 `json:"pull_request_url,omitempty"`
 	ReadOnly                 *string                 `json:"read_only,omitempty"`
 	Reasons                  []*PolicyOverrideReason `json:"reasons,omitempty"`
+	Referrer                 *string                 `json:"referrer,omitempty"`
 	Repo                     *string                 `json:"repo,omitempty"`
 	Repository               *string                 `json:"repository,omitempty"`
 	RepositoryPublic         *bool                   `json:"repository_public,omitempty"`
diff --git a/github/rate_limit.go b/github/rate_limit.go
index 0fc15f81..febe5edc 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()
 	}
 


Test patch:
diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go
index cdc27ab9..6ae89e45 100644
--- a/github/github-accessors_test.go
+++ b/github/github-accessors_test.go
@@ -1731,6 +1731,16 @@ func TestAuditEntry_GetReadOnly(tt *testing.T) {
 	a.GetReadOnly()
 }
 
+func TestAuditEntry_GetReferrer(tt *testing.T) {
+	var zeroValue string
+	a := &AuditEntry{Referrer: &zeroValue}
+	a.GetReferrer()
+	a = &AuditEntry{}
+	a.GetReferrer()
+	a = nil
+	a.GetReferrer()
+}
+
 func TestAuditEntry_GetRepo(tt *testing.T) {
 	var zeroValue string
 	a := &AuditEntry{Repo: &zeroValue}
@@ -20626,6 +20636,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()
@@ -20633,6 +20650,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()
diff --git a/github/github_test.go b/github/github_test.go
index b994496c..933d29e7 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.MethodPost,
+			url:      "/repos/google/go-github/dependency-graph/snapshots",
+			category: dependencySnapshotsCategory,
+		},
+		{
+			method:   http.MethodGet,
+			url:      "/search/code?q=rate",
+			category: codeSearchCategory,
+		},
 		// missing a check for actionsRunnerRegistrationCategory: API not found
 	}
 
diff --git a/github/rate_limit_test.go b/github/rate_limit_test.go
index 167288bc..da8a68c9 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 + `
 		}
 	}`