2019-12-13 11:55:45 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2020-05-25 09:44:42 +02:00
|
|
|
sliceUtils "github.com/SAP/jenkins-library/pkg/piperutils"
|
|
|
|
|
2019-12-13 11:55:45 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/command"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
2020-02-04 11:46:43 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/telemetry"
|
2020-07-27 12:01:59 +02:00
|
|
|
"github.com/SAP/jenkins-library/pkg/versioning"
|
2019-12-13 11:55:45 +02:00
|
|
|
)
|
|
|
|
|
2020-02-04 11:46:43 +02:00
|
|
|
func detectExecuteScan(config detectExecuteScanOptions, telemetryData *telemetry.CustomData) {
|
2019-12-13 11:55:45 +02:00
|
|
|
c := command.Command{}
|
|
|
|
// reroute command output to logging framework
|
2020-05-06 13:35:40 +02:00
|
|
|
c.Stdout(log.Writer())
|
|
|
|
c.Stderr(log.Writer())
|
2020-02-04 11:46:43 +02:00
|
|
|
runDetect(config, &c)
|
2019-12-13 11:55:45 +02:00
|
|
|
}
|
|
|
|
|
2020-07-01 11:28:16 +02:00
|
|
|
func runDetect(config detectExecuteScanOptions, command command.ShellRunner) {
|
2019-12-13 11:55:45 +02:00
|
|
|
// detect execution details, see https://synopsys.atlassian.net/wiki/spaces/INTDOCS/pages/88440888/Sample+Synopsys+Detect+Scan+Configuration+Scenarios+for+Black+Duck
|
|
|
|
|
|
|
|
args := []string{"bash <(curl -s https://detect.synopsys.com/detect.sh)"}
|
2020-02-04 11:46:43 +02:00
|
|
|
args = addDetectArgs(args, config)
|
2019-12-13 11:55:45 +02:00
|
|
|
script := strings.Join(args, " ")
|
|
|
|
|
2020-02-27 13:11:22 +02:00
|
|
|
command.SetDir(".")
|
2020-05-25 09:44:42 +02:00
|
|
|
command.SetEnv([]string{"BLACKDUCK_SKIP_PHONE_HOME=true"})
|
2019-12-13 11:55:45 +02:00
|
|
|
|
|
|
|
err := command.RunShell("/bin/bash", script)
|
|
|
|
if err != nil {
|
|
|
|
log.Entry().
|
|
|
|
WithError(err).
|
|
|
|
Fatal("failed to execute detect scan")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 11:46:43 +02:00
|
|
|
func addDetectArgs(args []string, config detectExecuteScanOptions) []string {
|
2019-12-13 11:55:45 +02:00
|
|
|
|
2020-07-27 12:01:59 +02:00
|
|
|
coordinates := struct {
|
|
|
|
Version string
|
|
|
|
}{
|
|
|
|
Version: config.Version,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, detectVersionName := versioning.DetermineProjectCoordinates("", config.VersioningModel, coordinates)
|
|
|
|
|
2020-02-04 11:46:43 +02:00
|
|
|
args = append(args, config.ScanProperties...)
|
2019-12-13 11:55:45 +02:00
|
|
|
|
2020-02-04 11:46:43 +02:00
|
|
|
args = append(args, fmt.Sprintf("--blackduck.url=%v", config.ServerURL))
|
|
|
|
args = append(args, fmt.Sprintf("--blackduck.api.token=%v", config.APIToken))
|
2019-12-13 11:55:45 +02:00
|
|
|
|
2020-02-04 11:46:43 +02:00
|
|
|
args = append(args, fmt.Sprintf("--detect.project.name=%v", config.ProjectName))
|
2020-07-27 12:01:59 +02:00
|
|
|
args = append(args, fmt.Sprintf("--detect.project.version.name=%v", detectVersionName))
|
2020-02-04 11:46:43 +02:00
|
|
|
codeLocation := config.CodeLocation
|
|
|
|
if len(codeLocation) == 0 && len(config.ProjectName) > 0 {
|
2020-07-27 12:01:59 +02:00
|
|
|
codeLocation = fmt.Sprintf("%v/%v", config.ProjectName, detectVersionName)
|
2019-12-13 11:55:45 +02:00
|
|
|
}
|
|
|
|
args = append(args, fmt.Sprintf("--detect.code.location.name=%v", codeLocation))
|
|
|
|
|
2020-04-24 10:41:49 +02:00
|
|
|
if sliceUtils.ContainsString(config.Scanners, "signature") {
|
2020-02-04 11:46:43 +02:00
|
|
|
args = append(args, fmt.Sprintf("--detect.blackduck.signature.scanner.paths=%v", strings.Join(config.ScanPaths, ",")))
|
2019-12-13 11:55:45 +02:00
|
|
|
}
|
|
|
|
|
2020-04-24 10:41:49 +02:00
|
|
|
if sliceUtils.ContainsString(config.Scanners, "source") {
|
2020-02-04 11:46:43 +02:00
|
|
|
args = append(args, fmt.Sprintf("--detect.source.path=%v", config.ScanPaths[0]))
|
2019-12-13 11:55:45 +02:00
|
|
|
}
|
|
|
|
return args
|
|
|
|
}
|