Skip to content

Commit 2869813

Browse files
fix notifications
1 parent 2ed1533 commit 2869813

2 files changed

Lines changed: 40 additions & 20 deletions

File tree

dojo/notifications/helper.py

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ class SlackNotificationManger(NotificationManagerHelpers):
200200

201201
"""Manger for slack notifications and their helpers."""
202202

203-
@app.task(base=DojoAsyncTask)
204203
def send_slack_notification(
205204
self,
206205
event: str,
@@ -317,7 +316,6 @@ class MSTeamsNotificationManger(NotificationManagerHelpers):
317316

318317
"""Manger for Microsoft Teams notifications and their helpers."""
319318

320-
@app.task(base=DojoAsyncTask)
321319
def send_msteams_notification(
322320
self,
323321
event: str,
@@ -367,7 +365,6 @@ class EmailNotificationManger(NotificationManagerHelpers):
367365

368366
"""Manger for email notifications and their helpers."""
369367

370-
@app.task(base=DojoAsyncTask)
371368
def send_mail_notification(
372369
self,
373370
event: str,
@@ -418,7 +415,6 @@ class WebhookNotificationManger(NotificationManagerHelpers):
418415
ERROR_PERMANENT = "permanent"
419416
ERROR_TEMPORARY = "temporary"
420417

421-
@app.task(base=DojoAsyncTask)
422418
def send_webhooks_notification(
423419
self,
424420
event: str,
@@ -477,11 +473,7 @@ def send_webhooks_notification(
477473
endpoint.first_error = now
478474
endpoint.status = Notification_Webhooks.Status.STATUS_INACTIVE_TMP
479475
# In case of failure within one day, endpoint can be deactivated temporally only for one minute
480-
self._webhook_reactivation.apply_async(
481-
args=[self],
482-
kwargs={"endpoint_id": endpoint.pk},
483-
countdown=60,
484-
)
476+
webhook_reactivation.apply_async(kwargs={"endpoint_id": endpoint.pk}, countdown=60)
485477
# There is no reason to keep endpoint active if it is returning 4xx errors
486478
else:
487479
endpoint.status = Notification_Webhooks.Status.STATUS_INACTIVE_PERMANENT
@@ -556,7 +548,6 @@ def _test_webhooks_notification(self, endpoint: Notification_Webhooks) -> None:
556548
# in "send_webhooks_notification", we are doing deeper analysis, why it failed
557549
# for now, "raise_for_status" should be enough
558550

559-
@app.task(ignore_result=True)
560551
def _webhook_reactivation(self, endpoint_id: int, **_kwargs: dict):
561552
endpoint = Notification_Webhooks.objects.get(pk=endpoint_id)
562553
# User already changed status of endpoint
@@ -830,9 +821,9 @@ def _process_notifications(
830821
):
831822
logger.debug("Sending Slack Notification")
832823
dojo_dispatch_task(
833-
self._get_manager_instance("slack").send_slack_notification,
824+
send_slack_notification,
834825
event,
835-
user=notifications.user,
826+
user_id=getattr(notifications.user, "id", None),
836827
**kwargs,
837828
)
838829

@@ -843,9 +834,9 @@ def _process_notifications(
843834
):
844835
logger.debug("Sending MSTeams Notification")
845836
dojo_dispatch_task(
846-
self._get_manager_instance("msteams").send_msteams_notification,
837+
send_msteams_notification,
847838
event,
848-
user=notifications.user,
839+
user_id=getattr(notifications.user, "id", None),
849840
**kwargs,
850841
)
851842

@@ -856,9 +847,9 @@ def _process_notifications(
856847
):
857848
logger.debug("Sending Mail Notification")
858849
dojo_dispatch_task(
859-
self._get_manager_instance("mail").send_mail_notification,
850+
send_mail_notification,
860851
event,
861-
user=notifications.user,
852+
user_id=getattr(notifications.user, "id", None),
862853
**kwargs,
863854
)
864855

@@ -869,13 +860,42 @@ def _process_notifications(
869860
):
870861
logger.debug("Sending Webhooks Notification")
871862
dojo_dispatch_task(
872-
self._get_manager_instance("webhooks").send_webhooks_notification,
863+
send_webhooks_notification,
873864
event,
874-
user=notifications.user,
865+
user_id=getattr(notifications.user, "id", None),
875866
**kwargs,
876867
)
877868

878869

870+
@app.task(base=DojoAsyncTask)
871+
def send_slack_notification(event: str, user_id: int | None = None, **kwargs: dict) -> None:
872+
user = Dojo_User.objects.get(pk=user_id) if user_id else None
873+
SlackNotificationManger().send_slack_notification(event, user=user, **kwargs)
874+
875+
876+
@app.task(base=DojoAsyncTask)
877+
def send_msteams_notification(event: str, user_id: int | None = None, **kwargs: dict) -> None:
878+
user = Dojo_User.objects.get(pk=user_id) if user_id else None
879+
MSTeamsNotificationManger().send_msteams_notification(event, user=user, **kwargs)
880+
881+
882+
@app.task(base=DojoAsyncTask)
883+
def send_mail_notification(event: str, user_id: int | None = None, **kwargs: dict) -> None:
884+
user = Dojo_User.objects.get(pk=user_id) if user_id else None
885+
EmailNotificationManger().send_mail_notification(event, user=user, **kwargs)
886+
887+
888+
@app.task(base=DojoAsyncTask)
889+
def send_webhooks_notification(event: str, user_id: int | None = None, **kwargs: dict) -> None:
890+
user = Dojo_User.objects.get(pk=user_id) if user_id else None
891+
WebhookNotificationManger().send_webhooks_notification(event, user=user, **kwargs)
892+
893+
894+
@app.task(ignore_result=True)
895+
def webhook_reactivation(endpoint_id: int, **_kwargs: dict) -> None:
896+
WebhookNotificationManger()._webhook_reactivation(endpoint_id=endpoint_id)
897+
898+
879899
@app.task(ignore_result=True)
880900
def webhook_status_cleanup(*_args: list, **_kwargs: dict):
881901
# If some endpoint was affected by some outage (5xx, 429, Timeout) but it was clean during last 24 hours,
@@ -903,4 +923,4 @@ def webhook_status_cleanup(*_args: list, **_kwargs: dict):
903923
)
904924
for endpoint in broken_endpoints:
905925
manager = WebhookNotificationManger()
906-
manager._webhook_reactivation(manager, endpoint_id=endpoint.pk)
926+
manager._webhook_reactivation(endpoint_id=endpoint.pk)

unittests/test_jira_import_and_pushing_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@ def test_engagement_epic_mapping_disabled_no_epic_and_push_findings(self):
971971
@patch("dojo.jira_link.helper.can_be_pushed_to_jira", return_value=(True, None, None))
972972
@patch("dojo.jira_link.helper.is_push_all_issues", return_value=False)
973973
@patch("dojo.jira_link.helper.push_to_jira", return_value=None)
974-
@patch("dojo.notifications.helper.WebhookNotificationManger.send_webhooks_notification")
974+
@patch("dojo.notifications.helper.send_webhooks_notification")
975975
def test_bulk_edit_mixed_findings_and_groups_jira_push_bug(self, mock_webhooks, mock_push_to_jira, mock_is_push_all_issues, mock_can_be_pushed):
976976
"""
977977
Test the bug in bulk edit: when bulk editing findings where some are in groups

0 commit comments

Comments
 (0)