diff --git a/github/event_types_test.go b/github/event_types_test.go index d11aa7c2834..1798cb1e645 100644 --- a/github/event_types_test.go +++ b/github/event_types_test.go @@ -18215,6 +18215,9 @@ func TestSecurityAndAnalysisEvent_Marshal(t *testing.T) { DependabotSecurityUpdates: &DependabotSecurityUpdates{ Status: Ptr("enabled"), }, + CodeSecurity: &CodeSecurity{ + Status: Ptr("enabled"), + }, }, }, }, @@ -18377,6 +18380,9 @@ func TestSecurityAndAnalysisEvent_Marshal(t *testing.T) { }, "dependabot_security_updates": { "status": "enabled" + }, + "code_security": { + "status": "enabled" } } } diff --git a/github/github-accessors.go b/github/github-accessors.go index f925dff0d29..e8387d6f06c 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -4990,6 +4990,14 @@ func (c *CodeSearchResult) GetTotal() int { return *c.Total } +// GetStatus returns the Status field if it's non-nil, zero value otherwise. +func (c *CodeSecurity) GetStatus() string { + if c == nil || c.Status == nil { + return "" + } + return *c.Status +} + // GetAdvancedSecurity returns the AdvancedSecurity field if it's non-nil, zero value otherwise. func (c *CodeSecurityConfiguration) GetAdvancedSecurity() string { if c == nil || c.AdvancedSecurity == nil { @@ -36894,6 +36902,14 @@ func (s *SecurityAndAnalysis) GetAdvancedSecurity() *AdvancedSecurity { return s.AdvancedSecurity } +// GetCodeSecurity returns the CodeSecurity field. +func (s *SecurityAndAnalysis) GetCodeSecurity() *CodeSecurity { + if s == nil { + return nil + } + return s.CodeSecurity +} + // GetDependabotSecurityUpdates returns the DependabotSecurityUpdates field. func (s *SecurityAndAnalysis) GetDependabotSecurityUpdates() *DependabotSecurityUpdates { if s == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 2b556a2ccce..fd65a63a20f 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -6362,6 +6362,17 @@ func TestCodeSearchResult_GetTotal(tt *testing.T) { c.GetTotal() } +func TestCodeSecurity_GetStatus(tt *testing.T) { + tt.Parallel() + var zeroValue string + c := &CodeSecurity{Status: &zeroValue} + c.GetStatus() + c = &CodeSecurity{} + c.GetStatus() + c = nil + c.GetStatus() +} + func TestCodeSecurityConfiguration_GetAdvancedSecurity(tt *testing.T) { tt.Parallel() var zeroValue string @@ -46366,6 +46377,14 @@ func TestSecurityAndAnalysis_GetAdvancedSecurity(tt *testing.T) { s.GetAdvancedSecurity() } +func TestSecurityAndAnalysis_GetCodeSecurity(tt *testing.T) { + tt.Parallel() + s := &SecurityAndAnalysis{} + s.GetCodeSecurity() + s = nil + s.GetCodeSecurity() +} + func TestSecurityAndAnalysis_GetDependabotSecurityUpdates(tt *testing.T) { tt.Parallel() s := &SecurityAndAnalysis{} diff --git a/github/github-stringify_test.go b/github/github-stringify_test.go index ab026ade256..5639c1475bf 100644 --- a/github/github-stringify_test.go +++ b/github/github-stringify_test.go @@ -355,6 +355,17 @@ func TestCodeResult_String(t *testing.T) { } } +func TestCodeSecurity_String(t *testing.T) { + t.Parallel() + v := CodeSecurity{ + Status: Ptr(""), + } + want := `github.CodeSecurity{Status:""}` + if got := v.String(); got != want { + t.Errorf("CodeSecurity.String = %v, want %v", got, want) + } +} + func TestCombinedStatus_String(t *testing.T) { t.Parallel() v := CombinedStatus{ @@ -2201,8 +2212,9 @@ func TestSecurityAndAnalysis_String(t *testing.T) { SecretScanningPushProtection: &SecretScanningPushProtection{}, DependabotSecurityUpdates: &DependabotSecurityUpdates{}, SecretScanningValidityChecks: &SecretScanningValidityChecks{}, + CodeSecurity: &CodeSecurity{}, } - want := `github.SecurityAndAnalysis{AdvancedSecurity:github.AdvancedSecurity{}, SecretScanning:github.SecretScanning{}, SecretScanningPushProtection:github.SecretScanningPushProtection{}, DependabotSecurityUpdates:github.DependabotSecurityUpdates{}, SecretScanningValidityChecks:github.SecretScanningValidityChecks{}}` + want := `github.SecurityAndAnalysis{AdvancedSecurity:github.AdvancedSecurity{}, SecretScanning:github.SecretScanning{}, SecretScanningPushProtection:github.SecretScanningPushProtection{}, DependabotSecurityUpdates:github.DependabotSecurityUpdates{}, SecretScanningValidityChecks:github.SecretScanningValidityChecks{}, CodeSecurity:github.CodeSecurity{}}` if got := v.String(); got != want { t.Errorf("SecurityAndAnalysis.String = %v, want %v", got, want) } diff --git a/github/repos.go b/github/repos.go index 315b5ca49ec..31edd8eaad5 100644 --- a/github/repos.go +++ b/github/repos.go @@ -200,6 +200,7 @@ type SecurityAndAnalysis struct { SecretScanningPushProtection *SecretScanningPushProtection `json:"secret_scanning_push_protection,omitempty"` DependabotSecurityUpdates *DependabotSecurityUpdates `json:"dependabot_security_updates,omitempty"` SecretScanningValidityChecks *SecretScanningValidityChecks `json:"secret_scanning_validity_checks,omitempty"` + CodeSecurity *CodeSecurity `json:"code_security,omitempty"` } // RepositoryPermissions represents the permissions a user has for a repository. @@ -266,6 +267,17 @@ type SecretScanningValidityChecks struct { Status *string `json:"status,omitempty"` } +// CodeSecurity represents the state of code security on a repository. +// +// GitHub API docs: https://docs.github.com/en/code-security/getting-started/github-security-features#available-with-github-code-security +type CodeSecurity struct { + Status *string `json:"status,omitempty"` +} + +func (c CodeSecurity) String() string { + return Stringify(c) +} + // List calls either RepositoriesService.ListByUser or RepositoriesService.ListByAuthenticatedUser // depending on whether user is empty. // diff --git a/github/repos_test.go b/github/repos_test.go index 17b0737c955..ce5be03c187 100644 --- a/github/repos_test.go +++ b/github/repos_test.go @@ -411,7 +411,7 @@ func TestRepositoriesService_Get(t *testing.T) { mux.HandleFunc("/repos/o/r", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "GET") testHeader(t, r, "Accept", strings.Join(wantAcceptHeaders, ", ")) - fmt.Fprint(w, `{"id":1,"name":"n","description":"d","owner":{"login":"l"},"license":{"key":"mit"},"security_and_analysis":{"advanced_security":{"status":"enabled"},"secret_scanning":{"status":"enabled"},"secret_scanning_push_protection":{"status":"enabled"},"dependabot_security_updates":{"status": "enabled"}, "secret_scanning_validity_checks":{"status":"enabled"}}}`) + fmt.Fprint(w, `{"id":1,"name":"n","description":"d","owner":{"login":"l"},"license":{"key":"mit"},"security_and_analysis":{"advanced_security":{"status":"enabled"},"secret_scanning":{"status":"enabled"},"secret_scanning_push_protection":{"status":"enabled"},"dependabot_security_updates":{"status": "enabled"}, "secret_scanning_validity_checks":{"status":"enabled"}, "code_security":{"status":"enabled"}}}`) }) ctx := t.Context() @@ -420,7 +420,7 @@ func TestRepositoriesService_Get(t *testing.T) { t.Errorf("Repositories.Get returned error: %v", err) } - want := &Repository{ID: Ptr(int64(1)), Name: Ptr("n"), Description: Ptr("d"), Owner: &User{Login: Ptr("l")}, License: &License{Key: Ptr("mit")}, SecurityAndAnalysis: &SecurityAndAnalysis{AdvancedSecurity: &AdvancedSecurity{Status: Ptr("enabled")}, SecretScanning: &SecretScanning{Ptr("enabled")}, SecretScanningPushProtection: &SecretScanningPushProtection{Ptr("enabled")}, DependabotSecurityUpdates: &DependabotSecurityUpdates{Ptr("enabled")}, SecretScanningValidityChecks: &SecretScanningValidityChecks{Ptr("enabled")}}} + want := &Repository{ID: Ptr(int64(1)), Name: Ptr("n"), Description: Ptr("d"), Owner: &User{Login: Ptr("l")}, License: &License{Key: Ptr("mit")}, SecurityAndAnalysis: &SecurityAndAnalysis{AdvancedSecurity: &AdvancedSecurity{Status: Ptr("enabled")}, SecretScanning: &SecretScanning{Ptr("enabled")}, SecretScanningPushProtection: &SecretScanningPushProtection{Ptr("enabled")}, DependabotSecurityUpdates: &DependabotSecurityUpdates{Ptr("enabled")}, SecretScanningValidityChecks: &SecretScanningValidityChecks{Ptr("enabled")}, CodeSecurity: &CodeSecurity{Ptr("enabled")}}} if !cmp.Equal(got, want) { t.Errorf("Repositories.Get returned %+v, want %+v", got, want) }