Skip to content

Commit

Permalink
refactor: stop iter vulns
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyLewen committed Feb 4, 2025
1 parent c78bfe5 commit ba8f756
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
25 changes: 10 additions & 15 deletions pkg/report/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,29 +207,24 @@ func splitAggregatedPackages(results types.Results) types.Results {
}

func splitAggregatedVulns(result types.Result) types.Results {
var newResults types.Results

// Save packages to display them in the table even if no vulnerabilities were found
vulns := lo.SliceToMap(result.Packages, func(pkg ftypes.Package) (string, []types.DetectedVulnerability) {
return rootJarFromPath(pkg.FilePath), []types.DetectedVulnerability{}
resultMap := lo.SliceToMap(result.Packages, func(pkg ftypes.Package) (string, *types.Result) {
filePath := rootJarFromPath(pkg.FilePath)
return filePath, &types.Result{
Target: lo.Ternary(filePath != "", filePath, result.Target),
Class: result.Class,
Type: result.Type,
}
})

for _, vuln := range result.Vulnerabilities {
pkgPath := rootJarFromPath(vuln.PkgPath)
vulns[pkgPath] = append(vulns[pkgPath], vuln)
}
for pkgPath, v := range vulns {
newResult := result
newResult.Target = lo.Ternary(pkgPath != "", pkgPath, result.Target)
newResult.Vulnerabilities = v

newResults = append(newResults, newResult)
resultMap[pkgPath].Vulnerabilities = append(resultMap[pkgPath].Vulnerabilities, vuln)
}

newResults := lo.Values(resultMap)
sort.Slice(newResults, func(i, j int) bool {
return newResults[i].Target < newResults[j].Target
})
return newResults
return lo.FromSlicePtr(newResults)
}

func splitAggregatedLicenses(result types.Result) types.Results {
Expand Down
10 changes: 10 additions & 0 deletions pkg/report/table/table_private_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ var (
Target: "Node.js",
Class: types.ClassLangPkg,
Type: ftypes.NodePkg,
Packages: []ftypes.Package{
{
Name: "[email protected]",
FilePath: "loader-utils/package.json",
},
{
Name: "[email protected]",
FilePath: "nanoid/package.json",
},
},
Vulnerabilities: []types.DetectedVulnerability{
{
VulnerabilityID: "CVE-2022-37601",
Expand Down

0 comments on commit ba8f756

Please sign in to comment.