Skip to content

Commit 8d465a8

Browse files
committed
Add the infer_download_urls in scan_all_packages_task
Signed-off-by: tdruez <[email protected]>
1 parent a2b2f2d commit 8d465a8

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

component_catalog/models.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,10 @@ def has_package_url(self):
17191719
"""Return objects with Package URL defined."""
17201720
return self.filter(~models.Q(type="") & ~models.Q(name=""))
17211721

1722+
def has_download_url(self):
1723+
"""Return objects with download URL defined."""
1724+
return self.filter(~models.Q(download_url=""))
1725+
17221726
def annotate_sortable_identifier(self):
17231727
"""
17241728
Annotate the QuerySet with a `sortable_identifier` value that combines
@@ -2036,9 +2040,13 @@ def package_url_filename(self):
20362040

20372041
@property
20382042
def inferred_repo_url(self):
2039-
"""Return the URL deduced from the information available in a Package URL (purl)."""
2043+
"""Return the repo URL deduced from the Package URL (purl)."""
20402044
return purl2url.get_repo_url(self.package_url)
20412045

2046+
def infer_download_url(self):
2047+
"""Infer the download URL deduced from the Package URL (purl)."""
2048+
return download.infer_download_url(self.package_url)
2049+
20422050
def get_url(self, name, params=None, include_identifier=False):
20432051
if not params:
20442052
params = [self.dataspace.name, quote_plus(str(self.uuid))]

product_portfolio/models.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -557,25 +557,40 @@ def assign_objects(self, related_objects, user, replace_version=False):
557557

558558
return created_count, updated_count, unchanged_count
559559

560-
def scan_all_packages_task(self, user):
560+
def scan_all_packages_task(self, user, infer_download_urls=False):
561561
"""
562562
Submit a Scan request to ScanCode.io for each package assigned to this Product.
563563
Only packages with a proper download URL are sent.
564564
"""
565-
package_urls = [
565+
if infer_download_urls:
566+
self.improve_packages_from_purl()
567+
568+
package_download_urls = [
566569
package.download_url
567-
for package in self.all_packages
570+
for package in self.all_packages.has_download_url()
568571
if package.download_url.startswith(("http", "https"))
569572
]
570573

571574
tasks.scancodeio_submit_scan.delay(
572-
uris=package_urls,
575+
uris=package_download_urls,
573576
user_uuid=user.uuid,
574577
dataspace_uuid=user.dataspace.uuid,
575578
)
576579

580+
def improve_packages_from_purl(self):
581+
"""Infer missing packages download URL using the Package URL when possible."""
582+
updated_packages = []
583+
584+
packages = self.all_packages.has_package_url().filter(models.Q(download_url=""))
585+
for package in packages:
586+
if download_url := package.infer_download_url():
587+
package.update(download_url=download_url)
588+
updated_packages.append(package)
589+
590+
return updated_packages
591+
577592
def improve_packages_from_purldb(self, user):
578-
"""Update all Packages assigned to the Product using PurlDB data."""
593+
"""Update all packages assigned to thepProduct using PurlDB data."""
579594
updated_packages = []
580595
for package in self.packages.all():
581596
updated_fields = package.update_from_purldb(user)

product_portfolio/templates/product_portfolio/modals/scan_all_packages_modal.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ <h5 class="modal-title">Scan all Packages</h5>
66
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
77
</div>
88
<div class="modal-body bg-body-tertiary">
9-
You are about to initiate multiple scans on the ScanCode.io server for all of the
9+
You are about to initiate multiple scans on the ScanCode.io server for all the
1010
Packages assigned to your Product, either by direct assignment or by assignment to a
1111
Component assigned to your Product.<br><br>
1212
<strong>Note that this may take some time to complete.</strong><br><br>

0 commit comments

Comments
 (0)