Skip to content

Commit 65b4437

Browse files
committed
Add option to infer_download_urls on Product importers
Signed-off-by: tdruez <[email protected]>
1 parent 0a0bb1f commit 65b4437

File tree

4 files changed

+53
-1
lines changed

4 files changed

+53
-1
lines changed

product_portfolio/forms.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,15 @@ class BaseProductImportFormView(forms.Form):
641641
"for all of the packages assigned to your product."
642642
),
643643
)
644+
infer_download_urls = forms.BooleanField(
645+
label=_("Infer missing download URLs"),
646+
required=False,
647+
initial=True,
648+
help_text=_(
649+
"When a download URL is missing from the input data, attempt to infer it "
650+
"from the Package URL (purl). A download URL is required for package scanning."
651+
),
652+
)
644653

645654
@property
646655
def helper(self):
@@ -652,6 +661,7 @@ def helper(self):
652661
Fieldset(
653662
None,
654663
"input_file",
664+
"infer_download_urls",
655665
"update_existing_packages",
656666
"scan_all_packages",
657667
StrictSubmit("submit", _("Import"), css_class="btn-success col-2"),
@@ -667,6 +677,7 @@ def submit(self, product, user):
667677
input_file=self.cleaned_data.get("input_file"),
668678
update_existing_packages=self.cleaned_data.get("update_existing_packages"),
669679
scan_all_packages=self.cleaned_data.get("scan_all_packages"),
680+
infer_download_urls=self.cleaned_data.get("infer_download_urls"),
670681
created_by=user,
671682
)
672683

product_portfolio/importers.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from component_catalog.models import PACKAGE_URL_FIELDS
2727
from component_catalog.models import Component
2828
from component_catalog.models import Package
29+
from dejacode_toolkit import download
2930
from dejacode_toolkit.scancodeio import ScanCodeIO
3031
from dje.copier import copy_object
3132
from dje.importers import BaseImporter
@@ -649,7 +650,15 @@ class ImportPackageFromScanCodeIO:
649650
"filename",
650651
]
651652

652-
def __init__(self, user, project_uuid, product, update_existing=False, scan_all_packages=False):
653+
def __init__(
654+
self,
655+
user,
656+
project_uuid,
657+
product,
658+
update_existing=False,
659+
scan_all_packages=False,
660+
infer_download_urls=False,
661+
):
653662
self.licensing = Licensing()
654663
self.created = defaultdict(list)
655664
self.existing = defaultdict(list)
@@ -662,6 +671,7 @@ def __init__(self, user, project_uuid, product, update_existing=False, scan_all_
662671
self.product = product
663672
self.update_existing = update_existing
664673
self.scan_all_packages = scan_all_packages
674+
self.infer_download_urls = infer_download_urls
665675

666676
scancodeio = ScanCodeIO(user.dataspace)
667677
self.packages = scancodeio.fetch_project_packages(self.project_uuid)
@@ -696,6 +706,15 @@ def import_package(self, package_data):
696706
# Check if the package already exists to prevent duplication.
697707
package = self.look_for_existing_package(package_data)
698708

709+
# Infer a download URL from the Package URL
710+
if (
711+
self.infer_download_urls
712+
and not package_data.get("download_url")
713+
and (purl := package_data.get("purl"))
714+
and (download_url := download.infer_download_url(purl))
715+
):
716+
package_data["download_url"] = download_url
717+
699718
if license_expression := package_data.get("declared_license_expression"):
700719
license_expression = str(self.licensing.dedup(license_expression))
701720
package_data["license_expression"] = license_expression
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 5.2.8 on 2025-12-16 04:14
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("product_portfolio", "0013_productstatus_is_locked_and_more"),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name="scancodeproject",
15+
name="infer_download_urls",
16+
field=models.BooleanField(default=False),
17+
),
18+
]

product_portfolio/models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,6 +1555,9 @@ class Status(models.TextChoices):
15551555
scan_all_packages = models.BooleanField(
15561556
default=False,
15571557
)
1558+
infer_download_urls = models.BooleanField(
1559+
default=False,
1560+
)
15581561
status = models.CharField(
15591562
max_length=10,
15601563
choices=Status.choices,
@@ -1615,6 +1618,7 @@ def import_data_from_scancodeio(self):
16151618
product=self.product,
16161619
update_existing=self.update_existing_packages,
16171620
scan_all_packages=self.scan_all_packages,
1621+
infer_download_urls=self.infer_download_urls,
16181622
)
16191623
created, existing, errors = importer.save()
16201624

0 commit comments

Comments
 (0)