Skip to content

Commit e76c7ee

Browse files
committed
fix(sbom): merge orphaned OS packages with existing PackageInfo
- Use lo.FindIndexOf to find existing PackageInfo with empty FilePath - Merge orphaned OS packages instead of creating separate entries - Sort packages after merging for consistent ordering - Resolves issue where out-of-graph OS packages were isolated
1 parent bff302c commit e76c7ee

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

pkg/sbom/io/decode.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,8 +391,19 @@ func (m *Decoder) addOrphanPkgs(sbom *types.SBOM) error {
391391
for _, pkgs := range osPkgMap {
392392
// TODO: mismatch between the OS and the packages should be rejected.
393393
// e.g. OS: debian, Packages: rpm
394-
sort.Sort(pkgs)
395-
sbom.Packages = append(sbom.Packages, ftypes.PackageInfo{Packages: pkgs})
394+
395+
// Find existing PackageInfo with empty FilePath to merge with
396+
if _, idx, found := lo.FindIndexOf(sbom.Packages, func(pkg ftypes.PackageInfo) bool {
397+
return pkg.FilePath == ""
398+
}); found {
399+
// Merge with existing PackageInfo
400+
sbom.Packages[idx].Packages = append(sbom.Packages[idx].Packages, pkgs...)
401+
sort.Sort(sbom.Packages[idx].Packages)
402+
} else {
403+
// Create new PackageInfo
404+
sort.Sort(pkgs)
405+
sbom.Packages = append(sbom.Packages, ftypes.PackageInfo{Packages: pkgs})
406+
}
396407

397408
break // Just take the first element
398409
}

pkg/sbom/io/decode_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,4 +347,3 @@ func TestDecoder_Decode_OSPackages(t *testing.T) {
347347
})
348348
}
349349
}
350-

0 commit comments

Comments
 (0)