@@ -145,17 +145,19 @@ def _safely_get_obj_status_for_jira(obj: Finding | Finding_Group, *, isenforced:
145145 return status or ["Inactive" ]
146146
147147
148- def is_keep_in_sync_with_jira (finding ):
149- keep_in_sync_enabled = False
150- # Check if there is a jira issue that needs to be updated
151- jira_issue_exists = finding .has_jira_issue or (finding .finding_group and finding .finding_group .has_jira_issue )
152- if jira_issue_exists :
153- # Determine if any automatic sync should occur
154- jira_instance = get_jira_instance (finding )
155- if jira_instance :
156- keep_in_sync_enabled = jira_instance .finding_jira_sync
157-
158- return keep_in_sync_enabled
148+ def is_keep_in_sync_with_jira (obj : Finding | Finding_Group , prefetched_jira_instance : JIRA_Instance = None ):
149+ """Determine if any automatic sync should occur"""
150+ jira_issue_exists = False
151+ # Check for a jira issue on each type of object
152+ if isinstance (obj , Finding ):
153+ jira_issue_exists = obj .has_jira_issue or (obj .finding_group and obj .finding_group .has_jira_issue )
154+ elif isinstance (obj , Finding_Group ):
155+ jira_issue_exists = obj .has_jira_issue
156+ # Now determine if we need to pull the jira instance to check if sync is enabled
157+ # but only if there is a jira issue that would need syncing
158+ if jira_issue_exists and (jira_instance := prefetched_jira_instance or get_jira_instance (obj )) is not None :
159+ return jira_instance .finding_jira_sync
160+ return False
159161
160162
161163# checks if a finding can be pushed to JIRA
@@ -225,8 +227,8 @@ def can_be_pushed_to_jira(obj, form=None):
225227
226228
227229# use_inheritance=True means get jira_project config from product if engagement itself has none
228- def get_jira_project (obj , * , use_inheritance = True ):
229- if not is_jira_enabled ():
230+ def get_jira_project (obj , * , use_inheritance = True , jira_enabled : bool = False ):
231+ if not jira_enabled and not ( jira_enabled := is_jira_enabled () ):
230232 return None
231233
232234 if obj is None :
@@ -242,19 +244,19 @@ def get_jira_project(obj, *, use_inheritance=True):
242244 return obj .jira_project
243245 # some old jira_issue records don't have a jira_project, so try to go via the finding instead
244246 if (hasattr (obj , "finding" ) and obj .finding ) or (hasattr (obj , "engagement" ) and obj .engagement ):
245- return get_jira_project (obj .finding , use_inheritance = use_inheritance )
247+ return get_jira_project (obj .finding , use_inheritance = use_inheritance , jira_enabled = jira_enabled )
246248 return None
247249
248250 if isinstance (obj , Finding | Stub_Finding ):
249251 finding = obj
250- return get_jira_project (finding .test )
252+ return get_jira_project (finding .test , jira_enabled = jira_enabled )
251253
252254 if isinstance (obj , Finding_Group ):
253- return get_jira_project (obj .test )
255+ return get_jira_project (obj .test , jira_enabled = jira_enabled )
254256
255257 if isinstance (obj , Test ):
256258 test = obj
257- return get_jira_project (test .engagement )
259+ return get_jira_project (test .engagement , jira_enabled = jira_enabled )
258260
259261 if isinstance (obj , Engagement ):
260262 engagement = obj
@@ -269,7 +271,7 @@ def get_jira_project(obj, *, use_inheritance=True):
269271
270272 if use_inheritance :
271273 logger .debug ("delegating to product %s for %s" , engagement .product , engagement )
272- return get_jira_project (engagement .product )
274+ return get_jira_project (engagement .product , jira_enabled = jira_enabled )
273275 logger .debug ("not delegating to product %s for %s" , engagement .product , engagement )
274276 return None
275277
@@ -286,11 +288,11 @@ def get_jira_project(obj, *, use_inheritance=True):
286288 return None
287289
288290
289- def get_jira_instance (obj ):
290- if not is_jira_enabled ():
291+ def get_jira_instance (obj , jira_enabled : bool = False ): # noqa: FBT001, FBT002
292+ if not jira_enabled and not ( jira_enabled := is_jira_enabled () ):
291293 return None
292294
293- jira_project = get_jira_project (obj )
295+ jira_project = get_jira_project (obj , jira_enabled = jira_enabled )
294296 if jira_project :
295297 logger .debug ("found jira_instance %s for %s" , jira_project .jira_instance , obj )
296298 return jira_project .jira_instance
@@ -415,17 +417,17 @@ def get_jira_finding_text(jira_instance):
415417 return None
416418
417419
418- def has_jira_issue (obj ) :
420+ def has_jira_issue (obj : Finding | Engagement | Finding_Group ) -> bool :
419421 return get_jira_issue (obj ) is not None
420422
421423
422- def get_jira_issue (obj ) :
423- if isinstance ( obj , Finding | Engagement | Finding_Group ):
424- try :
425- return obj .jira_issue
426- except JIRA_Issue . DoesNotExist :
427- return None
428- return None
424+ def get_jira_issue (obj : Finding | Engagement | Finding_Group ) -> JIRA_Issue | None :
425+ """
426+ This pattern is "cheaper" than the try/catch handling of the DoesNotExist exception
427+ that would happen if we try to access obj.jira_issue when there is none, and it also
428+ works with prefetch_related where the related object is None instead of a RelatedManager
429+ """
430+ return getattr ( obj , "jira_issue" , None )
429431
430432
431433def has_jira_configured (obj ):
0 commit comments