github.com/SAP/jenkins-library@v1.362.0/cmd/pipelineCreateScanSummary.go (about) 1 package cmd 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "os" 7 8 "github.com/SAP/jenkins-library/pkg/log" 9 "github.com/SAP/jenkins-library/pkg/piperutils" 10 "github.com/SAP/jenkins-library/pkg/reporting" 11 "github.com/SAP/jenkins-library/pkg/telemetry" 12 "github.com/pkg/errors" 13 ) 14 15 type pipelineCreateScanSummaryUtils interface { 16 FileRead(path string) ([]byte, error) 17 FileWrite(path string, content []byte, perm os.FileMode) error 18 Glob(pattern string) (matches []string, err error) 19 } 20 21 type pipelineCreateScanSummaryUtilsBundle struct { 22 *piperutils.Files 23 } 24 25 func newPipelineCreateScanSummaryUtils() pipelineCreateScanSummaryUtils { 26 utils := pipelineCreateScanSummaryUtilsBundle{ 27 Files: &piperutils.Files{}, 28 } 29 return &utils 30 } 31 32 func pipelineCreateScanSummary(config pipelineCreateScanSummaryOptions, telemetryData *telemetry.CustomData) { 33 utils := newPipelineCreateScanSummaryUtils() 34 35 err := runPipelineCreateScanSummary(&config, telemetryData, utils) 36 if err != nil { 37 log.Entry().WithError(err).Fatal("failed to create scan summary") 38 } 39 } 40 41 func runPipelineCreateScanSummary(config *pipelineCreateScanSummaryOptions, telemetryData *telemetry.CustomData, utils pipelineCreateScanSummaryUtils) error { 42 43 pattern := reporting.StepReportDirectory + "/*.json" 44 reports, _ := utils.Glob(pattern) 45 46 scanReports := []reporting.ScanReport{} 47 for _, report := range reports { 48 log.Entry().Debugf("reading file %v", report) 49 reportContent, err := utils.FileRead(report) 50 if err != nil { 51 log.SetErrorCategory(log.ErrorConfiguration) 52 return errors.Wrapf(err, "failed to read report %v", report) 53 } 54 scanReport := reporting.ScanReport{} 55 if err = json.Unmarshal(reportContent, &scanReport); err != nil { 56 return errors.Wrapf(err, "failed to parse report %v", report) 57 } 58 scanReports = append(scanReports, scanReport) 59 } 60 61 output := []byte{} 62 if len(config.PipelineLink) > 0 { 63 output = []byte(fmt.Sprintf("## Pipeline Source for Details\n\nAs listed results might be incomplete, it is crucial that you check the detailed [pipeline](%v) status.\n\n", config.PipelineLink)) 64 } 65 for _, scanReport := range scanReports { 66 if (config.FailedOnly && !scanReport.SuccessfulScan) || !config.FailedOnly { 67 mdReport, _ := scanReport.ToMarkdown() 68 output = append(output, mdReport...) 69 } 70 } 71 72 if err := utils.FileWrite(config.OutputFilePath, output, 0666); err != nil { 73 log.SetErrorCategory(log.ErrorConfiguration) 74 return errors.Wrapf(err, "failed to write %v", config.OutputFilePath) 75 } 76 77 return nil 78 }