Skip to content

Commit

Permalink
Simplify get_cyclonedx_bom #1066
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Jun 3, 2024
1 parent e30de4e commit 72d1ec6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
10 changes: 4 additions & 6 deletions scanpipe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3339,13 +3339,11 @@ def prefetch_for_serializer(self):
`DiscoveredDependencySerializer`.
Only the fields required by the serializer are fetched on the relations.
"""
manager = DiscoveredPackage.objects
return self.prefetch_related(
Prefetch(
"for_package", queryset=DiscoveredPackage.objects.only("package_uid")
),
Prefetch(
"datafile_resource", queryset=CodebaseResource.objects.only("path")
),
Prefetch("for_package", queryset=manager.only("package_uid")),
Prefetch("resolved_to_package", queryset=manager.only("package_uid")),
Prefetch("datafile_resource", queryset=manager.only("path")),
)


Expand Down
19 changes: 8 additions & 11 deletions scanpipe/pipes/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,24 +675,21 @@ def get_cyclonedx_bom(project):
],
)

components = []
vulnerabilities = []
for package in get_queryset(project, "discoveredpackage"):

package_qs = get_queryset(project, "discoveredpackage")
package_qs = package_qs.prefetch_related("children_packages")

for package in package_qs:
component = package.as_cyclonedx()
components.append(component)
bom.components.add(component)
bom.register_dependency(project_as_root_component, [component])

for vulnerability_data in package.affected_by_vulnerabilities:
vulnerabilities.append(
vulnerability_as_cyclonedx(
vulnerability_data=vulnerability_data,
component_bom_ref=component.bom_ref,
)
vulnerability_as_cyclonedx(vulnerability_data, component.bom_ref)
)

for component in components:
bom.components.add(component)
bom.register_dependency(project_as_root_component, [component])

bom.vulnerabilities = vulnerabilities

return bom
Expand Down
2 changes: 2 additions & 0 deletions scanpipe/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2165,6 +2165,8 @@ def test_scanpipe_package_model_integrity_with_toolkit_package_model(self):
"tag",
"declared_dependencies",
"resolved_from_dependencies",
"parent_packages",
"children_packages",
]

package_data_only_field = ["datasource_id", "dependencies"]
Expand Down

0 comments on commit 72d1ec6

Please sign in to comment.