diff --git a/scanpullrequest/scanpullrequest.go b/scanpullrequest/scanpullrequest.go index 5e0863677..a6ee115d6 100644 --- a/scanpullrequest/scanpullrequest.go +++ b/scanpullrequest/scanpullrequest.go @@ -325,6 +325,7 @@ func auditPullRequestCode(repoConfig *utils.Repository, scanDetails *utils.ScanD scanResults = aggregatedScanResults } + utils.PrintScanResultsTable(scanResults) return } diff --git a/scanrepository/scanrepository.go b/scanrepository/scanrepository.go index 111052f04..4deeeb0fe 100644 --- a/scanrepository/scanrepository.go +++ b/scanrepository/scanrepository.go @@ -193,6 +193,7 @@ func (cfp *ScanRepositoryCmd) scanAndFixProject(repository *utils.Repository) (b } continue } + utils.PrintScanResultsTable(scanResults) totalFindings += getTotalFindingsFromScanResults(scanResults) shouldFailBuild = shouldFailBuild || (scanResults.Violations != nil && scanResults.Violations.ShouldFailBuild()) if repository.GitProvider.String() == vcsutils.GitHub.String() { diff --git a/utils/utils.go b/utils/utils.go index a62dd3ef9..eecf95daf 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -17,6 +17,7 @@ import ( "github.com/jfrog/gofrog/datastructures" "github.com/jfrog/gofrog/version" "github.com/jfrog/jfrog-cli-core/v2/common/commands" + "github.com/jfrog/jfrog-cli-core/v2/common/format" "github.com/jfrog/jfrog-cli-core/v2/utils/config" "github.com/jfrog/jfrog-cli-core/v2/utils/usage" "github.com/jfrog/jfrog-cli-security/utils" @@ -590,3 +591,14 @@ func CreateErrorIfPartialResultsDisabled(allowPartial bool, messageForLog string } return err } + +func PrintScanResultsTable(scanResults *results.SecurityCommandResults) { + if scanResults == nil { + return + } + if err := output.NewResultsWriter(scanResults). + SetOutputFormat(format.Table). + PrintScanResults(); err != nil { + log.Warn(fmt.Sprintf("Failed to print scan results table: %s", err.Error())) + } +} diff --git a/utils/utils_test.go b/utils/utils_test.go index 4faf17873..c74f10006 100644 --- a/utils/utils_test.go +++ b/utils/utils_test.go @@ -1,6 +1,7 @@ package utils import ( + "github.com/jfrog/jfrog-client-go/xray/services" "net/http/httptest" "os" "path" @@ -575,3 +576,51 @@ func createTestSecurityCommandResults() *results.SecurityCommandResults { return scanResults } + +func TestPrintScanResultsTable(t *testing.T) { + tests := []struct { + name string + scanResults *results.SecurityCommandResults + }{ + { + name: "nil scan results should not panic", + scanResults: nil, + }, + { + name: "empty scan results should not panic", + scanResults: &results.SecurityCommandResults{ + Targets: []*results.TargetResults{}, + }, + }, + { + name: "scan results with vulnerabilities should print without error", + scanResults: &results.SecurityCommandResults{ + ResultsMetaData: results.ResultsMetaData{ + ResultContext: results.ResultContext{IncludeVulnerabilities: true}, + }, + Targets: []*results.TargetResults{{ + ScanTarget: results.ScanTarget{Target: "test-target"}, + ScaResults: &results.ScaScanResults{ + DeprecatedXrayResults: []services.ScanResponse{{ + Vulnerabilities: []services.Vulnerability{ + { + Cves: []services.Cve{{Id: "CVE-2022-0001"}}, + Severity: "High", + Components: map[string]services.Component{"test-dep:1.0.0": {FixedVersions: []string{"1.0.1"}}}, + }, + }, + }}, + }, + }}, + }, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + assert.NotPanics(t, func() { + PrintScanResultsTable(test.scanResults) + }) + }) + } +}