From 3a741795b5e3a7d8be82c38d3a19617fbce05e38 Mon Sep 17 00:00:00 2001 From: Ravi Teja Date: Sun, 28 Apr 2024 18:30:03 +0530 Subject: [PATCH 1/7] add private reporting check endpoint --- github/repos.go | 19 +++++++++++++++++++ github/repos_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/github/repos.go b/github/repos.go index 6fb0030a3b2..8e2df104e39 100644 --- a/github/repos.go +++ b/github/repos.go @@ -2402,3 +2402,22 @@ func (s *RepositoriesService) DisablePrivateReporting(ctx context.Context, owner return resp, nil } + +// IsPrivateReportingEnabled checks if private vulnerability reporting is enabled +// for the repository and returns a boolean indicating the status. +// +// GitHub API docs: https://docs.github.com/rest/repos/repos#check-if-private-vulnerability-reporting-is-enabled-for-a-repository +// +//meta:operation GET /repos/{owner}/{repo}/private-vulnerability-reporting +func (s *RepositoriesService) IsPrivateReportingEnabled(ctx context.Context, owner, repo string) (bool, *Response, error) { + u := fmt.Sprintf("repos/%v/%v/private-vulnerability-reporting", owner, repo) + + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return false, nil, err + } + + resp, err := s.client.Do(ctx, req, nil) + isPrivateReportingEnabled, err := parseBoolResponse(err) + return isPrivateReportingEnabled, resp, err +} diff --git a/github/repos_test.go b/github/repos_test.go index 2b742bef904..0dbc1e8c597 100644 --- a/github/repos_test.go +++ b/github/repos_test.go @@ -4430,3 +4430,36 @@ func TestRepositoriesService_DisablePrivateReporting(t *testing.T) { return client.Repositories.DisablePrivateReporting(ctx, "owner", "repo") }) } + +func TestRepositoriesService_IsPrivateReportingEnabled(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, "GET") + w.WriteHeader(http.StatusNoContent) + }) + + ctx := context.Background() + enabled, _, err := client.Repositories.IsPrivateReportingEnabled(ctx, "owner", "repo") + if err != nil { + t.Errorf("Repositories.IsPrivateReportingEnabled returned error: %v", err) + } + if want := true; enabled != want { + t.Errorf("Repositories.IsPrivateReportingEnabled returned %+v, want %+v", enabled, want) + } + + const methodName = "IsPrivateReportingEnabled" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Repositories.IsPrivateReportingEnabled(ctx, "\n", "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Repositories.IsPrivateReportingEnabled(ctx, "owner", "repo") + if got { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want false", methodName, got) + } + return resp, err + }) +} \ No newline at end of file From f1611d110a198b7f285314c2a4c8ef70e6a05340 Mon Sep 17 00:00:00 2001 From: Ravi Teja Date: Wed, 1 May 2024 09:18:55 +0530 Subject: [PATCH 2/7] update code for private vulnerability --- github/repos.go | 19 ++++++++++++++++--- github/repos_test.go | 8 ++++---- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/github/repos.go b/github/repos.go index 8e2df104e39..fd675837c5a 100644 --- a/github/repos.go +++ b/github/repos.go @@ -2403,6 +2403,11 @@ func (s *RepositoriesService) DisablePrivateReporting(ctx context.Context, owner return resp, nil } +// CheckPrivateReporting represents whether private vulnerability reporting is enabled. +type CheckPrivateReporting struct { + Enabled bool `json:"enabled,omitempty"` +} + // IsPrivateReportingEnabled checks if private vulnerability reporting is enabled // for the repository and returns a boolean indicating the status. // @@ -2417,7 +2422,15 @@ func (s *RepositoriesService) IsPrivateReportingEnabled(ctx context.Context, own return false, nil, err } - resp, err := s.client.Do(ctx, req, nil) - isPrivateReportingEnabled, err := parseBoolResponse(err) - return isPrivateReportingEnabled, resp, err + privateReporting := new(CheckPrivateReporting) + resp, err := s.client.Do(ctx, req, privateReporting) + if privateReporting.Enabled { + return true, resp, nil + } + + if err, ok := err.(*ErrorResponse); ok && err.Response.StatusCode == http.StatusNotFound { + return false, resp, nil + } + + return false, resp, err } diff --git a/github/repos_test.go b/github/repos_test.go index 0dbc1e8c597..601559252c2 100644 --- a/github/repos_test.go +++ b/github/repos_test.go @@ -4441,12 +4441,12 @@ func TestRepositoriesService_IsPrivateReportingEnabled(t *testing.T) { }) ctx := context.Background() - enabled, _, err := client.Repositories.IsPrivateReportingEnabled(ctx, "owner", "repo") + disabled, _, err := client.Repositories.IsPrivateReportingEnabled(ctx, "owner", "repo") if err != nil { t.Errorf("Repositories.IsPrivateReportingEnabled returned error: %v", err) } - if want := true; enabled != want { - t.Errorf("Repositories.IsPrivateReportingEnabled returned %+v, want %+v", enabled, want) + if want := false; disabled != want { + t.Errorf("Repositories.IsPrivateReportingEnabled returned %+v, want %+v", disabled, want) } const methodName = "IsPrivateReportingEnabled" @@ -4462,4 +4462,4 @@ func TestRepositoriesService_IsPrivateReportingEnabled(t *testing.T) { } return resp, err }) -} \ No newline at end of file +} From 8eef7bd843f19b3e9dcafbb63a21c6a7485591aa Mon Sep 17 00:00:00 2001 From: Sai Ravi Teja Chintakrindi Date: Fri, 3 May 2024 22:25:27 +0530 Subject: [PATCH 3/7] Update github/repos.go Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- github/repos.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/repos.go b/github/repos.go index fd675837c5a..897be7a8593 100644 --- a/github/repos.go +++ b/github/repos.go @@ -2424,7 +2424,7 @@ func (s *RepositoriesService) IsPrivateReportingEnabled(ctx context.Context, own privateReporting := new(CheckPrivateReporting) resp, err := s.client.Do(ctx, req, privateReporting) - if privateReporting.Enabled { + return privateReporting.Enabled, resp, err return true, resp, nil } From ddb5b590cfe6d57edbd9b7077caf511c444a706f Mon Sep 17 00:00:00 2001 From: Ravi Teja Date: Fri, 3 May 2024 22:28:23 +0530 Subject: [PATCH 4/7] make struct private --- github/repos.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/github/repos.go b/github/repos.go index fd675837c5a..acd0f5b2c79 100644 --- a/github/repos.go +++ b/github/repos.go @@ -2403,8 +2403,8 @@ func (s *RepositoriesService) DisablePrivateReporting(ctx context.Context, owner return resp, nil } -// CheckPrivateReporting represents whether private vulnerability reporting is enabled. -type CheckPrivateReporting struct { +// checkPrivateReporting represents whether private vulnerability reporting is enabled. +type checkPrivateReporting struct { Enabled bool `json:"enabled,omitempty"` } @@ -2422,7 +2422,7 @@ func (s *RepositoriesService) IsPrivateReportingEnabled(ctx context.Context, own return false, nil, err } - privateReporting := new(CheckPrivateReporting) + privateReporting := new(checkPrivateReporting) resp, err := s.client.Do(ctx, req, privateReporting) if privateReporting.Enabled { return true, resp, nil From 8c21e5e369e4328b36d42531799bdfe19e038d76 Mon Sep 17 00:00:00 2001 From: Ravi Teja Date: Fri, 3 May 2024 22:37:31 +0530 Subject: [PATCH 5/7] make suggested change --- github/repos.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/github/repos.go b/github/repos.go index ff53cae0136..91c04075d38 100644 --- a/github/repos.go +++ b/github/repos.go @@ -2424,12 +2424,8 @@ func (s *RepositoriesService) IsPrivateReportingEnabled(ctx context.Context, own privateReporting := new(checkPrivateReporting) resp, err := s.client.Do(ctx, req, privateReporting) - return privateReporting.Enabled, resp, err - return true, resp, nil - } - - if err, ok := err.(*ErrorResponse); ok && err.Response.StatusCode == http.StatusNotFound { - return false, resp, nil + if privateReporting.Enabled { + return privateReporting.Enabled, resp, err } return false, resp, err From 4be04cbb0428b72c91ce361774a0320664f01c3b Mon Sep 17 00:00:00 2001 From: Ravi Teja Date: Fri, 3 May 2024 23:08:47 +0530 Subject: [PATCH 6/7] update test --- github/repos_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/github/repos_test.go b/github/repos_test.go index 601559252c2..e07b1b367d5 100644 --- a/github/repos_test.go +++ b/github/repos_test.go @@ -4437,16 +4437,16 @@ func TestRepositoriesService_IsPrivateReportingEnabled(t *testing.T) { mux.HandleFunc("/repos/owner/repo/private-vulnerability-reporting", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") - w.WriteHeader(http.StatusNoContent) + fmt.Fprint(w, `{"enabled": true}`) }) ctx := context.Background() - disabled, _, err := client.Repositories.IsPrivateReportingEnabled(ctx, "owner", "repo") + enabled, _, err := client.Repositories.IsPrivateReportingEnabled(ctx, "owner", "repo") if err != nil { t.Errorf("Repositories.IsPrivateReportingEnabled returned error: %v", err) } - if want := false; disabled != want { - t.Errorf("Repositories.IsPrivateReportingEnabled returned %+v, want %+v", disabled, want) + if want := true; enabled != want { + t.Errorf("Repositories.IsPrivateReportingEnabled returned %+v, want %+v", enabled, want) } const methodName = "IsPrivateReportingEnabled" From 6e51ef7177d1adc0cb42a34dd799dac1295a0504 Mon Sep 17 00:00:00 2001 From: Sai Ravi Teja Chintakrindi Date: Fri, 3 May 2024 23:22:33 +0530 Subject: [PATCH 7/7] Update github/repos.go Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- github/repos.go | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/github/repos.go b/github/repos.go index 91c04075d38..630ce748c94 100644 --- a/github/repos.go +++ b/github/repos.go @@ -2424,9 +2424,5 @@ func (s *RepositoriesService) IsPrivateReportingEnabled(ctx context.Context, own privateReporting := new(checkPrivateReporting) resp, err := s.client.Do(ctx, req, privateReporting) - if privateReporting.Enabled { - return privateReporting.Enabled, resp, err - } - - return false, resp, err + return privateReporting.Enabled, resp, err }