Skip to content

Commit 17317ca

Browse files
committed
Address review comments
Signed-off-by: Tushar Goel <[email protected]>
1 parent 44248b5 commit 17317ca

File tree

5 files changed

+49
-17
lines changed

5 files changed

+49
-17
lines changed

vulnerabilities/api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def bulk_search(self, request):
247247
data={"Error": "A non-empty 'purls' list of PURLs is required."},
248248
)
249249

250-
query = Package.objects.filter(package__in=purls)
250+
query = Package.objects.filter(package_url__in=purls)
251251

252252
if not purl_only:
253253
return Response(
@@ -264,8 +264,8 @@ def all(self, request):
264264
"""
265265
Return the Package URLs of all packages known to be vulnerable.
266266
"""
267-
vulnerable_packages = Package.objects.vulnerable().only(*PackageURL._fields).distinct()
268-
vulnerable_purls = [str(package.purl) for package in vulnerable_packages]
267+
vulnerable_packages = Package.objects.vulnerable().only("package_url").distinct()
268+
vulnerable_purls = [str(package.package_url) for package in vulnerable_packages]
269269
return Response(vulnerable_purls)
270270

271271

vulnerabilities/migrations/0034_package_package.py renamed to vulnerabilities/migrations/0034_package_package_url.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generated by Django 4.0.7 on 2022-11-25 12:57
1+
# Generated by Django 4.0.7 on 2022-11-25 17:28
22

33
from django.db import migrations, models
44

@@ -12,7 +12,7 @@ class Migration(migrations.Migration):
1212
operations = [
1313
migrations.AddField(
1414
model_name='package',
15-
name='package',
16-
field=models.CharField(blank=True, help_text='The Package URL for this package.', max_length=255),
15+
name='package_url',
16+
field=models.CharField(blank=True, db_index=True, help_text='The Package URL for this package.', max_length=255),
1717
),
1818
]
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
# Generated by Django 4.0.7 on 2022-11-25 12:57
2-
31
from django.db import migrations
42
from packageurl import PackageURL
53

64
class Migration(migrations.Migration):
75

8-
def save_package(apps, schema_editor):
6+
def save_purls(apps, schema_editor):
97
Package = apps.get_model("vulnerabilities", "Package")
8+
updatables = []
109
for package in Package.objects.all():
1110
purl = PackageURL(
1211
type=package.type,
@@ -16,13 +15,20 @@ def save_package(apps, schema_editor):
1615
qualifiers=package.qualifiers,
1716
subpath=package.subpath,
1817
)
19-
package.package = str(purl)
20-
package.save()
18+
package.package_url = str(purl)
19+
updatables.append(package)
20+
21+
updated = Package.objects.bulk_update(
22+
objs = updatables,
23+
fields=["package_url"],
24+
batch_size=500,
25+
)
26+
print(f"Migrated {updated} packages with package_url")
2127

2228
dependencies = [
23-
('vulnerabilities', '0034_package_package'),
29+
('vulnerabilities', '0034_package_package_url'),
2430
]
2531

2632
operations = [
27-
migrations.RunPython(save_package, reverse_code=migrations.RunPython.noop),
28-
]
33+
migrations.RunPython(save_purls, reverse_code=migrations.RunPython.noop),
34+
]
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 4.0.7 on 2022-11-25 17:34
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('vulnerabilities', '0035_add_package_url_to_packages'),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name='package',
15+
name='package_url',
16+
field=models.CharField(db_index=True, help_text='The Package URL for this package.', max_length=255),
17+
),
18+
]

vulnerabilities/models.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -531,17 +531,25 @@ class Package(PackageURLMixin):
531531
to="Vulnerability", through="PackageRelatedVulnerability"
532532
)
533533

534-
package = models.CharField(
534+
package_url = models.CharField(
535535
max_length=255,
536-
blank=True,
537536
null=False,
538537
help_text="The Package URL for this package.",
538+
db_index=True,
539539
)
540540

541541
objects = PackageQuerySet.as_manager()
542542

543543
def save(self, *args, **kwargs):
544-
self.package = self.purl
544+
purl_object = PackageURL(
545+
type=self.type,
546+
namespace=self.namespace,
547+
name=self.name,
548+
version=self.version,
549+
qualifiers=self.qualifiers,
550+
subpath=self.subpath,
551+
)
552+
self.package_url = str(purl_object)
545553
super().save(*args, **kwargs)
546554

547555
@property

0 commit comments

Comments
 (0)