Skip to content

Commit ad34f65

Browse files
jira: add none checks in a few places (#13886)
1 parent a43269c commit ad34f65

4 files changed

Lines changed: 56 additions & 13 deletions

File tree

dojo/finding/helper.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ def add_to_finding_group(finding_group, finds):
236236
finding_group.findings.add(*available_findings)
237237

238238
# Now update the JIRA to add the finding to the finding group
239-
if finding_group.has_jira_issue and jira_helper.get_jira_instance(finding_group).finding_jira_sync:
239+
jira_instance = jira_helper.get_jira_instance(finding_group)
240+
if finding_group.has_jira_issue and jira_instance and jira_instance.finding_jira_sync:
240241
logger.debug("pushing to jira from finding.finding_bulk_update_all()")
241242
jira_helper.push_to_jira(finding_group)
242243

@@ -263,7 +264,8 @@ def remove_from_finding_group(finds):
263264

264265
# Now update the JIRA to remove the finding from the finding group
265266
for group in affected_groups:
266-
if group.has_jira_issue and jira_helper.get_jira_instance(group).finding_jira_sync:
267+
jira_instance = jira_helper.get_jira_instance(group)
268+
if group.has_jira_issue and jira_instance and jira_instance.finding_jira_sync:
267269
logger.debug("pushing to jira from finding.finding_bulk_update_all()")
268270
jira_helper.push_to_jira(group)
269271

@@ -340,7 +342,8 @@ def group_findings_by(finds, finding_group_by_option):
340342

341343
# Now update the JIRA to add the finding to the finding group
342344
for group in affected_groups:
343-
if group.has_jira_issue and jira_helper.get_jira_instance(group).finding_jira_sync:
345+
jira_instance = jira_helper.get_jira_instance(group)
346+
if group.has_jira_issue and jira_instance and jira_instance.finding_jira_sync:
344347
logger.debug("pushing to jira from finding.finding_bulk_update_all()")
345348
jira_helper.push_to_jira(group)
346349

dojo/finding/views.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -983,10 +983,11 @@ def process_jira_form(self, request: HttpRequest, finding: Finding, context: dic
983983
jira_helper.finding_link_jira(request, finding, new_jira_issue_key)
984984
jira_message = "Linked a JIRA issue successfully."
985985
# any existing finding should be updated
986+
jira_instance = jira_helper.get_jira_instance(finding)
986987
push_to_jira = (
987988
push_to_jira
988989
and not (push_to_jira and finding.finding_group)
989-
and (finding.has_jira_issue or jira_helper.get_jira_instance(finding).finding_jira_sync)
990+
and (finding.has_jira_issue or (jira_instance and jira_instance.finding_jira_sync))
990991
)
991992
# Determine if a message should be added
992993
if jira_message:
@@ -1251,8 +1252,9 @@ def defect_finding_review(request, fid):
12511252
# Only push if the finding is not in a group
12521253
if jira_issue_exists:
12531254
# Determine if any automatic sync should occur
1255+
jira_instance = jira_helper.get_jira_instance(finding)
12541256
push_to_jira = jira_helper.is_push_all_issues(finding) \
1255-
or jira_helper.get_jira_instance(finding).finding_jira_sync
1257+
or (jira_instance and jira_instance.finding_jira_sync)
12561258
# Add the closing note
12571259
if push_to_jira and not finding_in_group:
12581260
if defect_choice == "Close Finding":
@@ -1543,8 +1545,9 @@ def request_finding_review(request, fid):
15431545
# Only push if the finding is not in a group
15441546
if jira_issue_exists:
15451547
# Determine if any automatic sync should occur
1548+
jira_instance = jira_helper.get_jira_instance(finding)
15461549
push_to_jira = jira_helper.is_push_all_issues(finding) \
1547-
or jira_helper.get_jira_instance(finding).finding_jira_sync
1550+
or (jira_instance and jira_instance.finding_jira_sync)
15481551
# Add the closing note
15491552
if push_to_jira and not finding_in_group:
15501553
jira_helper.add_comment(finding, new_note, force_push=True)
@@ -1637,8 +1640,9 @@ def clear_finding_review(request, fid):
16371640
# Only push if the finding is not in a group
16381641
if jira_issue_exists:
16391642
# Determine if any automatic sync should occur
1643+
jira_instance = jira_helper.get_jira_instance(finding)
16401644
push_to_jira = jira_helper.is_push_all_issues(finding) \
1641-
or jira_helper.get_jira_instance(finding).finding_jira_sync
1645+
or (jira_instance and jira_instance.finding_jira_sync)
16421646
# Add the closing note
16431647
if push_to_jira and not finding_in_group:
16441648
jira_helper.add_comment(finding, new_note, force_push=True)

dojo/finding_group/views.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,16 @@ def view_finding_group(request, fgid):
8282

8383
if jira_issue:
8484
# See if the submitted issue was a issue key or the full URL
85-
jira_instance = jira_helper.get_jira_project(finding_group).jira_instance
85+
jira_project = jira_helper.get_jira_project(finding_group)
86+
if not jira_project or not jira_project.jira_instance:
87+
messages.add_message(
88+
request,
89+
messages.ERROR,
90+
"Cannot process JIRA issue: JIRA instance is not configured or has been deleted.",
91+
extra_tags="alert-danger",
92+
)
93+
return render(request, "dojo/edit_finding_group.html", {"form": edit_finding_group_form, "finding_group": finding_group})
94+
jira_instance = jira_project.jira_instance
8695
jira_issue = jira_issue.removeprefix(jira_instance.url + "/browse/")
8796

8897
if finding_group.has_jira_issue and jira_issue != jira_helper.get_jira_key(finding_group):

dojo/jira_link/helper.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,10 @@ def get_jira_project_key(obj):
369369
def 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

12011213
def 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

13021319
def 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

Comments
 (0)