Skip to content

Commit b3faa7d

Browse files
committed
Update ProductPackage license unknown during Scan all Packages
Signed-off-by: tdruez <[email protected]>
1 parent 164a092 commit b3faa7d

File tree

5 files changed

+41
-19
lines changed

5 files changed

+41
-19
lines changed

component_catalog/models.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2630,7 +2630,7 @@ def update_from_purldb(self, user):
26302630
)
26312631
return updated_fields
26322632

2633-
def update_from_scan(self, user):
2633+
def update_from_scan(self, user, update_product_relations=False):
26342634
scancodeio = ScanCodeIO(self.dataspace)
26352635
can_update_from_scan = all(
26362636
[
@@ -2640,9 +2640,16 @@ def update_from_scan(self, user):
26402640
]
26412641
)
26422642

2643-
if can_update_from_scan:
2644-
updated_fields = scancodeio.update_from_scan(package=self, user=user)
2645-
return updated_fields
2643+
if not can_update_from_scan:
2644+
return
2645+
2646+
updated_fields = scancodeio.update_from_scan(package=self, user=user)
2647+
2648+
if update_product_relations and "declared_license_expression" in updated_fields:
2649+
for product_package in self.productpackages.license_unknown():
2650+
product_package.update_license_unknown()
2651+
2652+
return updated_fields
26462653

26472654
def get_related_packages_qs(self):
26482655
"""

component_catalog/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1761,7 +1761,7 @@ def send_scan_notification(request, key):
17611761
run = json_data.get("run")
17621762
scan_status = run.get("status")
17631763
if scan_status.lower() == "success":
1764-
updated_fields = package.update_from_scan(user)
1764+
updated_fields = package.update_from_scan(user, update_product_relations=True)
17651765

17661766
if updated_fields:
17671767
description = (

product_portfolio/models.py

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -579,13 +579,11 @@ def improve_packages_from_purldb(self, user):
579579
# Update the Product Package relationship `license_expression` if the
580580
# Package.declared_license_expression was updated from "unknwon" value using
581581
# PurlDB data.
582-
productpackages_unknown_licenses = self.productpackages.filter(
583-
package__in=updated_packages, license_expression="unknown"
584-
)
585-
for product_package in productpackages_unknown_licenses:
586-
package_license_expression = product_package.package.declared_license_expression
587-
if package_license_expression and package_license_expression != "unknown":
588-
product_package.update(license_expression=package_license_expression)
582+
productpackages_qs = self.productpackages.filter(
583+
package__in=updated_packages
584+
).license_unknown()
585+
for product_package in productpackages_qs:
586+
product_package.update_license_unknown()
589587

590588
return updated_packages
591589

@@ -707,6 +705,9 @@ class ProductPackageQuerySet(ProductSecuredQuerySet):
707705
def vulnerable(self):
708706
return self.filter(weighted_risk_score__isnull=False)
709707

708+
def license_unknown(self):
709+
return self.filter(license_expression="unknown")
710+
710711
def annotate_weighted_risk_score(self):
711712
"""Annotate the Queeryset with the weighted_risk_score computed value."""
712713
purpose = ProductItemPurpose.objects.filter(productpackage=OuterRef("pk"))
@@ -1100,6 +1101,20 @@ def __str__(self):
11001101
def permission_protected_fields(self):
11011102
return {"review_status": "change_review_status_on_productpackage"}
11021103

1104+
def update_license_unknown(self):
1105+
"""
1106+
Update this Product Package relationship `license_expression` from "unknown"
1107+
if the related Package `declared_license_expression` has known value.
1108+
"""
1109+
package_license_expression = self.package.declared_license_expression
1110+
conditions = [
1111+
self.license_expression == "unknown",
1112+
package_license_expression,
1113+
package_license_expression != "unknown",
1114+
]
1115+
if all(conditions):
1116+
self.update(license_expression=package_license_expression)
1117+
11031118

11041119
class ProductAssignedLicense(DataspacedModel):
11051120
product = models.ForeignKey(

product_portfolio/templates/product_portfolio/tables/scan_action_cell.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<span{% if package.download_url %} data-bs-toggle="modal" data-bs-target="#scan-package-modal" data-package-scan-url="{% url 'component_catalog:package_scan' user.dataspace package.uuid %}"{% endif %}>
22
<span data-bs-toggle="tooltip" title="{% if package.download_url %}Submit Scan Request{% else %}Download URL not available{% endif %}">
3-
<button type="button" class="btn btn-outline-dark btn-sm{% if not package.download_url %} disabled{% endif %}">
3+
<button type="button" style="width:max-content;" class="btn btn-outline-dark btn-sm{% if not package.download_url %} disabled{% endif %}">
44
<i class="fas fa-barcode"></i> Scan
55
</button>
66
</span>

product_portfolio/templates/product_portfolio/tabs/tab_imports.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<thead>
1919
<tr>
2020
<th>{% trans 'Import type' %}</th>
21-
<th style="width:115px;">{% trans 'Status' %}</th>
21+
<th style="width:140px;">{% trans 'Status' %}</th>
2222
<th>{% trans 'Input' %}</th>
2323
<th colspan="2">{% trans 'Log' %}</th>
2424
</tr>
@@ -41,17 +41,17 @@
4141
</div>
4242
</td>
4343
<td>
44+
{% if scancode_project.project_uuid %}
45+
<a href="#" role="button" class="ms-1 float-end" data-bs-toggle="modal" data-bs-target="#scancode-project-status-modal" data-fetch-status-url="{% url 'product_portfolio:scancodeio_project_status' scancode_project.uuid %}">
46+
<i class="fas fa-info-circle"></i>
47+
</a>
48+
{% endif %}
4449
<strong>
4550
{{ scancode_project.get_status_display|title }}
4651
{% if scancode_project.has_errors %}
4752
<span class="float-start"> with errors</span>
4853
{% endif %}
4954
</strong>
50-
{% if scancode_project.project_uuid %}
51-
<a href="#" role="button" class="ms-1 float-end" data-bs-toggle="modal" data-bs-target="#scancode-project-status-modal" data-fetch-status-url="{% url 'product_portfolio:scancodeio_project_status' scancode_project.uuid %}">
52-
<i class="fas fa-info-circle"></i>
53-
</a>
54-
{% endif %}
5555
{% include 'component_catalog/includes/scan_status.html' with status=scancode_project.status has_errors=scancode_project.has_errors only %}
5656
</td>
5757
<td>

0 commit comments

Comments
 (0)