Repository: google/go-github
Pull request #2446 (2022-08-25 20:35:15)

List Secret Scan Alerts with index based pagination

codecov[bot]: # [Codecov](https://codecov.io/gh/google/go-github/pull/2446?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) Report
> Merging [#2446](https://codecov.io/gh/google/go-github/pull/2446?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) (369eba3) into [master](https://codecov.io/gh/google/go-github/commit/243bda850b1f78957ba0d50fb62ecdb4fbe977d5?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) (243bda8) will **not change** coverage.
> The diff coverage is `n/a`.

```diff
@@           Coverage Diff           @@
##           master    #2446   +/-   ##
=======================================
  Coverage   98.03%   98.03%           
=======================================
  Files         122      122           
  Lines       10690    10690           
=======================================
  Hits        10480    10480           
  Misses        144      144           
  Partials       66       66           
```


| [Impacted Files](https://codecov.io/gh/google/go-github/pull/2446?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) | Coverage Δ | |
|---|---|---|
| [github/secret\_scanning.go](https://codecov.io/gh/google/go-github/pull/2446/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google#diff-Z2l0aHViL3NlY3JldF9zY2FubmluZy5nbw==) | `100.00% <ø> (ø)` | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google)

gmlewis: Thank you, @AbbanMustafa - were you ever able to test this successfully?
AbbanMustafa: > Thank you, @AbbanMustafa - were you ever able to test this successfully?

Yes! I verified works for both list option types
gmlewis: @AbbanMustafa - it looks like some unit tests broke - could you please fix them?  Also make sure your code is formatted... I haven't taken a look yet... just reporting the breakage.


Patch:
diff --git a/github/secret_scanning.go b/github/secret_scanning.go
index ec64950a..d512560d 100644
--- a/github/secret_scanning.go
+++ b/github/secret_scanning.go
@@ -61,6 +61,14 @@ type SecretScanningAlertListOptions struct {
 	Resolution string `url:"resolution,omitempty"`
 
 	ListCursorOptions
+
+	// List options can vary on the Enterprise type.
+	// On Enterprise Cloud, Secret Scan alerts support requesting by page number
+	// along with providing a cursor for an "after" param.
+	// See: https://docs.github.com/en/enterprise-cloud@latest/rest/secret-scanning#list-secret-scanning-alerts-for-an-organization
+	// Whereas on Enterprise Server, pagination is by index.
+	// See: https://docs.github.com/en/enterprise-server@3.6/rest/secret-scanning#list-secret-scanning-alerts-for-an-organization
+	ListOptions
 }
 
 // SecretScanningAlertUpdateOptions specifies optional parameters to the SecretScanningService.UpdateAlert method.


Test patch:
diff --git a/github/secret_scanning_test.go b/github/secret_scanning_test.go
index afe6144e..af70fc80 100644
--- a/github/secret_scanning_test.go
+++ b/github/secret_scanning_test.go
@@ -146,6 +146,73 @@ func TestSecretScanningService_ListAlertsForOrg(t *testing.T) {
 	})
 }
 
+func TestSecretScanningService_ListAlertsForOrgListOptions(t *testing.T) {
+	client, mux, _, teardown := setup()
+	defer teardown()
+
+	mux.HandleFunc("/orgs/o/secret-scanning/alerts", func(w http.ResponseWriter, r *http.Request) {
+		testMethod(t, r, "GET")
+		testFormValues(t, r, values{"state": "open", "secret_type": "mailchimp_api_key", "per_page": "1", "page": "1"})
+
+		fmt.Fprint(w, `[{
+			"number": 1,
+			"created_at": "1996-06-20T00:00:00Z",
+			"url": "https://api.github.com/repos/o/r/secret-scanning/alerts/1",
+			"html_url": "https://github.com/o/r/security/secret-scanning/1",
+			"locations_url": "https://api.github.com/repos/o/r/secret-scanning/alerts/1/locations",
+			"state": "open",
+			"resolution": null,
+			"resolved_at": null,
+			"resolved_by": null,
+			"secret_type": "mailchimp_api_key",
+			"secret": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us2"
+		}]`)
+	})
+
+	ctx := context.Background()
+
+	// Testing pagination by index
+	opts := &SecretScanningAlertListOptions{State: "open", SecretType: "mailchimp_api_key", ListOptions: ListOptions{Page: 1, PerPage: 1}}
+
+	alerts, _, err := client.SecretScanning.ListAlertsForOrg(ctx, "o", opts)
+	if err != nil {
+		t.Errorf("SecretScanning.ListAlertsForOrg returned error: %v", err)
+	}
+
+	date := Timestamp{time.Date(1996, time.June, 20, 00, 00, 00, 0, time.UTC)}
+	want := []*SecretScanningAlert{
+		{
+			Number:       Int(1),
+			CreatedAt:    &date,
+			URL:          String("https://api.github.com/repos/o/r/secret-scanning/alerts/1"),
+			HTMLURL:      String("https://github.com/o/r/security/secret-scanning/1"),
+			LocationsURL: String("https://api.github.com/repos/o/r/secret-scanning/alerts/1/locations"),
+			State:        String("open"),
+			Resolution:   nil,
+			ResolvedAt:   nil,
+			ResolvedBy:   nil,
+			SecretType:   String("mailchimp_api_key"),
+			Secret:       String("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us2"),
+		},
+	}
+
+	if !cmp.Equal(alerts, want) {
+		t.Errorf("SecretScanning.ListAlertsForOrg returned %+v, want %+v", alerts, want)
+	}
+
+	const methodName = "ListAlertsForOrg"
+
+	testBadOptions(t, methodName, func() (err error) {
+		_, _, err = client.SecretScanning.ListAlertsForOrg(ctx, "\n", opts)
+		return err
+	})
+
+	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
+		_, resp, err := client.SecretScanning.ListAlertsForOrg(ctx, "o", opts)
+		return resp, err
+	})
+}
+
 func TestSecretScanningService_ListAlertsForRepo(t *testing.T) {
 	client, mux, _, teardown := setup()
 	defer teardown()