From 9c5a8e9e035d29aeb6c4a446130309b1fc5420f6 Mon Sep 17 00:00:00 2001 From: David Whittaker Date: Mon, 27 Nov 2023 09:58:14 -0800 Subject: [PATCH 1/2] Swallow send exceptions if channel is archived or evergreen message has no owner --- src/dispatch/evergreen/scheduled.py | 6 +++ src/dispatch/plugins/dispatch_slack/enums.py | 1 + src/dispatch/plugins/dispatch_slack/plugin.py | 49 ++++++++++++------- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/dispatch/evergreen/scheduled.py b/src/dispatch/evergreen/scheduled.py index ed9fdcb8fbb7..64c308958cfc 100644 --- a/src/dispatch/evergreen/scheduled.py +++ b/src/dispatch/evergreen/scheduled.py @@ -30,6 +30,12 @@ def create_evergreen_reminder( db_session: SessionLocal, project: Project, owner_email: str, resource_groups: Any ): """Contains the logic for evergreen reminders.""" + if not owner_email: + log.warning( + "Evergreen reminder not sent. No owner email. Project: {project.name}. Organization: {project.organization.name}" + ) + return + plugin = plugin_service.get_active_instance( db_session=db_session, plugin_type="email", project_id=project.id ) diff --git a/src/dispatch/plugins/dispatch_slack/enums.py b/src/dispatch/plugins/dispatch_slack/enums.py index 58125b82562a..33248b46c0b7 100644 --- a/src/dispatch/plugins/dispatch_slack/enums.py +++ b/src/dispatch/plugins/dispatch_slack/enums.py @@ -35,3 +35,4 @@ class SlackAPIErrorCode(DispatchEnum): USERS_NOT_FOUND = "users_not_found" VIEW_NOT_FOUND = "not_found" # Could not find corresponding view for the provided view_id VIEW_EXPIRED = "expired_trigger_id" # The provided trigger_id is no longer valid + IS_ARCHIVED = "is_archived" # Channel is archived diff --git a/src/dispatch/plugins/dispatch_slack/plugin.py b/src/dispatch/plugins/dispatch_slack/plugin.py index 867604b5f101..e01a07af4584 100644 --- a/src/dispatch/plugins/dispatch_slack/plugin.py +++ b/src/dispatch/plugins/dispatch_slack/plugin.py @@ -49,6 +49,8 @@ unarchive_conversation, update_message, ) +from slack_sdk.errors import SlackApiError +from .enums import SlackAPIErrorCode logger = logging.getLogger(__name__) @@ -162,26 +164,37 @@ def send( **kwargs, ): """Sends a new message based on data and type.""" - client = create_slack_client(self.configuration) - messages = [] - if not blocks: - blocks = create_message_blocks(message_template, notification_type, items, **kwargs) - - for c in chunks(blocks, 50): - messages.append( - send_message( - client, - conversation_id, - text, - ts, - Message(blocks=c).build()["blocks"], - persist, + try: + client = create_slack_client(self.configuration) + messages = [] + if not blocks: + blocks = create_message_blocks(message_template, notification_type, items, **kwargs) + + for c in chunks(blocks, 50): + messages.append( + send_message( + client, + conversation_id, + text, + ts, + Message(blocks=c).build()["blocks"], + persist, + ) ) + else: + for c in chunks(blocks, 50): + messages.append(send_message(client, conversation_id, text, ts, c, persist)) + return messages + except SlackApiError as exception: + error = exception.response["error"] + if error == SlackAPIErrorCode.IS_ARCHIVED: + # swallow send errors if the channel is archived + message = ( + f"SlackAPIError trying to send: {exception.response}. Message: {text}. Type: {notification_type}" ) - else: - for c in chunks(blocks, 50): - messages.append(send_message(client, conversation_id, text, ts, c, persist)) - return messages + logger.error(message) + else: + raise exception def send_direct( self, From f69790aaae24a42ea8ec8a1856dd1d307496bfbc Mon Sep 17 00:00:00 2001 From: David Whittaker Date: Mon, 27 Nov 2023 10:13:22 -0800 Subject: [PATCH 2/2] Reformat --- src/dispatch/plugins/dispatch_slack/plugin.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/dispatch/plugins/dispatch_slack/plugin.py b/src/dispatch/plugins/dispatch_slack/plugin.py index e01a07af4584..55320cc5e3e1 100644 --- a/src/dispatch/plugins/dispatch_slack/plugin.py +++ b/src/dispatch/plugins/dispatch_slack/plugin.py @@ -189,9 +189,7 @@ def send( error = exception.response["error"] if error == SlackAPIErrorCode.IS_ARCHIVED: # swallow send errors if the channel is archived - message = ( - f"SlackAPIError trying to send: {exception.response}. Message: {text}. Type: {notification_type}" - ) + message = f"SlackAPIError trying to send: {exception.response}. Message: {text}. Type: {notification_type}" logger.error(message) else: raise exception