Skip to content

Commit 57ac8f3

Browse files
authored
Merge pull request #2730 from cody-m-tibco/master
Add migration to create endpoint_status objects
2 parents e0fa585 + 9c26097 commit 57ac8f3

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

PULL_REQUEST_TEMPLATE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This template is for your information. Please clear everything when submitting y
44

55
When submitting a pull request, please make sure you have completed the following checklist:
66

7-
- [ ] Give a meaninful name to your PR, as it may end up being used in the release notes.
7+
- [ ] Give a meaningful name to your PR, as it may end up being used in the release notes.
88
- [ ] Your code is flake8 compliant.
99
- [ ] Your code is python 3.6 compliant (specific python >3.6 syntax is currently not accepted).
1010
- [ ] If this is a new feature and not a bug fix, you've included the proper documentation in the ReadTheDocs documentation folder. https://github.com/DefectDojo/Documentation/tree/master/docs or provide feature documentation in the PR.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
from django.db import migrations, models
3+
4+
5+
class Migration(migrations.Migration):
6+
7+
"""
8+
This script will create endpoint status objects for findings and endpoints for
9+
databases that already contain those objects.
10+
"""
11+
def create_status_objects(apps, schema_editor):
12+
# Retreive the correct models
13+
Finding = apps.get_model('dojo', 'Finding')
14+
Endpoint_Status = apps.get_model('dojo', 'Endpoint_Status')
15+
# Get a list of findings that have endpoints
16+
findings = Finding.objects.annotate(count=models.Count('endpoints')).filter(count__gt=0)
17+
for finding in findings:
18+
# Get the list of endpoints on the current finding
19+
endpoints = finding.endpoints.all()
20+
for endpoint in endpoints:
21+
# Superflous error checking
22+
try:
23+
# Create a new status for each endpoint
24+
status, created = Endpoint_Status.objects.get_or_create(
25+
finding=finding,
26+
endpoint=endpoint,
27+
)
28+
# Check if the status object was created, otherwise, there is nothing to do
29+
if created:
30+
status.date = finding.date
31+
# If the parent endpoint was mitigated with the old system,
32+
# reflect the same on the endpoint status object
33+
if endpoint.mitigated:
34+
status.mitigated = True
35+
status.mitigated_by = finding.reporter
36+
# Save the status object with at least one updated field
37+
status.save()
38+
# Attach the status to the endpoint and finding
39+
endpoint.endpoint_status.add(status)
40+
finding.endpoint_status.add(status)
41+
except Exception as e:
42+
# Something wild happened
43+
print(e)
44+
pass
45+
46+
dependencies = [
47+
('dojo', '0047_jira_minimum_severity_default'),
48+
]
49+
50+
operations = [migrations.RunPython(create_status_objects)]

0 commit comments

Comments
 (0)