From ae920610bb8700b8cfdcf7bb313eef1fe1177886 Mon Sep 17 00:00:00 2001 From: Carlos Tadeu Panato Junior Date: Tue, 11 Jun 2019 14:00:20 +0200 Subject: [PATCH] Add Automated Security Fixes API preview endpoint (#1188) Fixes #1184. --- github/github.go | 3 +++ github/repos.go | 34 ++++++++++++++++++++++++++++++++++ github/repos_test.go | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/github/github.go b/github/github.go index 63f8a966439..45a72d7acbe 100644 --- a/github/github.go +++ b/github/github.go @@ -138,6 +138,9 @@ const ( // https://developer.github.com/changes/2019-04-24-vulnerability-alerts/ mediaTypeRequiredVulnerabilityAlertsPreview = "application/vnd.github.dorian-preview+json" + // https://developer.github.com/changes/2019-06-04-automated-security-fixes/ + mediaTypeRequiredAutomatedSecurityFixesPreview = "application/vnd.github.london-preview+json" + // https://developer.github.com/changes/2019-05-29-update-branch-api/ mediaTypeUpdatePullRequestBranchPreview = "application/vnd.github.lydian-preview+json" diff --git a/github/repos.go b/github/repos.go index 154d28bb56a..5f44c6d86fc 100644 --- a/github/repos.go +++ b/github/repos.go @@ -496,6 +496,40 @@ func (s *RepositoriesService) DisableVulnerabilityAlerts(ctx context.Context, ow return s.client.Do(ctx, req, nil) } +// EnableAutomatedSecurityFixes enables the automated security fixes for a repository. +// +// GitHub API docs: https://developer.github.com/v3/repos/#enable-automated-security-fixes +func (s *RepositoriesService) EnableAutomatedSecurityFixes(ctx context.Context, owner, repository string) (*Response, error) { + u := fmt.Sprintf("repos/%v/%v/automated-security-fixes", owner, repository) + + req, err := s.client.NewRequest("PUT", u, nil) + if err != nil { + return nil, err + } + + // TODO: remove custom Accept header when this API fully launches + req.Header.Set("Accept", mediaTypeRequiredAutomatedSecurityFixesPreview) + + return s.client.Do(ctx, req, nil) +} + +// DisableAutomatedSecurityFixes disables vulnerability alerts and the dependency graph for a repository. +// +// GitHub API docs: https://developer.github.com/v3/repos/#disable-automated-security-fixes +func (s *RepositoriesService) DisableAutomatedSecurityFixes(ctx context.Context, owner, repository string) (*Response, error) { + u := fmt.Sprintf("repos/%v/%v/automated-security-fixes", owner, repository) + + req, err := s.client.NewRequest("DELETE", u, nil) + if err != nil { + return nil, err + } + + // TODO: remove custom Accept header when this API fully launches + req.Header.Set("Accept", mediaTypeRequiredAutomatedSecurityFixesPreview) + + return s.client.Do(ctx, req, nil) +} + // ListContributors lists contributors for a repository. // // GitHub API docs: https://developer.github.com/v3/repos/#list-contributors diff --git a/github/repos_test.go b/github/repos_test.go index cb91a38671e..6dd9973cd59 100644 --- a/github/repos_test.go +++ b/github/repos_test.go @@ -399,6 +399,38 @@ func TestRepositoriesService_DisableVulnerabilityAlerts(t *testing.T) { } } +func TestRepositoriesService_EnableAutomatedSecurityFixes(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/automated-security-fixes", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + testHeader(t, r, "Accept", mediaTypeRequiredAutomatedSecurityFixesPreview) + + w.WriteHeader(http.StatusNoContent) + }) + + if _, err := client.Repositories.EnableAutomatedSecurityFixes(context.Background(), "o", "r"); err != nil { + t.Errorf("Repositories.EnableAutomatedSecurityFixes returned error: %v", err) + } +} + +func TestRepositoriesService_DisableAutomatedSecurityFixes(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/automated-security-fixes", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + testHeader(t, r, "Accept", mediaTypeRequiredAutomatedSecurityFixesPreview) + + w.WriteHeader(http.StatusNoContent) + }) + + if _, err := client.Repositories.DisableAutomatedSecurityFixes(context.Background(), "o", "r"); err != nil { + t.Errorf("Repositories.DisableAutomatedSecurityFixes returned error: %v", err) + } +} + func TestRepositoriesService_ListContributors(t *testing.T) { client, mux, _, teardown := setup() defer teardown()