66
77import dojo .finding .helper as finding_helper
88import dojo .jira_link .helper as jira_helper
9+ from dojo .decorators import we_want_async
910from dojo .importers .base_importer import BaseImporter , Parser
1011from dojo .importers .options import ImporterOptions
1112from dojo .models import (
1516 Test ,
1617 Test_Import ,
1718)
19+ from dojo .tasks import wait_for_tasks_and_calculate_grade
20+ from dojo .utils import calculate_grade
1821from dojo .validators import clean_tags
1922
2023logger = 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