Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for check private vulnerability reporting endpoint #3157

Merged
merged 10 commits into from
May 5, 2024
24 changes: 24 additions & 0 deletions github/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -2402,3 +2402,27 @@ 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.
//
// 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
}

privateReporting := new(checkPrivateReporting)
resp, err := s.client.Do(ctx, req, privateReporting)
return privateReporting.Enabled, resp, err
}
33 changes: 33 additions & 0 deletions github/repos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
fmt.Fprint(w, `{"enabled": true}`)
})

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
})
}