Skip to content

Commit 0827161

Browse files
committed
refactor: update push_to_jira and related functions to return status and messages
1 parent 7491625 commit 0827161

1 file changed

Lines changed: 38 additions & 33 deletions

File tree

dojo/jira_link/helper.py

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ def jira_environment(obj):
757757
return ""
758758

759759

760-
def push_to_jira(obj, *args, **kwargs):
760+
def push_to_jira(obj, *args, **kwargs) -> tuple[str, bool]:
761761
if obj is None:
762762
msg = "Cannot push None to JIRA"
763763
raise ValueError(msg)
@@ -773,29 +773,32 @@ def push_to_jira(obj, *args, **kwargs):
773773

774774
if isinstance(obj, Engagement):
775775
return dojo_dispatch_task(push_engagement_to_jira, obj.id, *args, **kwargs)
776-
logger.error("unsupported object passed to push_to_jira: %s %i %s", obj.__name__, obj.id, obj)
777-
return None
776+
message = f"unsupported object passed to push_to_jira: {obj.__class__.__name__} {obj.id} {obj}"
777+
logger.error(message)
778+
return False, message
778779

779780

780781
# we need thre separate celery tasks due to the decorators we're using to map to/from ids
781782
@app.task
782-
def push_finding_to_jira(finding_id, *args, **kwargs):
783+
def push_finding_to_jira(finding_id, *args, **kwargs) -> tuple[str, bool]:
783784
finding = get_object_or_none(Finding, id=finding_id)
784785
if not finding:
785-
logger.warning("Finding with id %s does not exist, skipping push_finding_to_jira", finding_id)
786-
return None
786+
message = f"Finding with id {finding_id} does not exist, skipping push_finding_to_jira"
787+
logger.warning(message)
788+
return False, message
787789

788790
if finding.has_jira_issue:
789791
return update_jira_issue(finding, *args, **kwargs)
790792
return add_jira_issue(finding, *args, **kwargs)
791793

792794

793795
@app.task
794-
def push_finding_group_to_jira(finding_group_id, *args, **kwargs):
796+
def push_finding_group_to_jira(finding_group_id, *args, **kwargs) -> tuple[str, bool]:
795797
finding_group = get_object_or_none(Finding_Group, id=finding_group_id)
796798
if not finding_group:
797-
logger.warning("Finding_Group with id %s does not exist, skipping push_finding_group_to_jira", finding_group_id)
798-
return None
799+
message = f"Finding_Group with id {finding_group_id} does not exist, skipping push_finding_group_to_jira"
800+
logger.warning(message)
801+
return False, message
799802

800803
# Look for findings that have single ticket associations separate from the group
801804
for finding in finding_group.findings.filter(jira_issue__isnull=False):
@@ -807,11 +810,12 @@ def push_finding_group_to_jira(finding_group_id, *args, **kwargs):
807810

808811

809812
@app.task
810-
def push_engagement_to_jira(engagement_id, *args, **kwargs):
813+
def push_engagement_to_jira(engagement_id, *args, **kwargs) -> tuple[str, bool]:
811814
engagement = get_object_or_none(Engagement, id=engagement_id)
812815
if not engagement:
813-
logger.warning("Engagement with id %s does not exist, skipping push_engagement_to_jira", engagement_id)
814-
return None
816+
message = f"Engagement with id {engagement_id} does not exist, skipping push_engagement_to_jira"
817+
logger.warning(message)
818+
return False, message
815819

