diff --git a/github/github-accessors.go b/github/github-accessors.go index 1fabd520ee..8b82604e9e 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -1886,6 +1886,22 @@ func (a *AutolinkOptions) GetURLTemplate() string { return *a.URLTemplate } +// GetEnabled returns the Enabled field if it's non-nil, zero value otherwise. +func (a *AutomatedSecurityFixes) GetEnabled() bool { + if a == nil || a.Enabled == nil { + return false + } + return *a.Enabled +} + +// GetPaused returns the Paused field if it's non-nil, zero value otherwise. +func (a *AutomatedSecurityFixes) GetPaused() bool { + if a == nil || a.Paused == nil { + return false + } + return *a.Paused +} + // GetAppID returns the AppID field if it's non-nil, zero value otherwise. func (a *AutoTriggerCheck) GetAppID() int64 { if a == nil || a.AppID == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 12d05f2d12..7d8729ee3f 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -2265,6 +2265,26 @@ func TestAutolinkOptions_GetURLTemplate(tt *testing.T) { a.GetURLTemplate() } +func TestAutomatedSecurityFixes_GetEnabled(tt *testing.T) { + var zeroValue bool + a := &AutomatedSecurityFixes{Enabled: &zeroValue} + a.GetEnabled() + a = &AutomatedSecurityFixes{} + a.GetEnabled() + a = nil + a.GetEnabled() +} + +func TestAutomatedSecurityFixes_GetPaused(tt *testing.T) { + var zeroValue bool + a := &AutomatedSecurityFixes{Paused: &zeroValue} + a.GetPaused() + a = &AutomatedSecurityFixes{} + a.GetPaused() + a = nil + a.GetPaused() +} + func TestAutoTriggerCheck_GetAppID(tt *testing.T) { var zeroValue int64 a := &AutoTriggerCheck{AppID: &zeroValue} diff --git a/github/github.go b/github/github.go index 799608359b..39f4479d4a 100644 --- a/github/github.go +++ b/github/github.go @@ -126,9 +126,6 @@ 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 2451ed4368..2309d927a2 100644 --- a/github/repos.go +++ b/github/repos.go @@ -703,6 +703,25 @@ func (s *RepositoriesService) DisableVulnerabilityAlerts(ctx context.Context, ow return s.client.Do(ctx, req, nil) } +// GetAutomatedSecurityFixes checks if the automated security fixes for a repository are enabled. +// +// GitHub API docs: https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#check-if-automated-security-fixes-are-enabled-for-a-repository +func (s *RepositoriesService) GetAutomatedSecurityFixes(ctx context.Context, owner, repository string) (*AutomatedSecurityFixes, *Response, error) { + u := fmt.Sprintf("repos/%v/%v/automated-security-fixes", owner, repository) + + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + p := new(AutomatedSecurityFixes) + resp, err := s.client.Do(ctx, req, p) + if err != nil { + return nil, resp, err + } + return p, resp, nil +} + // EnableAutomatedSecurityFixes enables the automated security fixes for a repository. // // GitHub API docs: https://docs.github.com/en/rest/repos/repos#enable-automated-security-fixes @@ -714,9 +733,6 @@ func (s *RepositoriesService) EnableAutomatedSecurityFixes(ctx context.Context, 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) } @@ -731,9 +747,6 @@ func (s *RepositoriesService) DisableAutomatedSecurityFixes(ctx context.Context, 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) } @@ -1228,6 +1241,12 @@ type SignaturesProtectedBranch struct { Enabled *bool `json:"enabled,omitempty"` } +// AutomatedSecurityFixes represents their status. +type AutomatedSecurityFixes struct { + Enabled *bool `json:"enabled"` + Paused *bool `json:"paused"` +} + // ListBranches lists branches for the specified repository. // // GitHub API docs: https://docs.github.com/en/rest/branches/branches#list-branches diff --git a/github/repos_test.go b/github/repos_test.go index 70321fd7c1..c2abf6da4f 100644 --- a/github/repos_test.go +++ b/github/repos_test.go @@ -653,7 +653,6 @@ func TestRepositoriesService_EnableAutomatedSecurityFixes(t *testing.T) { 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) }) @@ -664,13 +663,49 @@ func TestRepositoriesService_EnableAutomatedSecurityFixes(t *testing.T) { } } +func TestRepositoriesService_GetAutomatedSecurityFixes(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, "GET") + fmt.Fprint(w, `{"enabled": true, "paused": false}`) + }) + + ctx := context.Background() + fixes, _, err := client.Repositories.GetAutomatedSecurityFixes(ctx, "o", "r") + if err != nil { + t.Errorf("Repositories.GetAutomatedSecurityFixes returned errpr: #{err}") + } + + want := &AutomatedSecurityFixes{ + Enabled: Bool(true), + Paused: Bool(false), + } + if !cmp.Equal(fixes, want) { + t.Errorf("Repositories.GetAutomatedSecurityFixes returned #{fixes}, want #{want}") + } + + const methodName = "GetAutomatedSecurityFixes" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Repositories.GetAutomatedSecurityFixes(ctx, "\n", "\n") + return err + }) + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Repositories.GetAutomatedSecurityFixes(ctx, "o", "r") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, 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) })