@@ -532,6 +532,20 @@ def get_jira_status(finding):
532532 return None
533533
534534
535+ # Used for unit testing so geting all the connections is manadatory
536+ def get_jira_priortiy (finding ):
537+ if finding .has_jira_issue :
538+ j_issue = finding .jira_issue .jira_id
539+ elif finding .finding_group and finding .finding_group .has_jira_issue :
540+ j_issue = finding .finding_group .jira_issue .jira_id
541+
542+ if j_issue :
543+ project = get_jira_project (finding )
544+ issue = jira_get_issue (project , j_issue )
545+ return issue .fields .priority
546+ return None
547+
548+
535549# Used for unit testing so geting all the connections is manadatory
536550def get_jira_comments (finding ):
537551 if finding .has_jira_issue :
@@ -676,15 +690,20 @@ def jira_description(obj):
676690def jira_priority (obj ):
677691 if isinstance (obj , Finding ):
678692 return get_jira_instance (obj ).get_priority (obj .severity )
693+
679694 if isinstance (obj , Finding_Group ):
680- finding_group_severity_for_jira = get_finding_group_findings_above_threshold (obj )
695+ # priority based on qualified findings, so if alls criticals get closed, the priority will gets lowered etc
696+ active_findings = get_qualified_findings (obj )
681697
682- max_number_severity = max (Finding .get_number_severity (find .severity ) for find in finding_group_severity_for_jira )
683- return Finding .get_severity (max_number_severity )
698+ if not active_findings :
699+ # using a string literal "Info" as we don't really have a "enum" for this anywhere
700+ max_number_severity = Finding .get_number_severity ("Info" )
701+ else :
702+ max_number_severity = max (Finding .get_number_severity (find .severity ) for find in active_findings )
703+ return get_jira_instance (obj ).get_priority (Finding .get_severity (max_number_severity ))
684704
685- logger .error ("unsupported object passed to push_to_jira: %s %i %s" , obj .__name__ , obj .id , obj )
686- msg = f"Unsupported object passed to push_to_jira: { type (obj )} "
687- raise RuntimeError (msg )
705+ msg = f"Unsupported object type for jira_priority: { obj .__class__ .__name__ } "
706+ raise ValueError (msg )
688707
689708
690709def jira_environment (obj ):
@@ -895,7 +914,7 @@ def failure_to_add_message(message: str, exception: Exception, _: Any) -> bool:
895914 return failure_to_add_message (message , e , obj )
896915 # Create a new issue in Jira with the fields set in the last step
897916 try :
898- logger .debug ("sending fields to JIRA: %s" , fields )
917+ logger .debug ("Creating new JIRA issue with fields : %s" , json . dumps ( fields , indent = 4 ) )
899918 new_issue = jira .create_issue (fields )
900919 logger .debug ("saving JIRA_Issue for %s finding %s" , new_issue .key , obj .id )
901920 j_issue = JIRA_Issue (jira_id = new_issue .id , jira_key = new_issue .key , jira_project = jira_project )
@@ -1016,6 +1035,14 @@ def failure_to_update_message(message: str, exception: Exception, obj: Any) -> b
10161035 labels = get_labels (obj ) + get_tags (obj )
10171036 if labels :
10181037 labels = list (dict .fromkeys (labels )) # de-dup
1038+
1039+ # Only Finding Groups will have their priority synced on updates.
1040+ # For Findings we resepect any priority change made in JIRA
1041+ # https://github.com/DefectDojo/django-DefectDojo/pull/9571 and https://github.com/DefectDojo/django-DefectDojo/pull/12475
1042+ jira_priority_name = None
1043+ if isinstance (obj , Finding_Group ):
1044+ jira_priority_name = jira_priority (obj )
1045+
10191046 # Set the fields that will compose the jira issue
10201047 try :
10211048 issuetype_fields = get_issuetype_fields (jira , jira_project .project_key , jira_instance .default_issue_type )
@@ -1027,20 +1054,18 @@ def failure_to_update_message(message: str, exception: Exception, obj: Any) -> b
10271054 component_name = jira_project .component if not issue .fields .components else None ,
10281055 labels = labels + issue .fields .labels ,
10291056 environment = jira_environment (obj ),
1030- # Do not update the priority in jira after creation as this could have changed in jira, but should not change in dojo
1031- # priority_name=jira_priority(obj),
1057+ priority_name = jira_priority_name ,
10321058 issuetype_fields = issuetype_fields )
10331059 except Exception as e :
10341060 message = f"Failed to fetch fields for { jira_instance .default_issue_type } under project { jira_project .project_key } - { e } "
10351061 return failure_to_update_message (message , e , obj )
1062+
10361063 # Update the issue in jira
10371064 try :
1038- logger .debug ("sending fields to JIRA : %s" , fields )
1065+ logger .debug ("Updating JIRA issue with fields : %s" , json . dumps ( fields , indent = 4 ) )
10391066 issue .update (
10401067 summary = fields ["summary" ],
10411068 description = fields ["description" ],
1042- # Do not update the priority in jira after creation as this could have changed in jira, but should not change in dojo
1043- # priority=fields['priority'],
10441069 fields = fields )
10451070 j_issue .jira_change = timezone .now ()
10461071 j_issue .save ()
@@ -1195,12 +1220,14 @@ def get_issuetype_fields(
11951220 try :
11961221 project = meta ["projects" ][0 ]
11971222 except Exception :
1223+ logger .debug ("JIRA meta: %s" , json .dumps (meta , indent = 4 )) # this is None safe
11981224 msg = "Project misconfigured or no permissions in Jira ?"
11991225 raise JIRAError (msg )
12001226
12011227 try :
12021228 issuetype_fields = project ["issuetypes" ][0 ]["fields" ].keys ()
12031229 except Exception :
1230+ logger .debug ("JIRA meta: %s" , json .dumps (meta , indent = 4 )) # this is None safe
12041231 msg = "Misconfigured default issue type ?"
12051232 raise JIRAError (msg )
12061233
@@ -1824,20 +1851,20 @@ def is_qualified(finding):
18241851 return finding .active and (finding .verified or not isenforced ) and (finding .numerical_severity <= jira_minimum_threshold )
18251852
18261853
1827- def get_qualified_findings (findings ):
1854+ def get_qualified_findings (finding_group ):
18281855 """Filters findings to return only findings qualified to be pushed to JIRA, i.e. active, verified (unless not enforced) and severity is above the threshold"""
1829- if not findings :
1856+ if not finding_group . findings . all () :
18301857 return None
18311858
1832- return [find for find in findings if is_qualified (find )]
1859+ return [find for find in finding_group . findings . all () if is_qualified (find )]
18331860
18341861
1835- def get_non_qualified_findings (findings ):
1862+ def get_non_qualified_findings (finding_group ):
18361863 """Filters findings to return only findings not qualified to be pushed to JIRA, i.e. inactive, not-verified (unless not enforced) and severity is below the threshold"""
1837- if not findings :
1864+ if not finding_group . findings . all () :
18381865 return None
18391866
1840- return [find for find in findings if not is_qualified (find )]
1867+ return [find for find in finding_group . findings . all () if not is_qualified (find )]
18411868
18421869
18431870def get_sla_deadline (obj ):
@@ -1849,10 +1876,10 @@ def get_sla_deadline(obj):
18491876 return obj .sla_deadline ()
18501877
18511878 if isinstance (obj , Finding_Group ):
1852- return min ([find .sla_deadline () for find in get_qualified_findings (obj . findings . all () ) if find .sla_deadline ()], default = None )
1879+ return min ([find .sla_deadline () for find in get_qualified_findings (obj ) if find .sla_deadline ()], default = None )
18531880
1854- logger . warning ( "get_sla_deadline: obj passed that is not a Finding or Finding_Group" )
1855- return None
1881+ msg = f "get_sla_deadline: obj passed that is not a Finding or Finding_Group: { type ( obj ) } "
1882+ raise ValueError ( msg )
18561883
18571884
18581885def get_severity (findings ):
0 commit comments