Skip to content

Commit

Permalink
fix: Handle bom-ref as optional parameter (Fixes #11)
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyharrison committed Aug 15, 2023
1 parent 8139d29 commit cf20e84
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
15 changes: 11 additions & 4 deletions lib4sbom/cyclonedx/cyclonedx_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,15 @@ def parse_cyclonedx_json(self, sbom_file):
)
if "component" in data["metadata"]:
cyclonedx_document.set_name(data["metadata"]["component"]["name"])
id[data["metadata"]["component"]["bom-ref"]] = data["metadata"][
"component"
]["name"]
if "bom-ref" in data["metadata"]["component"]:
bom_ref = data["metadata"]["component"]["bom-ref"]
else:
bom_ref = "CylconeDX-Component-0000"
id[bom_ref] = data["metadata"]["component"]["name"]
component_id = 0
for d in data["components"]:
cyclonedx_package.initialise()
component_id = component_id + 1
if d["type"] in ["file", "library", "application", "operating-system"]:
package = d["name"]
cyclonedx_package.set_name(package)
Expand All @@ -71,6 +75,8 @@ def parse_cyclonedx_json(self, sbom_file):
version = "MISSING"
# Record type of component
cyclonedx_package.set_type(d["type"])
# If bom-ref not present, auto generate one
bom_ref = d.get("bom-ref", f"CycloneDX-Component-{component_id}")
if "supplier" in d:
# Assume that this refers to an organisation
supplier_name = d["supplier"]["name"]
Expand Down Expand Up @@ -145,7 +151,8 @@ def parse_cyclonedx_json(self, sbom_file):
cyclonedx_package.set_downloadlocation(ref_url)
# Save package metadata
packages[(package, version)] = cyclonedx_package.get_package()
id[d["bom-ref"]] = package
print(f"{package} {bom_ref}")
id[bom_ref] = package
if "dependencies" in data:
# First relationship is assumed to be the root element
relationship_type = " DESCRIBES "
Expand Down
2 changes: 1 addition & 1 deletion lib4sbom/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2023 Anthony Harrison
# SPDX-License-Identifier: Apache-2.0

VERSION: str = "0.4.2"
VERSION: str = "0.4.3"

0 comments on commit cf20e84

Please sign in to comment.