2021-02-02 15:36:40 +02:00
package cmd
import (
"encoding/json"
2021-05-19 07:57:44 +02:00
"fmt"
2021-02-02 15:36:40 +02:00
"os"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/piperutils"
"github.com/SAP/jenkins-library/pkg/reporting"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/pkg/errors"
)
type pipelineCreateScanSummaryUtils interface {
FileRead ( path string ) ( [ ] byte , error )
FileWrite ( path string , content [ ] byte , perm os . FileMode ) error
Glob ( pattern string ) ( matches [ ] string , err error )
}
type pipelineCreateScanSummaryUtilsBundle struct {
* piperutils . Files
}
func newPipelineCreateScanSummaryUtils ( ) pipelineCreateScanSummaryUtils {
utils := pipelineCreateScanSummaryUtilsBundle {
Files : & piperutils . Files { } ,
}
return & utils
}
func pipelineCreateScanSummary ( config pipelineCreateScanSummaryOptions , telemetryData * telemetry . CustomData ) {
utils := newPipelineCreateScanSummaryUtils ( )
err := runPipelineCreateScanSummary ( & config , telemetryData , utils )
if err != nil {
log . Entry ( ) . WithError ( err ) . Fatal ( "failed to create scan summary" )
}
}
func runPipelineCreateScanSummary ( config * pipelineCreateScanSummaryOptions , telemetryData * telemetry . CustomData , utils pipelineCreateScanSummaryUtils ) error {
2021-03-19 12:10:08 +02:00
pattern := reporting . StepReportDirectory + "/*.json"
2021-02-02 15:36:40 +02:00
reports , _ := utils . Glob ( pattern )
scanReports := [ ] reporting . ScanReport { }
for _ , report := range reports {
2021-04-15 07:45:06 +02:00
log . Entry ( ) . Debugf ( "reading file %v" , report )
2021-02-02 15:36:40 +02:00
reportContent , err := utils . FileRead ( report )
if err != nil {
log . SetErrorCategory ( log . ErrorConfiguration )
return errors . Wrapf ( err , "failed to read report %v" , report )
}
scanReport := reporting . ScanReport { }
if err = json . Unmarshal ( reportContent , & scanReport ) ; err != nil {
return errors . Wrapf ( err , "failed to parse report %v" , report )
}
scanReports = append ( scanReports , scanReport )
}
output := [ ] byte { }
2021-05-19 07:57:44 +02:00
if len ( config . PipelineLink ) > 0 {
2021-07-20 16:38:11 +02:00
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 ) )
2021-05-19 07:57:44 +02:00
}
2021-02-02 15:36:40 +02:00
for _ , scanReport := range scanReports {
if ( config . FailedOnly && ! scanReport . SuccessfulScan ) || ! config . FailedOnly {
2021-02-10 17:18:00 +02:00
mdReport , _ := scanReport . ToMarkdown ( )
output = append ( output , mdReport ... )
2021-02-02 15:36:40 +02:00
}
}
if err := utils . FileWrite ( config . OutputFilePath , output , 0666 ) ; err != nil {
log . SetErrorCategory ( log . ErrorConfiguration )
return errors . Wrapf ( err , "failed to write %v" , config . OutputFilePath )
}
return nil
}