Repository: google/go-github
Pull request #2887 (2023-08-19 07:44:07)

Add support for enable/disable private vulnerability reporting on repositories
Closes https://github.com/google/go-github/issues/2883

- [x] go generate
- [x] go vet
- [x] go test

Please let me know if the method names are not descriptive enough or just need to be changed. 

Also, something that's been bugging me. In my previous [PR](https://github.com/google/go-github/pull/2869), I did some actual testing aside from the unit tests. While not present in [CONTRIBUTING.md](https://github.com/google/go-github/blob/master/CONTRIBUTING.md) , is it advisable/necessary/optional to perform actual testing (importing the library, calling the new method and examine response). 

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/google/go-github/pull/2887?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) Report
> Merging [#2887](https://app.codecov.io/gh/google/go-github/pull/2887?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) (bf3c0ec) into [master](https://app.codecov.io/gh/google/go-github/commit/a0e8f35c5cefc688733d566ec3701e86972df056?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) (a0e8f35) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.

```diff
@@           Coverage Diff           @@
##           master    #2887   +/-   ##
=======================================
  Coverage   98.06%   98.06%           
=======================================
  Files         140      140           
  Lines       12269    12293   +24     
=======================================
+ Hits        12031    12055   +24     
  Misses        162      162           
  Partials       76       76           
```


| [Files Changed](https://app.codecov.io/gh/google/go-github/pull/2887?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) | Coverage Δ | |
|---|---|---|
| [github/repos.go](https://app.codecov.io/gh/google/go-github/pull/2887?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google#diff-Z2l0aHViL3JlcG9zLmdv) | `98.89% <100.00%> (+0.03%)` | :arrow_up: |


gmlewis: > Also, something that's been bugging me. In my previous [PR](https://github.com/google/go-github/pull/2869), I did some actual testing aside from the unit tests. While not present in [CONTRIBUTING.md](https://github.com/google/go-github/blob/master/CONTRIBUTING.md) , is it advisable/necessary/optional to perform actual testing (importing the library, calling the new method and examine response).

In this repo, we do not typically add to the integration tests anymore.
They were extremely helpful in the early design of the repo, but in my opinion, the surface area of this API is so huge that it would be an onerous task to attempt to keep up-to-date in addition to the unit tests.

Having said that, we place a high value on unit tests in this repo and want every endpoint to be exercised "round-trip" with an in-memory http server to verify that every looks like it should work in real practice.

So you are proceeding along just as planned, and we really appreciate your contributions, @vandanrohatgi !

Keep up the great work!  😁 ❤️ 
vandanrohatgi: Thank you for making this project such a pleasure to work with!
mnafees: LGTM 👍🏽 
gmlewis: Thank you, @mnafees !
Merging.

Patch:
diff --git a/github/repos.go b/github/repos.go
index 2309d927..83fc3f8e 100644
--- a/github/repos.go
+++ b/github/repos.go
@@ -2069,3 +2069,43 @@ func isBranchNotProtected(err error) bool {
 	errorResponse, ok := err.(*ErrorResponse)
 	return ok && errorResponse.Message == githubBranchNotProtected
 }
+
+// EnablePrivateReporting enables private reporting of vulnerabilities for a
+// repository.
+//
+// GitHub API docs: https://docs.github.com/en/rest/repos/repos#enable-private-vulnerability-reporting-for-a-repository
+func (s *RepositoriesService) EnablePrivateReporting(ctx context.Context, owner, repo string) (*Response, error) {
+	u := fmt.Sprintf("repos/%v/%v/private-vulnerability-reporting", owner, repo)
+
+	req, err := s.client.NewRequest("PUT", u, nil)
+	if err != nil {
+		return nil, err
+	}
+
+	resp, err := s.client.Do(ctx, req, nil)
+	if err != nil {
+		return resp, err
+	}
+
+	return resp, nil
+}
+
+// DisablePrivateReporting disables private reporting of vulnerabilities for a
+// repository.
+//
+// GitHub API docs: https://docs.github.com/en/rest/repos/repos#disable-private-vulnerability-reporting-for-a-repository
+func (s *RepositoriesService) DisablePrivateReporting(ctx context.Context, owner, repo string) (*Response, error) {
+	u := fmt.Sprintf("repos/%v/%v/private-vulnerability-reporting", owner, repo)
+
+	req, err := s.client.NewRequest("DELETE", u, nil)
+	if err != nil {
+		return nil, err
+	}
+
+	resp, err := s.client.Do(ctx, req, nil)
+	if err != nil {
+		return resp, err
+	}
+
+	return resp, nil
+}


Test patch:
diff --git a/github/repos_test.go b/github/repos_test.go
index c2abf6da..a3164edf 100644
--- a/github/repos_test.go
+++ b/github/repos_test.go
@@ -3569,3 +3569,55 @@ func TestRepositoryTag_Marshal(t *testing.T) {
 
 	testJSONMarshal(t, u, want)
 }
+
+func TestRepositoriesService_EnablePrivateReporting(t *testing.T) {
+	client, mux, _, teardown := setup()
+	defer teardown()
+
+	mux.HandleFunc("/repos/owner/repo/private-vulnerability-reporting", func(w http.ResponseWriter, r *http.Request) {
+		testMethod(t, r, "PUT")
+		w.WriteHeader(http.StatusNoContent)
+	})
+
+	ctx := context.Background()
+	_, err := client.Repositories.EnablePrivateReporting(ctx, "owner", "repo")
+	if err != nil {
+		t.Errorf("Repositories.EnablePrivateReporting returned error: %v", err)
+	}
+
+	const methodName = "EnablePrivateReporting"
+	testBadOptions(t, methodName, func() (err error) {
+		_, err = client.Repositories.EnablePrivateReporting(ctx, "\n", "\n")
+		return err
+	})
+
+	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
+		return client.Repositories.EnablePrivateReporting(ctx, "owner", "repo")
+	})
+}
+
+func TestRepositoriesService_DisablePrivateReporting(t *testing.T) {
+	client, mux, _, teardown := setup()
+	defer teardown()
+
+	mux.HandleFunc("/repos/owner/repo/private-vulnerability-reporting", func(w http.ResponseWriter, r *http.Request) {
+		testMethod(t, r, "DELETE")
+		w.WriteHeader(http.StatusNoContent)
+	})
+
+	ctx := context.Background()
+	_, err := client.Repositories.DisablePrivateReporting(ctx, "owner", "repo")
+	if err != nil {
+		t.Errorf("Repositories.DisablePrivateReporting returned error: %v", err)
+	}
+
+	const methodName = "DisablePrivateReporting"
+	testBadOptions(t, methodName, func() (err error) {
+		_, err = client.Repositories.DisablePrivateReporting(ctx, "\n", "\n")
+		return err
+	})
+
+	testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
+		return client.Repositories.DisablePrivateReporting(ctx, "owner", "repo")
+	})
+}