github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/artifactory/commands/buildinfo/xrayscan.go (about) 1 package buildinfo 2 3 import ( 4 "encoding/json" 5 "github.com/jfrog/jfrog-cli-go/artifactory/utils" 6 "github.com/jfrog/jfrog-cli-go/utils/config" 7 "github.com/jfrog/jfrog-client-go/artifactory/services" 8 clientutils "github.com/jfrog/jfrog-client-go/utils" 9 "github.com/jfrog/jfrog-client-go/utils/errorutils" 10 "github.com/jfrog/jfrog-client-go/utils/log" 11 ) 12 13 type BuildScanCommand struct { 14 buildConfiguration *utils.BuildConfiguration 15 failBuild bool 16 rtDetails *config.ArtifactoryDetails 17 } 18 19 func NewBuildScanCommand() *BuildScanCommand { 20 return &BuildScanCommand{} 21 } 22 23 func (bsc *BuildScanCommand) SetRtDetails(rtDetails *config.ArtifactoryDetails) *BuildScanCommand { 24 bsc.rtDetails = rtDetails 25 return bsc 26 } 27 28 func (bsc *BuildScanCommand) SetFailBuild(failBuild bool) *BuildScanCommand { 29 bsc.failBuild = failBuild 30 return bsc 31 } 32 33 func (bsc *BuildScanCommand) SetBuildConfiguration(buildConfiguration *utils.BuildConfiguration) *BuildScanCommand { 34 bsc.buildConfiguration = buildConfiguration 35 return bsc 36 } 37 38 func (bsc *BuildScanCommand) CommandName() string { 39 return "rt_build_scan" 40 } 41 42 func (bsc *BuildScanCommand) RtDetails() (*config.ArtifactoryDetails, error) { 43 return bsc.rtDetails, nil 44 } 45 46 func (bsc *BuildScanCommand) Run() error { 47 log.Info("Triggered Xray build scan... The scan may take a few minutes.") 48 servicesManager, err := utils.CreateServiceManager(bsc.rtDetails, false) 49 if err != nil { 50 return err 51 } 52 53 xrayScanParams := getXrayScanParams(bsc.buildConfiguration.BuildName, bsc.buildConfiguration.BuildNumber) 54 result, err := servicesManager.XrayScanBuild(xrayScanParams) 55 if err != nil { 56 return err 57 } 58 59 var scanResults scanResult 60 err = json.Unmarshal(result, &scanResults) 61 if errorutils.CheckError(err) != nil { 62 return err 63 } 64 65 log.Info("Xray scan completed.") 66 log.Output(clientutils.IndentJson(result)) 67 68 // Check if should fail build 69 if bsc.failBuild && scanResults.Summary.FailBuild { 70 // We're specifically returning the 'buildScanError' and not a regular error 71 // to indicate that Xray indeed scanned the build, and the failure is not due to 72 // networking connectivity or other issues. 73 return errorutils.CheckError(utils.GetBuildScanError()) 74 } 75 76 return err 77 } 78 79 // To unmarshal xray scan summary result 80 type scanResult struct { 81 Summary scanSummary `json:"summary,omitempty"` 82 } 83 84 type scanSummary struct { 85 TotalAlerts int `json:"total_alerts,omitempty"` 86 FailBuild bool `json:"fail_build,omitempty"` 87 Message string `json:"message,omitempty"` 88 Url string `json:"more_details_url,omitempty"` 89 } 90 91 func getXrayScanParams(buildName, buildNumber string) services.XrayScanParams { 92 xrayScanParams := services.NewXrayScanParams() 93 xrayScanParams.BuildName = buildName 94 xrayScanParams.BuildNumber = buildNumber 95 96 return xrayScanParams 97 }