11package enforcer
22
33import (
4+ "encoding/json"
45 "errors"
56 "fmt"
67 "slices"
@@ -38,7 +39,7 @@ type PolicyEnforcerViolationGenerator struct {
3839 projectKey string
3940 watches []string
4041 // Run options
41- threadId int
42+ resultsOutputDir string
4243}
4344
4445func NewPolicyEnforcerViolationGenerator () * PolicyEnforcerViolationGenerator {
@@ -61,7 +62,15 @@ func WithProjectKey(projectKey string) policy.PolicyHandlerOption {
6162 }
6263}
6364
64- func WithParams (repo , path string ) policy.PolicyHandlerOption {
65+ func WithResultsOutputDir (resultsOutputDir string ) policy.PolicyHandlerOption {
66+ return func (generator policy.PolicyHandler ) {
67+ if p , ok := generator .(* PolicyEnforcerViolationGenerator ); ok {
68+ p .resultsOutputDir = resultsOutputDir
69+ }
70+ }
71+ }
72+
73+ func WithArtifactParams (repo , path string ) policy.PolicyHandlerOption {
6574 return func (generator policy.PolicyHandler ) {
6675 if p , ok := generator .(* PolicyEnforcerViolationGenerator ); ok {
6776 p .rtRepository = repo
@@ -119,9 +128,23 @@ func (p *PolicyEnforcerViolationGenerator) GenerateViolations(cmdResults *result
119128 } else {
120129 log .Debug (fmt .Sprintf ("Xray scans completed with %d violations" , generatedViolations .Total ))
121130 }
131+ if err = dumpViolationsResponseToFileIfNeeded (generatedViolations , p .resultsOutputDir ); err != nil {
132+ return
133+ }
122134 return convertToViolations (cmdResults , generatedViolations .Violations )
123135}
124136
137+ func dumpViolationsResponseToFileIfNeeded (generatedViolations * services.ViolationsResponse , resultsOutputDir string ) (err error ) {
138+ if resultsOutputDir == "" {
139+ return
140+ }
141+ fileContent , err := json .Marshal (generatedViolations )
142+ if err != nil {
143+ return fmt .Errorf ("failed to write fetched violations to file: %s" , err .Error ())
144+ }
145+ return utils .DumpJsonContentToFile (fileContent , resultsOutputDir , "violations" , - 1 )
146+ }
147+
125148func convertToViolations (cmdResults * results.SecurityCommandResults , generatedViolations []services.XrayViolation ) (convertedViolations violationutils.Violations , err error ) {
126149 convertedViolations = violationutils.Violations {}
127150 for _ , violation := range generatedViolations {
0 commit comments