Normalize syft-json output (#1194)

This commit is contained in:
Scott Andrews 2022-09-07 10:56:49 -04:00 committed by GitHub
parent 586d3fe77f
commit 1c7b7c5f8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 1 deletions

View file

@ -215,6 +215,15 @@ func toRelationshipModel(relationships []artifact.Relationship) []model.Relation
Metadata: r.Data,
}
}
sort.Slice(result, func(i, j int) bool {
if iParent, jParent := result[i].Parent, result[j].Parent; iParent != jParent {
return iParent < jParent
}
if iChild, jChild := result[i].Child, result[j].Child; iChild != jChild {
return iChild < jChild
}
return result[i].Type < result[j].Type
})
return result
}

View file

@ -203,6 +203,10 @@ func (c *Catalog) Sorted(types ...Type) (pkgs []Package) {
iLocations := pkgs[i].Locations.ToSlice()
jLocations := pkgs[j].Locations.ToSlice()
if pkgs[i].Type == pkgs[j].Type && len(iLocations) > 0 && len(jLocations) > 0 {
if iLocations[0].String() == jLocations[0].String() {
// compare IDs as a final fallback
return pkgs[i].ID() < pkgs[j].ID()
}
return iLocations[0].String() < jLocations[0].String()
}
return pkgs[i].Type < pkgs[j].Type

View file

@ -1,6 +1,8 @@
package pkg
import (
"sort"
"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft/artifact"
"github.com/bmatcuk/doublestar/v4"
@ -34,12 +36,14 @@ func RelationshipsByFileOwnership(catalog *Catalog) []artifact.Relationship {
var edges []artifact.Relationship
for parentID, children := range relationships {
for childID, files := range children {
fs := files.List()
sort.Strings(fs)
edges = append(edges, artifact.Relationship{
From: catalog.byID[parentID],
To: catalog.byID[childID],
Type: artifact.OwnershipByFileOverlapRelationship,
Data: ownershipByFilesMetadata{
Files: files.List(),
Files: fs,
},
})
}