Skip to content

Commit 4a5aa8c

Browse files
Enable secret scanning with the enterprise-level REST API (#2607)
Fixes: #2599.
1 parent 84cc7d5 commit 4a5aa8c

File tree

4 files changed

+282
-0
lines changed

4 files changed

+282
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright 2022 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package github
7+
8+
import (
9+
"context"
10+
"fmt"
11+
)
12+
13+
// EnterpriseSecurityAnalysisSettings represents security analysis settings for an enterprise.
14+
type EnterpriseSecurityAnalysisSettings struct {
15+
AdvancedSecurityEnabledForNewRepositories *bool `json:"advanced_security_enabled_for_new_repositories,omitempty"`
16+
SecretScanningEnabledForNewRepositories *bool `json:"secret_scanning_enabled_for_new_repositories,omitempty"`
17+
SecretScanningPushProtectionEnabledForNewRepositories *bool `json:"secret_scanning_push_protection_enabled_for_new_repositories,omitempty"`
18+
SecretScanningPushProtectionCustomLink *string `json:"secret_scanning_push_protection_custom_link,omitempty"`
19+
}
20+
21+
// GetCodeSecurityAndAnalysis gets code security and analysis features for an enterprise.
22+
//
23+
// GitHub API docs: https://docs.github.com/en/rest/enterprise-admin/code-security-and-analysis?apiVersion=2022-11-28#get-code-security-and-analysis-features-for-an-enterprise
24+
func (s *EnterpriseService) GetCodeSecurityAndAnalysis(ctx context.Context, enterprise string) (*EnterpriseSecurityAnalysisSettings, *Response, error) {
25+
u := fmt.Sprintf("enterprises/%v/code_security_and_analysis", enterprise)
26+
27+
req, err := s.client.NewRequest("GET", u, nil)
28+
if err != nil {
29+
return nil, nil, err
30+
}
31+
32+
settings := new(EnterpriseSecurityAnalysisSettings)
33+
resp, err := s.client.Do(ctx, req, settings)
34+
if err != nil {
35+
return nil, resp, err
36+
}
37+
38+
return settings, resp, nil
39+
}
40+
41+
// UpdateCodeSecurityAndAnalysis updates code security and analysis features for new repositories in an enterprise.
42+
//
43+
// GitHub API docs: https://docs.github.com/en/rest/enterprise-admin/code-security-and-analysis?apiVersion=2022-11-28#update-code-security-and-analysis-features-for-an-enterprise
44+
func (s *EnterpriseService) UpdateCodeSecurityAndAnalysis(ctx context.Context, enterprise string, settings *EnterpriseSecurityAnalysisSettings) (*Response, error) {
45+
u := fmt.Sprintf("enterprises/%v/code_security_and_analysis", enterprise)
46+
req, err := s.client.NewRequest("PATCH", u, settings)
47+
if err != nil {
48+
return nil, err
49+
}
50+
51+
resp, err := s.client.Do(ctx, req, nil)
52+
if err != nil {
53+
return resp, err
54+
}
55+
56+
return resp, nil
57+
}
58+
59+
// EnableDisableSecurityFeature enables or disables a security feature for all repositories in an enterprise.
60+
//
61+
// Valid values for securityProduct: "advanced_security", "secret_scanning", "secret_scanning_push_protection".
62+
// Valid values for enablement: "enable_all", "disable_all".
63+
//
64+
// GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/enterprise-admin/code-security-and-analysis?apiVersion=2022-11-28#enable-or-disable-a-security-feature
65+
func (s *EnterpriseService) EnableDisableSecurityFeature(ctx context.Context, enterprise, securityProduct, enablement string) (*Response, error) {
66+
u := fmt.Sprintf("enterprises/%v/%v/%v", enterprise, securityProduct, enablement)
67+
req, err := s.client.NewRequest("POST", u, nil)
68+
if err != nil {
69+
return nil, err
70+
}
71+
72+
resp, err := s.client.Do(ctx, req, nil)
73+
if err != nil {
74+
return resp, err
75+
}
76+
77+
return resp, nil
78+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Copyright 2022 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package github
7+
8+
import (
9+
"context"
10+
"encoding/json"
11+
"fmt"
12+
"net/http"
13+
"testing"
14+
15+
"github.com/google/go-cmp/cmp"
16+
)
17+
18+
func TestEnterpriseService_GetCodeSecurityAndAnalysis(t *testing.T) {
19+
client, mux, _, teardown := setup()
20+
defer teardown()
21+
22+
mux.HandleFunc("/enterprises/e/code_security_and_analysis", func(w http.ResponseWriter, r *http.Request) {
23+
testMethod(t, r, "GET")
24+
25+
fmt.Fprint(w, `
26+
{
27+
"advanced_security_enabled_for_new_repositories": true,
28+
"secret_scanning_enabled_for_new_repositories": true,
29+
"secret_scanning_push_protection_enabled_for_new_repositories": true,
30+
"secret_scanning_push_protection_custom_link": "https://github.com/test-org/test-repo/blob/main/README.md"
31+
}`)
32+
})
33+
34+
ctx := context.Background()
35+
36+
const methodName = "GetCodeSecurityAndAnalysis"
37+
38+
settings, _, err := client.Enterprise.GetCodeSecurityAndAnalysis(ctx, "e")
39+
if err != nil {
40+
t.Errorf("Enterprise.%v returned error: %v", methodName, err)
41+
}
42+
want := &EnterpriseSecurityAnalysisSettings{
43+
AdvancedSecurityEnabledForNewRepositories: Bool(true),
44+
SecretScanningEnabledForNewRepositories: Bool(true),
45+
SecretScanningPushProtectionEnabledForNewRepositories: Bool(true),
46+
SecretScanningPushProtectionCustomLink: String("https://github.com/test-org/test-repo/blob/main/README.md"),
47+
}
48+
49+
if !cmp.Equal(settings, want) {
50+
t.Errorf("Enterprise.%v return \ngot: %+v,\nwant:%+v", methodName, settings, want)
51+
}
52+
53+
testBadOptions(t, methodName, func() (err error) {
54+
_, _, err = client.Enterprise.GetCodeSecurityAndAnalysis(ctx, "o")
55+
return err
56+
})
57+
58+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
59+
got, resp, err := client.Enterprise.GetCodeSecurityAndAnalysis(ctx, "e")
60+
if got != nil {
61+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
62+
}
63+
return resp, err
64+
})
65+
}
66+
67+
func TestEnterpriseService_UpdateCodeSecurityAndAnalysis(t *testing.T) {
68+
client, mux, _, teardown := setup()
69+
defer teardown()
70+
71+
input := &EnterpriseSecurityAnalysisSettings{
72+
AdvancedSecurityEnabledForNewRepositories: Bool(true),
73+
SecretScanningEnabledForNewRepositories: Bool(true),
74+
SecretScanningPushProtectionEnabledForNewRepositories: Bool(true),
75+
SecretScanningPushProtectionCustomLink: String("https://github.com/test-org/test-repo/blob/main/README.md"),
76+
}
77+
78+
mux.HandleFunc("/enterprises/e/code_security_and_analysis", func(w http.ResponseWriter, r *http.Request) {
79+
v := new(EnterpriseSecurityAnalysisSettings)
80+
json.NewDecoder(r.Body).Decode(v)
81+
82+
testMethod(t, r, "PATCH")
83+
if !cmp.Equal(v, input) {
84+
t.Errorf("Request body = %+v, want %+v", v, input)
85+
}
86+
})
87+
88+
ctx := context.Background()
89+
90+
const methodName = "UpdateCodeSecurityAndAnalysis"
91+
92+
_, err := client.Enterprise.UpdateCodeSecurityAndAnalysis(ctx, "e", input)
93+
if err != nil {
94+
t.Errorf("Enterprise.%v returned error: %v", methodName, err)
95+
}
96+
97+
testBadOptions(t, methodName, func() (err error) {
98+
_, err = client.Enterprise.UpdateCodeSecurityAndAnalysis(ctx, "o", input)
99+
return err
100+
})
101+
102+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
103+
return client.Enterprise.UpdateCodeSecurityAndAnalysis(ctx, "e", input)
104+
})
105+
}
106+
107+
func TestEnterpriseService_EnableAdvancedSecurity(t *testing.T) {
108+
client, mux, _, teardown := setup()
109+
defer teardown()
110+
111+
mux.HandleFunc("/enterprises/e/advanced_security/enable_all", func(w http.ResponseWriter, r *http.Request) {
112+
testMethod(t, r, "POST")
113+
})
114+
115+
ctx := context.Background()
116+
117+
const methodName = "EnableDisableSecurityFeature"
118+
119+
_, err := client.Enterprise.EnableDisableSecurityFeature(ctx, "e", "advanced_security", "enable_all")
120+
if err != nil {
121+
t.Errorf("Enterprise.%v returned error: %v", methodName, err)
122+
}
123+
124+
testBadOptions(t, methodName, func() (err error) {
125+
_, err = client.Enterprise.EnableDisableSecurityFeature(ctx, "o", "advanced_security", "enable_all")
126+
return err
127+
})
128+
129+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
130+
return client.Enterprise.EnableDisableSecurityFeature(ctx, "e", "advanced_security", "enable_all")
131+
})
132+
}

github/github-accessors.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-accessors_test.go

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)