Skip to content

Commit a0dc279

Browse files
committed
Reference: #729
Fix severity model Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent b585b16 commit a0dc279

4 files changed

Lines changed: 85 additions & 3 deletions

File tree

vulnerabilities/improve_runner.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ def process_inferences(inferences: List[Inference], advisory: Advisory, improver
7272

7373
for severity in ref.severities:
7474
_vs, updated = models.VulnerabilitySeverity.objects.update_or_create(
75-
vulnerability=vuln,
7675
scoring_system=severity.system.identifier,
7776
reference=reference,
7877
defaults={"value": str(severity.value)},
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright (c) nexB Inc. and others. All rights reserved.
2+
# http://nexb.com and https://github.com/nexB/vulnerablecode/
3+
# The VulnerableCode software is licensed under the Apache License version 2.0.
4+
# Data generated with VulnerableCode require an acknowledgment.
5+
#
6+
# You may not use this software except in compliance with the License.
7+
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
8+
# Unless required by applicable law or agreed to in writing, software distributed
9+
# under the License is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES OR
10+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
# specific language governing permissions and limitations under the License.
12+
#
13+
# When you publish or redistribute any data created with VulnerableCode or any VulnerableCode
14+
# derivative work, you must accompany this data with the following acknowledgment:
15+
#
16+
# Generated with VulnerableCode and provided on an 'AS IS' BASIS, WITHOUT WARRANTIES
17+
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
18+
# VulnerableCode should be considered or used as legal advice. Consult an Attorney
19+
# for any legal advice.
20+
# VulnerableCode is a free software code scanning tool from nexB Inc. and others.
21+
# Visit https://github.com/nexB/vulnerablecode/ for support and download.
22+
23+
from django.db import migrations
24+
from django.db.models import Count
25+
from django.db.models import Max
26+
27+
28+
class Migration(migrations.Migration):
29+
30+
dependencies = [
31+
("vulnerabilities", "0013_auto_20220503_0941"),
32+
]
33+
34+
def remove_duplicate_rows(apps, schema_editor):
35+
"""
36+
Find all duplicate rows and remove all of them except the latest one.
37+
"""
38+
unique_fields = [
39+
"reference",
40+
"scoring_system",
41+
"value",
42+
]
43+
Severities = apps.get_model("vulnerabilities", "VulnerabilitySeverity")
44+
# Get all duplicates according to the unique_fields
45+
duplicates = (
46+
Severities.objects.values(*unique_fields)
47+
.order_by()
48+
.annotate(max_id=Max("id"), count_id=Count("id"))
49+
.filter(count_id__gt=1)
50+
)
51+
for duplicate in duplicates:
52+
unique_fields_data = {
53+
uniqe_field: duplicate[uniqe_field] for uniqe_field in unique_fields
54+
}
55+
# Get all rows with the same unique_fields_data
56+
# exclude the latest one
57+
# and delete rest of them
58+
(
59+
Severities.objects.filter(**unique_fields_data)
60+
.exclude(id=duplicate["max_id"])
61+
.delete()
62+
)
63+
64+
operations = [migrations.RunPython(remove_duplicate_rows)]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Generated by Django 4.0.4 on 2022-05-16 15:07
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('vulnerabilities', '0014_remove_duplicate_severities'),
10+
]
11+
12+
operations = [
13+
migrations.AlterUniqueTogether(
14+
name='vulnerabilityseverity',
15+
unique_together={('reference', 'scoring_system', 'value')},
16+
),
17+
migrations.RemoveField(
18+
model_name='vulnerabilityseverity',
19+
name='vulnerability',
20+
),
21+
]

vulnerabilities/models.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,6 @@ def update_or_create(self):
300300

301301
class VulnerabilitySeverity(models.Model):
302302

303-
vulnerability = models.ForeignKey(Vulnerability, on_delete=models.CASCADE)
304303
reference = models.ForeignKey(VulnerabilityReference, on_delete=models.CASCADE)
305304

306305
scoring_system_choices = tuple(
@@ -322,7 +321,6 @@ class VulnerabilitySeverity(models.Model):
322321

323322
class Meta:
324323
unique_together = (
325-
"vulnerability",
326324
"reference",
327325
"scoring_system",
328326
"value",

0 commit comments

Comments
 (0)