Skip to content

Commit

Permalink
feat: add SPDX field
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyLewen committed Jun 26, 2024
1 parent a9fe388 commit 68abc78
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
3 changes: 2 additions & 1 deletion pkg/fanal/types/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ func (r *Relationship) UnmarshalJSON(data []byte) error {
type PkgIdentifier struct {
UID string `json:",omitempty"` // Calculated by the package struct
PURL *packageurl.PackageURL `json:"-"`
BOMRef string `json:",omitempty"` // From SBOM file: `component.BOMRef` or `package.SPDXID`
BOMRef string `json:",omitempty"` // For CycloneDX
SPDXID string `json:",omitempty"` // For SPDX
}

// MarshalJSON customizes the JSON encoding of PkgIdentifier.
Expand Down
3 changes: 3 additions & 0 deletions pkg/sbom/core/bom.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ type Component struct {
// SPDX: package.externalRefs.referenceLocator
// BOMRef:
// CycloneDX: component.bom-ref
// SPDX: N/A
// SPDXID:
// CycloneDX: N/A
// SPDX: package.SPDXID
PkgIdentifier ftypes.PkgIdentifier

Expand Down
8 changes: 7 additions & 1 deletion pkg/sbom/io/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,12 @@ func (m *Decoder) selectOS(osComponents []*core.Component, sbom *types.SBOM) {
if numberOfIPkgs != numberOfJPkgs {
return numberOfIPkgs < numberOfJPkgs
}
return osComponents[i].PkgIdentifier.BOMRef < osComponents[j].PkgIdentifier.BOMRef
// For CycloneDX
if osComponents[i].PkgIdentifier.BOMRef != "" || osComponents[j].PkgIdentifier.BOMRef != "" {
return osComponents[i].PkgIdentifier.BOMRef < osComponents[j].PkgIdentifier.BOMRef
}
// For SPDX
return osComponents[i].PkgIdentifier.SPDXID < osComponents[j].PkgIdentifier.SPDXID
})

if len(osComponents) > 1 {
Expand Down Expand Up @@ -244,6 +249,7 @@ func (m *Decoder) decodeLibrary(c *core.Component) (*ftypes.Package, error) {
}

pkg.Identifier.BOMRef = c.PkgIdentifier.BOMRef
pkg.Identifier.SPDXID = c.PkgIdentifier.SPDXID
pkg.Licenses = c.Licenses

for _, f := range c.Files {
Expand Down
2 changes: 1 addition & 1 deletion pkg/sbom/spdx/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (s *SPDX) parsePackage(spdxPkg spdx.Package) (*core.Component, error) {
Name: spdxPkg.PackageName,
Version: spdxPkg.PackageVersion,
PkgIdentifier: types.PkgIdentifier{
BOMRef: string(spdxPkg.PackageSPDXIdentifier),
SPDXID: string(spdxPkg.PackageSPDXIdentifier),
},
}

Expand Down

0 comments on commit 68abc78

Please sign in to comment.