Skip to content

Commit

Permalink
Add GetAutomatedSecurityFixes to report status (#2842)
Browse files Browse the repository at this point in the history
Fixes: #2080.
  • Loading branch information
grahamhar authored Aug 15, 2023
1 parent bb00f57 commit 352e4c8
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 11 deletions.
16 changes: 16 additions & 0 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions github/github-accessors_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
31 changes: 25 additions & 6 deletions github/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down
39 changes: 37 additions & 2 deletions github/repos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand All @@ -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)
})
Expand Down

0 comments on commit 352e4c8

Please sign in to comment.