Skip to content

Commit 061ef76

Browse files
jira push error reasons should not be propagated to all channels (#11738)
* jira push error reasons should not be propagated to all notification channels * linting * linting
1 parent 07671bc commit 061ef76

File tree

4 files changed

+79
-52
lines changed

4 files changed

+79
-52
lines changed

dojo/finding/views.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2983,7 +2983,7 @@ def finding_bulk_update_all(request, pid=None):
29832983
) = jira_helper.can_be_pushed_to_jira(group)
29842984
if not can_be_pushed_to_jira:
29852985
error_counts[error_message] += 1
2986-
jira_helper.log_jira_alert(error_message, group)
2986+
jira_helper.log_jira_cannot_be_pushed_reason(error_message, group)
29872987
else:
29882988
logger.debug(
29892989
"pushing to jira from finding.finding_bulk_update_all()",
@@ -3033,10 +3033,10 @@ def finding_bulk_update_all(request, pid=None):
30333033
"finding already pushed as part of Finding Group"
30343034
)
30353035
error_counts[error_message] += 1
3036-
jira_helper.log_jira_alert(error_message, finding)
3036+
jira_helper.log_jira_cannot_be_pushed_reason(error_message, finding)
30373037
elif not can_be_pushed_to_jira:
30383038
error_counts[error_message] += 1
3039-
jira_helper.log_jira_alert(error_message, finding)
3039+
jira_helper.log_jira_cannot_be_pushed_reason(error_message, finding)
30403040
else:
30413041
logger.debug(
30423042
"pushing to jira from finding.finding_bulk_update_all()",

dojo/jira_link/helper.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,8 @@ def get_jira_comments(finding):
513513
return None
514514

515515

516-
# Logs the error to the alerts table, which appears in the notification toolbar
517516
def log_jira_generic_alert(title, description):
517+
"""Creates a notification for JIRA errors happening outside the scope of a specific (finding/group/epic) object"""
518518
create_notification(
519519
event="jira_update",
520520
title=title,
@@ -523,8 +523,8 @@ def log_jira_generic_alert(title, description):
523523
source="JIRA")
524524

525525

526-
# Logs the error to the alerts table, which appears in the notification toolbar
527526
def log_jira_alert(error, obj):
527+
"""Creates a notification for JIRA errors when handling a specific (finding/group/epic) object"""
528528
create_notification(
529529
event="jira_update",
530530
title="Error pushing to JIRA " + "(" + truncate_with_dots(prod_name(obj), 25) + ")",
@@ -535,6 +535,19 @@ def log_jira_alert(error, obj):
535535
obj=obj)
536536

537537

538+
def log_jira_cannot_be_pushed_reason(error, obj):
539+
"""Creates an Alert for GUI display when handling a specific (finding/group/epic) object"""
540+
create_notification(
541+
event="jira_update",
542+
title="Error pushing to JIRA " + "(" + truncate_with_dots(prod_name(obj), 25) + ")",
543+
description=obj.__class__.__name__ + ": " + error,
544+
url=obj.get_absolute_url(),
545+
icon="bullseye",
546+
source="Push to JIRA",
547+
obj=obj,
548+
alert_only=True)
549+
550+
538551
# Displays an alert for Jira notifications
539552
def log_jira_message(text, finding):
540553
create_notification(
@@ -787,10 +800,12 @@ def failure_to_add_message(message: str, exception: Exception, object: Any) -> b
787800

788801
obj_can_be_pushed_to_jira, error_message, _error_code = can_be_pushed_to_jira(obj)
789802
if not obj_can_be_pushed_to_jira:
803+
# not sure why this check is not part of can_be_pushed_to_jira, but afraid to change it
790804
if isinstance(obj, Finding) and obj.duplicate and not obj.active:
791805
logger.warning("%s will not be pushed to JIRA as it's a duplicate finding", to_str_typed(obj))
806+
log_jira_cannot_be_pushed_reason(error_message + " and findis a duplicate", obj)
792807
else:
793-
log_jira_alert(error_message, obj)
808+
log_jira_cannot_be_pushed_reason(error_message, obj)
794809
logger.warning("%s cannot be pushed to JIRA: %s.", to_str_typed(obj), error_message)
795810
logger.warning("The JIRA issue will NOT be created.")
796811
return False

dojo/notifications/helper.py

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def create_notification(
5454
no_users: bool = False, # noqa: FBT001
5555
url: str | None = None,
5656
url_api: str | None = None,
57+
alert_only: bool = False, # noqa: FBT001
5758
**kwargs: dict,
5859
) -> None:
5960
"""Create an instance of a NotificationManager and dispatch the notification."""
@@ -86,6 +87,7 @@ def create_notification(
8687
no_users=no_users,
8788
url=url,
8889
url_api=url_api,
90+
alert_only=alert_only,
8991
**kwargs,
9092
)
9193

@@ -802,61 +804,68 @@ def _process_notifications(
802804
)
803805
logger.debug("process notifications for %s", notifications.user)
804806

805-
if self.system_settings.enable_slack_notifications and "slack" in getattr(
806-
notifications,
807-
event,
808-
getattr(notifications, "other"),
809-
):
810-
logger.debug("Sending Slack Notification")
811-
self._get_manager_instance("slack").send_slack_notification(
807+
alert_only = kwargs.get("alert_only", False)
808+
if alert_only:
809+
logger.debug("sending alert only")
810+
811+
if "alert" in getattr(notifications, event, getattr(notifications, "other")):
812+
logger.debug(f"Sending Alert to {notifications.user}")
813+
self._get_manager_instance("alert").send_alert_notification(
812814
event,
813815
user=notifications.user,
814816
**kwargs,
815817
)
816818

817-
if self.system_settings.enable_msteams_notifications and "msteams" in getattr(
818-
notifications,
819-
event,
820-
getattr(notifications, "other"),
821-
):
822-
logger.debug("Sending MSTeams Notification")
823-
self._get_manager_instance("msteams").send_msteams_notification(
819+
# Some errors should not be pushed to all channels, only to alerts.
820+
# For example reasons why JIRA Issues: https://github.com/DefectDojo/django-DefectDojo/issues/11575
821+
if not alert_only:
822+
if self.system_settings.enable_slack_notifications and "slack" in getattr(
823+
notifications,
824824
event,
825-
user=notifications.user,
826-
**kwargs,
827-
)
825+
getattr(notifications, "other"),
826+
):
827+
logger.debug("Sending Slack Notification")
828+
self._get_manager_instance("slack").send_slack_notification(
829+
event,
830+
user=notifications.user,
831+
**kwargs,
832+
)
828833

829-
if self.system_settings.enable_mail_notifications and "mail" in getattr(
830-
notifications,
831-
event,
832-
getattr(notifications, "other"),
833-
):
834-
logger.debug("Sending Mail Notification")
835-
self._get_manager_instance("mail").send_mail_notification(
834+
if self.system_settings.enable_msteams_notifications and "msteams" in getattr(
835+
notifications,
836836
event,
837-
user=notifications.user,
838-
**kwargs,
839-
)
837+
getattr(notifications, "other"),
838+
):
839+
logger.debug("Sending MSTeams Notification")
840+
self._get_manager_instance("msteams").send_msteams_notification(
841+
event,
842+
user=notifications.user,
843+
**kwargs,
844+
)
840845

841-
if self.system_settings.enable_webhooks_notifications and "webhooks" in getattr(
842-
notifications,
843-
event,
844-
getattr(notifications, "other"),
845-
):
846-
logger.debug("Sending Webhooks Notification")
847-
self._get_manager_instance("webhooks").send_webhooks_notification(
846+
if self.system_settings.enable_mail_notifications and "mail" in getattr(
847+
notifications,
848848
event,
849-
user=notifications.user,
850-
**kwargs,
851-
)
849+
getattr(notifications, "other"),
850+
):
851+
logger.debug("Sending Mail Notification")
852+
self._get_manager_instance("mail").send_mail_notification(
853+
event,
854+
user=notifications.user,
855+
**kwargs,
856+
)
852857

853-
if "alert" in getattr(notifications, event, getattr(notifications, "other")):
854-
logger.debug(f"Sending Alert to {notifications.user}")
855-
self._get_manager_instance("alert").send_alert_notification(
858+
if self.system_settings.enable_webhooks_notifications and "webhooks" in getattr(
859+
notifications,
856860
event,
857-
user=notifications.user,
858-
**kwargs,
859-
)
861+
getattr(notifications, "other"),
862+
):
863+
logger.debug("Sending Webhooks Notification")
864+
self._get_manager_instance("webhooks").send_webhooks_notification(
865+
event,
866+
user=notifications.user,
867+
**kwargs,
868+
)
860869

861870

862871
@app.task(ignore_result=True)

dojo/templates/base.html

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@
159159
{% if request.user.is_authenticated %}
160160
<li>
161161
<a href="{% url 'view_profile' %}">
162-
<i class="fa-solid fa-user fa-fw"></i>
162+
<i class="fa-solid fa-user fa-fw"></i>
163163
{{ request.user.username }}
164164
</a>
165165
</li>
@@ -438,7 +438,7 @@
438438
</a>
439439
<ul class="nav nav-second-level">
440440
{% if "auth.view_user"|has_configuration_permission:request %}
441-
<li>
441+
<li>
442442
<a href="{% url 'users' %}">
443443
{% trans "Users" %}
444444
</a>
@@ -666,7 +666,7 @@ <h3 class="no-margin-top" style="padding-bottom: 5px;">
666666
<a class="dropdown-toggle" data-toggle="dropdown" href="">
667667
<span class="fa-solid fa-calendar-days" aria-hidden="true"></span>
668668
<span class="hidden-xs">{% trans "Engagements" %}
669-
{% if product_tab.engagements > 0 %}
669+
{% if product_tab.engagements > 0 %}
670670
<span class="badge">{{ product_tab.engagements }}</span>
671671
{% endif %}
672672
</span>
@@ -1136,6 +1136,9 @@ <h3 class="no-margin-top" style="padding-bottom: 5px;">
11361136
{% endif %}
11371137

11381138
function htmlEscape(str) {
1139+
if (!str) {
1140+
return '';
1141+
}
11391142
return str
11401143
.replace(/\n/g, " ")
11411144
.replace(/&/g, '&amp;')

0 commit comments

Comments
 (0)