1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/cmd/pipelineCreateScanSummary.go
Oliver Nocon d47a17c8fc
feat(whitesource): consolidated reporting and versioning alignment (#2571)
* update reporting and add todo comments

* enhance reporting, allow directory creation for reports

* properly pass reports

* update templating and increase verbosity of errors

* add todo

* add detail table

* update sorting

* add test and improve error message

* fix error message in test

* extend tests

* enhance tests

* enhance versioning behavior accoring to #1846

* create markdown overview report

* small fix

* fix small issue

* make sure that report directory exists

* align reporting directory with default directory from UA

* add missing comments

* add policy check incl. tests

* enhance logging and tests

* update versioning to allow custom version usage properly

* fix report paths and golang image

* update styling of md

* update test
2021-02-10 16:18:00 +01:00

74 lines
2.2 KiB
Go

package cmd
import (
"encoding/json"
"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 {
pattern := reporting.MarkdownReportDirectory + "/*.json"
reports, _ := utils.Glob(pattern)
scanReports := []reporting.ScanReport{}
for _, report := range reports {
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{}
for _, scanReport := range scanReports {
if (config.FailedOnly && !scanReport.SuccessfulScan) || !config.FailedOnly {
mdReport, _ := scanReport.ToMarkdown()
output = append(output, mdReport...)
}
}
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
}