Skip to content

Commit b78ee53

Browse files
committed
Address suggestions
Signed-off-by: Keshav Priyadarshi <git@keshav.space>
1 parent 1f2c7ce commit b78ee53

4 files changed

Lines changed: 30 additions & 43 deletions

File tree

scanpipe/cyclonedx/__init__.py

Lines changed: 21 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import json
2424
import pathlib
25+
from collections import defaultdict
2526

2627
import jsonschema
2728
from hoppr_cyclonedx_models.cyclonedx_1_4 import Component
@@ -40,26 +41,26 @@
4041
)
4142

4243

43-
def get_bom(cyclonedx_document: dict):
44+
def get_bom(cyclonedx_document):
4445
"""
45-
Return CycloneDx BOM object.
46+
Return CycloneDX BOM object.
4647
"""
4748
return Bom_1_4(**cyclonedx_document)
4849

4950

5051
def get_components(bom: Bom_1_4):
5152
"""
52-
Return list of components from CycloneDx BOM.
53+
Return list of components from CycloneDX BOM.
5354
"""
5455
return recursive_component_collector(bom.components, [])
5556

5657

5758
def bom_attributes_to_dict(cyclonedx_attributes):
5859
"""
59-
Return list dict from a list of CycloneDx attributes.
60+
Return list of dict from a list of CycloneDX attributes.
6061
"""
6162
if not cyclonedx_attributes:
62-
return {}
63+
return []
6364

6465
return [
6566
json.loads(attribute.json(exclude_unset=True, by_alias=True))
@@ -75,11 +76,10 @@ def recursive_component_collector(root_component_list, collected):
7576
return
7677

7778
for component in root_component_list:
78-
extra_data = (
79-
bom_attributes_to_dict(component.components)
80-
if component.components is not None
81-
else {}
82-
)
79+
extra_data = {}
80+
if component.components is not None:
81+
extra_data = bom_attributes_to_dict(component.components)
82+
8383
collected.append({"cdx_package": component, "nested_components": extra_data})
8484
recursive_component_collector(component.components, collected)
8585
return collected
@@ -104,12 +104,13 @@ def get_declared_licenses(licenses):
104104
if not licenses:
105105
return ""
106106

107-
return "\n".join(
108-
[resolve_license(license) for license in bom_attributes_to_dict(licenses)]
109-
)
107+
resolved_licenses = [
108+
resolve_license(license) for license in bom_attributes_to_dict(licenses)
109+
]
110+
return "\n".join(resolved_licenses)
110111

111112

112-
def get_checksums(component: Component):
113+
def get_checksums(component):
113114
"""
114115
Return dict of all the checksums from a component.
115116
"""
@@ -129,40 +130,24 @@ def get_checksums(component: Component):
129130
}
130131

131132

132-
def get_external_refrences(external_references):
133+
def get_external_references(external_references):
133134
"""
134-
Return dict of refrence urls from list of `externalRefrences`.
135+
Return dict of reference urls from list of `externalReferences`.
135136
"""
136137
if not external_references:
137138
return {}
138139

139-
refrences = {
140-
"vcs": [],
141-
"issue-tracker": [],
142-
"website": [],
143-
"advisories": [],
144-
"bom": [],
145-
"mailing-list": [],
146-
"social": [],
147-
"chat": [],
148-
"documentation": [],
149-
"support": [],
150-
"distribution": [],
151-
"license": [],
152-
"build-meta": [],
153-
"build-system": [],
154-
"release-notes": [],
155-
"other": [],
156-
}
140+
refrences = defaultdict(lambda: [])
141+
157142
for ref in external_references:
158143
refrences[ref.type.value].append(ref.url)
159144

160-
return {key: value for key, value in refrences.items() if value}
145+
return dict(refrences)
161146

162147

163148
def validate_document(document, schema=CYCLONEDX_JSON_SCHEMA_PATH):
164149
"""
165-
CYCLONEDX document validation.
150+
CycloneDX document validation.
166151
"""
167152
if isinstance(document, str):
168153
document = json.loads(document)

scanpipe/pipes/resolve.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def resolve_spdx_packages(input_location):
128128

129129
def cyclonedx_component_to_discovered_package_data(component_data):
130130
"""
131-
Return package_data from CycloneDx component.
131+
Return package_data from CycloneDX component.
132132
"""
133133
extra_data = {}
134134
component = component_data["cdx_package"]
@@ -140,7 +140,9 @@ def cyclonedx_component_to_discovered_package_data(component_data):
140140
)
141141

142142
checksum_data = cyclonedx.get_checksums(component)
143-
external_references = cyclonedx.get_external_refrences(component.externalReferences)
143+
external_references = cyclonedx.get_external_references(
144+
component.externalReferences
145+
)
144146

145147
if homepage_url := external_references.get("website"):
146148
homepage_url = homepage_url[0]
@@ -177,7 +179,7 @@ def cyclonedx_component_to_discovered_package_data(component_data):
177179

178180
def resolve_cyclonedx_packages(input_location):
179181
"""
180-
Resolve the packages from the `input_location` CycloneDx document file.
182+
Resolve the packages from the `input_location` CycloneDX document file.
181183
"""
182184
input_path = Path(input_location)
183185
cyclonedx_document = json.loads(input_path.read_text())
@@ -186,7 +188,7 @@ def resolve_cyclonedx_packages(input_location):
186188
cyclonedx.validate_document(cyclonedx_document)
187189
except Exception as e:
188190
raise Exception(
189-
f'CycloneDx document "{input_path.name}" is not valid: {e.message}'
191+
f'CycloneDX document "{input_path.name}" is not valid: {e.message}'
190192
)
191193

192194
cyclonedx_bom = cyclonedx.get_bom(cyclonedx_document)

scanpipe/tests/test_cyclonedx.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ def test_scanpipe_cyclonedx_get_checksums(self):
154154

155155
self.assertEqual(result, expected)
156156

157-
def test_scanpipe_cyclonedx_get_external_refrences(self):
157+
def test_scanpipe_cyclonedx_get_external_references(self):
158158
component = self.bom.components[0]
159-
result = cyclonedx.get_external_refrences(component.externalReferences)
159+
result = cyclonedx.get_external_references(component.externalReferences)
160160
expected = {
161161
"vcs": ["https://cyclonedx.org/vcs"],
162162
"issue-tracker": ["https://cyclonedx.org/issue-tracker"],

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ install_requires =
8989
jsonschema==4.17.3
9090
# CycloneDX
9191
cyclonedx-python-lib==3.1.5
92-
hoppr-cyclonedx-models==0.4.0
92+
hoppr-cyclonedx-models==0.4.10
9393

9494
[options.extras_require]
9595
dev =

0 commit comments

Comments
 (0)