-
Notifications
You must be signed in to change notification settings - Fork 201
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make VulnerabilityReference.url unique #818
Also validate full_clean in the improve_runner to ensure we do not have empty, invalid or blank URLs. Refactor code to add new Manager to VulnerabilityReference and Package Add convenience method accordingly to create Pckage from purls Reference: #818 Co-authored-by: Tushar Goel <[email protected]> Signed-off-by: Philippe Ombredanne <[email protected]>
- Loading branch information
1 parent
caa7268
commit 6d379d0
Showing
8 changed files
with
234 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
vulnerabilities/migrations/0025_remove_duplicate_reference_urls.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from django.db import migrations | ||
from django.db.models import Count | ||
from django.db.models import Max | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('vulnerabilities', '0024_alter_all_models_to_add_ordering'), | ||
] | ||
|
||
def remove_duplicate_reference_urls(apps, _): | ||
""" | ||
Find all duplicate references and remove all of them except for one. | ||
Any duplication will be reprocessed by reimports if needed to correct | ||
trhe relationships. | ||
""" | ||
|
||
VulnerabilityReference = apps.get_model("vulnerabilities", "VulnerabilityReference") | ||
|
||
duplicates = ( | ||
VulnerabilityReference.objects.values("url") | ||
.order_by("url") | ||
.annotate(max_id=Max("id"), count_id=Count("id")) | ||
.filter(count_id__gt=1) | ||
) | ||
|
||
for duplicate in duplicates: | ||
# Get all rows with the same url, | ||
# exclude the latest one | ||
# and delete rest of them | ||
( | ||
VulnerabilityReference.objects | ||
.filter(url=duplicate["url"]) | ||
.exclude(id=duplicate["max_id"]) | ||
.delete() | ||
) | ||
|
||
operations = [ | ||
migrations.RunPython(remove_duplicate_reference_urls, migrations.RunPython.noop), | ||
] |
22 changes: 22 additions & 0 deletions
22
vulnerabilities/migrations/0026_alter_vulnerabilityreference_unique_together_and_more.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Generated by Django 4.0.7 on 2022-09-09 12:34 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('vulnerabilities', '0025_remove_duplicate_reference_urls'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterUniqueTogether( | ||
name='vulnerabilityreference', | ||
unique_together=set(), | ||
), | ||
migrations.AlterField( | ||
model_name='vulnerabilityreference', | ||
name='url', | ||
field=models.URLField(help_text='URL to the vulnerability reference', max_length=1024, unique=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.