816820
if engagement.has_jira_issue:
817821
return dojo_dispatch_task(update_epic, engagement.id, *args, **kwargs)
@@ -894,18 +898,18 @@ def prepare_jira_issue_fields(
894898
return fields
895899

896900

897-
def add_jira_issue(obj, *args, **kwargs):
898-
def failure_to_add_message(message: str, exception: Exception, _: Any) -> bool:
901+
def add_jira_issue(obj, *args, **kwargs) -> tuple[str, bool]:
902+
def failure_to_add_message(message: str, exception: Exception, _: Any) -> tuple[str, bool]:
899903
if exception:
900904
logger.error("Exception occurred", exc_info=exception)
901905
logger.error(message)
902906
log_jira_alert(message, obj)
903-
return False
907+
return False, message
904908

905909
logger.info("trying to create a new jira issue for %d:%s", obj.id, to_str_typed(obj))
906910

907911
if not is_jira_enabled():
908-
return False
912+
return False, "JIRA integration is not enabled."
909913

910914
if not is_jira_configured_and_enabled(obj):
911915
message = f"Object {obj.id} cannot be pushed to JIRA as there is no JIRA configuration for {to_str_typed(obj)}."
@@ -932,20 +936,20 @@ def failure_to_add_message(message: str, exception: Exception, _: Any) -> bool:
932936

933937
# not sure why this check is not part of can_be_pushed_to_jira, but afraid to change it
934938
if isinstance(obj, Finding) and obj.duplicate and not obj.active:
935-
logger.info("%s will not be pushed to JIRA as it's a duplicate finding", to_str_typed(obj))
939+
message = f"{to_str_typed(obj)} is a duplicate and inactive finding, and will not be pushed to JIRA: {error_message}."
936940
# Duplicates are expected, don't create alerts
937-
logger.info("%s cannot be pushed to JIRA: %s (expected - duplicate finding)",
938-
to_str_typed(obj), error_message)
941+
logger.info(message)
939942
elif error_code in expected_validation_errors:
943+
message = f"{to_str_typed(obj)} cannot be pushed to JIRA: {error_message}."
940944
# These are expected when auto-pushing, only log, don't alert
941-
logger.info("%s cannot be pushed to JIRA: %s (expected - finding not ready yet)",
942-
to_str_typed(obj), error_message)
945+
logger.info(message)
943946
else:
944947
# Unexpected errors (configuration issues, etc.) should still alert
945-
log_jira_cannot_be_pushed_reason(error_message, obj)
946-
logger.warning("%s cannot be pushed to JIRA: %s.", to_str_typed(obj), error_message)
948+
message = f"{to_str_typed(obj)} cannot be pushed to JIRA due to an unexpected error: {error_message}."
949+
log_jira_cannot_be_pushed_reason(message, obj)
950+
logger.warning("%s cannot be pushed to JIRA: %s.", to_str_typed(obj), message)
947951
logger.warning("The JIRA issue will NOT be created.")
948-
return False
952+
return False, message
949953
logger.debug("Trying to create a new JIRA issue for %s...", to_str_typed(obj))
950954
# Attempt to get the jira connection
951955
try:
@@ -1056,21 +1060,21 @@ def failure_to_add_message(message: str, exception: Exception, _: Any) -> bool:
10561060
message = f"Failed to assign jira issue to existing epic: {e}"
10571061
return failure_to_add_message(message, e, obj)
10581062

1059-
return True
1063+
return True, "JIRA issue created successfully."
10601064

10611065

1062-
def update_jira_issue(obj, *args, **kwargs):
1063-
def failure_to_update_message(message: str, exception: Exception, obj: Any) -> bool:
1066+
def update_jira_issue(obj, *args, **kwargs) -> tuple[str, bool]:
1067+
def failure_to_update_message(message: str, exception: Exception, obj: Any) -> tuple[str, bool]:
10641068
if exception:
10651069
logger.error(exception)
10661070
logger.error(message)
10671071
log_jira_alert(message, obj)
1068-
return False
1072+
return False, message
10691073

10701074
logger.debug("trying to update a linked jira issue for %d:%s", obj.id, to_str_typed(obj))
10711075

10721076
if not is_jira_enabled():
1073-
return False
1077+
return False, "JIRA integration is not enabled."
10741078

10751079
jira_project = get_jira_project(obj)
10761080
jira_instance = get_jira_instance(obj)
@@ -1181,7 +1185,7 @@ def failure_to_update_message(message: str, exception: Exception, obj: Any) -> b
11811185
message = f"Failed to assign jira issue to existing epic: {e}"
11821186
return failure_to_update_message(message, e, obj)
11831187

1184-
return True
1188+
return True, "JIRA issue updated successfully."
11851189

11861190

11871191
def get_jira_issue_from_jira(find):
@@ -1847,7 +1851,8 @@ def process_jira_epic_form(request, engagement=None):
18471851
epic_priority = None
18481852
if jira_epic_form.cleaned_data.get("epic_priority"):
18491853
epic_priority = jira_epic_form.cleaned_data.get("epic_priority")
1850-
if push_to_jira(engagement, epic_name=epic_name, epic_priority=epic_priority):
1854+
success, message = push_to_jira(engagement, epic_name=epic_name, epic_priority=epic_priority)
1855+
if success:
18511856
logger.debug("Push to JIRA for Epic queued successfully")
18521857
messages.add_message(
18531858
request,
@@ -1856,11 +1861,11 @@ def process_jira_epic_form(request, engagement=None):
18561861
extra_tags="alert-success")
18571862
else:
18581863
error = True
1859-
logger.debug("Push to JIRA for Epic failey")
1864+
logger.debug("Push to JIRA for Epic failed")
18601865
messages.add_message(
18611866
request,
18621867
messages.ERROR,
1863-
"Push to JIRA for Epic failed, check alerts on the top right for errors",
1868+
message,
18641869
extra_tags="alert-danger")
18651870
else:
18661871
logger.debug("invalid jira epic form")

0 commit comments

Comments
 (0)