-
Notifications
You must be signed in to change notification settings - Fork 58
/
xrayscan.go
97 lines (79 loc) · 2.74 KB
/
xrayscan.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package buildinfo
import (
"encoding/json"
"github.com/jfrog/jfrog-cli-core/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/utils/config"
"github.com/jfrog/jfrog-client-go/artifactory/services"
clientutils "github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/log"
)
type BuildScanCommand struct {
buildConfiguration *utils.BuildConfiguration
failBuild bool
rtDetails *config.ArtifactoryDetails
}
func NewBuildScanCommand() *BuildScanCommand {
return &BuildScanCommand{}
}
func (bsc *BuildScanCommand) SetRtDetails(rtDetails *config.ArtifactoryDetails) *BuildScanCommand {
bsc.rtDetails = rtDetails
return bsc
}
func (bsc *BuildScanCommand) SetFailBuild(failBuild bool) *BuildScanCommand {
bsc.failBuild = failBuild
return bsc
}
func (bsc *BuildScanCommand) SetBuildConfiguration(buildConfiguration *utils.BuildConfiguration) *BuildScanCommand {
bsc.buildConfiguration = buildConfiguration
return bsc
}
func (bsc *BuildScanCommand) CommandName() string {
return "rt_build_scan"
}
func (bsc *BuildScanCommand) RtDetails() (*config.ArtifactoryDetails, error) {
return bsc.rtDetails, nil
}
func (bsc *BuildScanCommand) Run() error {
log.Info("Triggered Xray build scan... The scan may take a few minutes.")
servicesManager, err := utils.CreateServiceManager(bsc.rtDetails, false)
if err != nil {
return err
}
xrayScanParams := getXrayScanParams(bsc.buildConfiguration.BuildName, bsc.buildConfiguration.BuildNumber)
result, err := servicesManager.XrayScanBuild(xrayScanParams)
if err != nil {
return err
}
var scanResults scanResult
err = json.Unmarshal(result, &scanResults)
if errorutils.CheckError(err) != nil {
return err
}
log.Info("Xray scan completed.")
log.Output(clientutils.IndentJson(result))
// Check if should fail build
if bsc.failBuild && scanResults.Summary.FailBuild {
// We're specifically returning the 'buildScanError' and not a regular error
// to indicate that Xray indeed scanned the build, and the failure is not due to
// networking connectivity or other issues.
return errorutils.CheckError(utils.GetBuildScanError())
}
return err
}
// To unmarshal xray scan summary result
type scanResult struct {
Summary scanSummary `json:"summary,omitempty"`
}
type scanSummary struct {
TotalAlerts int `json:"total_alerts,omitempty"`
FailBuild bool `json:"fail_build,omitempty"`
Message string `json:"message,omitempty"`
Url string `json:"more_details_url,omitempty"`
}
func getXrayScanParams(buildName, buildNumber string) services.XrayScanParams {
xrayScanParams := services.NewXrayScanParams()
xrayScanParams.BuildName = buildName
xrayScanParams.BuildNumber = buildNumber
return xrayScanParams
}