Skip to content

fix(drift): fix SplitCodeBlocks line duplication and handle plain text messages#2634

Open
raman1236 wants to merge 1 commit intodiggerhq:developfrom
raman1236:fix/slack-drift-split-code-blocks
Open

fix(drift): fix SplitCodeBlocks line duplication and handle plain text messages#2634
raman1236 wants to merge 1 commit intodiggerhq:developfrom
raman1236:fix/slack-drift-split-code-blocks

Conversation

@raman1236
Copy link
Copy Markdown

Summary

Fixes #745 - Drift alerts in Slack > 4000 characters break formatting

Bugs Fixed

1. Line duplication in SplitCodeBlocks

When a line caused the accumulated message to exceed Slack's 4000-character limit, the splitting logic appended the triggering line to both the current chunk and the start of the next chunk:

// Before (buggy):
res = append(res, part+"\n"+line+"\n```")  // line added to OLD part
part = "```\n" + line                       // line ALSO added to NEW part

This caused every line at a split boundary to appear twice in the Slack notification.

Fix: Close the current chunk cleanly without the triggering line, then start the new chunk with it:

// After:
res = append(res, part+"\n```")
part = "```\n" + line

2. Messages without code blocks skipped splitting

The function returned messages with fewer than 2 ``` markers unsplit, even when they exceeded 4000 characters. This caused the Slack API to reject them.

Fix: Remove the early return and add a plain-text splitting path.

3. SendErrorNotificationForProject didn't split messages

Unlike SendNotificationForProject, the error notification method sent messages directly via SendSlackMessage without going through SplitCodeBlocks. Long error stack traces would be rejected by Slack.

Fix: Use the same split-and-loop pattern.

Testing

Added 2 new unit tests:

  • TestSplitCodeBlocksNoLineDuplication — verifies no line appears in multiple parts after splitting
  • TestSplitCodeBlocksPlainTextOver4000 — verifies plain text messages > 4000 chars are split

All existing tests continue to pass:

=== RUN   TestSlackSplitLargerMessage      --- PASS
=== RUN   TestSlackSmallerMessageNotSplit   --- PASS
=== RUN   TestSplitCodeBlocksNoLineDuplication --- PASS
=== RUN   TestSplitCodeBlocksPlainTextOver4000 --- PASS

…t messages

SplitCodeBlocks had two bugs:

1. When a line caused the message to exceed Slack's 4000-char limit,
   the line was appended to both the closing chunk and the opening of
   the next chunk, causing content duplication in the notification.

2. Messages without code block markers (```) that exceeded 4000 chars
   were returned unsplit, causing the Slack API to reject them.

Also fix SendErrorNotificationForProject to split long error messages
through SplitCodeBlocks instead of sending them directly, matching
the existing behavior of SendNotificationForProject.

Fixes diggerhq#745
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Drift alerts in Slack > 4000 characters break formatting

2 participants