@@ -369,10 +369,10 @@ def get_jira_project_key(obj):
369369def get_jira_issue_template (obj ):
370370 jira_project = get_jira_project (obj )
371371
372- template_dir = jira_project .issue_template_dir
372+ template_dir = jira_project .issue_template_dir if jira_project else None
373373 if not template_dir :
374374 jira_instance = get_jira_instance (obj )
375- template_dir = jira_instance .issue_template_dir
375+ template_dir = jira_instance .issue_template_dir if jira_instance else None
376376
377377 # fallback to default as before
378378 if not template_dir :
@@ -902,6 +902,10 @@ def failure_to_add_message(message: str, exception: Exception, _: Any) -> bool:
902902 jira_project = get_jira_project (obj )
903903 jira_instance = get_jira_instance (obj )
904904
905+ if not jira_instance :
906+ message = f"Object { obj .id } cannot be pushed to JIRA as the JIRA instance has been deleted or is not available."
907+ return failure_to_add_message (message , None , obj )
908+
905909 obj_can_be_pushed_to_jira , error_message , _error_code = can_be_pushed_to_jira (obj )
906910 if not obj_can_be_pushed_to_jira :
907911 # not sure why this check is not part of can_be_pushed_to_jira, but afraid to change it
@@ -1046,10 +1050,17 @@ def failure_to_update_message(message: str, exception: Exception, obj: Any) -> b
10461050 message = f"Object { obj .id } cannot be pushed to JIRA as there is no JIRA configuration for { to_str_typed (obj )} ."
10471051 return failure_to_update_message (message , None , obj )
10481052
1053+ if not jira_instance :
1054+ message = f"Object { obj .id } cannot be pushed to JIRA as the JIRA instance has been deleted or is not available."
1055+ return failure_to_update_message (message , None , obj )
1056+
10491057 j_issue = obj .jira_issue
10501058 try :
10511059 JIRAError .log_to_tempfile = False
10521060 jira = get_jira_connection (jira_instance )
1061+ if not jira :
1062+ message = f"Object { obj .id } cannot be pushed to JIRA as the JIRA connection could not be established."
1063+ return failure_to_update_message (message , None , obj )
10531064 issue = jira .issue (j_issue .jira_id )
10541065 except Exception as e :
10551066 message = f"The following jira instance could not be connected: { jira_instance } - { e } "
@@ -1168,7 +1179,8 @@ def get_jira_issue_from_jira(find):
11681179 return jira .issue (j_issue .jira_id )
11691180
11701181 except JIRAError as e :
1171- logger .exception ("jira_meta for project: %s and url: %s meta: %s" , jira_project .project_key , jira_project .jira_instance .url , json .dumps (meta , indent = 4 )) # this is None safe
1182+ jira_url = jira_project .jira_instance .url if (jira_project and jira_project .jira_instance ) else "N/A"
1183+ logger .exception ("jira_meta for project: %s and url: %s meta: %s" , jira_project .project_key if jira_project else "N/A" , jira_url , json .dumps (meta , indent = 4 ))
11721184 log_jira_alert (e .text , find )
11731185 return None
11741186
@@ -1199,6 +1211,10 @@ def issue_from_jira_is_active(issue_from_jira):
11991211
12001212
12011213def push_status_to_jira (obj , jira_instance , jira , issue , * , save = False ):
1214+ if not jira_instance :
1215+ logger .warning ("Cannot push status to JIRA for %d:%s - jira_instance is None" , obj .id , to_str_typed (obj ))
1216+ return False
1217+
12021218 status_list = _safely_get_obj_status_for_jira (obj )
12031219 issue_closed = False
12041220 updated = False
@@ -1224,6 +1240,7 @@ def push_status_to_jira(obj, jira_instance, jira, issue, *, save=False):
12241240 if updated and save :
12251241 obj .jira_issue .jira_change = timezone .now ()
12261242 obj .jira_issue .save ()
1243+ return updated
12271244
12281245
12291246# gets the metadata for the provided issue type in the provided jira project
@@ -1300,6 +1317,8 @@ def get_issuetype_fields(
13001317
13011318
13021319def is_jira_project_valid (jira_project ):
1320+ if not jira_project or not jira_project .jira_instance :
1321+ return False
13031322 try :
13041323 jira = get_jira_connection (jira_project )
13051324 get_issuetype_fields (jira , jira_project .project_key , jira_project .jira_instance .default_issue_type )
@@ -1361,7 +1380,11 @@ def close_epic(eng, push_to_jira, **kwargs):
13611380
13621381 jira_project = get_jira_project (engagement )
13631382 jira_instance = get_jira_instance (engagement )
1364- if jira_project .enable_engagement_epic_mapping :
1383+ if not jira_instance :
1384+ logger .warning ("JIRA close epic failed: jira_instance is None" )
1385+ return False
1386+
1387+ if jira_project and jira_project .enable_engagement_epic_mapping :
13651388 if push_to_jira :
13661389 try :
13671390 jissue = get_jira_issue (eng )
@@ -1449,7 +1472,11 @@ def add_epic(engagement, **kwargs):
14491472
14501473 jira_project = get_jira_project (engagement )
14511474 jira_instance = get_jira_instance (engagement )
1452- if jira_project .enable_engagement_epic_mapping :
1475+ if not jira_instance :
1476+ logger .warning ("JIRA add epic failed: jira_instance is None" )
1477+ return False
1478+
1479+ if jira_project and jira_project .enable_engagement_epic_mapping :
14531480 epic_name = kwargs .get ("epic_name" )
14541481 epic_issue_type_name = getattr (jira_project , "epic_issue_type_name" , "Epic" )
14551482 if not epic_name :
0 commit comments