github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/pkg/whitesource/scanReports.go (about) 1 package whitesource 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "strings" 8 9 "github.com/SAP/jenkins-library/pkg/log" 10 "github.com/SAP/jenkins-library/pkg/piperutils" 11 "github.com/pkg/errors" 12 ) 13 14 // ReportOptions defines options for downloading reports after scanning. 15 type ReportOptions struct { 16 // ReportDirectory defines the target directory for downloading reports. 17 ReportDirectory string 18 // VulnerabilityReportFormat defines the requested file format of the vulnerability report (i.e. pdf). 19 VulnerabilityReportFormat string 20 } 21 22 type scanUtils interface { 23 MkdirAll(path string, perm os.FileMode) error 24 FileWrite(path string, content []byte, perm os.FileMode) error 25 } 26 27 // DownloadReports downloads a Project's risk and vulnerability reports 28 func (s *Scan) DownloadReports(options ReportOptions, utils scanUtils, sys whitesource) ([]piperutils.Path, error) { 29 if err := utils.MkdirAll(options.ReportDirectory, os.ModePerm); err != nil { 30 return nil, err 31 } 32 33 var paths []piperutils.Path 34 for _, project := range s.scannedProjects { 35 vulnPath, err := downloadVulnerabilityReport(options, project, utils, sys) 36 if err != nil { 37 return nil, err 38 } 39 riskPath, err := downloadRiskReport(options, project, utils, sys) 40 if err != nil { 41 return nil, err 42 } 43 paths = append(paths, *vulnPath, *riskPath) 44 } 45 return paths, nil 46 } 47 48 func downloadVulnerabilityReport(options ReportOptions, project Project, utils scanUtils, sys whitesource) (*piperutils.Path, error) { 49 reportBytes, err := sys.GetProjectVulnerabilityReport(project.Token, options.VulnerabilityReportFormat) 50 if err != nil { 51 return nil, errors.Wrapf(err, "unable to download vulnerability report from url") 52 } 53 54 rptFileName := fmt.Sprintf("%s-vulnerability-report.%s", strings.ReplaceAll(project.Name, "/", "_"), options.VulnerabilityReportFormat) 55 rptFileName = filepath.Join(options.ReportDirectory, rptFileName) 56 if err := utils.FileWrite(rptFileName, reportBytes, 0644); err != nil { 57 return nil, errors.Wrapf(err, "unable to copy content from url to file %v", rptFileName) 58 } 59 60 log.Entry().Infof("Successfully downloaded vulnerability report to %s", rptFileName) 61 pathName := fmt.Sprintf("%s Vulnerability Report", project.Name) 62 return &piperutils.Path{Name: pathName, Target: rptFileName}, nil 63 } 64 65 func downloadRiskReport(options ReportOptions, project Project, utils scanUtils, sys whitesource) (*piperutils.Path, error) { 66 reportBytes, err := sys.GetProjectRiskReport(project.Token) 67 if err != nil { 68 return nil, errors.Wrapf(err, "unable to download risk report from url") 69 } 70 71 rptFileName := fmt.Sprintf("%s-risk-report.pdf", strings.ReplaceAll(project.Name, "/", "_")) 72 rptFileName = filepath.Join(options.ReportDirectory, rptFileName) 73 if err := utils.FileWrite(rptFileName, reportBytes, 0644); err != nil { 74 return nil, errors.Wrapf(err, "unable to copy content from url to file %v", rptFileName) 75 } 76 77 log.Entry().Infof("Successfully downloaded risk report to %s", rptFileName) 78 pathName := fmt.Sprintf("%s PDF Risk Report", project.Name) 79 return &piperutils.Path{Name: pathName, Target: rptFileName}, nil 80 }