-
Notifications
You must be signed in to change notification settings - Fork 583
/
pipelineCreateScanSummary.go
78 lines (65 loc) · 2.44 KB
/
pipelineCreateScanSummary.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package cmd
import (
"encoding/json"
"fmt"
"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.StepReportDirectory + "/*.json"
reports, _ := utils.Glob(pattern)
scanReports := []reporting.ScanReport{}
for _, report := range reports {
log.Entry().Debugf("reading file %v", report)
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{}
if len(config.PipelineLink) > 0 {
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))
}
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
}