From 510403a071e9f2baf9510f35240614bc9d405361 Mon Sep 17 00:00:00 2001 From: Amit Phulera Date: Fri, 10 Jan 2025 15:31:23 +0530 Subject: [PATCH] make slack deploy diff work for longer diffs as well We sometimes do not receive diffs during deploy so they have to be manually copied. On looking I found out it is because of the 3000 charachter limit on the message. The fix that is made here is chunking the longer messages and sending as seperate messages on slack --- src/commcare_cloud/commands/deploy/slack.py | 30 +++++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/commcare_cloud/commands/deploy/slack.py b/src/commcare_cloud/commands/deploy/slack.py index 50926252c8..453666a9e3 100644 --- a/src/commcare_cloud/commands/deploy/slack.py +++ b/src/commcare_cloud/commands/deploy/slack.py @@ -67,11 +67,31 @@ def send_deploy_start_message(self, context): response = self._post_message(message, blocks) context.set_meta_value('slack_thread_ts', response["ts"]) if self.environment.fab_settings_config.generate_deploy_diffs: - diff_blocks = [{ - "type": "section", - "text": {"type": "mrkdwn", "text": context.diff.get_slack_diff()} - }] - self._post_message("Deploy diff", diff_blocks, thread_ts=response["ts"]) + diff_text = context.diff.get_slack_diff() + for chunked_diff_text in self._chunk_diff(diff_text): + diff_blocks = [{ + "type": "section", + "text": {"type": "mrkdwn", "text": chunked_diff_text} + }] + self._post_message("Deploy diff", diff_blocks, thread_ts=response["ts"]) + + def _chunk_diff(self, diff_text, chunk_size=2800): + # Slack has a limit of 3000 characters per message block + # https://api.slack.com/reference/block-kit/blocks#section_fields + diff_lines = diff_text.split('\n') + current_chunk = [] + current_length = 0 + for line in diff_lines: + line_length = len(line) + 1 + if current_length + line_length > chunk_size: + yield '\n'.join(current_chunk) + current_chunk = [line] + current_length = line_length + else: + current_chunk.append(line) + current_length += line_length + if current_chunk: + yield '\n'.join(current_chunk) def send_deploy_end_message(self, context, is_success): thread_ts = context.get_meta_value('slack_thread_ts')