Skip to content

Commit 9010303

Browse files
cpanatogmlewis
authored andcommitted
add vulnerability-alerts endpoint (#1166)
1 parent 2680886 commit 9010303

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

github/github.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ const (
134134

135135
// https://developer.github.com/changes/2019-03-14-enabling-disabling-pages/
136136
mediaTypeEnablePagesAPIPreview = "application/vnd.github.switcheroo-preview+json"
137+
138+
// https://developer.github.com/changes/2019-04-24-vulnerability-alerts/
139+
mediaTypeRequiredVulnerabilityAlertsPreview = "application/vnd.github.dorian-preview+json"
137140
)
138141

139142
// A Client manages communication with the GitHub API.

github/repos.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,40 @@ type ListContributorsOptions struct {
462462
ListOptions
463463
}
464464

465+
// EnableVulnerabilityAlerts enables vulnerability alerts and the dependency graph for a repository.
466+
//
467+
// GitHub API docs: https://developer.github.com/v3/repos/#enable-vulnerability-alerts
468+
func (s *RepositoriesService) EnableVulnerabilityAlerts(ctx context.Context, owner, repository string) (*Response, error) {
469+
u := fmt.Sprintf("repos/%v/%v/vulnerability-alerts", owner, repository)
470+
471+
req, err := s.client.NewRequest("PUT", u, nil)
472+
if err != nil {
473+
return nil, err
474+
}
475+
476+
// TODO: remove custom Accept header when this API fully launches
477+
req.Header.Set("Accept", mediaTypeRequiredVulnerabilityAlertsPreview)
478+
479+
return s.client.Do(ctx, req, nil)
480+
}
481+
482+
// DisableVulnerabilityAlerts disables vulnerability alerts and the dependency graph for a repository.
483+
//
484+
// GitHub API docs: https://developer.github.com/v3/repos/#disable-vulnerability-alerts
485+
func (s *RepositoriesService) DisableVulnerabilityAlerts(ctx context.Context, owner, repository string) (*Response, error) {
486+
u := fmt.Sprintf("repos/%v/%v/vulnerability-alerts", owner, repository)
487+
488+
req, err := s.client.NewRequest("DELETE", u, nil)
489+
if err != nil {
490+
return nil, err
491+
}
492+
493+
// TODO: remove custom Accept header when this API fully launches
494+
req.Header.Set("Accept", mediaTypeRequiredVulnerabilityAlertsPreview)
495+
496+
return s.client.Do(ctx, req, nil)
497+
}
498+
465499
// ListContributors lists contributors for a repository.
466500
//
467501
// GitHub API docs: https://developer.github.com/v3/repos/#list-contributors

github/repos_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,38 @@ func TestRepositoriesService_Edit_invalidOwner(t *testing.T) {
367367
testURLParseError(t, err)
368368
}
369369

370+
func TestRepositoriesService_EnableVulnerabilityAlerts(t *testing.T) {
371+
client, mux, _, teardown := setup()
372+
defer teardown()
373+
374+
mux.HandleFunc("/repos/o/r/vulnerability-alerts", func(w http.ResponseWriter, r *http.Request) {
375+
testMethod(t, r, "PUT")
376+
testHeader(t, r, "Accept", mediaTypeRequiredVulnerabilityAlertsPreview)
377+
378+
w.WriteHeader(http.StatusNoContent)
379+
})
380+
381+
if _, err := client.Repositories.EnableVulnerabilityAlerts(context.Background(), "o", "r"); err != nil {
382+
t.Errorf("Repositories.EnableVulnerabilityAlerts returned error: %v", err)
383+
}
384+
}
385+
386+
func TestRepositoriesService_DisableVulnerabilityAlerts(t *testing.T) {
387+
client, mux, _, teardown := setup()
388+
defer teardown()
389+
390+
mux.HandleFunc("/repos/o/r/vulnerability-alerts", func(w http.ResponseWriter, r *http.Request) {
391+
testMethod(t, r, "DELETE")
392+
testHeader(t, r, "Accept", mediaTypeRequiredVulnerabilityAlertsPreview)
393+
394+
w.WriteHeader(http.StatusNoContent)
395+
})
396+
397+
if _, err := client.Repositories.DisableVulnerabilityAlerts(context.Background(), "o", "r"); err != nil {
398+
t.Errorf("Repositories.DisableVulnerabilityAlerts returned error: %v", err)
399+
}
400+
}
401+
370402
func TestRepositoriesService_ListContributors(t *testing.T) {
371403
client, mux, _, teardown := setup()
372404
defer teardown()

0 commit comments

Comments
 (0)