Skip to content

Commit b9b4815

Browse files
handle reimport and close old findings
1 parent cb2d0e3 commit b9b4815

3 files changed

Lines changed: 46 additions & 4 deletions

File tree

dojo/importers/base_importer.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,7 @@ def mitigate_finding(
743743
note_message: str,
744744
*,
745745
finding_groups_enabled: bool,
746+
product_grading_option: bool = True,
746747
) -> None:
747748
"""
748749
Mitigates a finding, all endpoint statuses, leaves a note on the finding
@@ -764,9 +765,9 @@ def mitigate_finding(
764765
# to avoid pushing a finding group multiple times, we push those outside of the loop
765766
if finding_groups_enabled and finding.finding_group:
766767
# don't try to dedupe findings that we are closing
767-
finding.save(dedupe_option=False)
768+
finding.save(dedupe_option=False, product_grading_option=product_grading_option)
768769
else:
769-
finding.save(dedupe_option=False, push_to_jira=self.push_to_jira)
770+
finding.save(dedupe_option=False, push_to_jira=self.push_to_jira, product_grading_option=product_grading_option)
770771

771772
def notify_scan_added(
772773
self,

dojo/importers/default_importer.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,18 @@ def close_old_findings(
357357
"as it is not present anymore in recent scans."
358358
),
359359
finding_groups_enabled=self.findings_groups_enabled,
360+
product_grading_option=False,
360361
)
361362
# push finding groups to jira since we only only want to push whole groups
362363
if self.findings_groups_enabled and self.push_to_jira:
363364
for finding_group in {finding.finding_group for finding in old_findings if finding.finding_group is not None}:
364365
jira_helper.push_to_jira(finding_group)
365366

367+
# Calculate grade once after all findings have been closed
368+
if old_findings:
369+
product = self.test.engagement.product
370+
calculate_grade(product)
371+
366372
return old_findings
367373

368374
def parse_findings_static_test_type(

dojo/importers/default_reimporter.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import dojo.finding.helper as finding_helper
88
import dojo.jira_link.helper as jira_helper
9+
from dojo.decorators import we_want_async
910
from dojo.importers.base_importer import BaseImporter, Parser
1011
from dojo.importers.options import ImporterOptions
1112
from dojo.models import (
@@ -15,6 +16,8 @@
1516
Test,
1617
Test_Import,
1718
)
19+
from dojo.tasks import wait_for_tasks_and_calculate_grade
20+
from dojo.utils import calculate_grade
1821
from dojo.validators import clean_tags
1922

2023
logger = logging.getLogger(__name__)
@@ -176,6 +179,7 @@ def process_findings(
176179
self.reactivated_items = []
177180
self.unchanged_items = []
178181
self.group_names_to_findings_dict = {}
182+
async_task_ids = []
179183

180184
logger.debug(f"starting reimport of {len(parsed_findings) if parsed_findings else 0} items.")
181185
logger.debug("STEP 1: looping over findings from the reimported report and trying to match them to existing findings")
@@ -238,9 +242,24 @@ def process_findings(
238242
)
239243
# all data is already saved on the finding, we only need to trigger post processing
240244

241-
# to avoid pushing a finding group multiple times, we push those outside of the loop
245+
# Execute post-processing task immediately if async, otherwise execute synchronously
242246
push_to_jira = self.push_to_jira and (not self.findings_groups_enabled or not self.group_by)
243-
finding_helper.post_process_finding_save(finding, dedupe_option=True, rules_option=True, product_grading_option=True, issue_updater_option=True, push_to_jira=push_to_jira)
247+
248+
post_processing_task_signature = finding_helper.post_process_finding_save_signature(
249+
finding,
250+
dedupe_option=True,
251+
rules_option=True,
252+
product_grading_option=False,
253+
issue_updater_option=True,
254+
push_to_jira=push_to_jira,
255+
)
256+
if we_want_async(async_user=self.user):
257+
# Execute task immediately and collect task ID
258+
result = post_processing_task_signature.apply_async()
259+
async_task_ids.append(result.id)
260+
else:
261+
# Execute task immediately for synchronous processing
262+
post_processing_task_signature()
244263

245264
self.to_mitigate = (set(self.original_items) - set(self.reactivated_items) - set(self.unchanged_items))
246265
# due to #3958 we can have duplicates inside the same report
@@ -252,6 +271,16 @@ def process_findings(
252271
self.untouched = set(self.unchanged_items) - set(self.to_mitigate) - set(self.new_items) - set(self.reactivated_items)
253272
# Process groups
254273
self.process_groups_for_all_findings(**kwargs)
274+
275+
# Calculate product grade once after all findings are processed
276+
product = self.test.engagement.product
277+
278+
if we_want_async(async_user=self.user) and async_task_ids:
279+
# Tasks were executed immediately during processing, now coordinate final grade calculation
280+
wait_for_tasks_and_calculate_grade.delay(async_task_ids, product.id)
281+
# Synchronous tasks were already executed during processing, just calculate grade
282+
calculate_grade(product)
283+
255284
# Process the results and return them back
256285
return self.process_results(**kwargs)
257286

@@ -286,13 +315,19 @@ def close_old_findings(
286315
finding,
287316
f"Mitigated by {self.test.test_type} re-upload.",
288317
finding_groups_enabled=self.findings_groups_enabled,
318+
product_grading_option=False,
289319
)
290320
mitigated_findings.append(finding)
291321
# push finding groups to jira since we only only want to push whole groups
292322
if self.findings_groups_enabled and self.push_to_jira:
293323
for finding_group in {finding.finding_group for finding in findings if finding.finding_group is not None}:
294324
jira_helper.push_to_jira(finding_group)
295325

326+
# Calculate grade once after all findings have been closed
327+
if mitigated_findings:
328+
product = self.test.engagement.product
329+
calculate_grade(product)
330+
296331
return mitigated_findings
297332

298333
def parse_findings_static_test_type(

0 commit comments

Comments
 (0)