Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Code monitors: truncate long Slack blocks#50083

Merged
jtibshirani merged 3 commits into
mainfrom
jtibs/truncate-message
Apr 3, 2023
Merged

Code monitors: truncate long Slack blocks#50083
jtibshirani merged 3 commits into
mainfrom
jtibs/truncate-message

Conversation

@jtibshirani

Copy link
Copy Markdown
Contributor

Slack requires that text blocks be no longer than 3000 characters. We already limit the number of lines in the match blocks, but if the lines are long then the block can still exceed the character limit. With this change, we also truncate lines if they will push the block over the character limit.

Fixes #49979.

Test plan

New unit test for character-based truncation.

Also tested manually -- reproduced the error, then verified this change resolves it.

@cla-bot cla-bot Bot added the cla-signed label Mar 29, 2023
@jtibshirani

Copy link
Copy Markdown
Contributor Author

Here's an example of truncating lines based on the character limit:
Screenshot 2023-03-28 at 4 50 57 PM

@jtibshirani jtibshirani force-pushed the jtibs/truncate-message branch from a6d67dc to 70d4d9d Compare March 29, 2023 01:07
@jtibshirani jtibshirani marked this pull request as ready for review March 29, 2023 01:07
@jtibshirani jtibshirani requested a review from a team March 29, 2023 15:37
Comment on lines +87 to +88
// We limit the bytes to ensure we don't hit Slack's max block size of 3000
// characters. To be conservative, we truncate to 2500 bytes. We also limit the

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q: Is it possible that multi-byte character (eg. emojis or CJK character) will be split up by using this truncation method? If yes, is there a way we can prevent this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't actually truncate the text by byte. We always truncate entire lines -- if a line will push us over the 2500 limit, then we truncate it and all subsequent lines. This seemed simplest and also produces a clean output. I now see this comment is not clear, I can update it :)

For context, we could use a rune/ character limit instead, but bytes seems safest so we don't count on the Slack API's meaning of "character".

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, makes sense! So if the first line was already 2500+ characters, no preview will be sent?

@jtibshirani jtibshirani Mar 29, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will send a result preview that looks like this:

Screenshot 2023-03-29 at 12 12 59 PM

This is very unlikely to happen in practice though:

  • Diff matches begin with the file name, which are not likely to exceed 2500 bytes
  • Commit message matches begin with the (short) title of the commit

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks for looking into this, it all sounds very reasonable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, I had all the same questions when I was developing the fix!

@jtibshirani jtibshirani merged commit 4edebd8 into main Apr 3, 2023
@jtibshirani jtibshirani deleted the jtibs/truncate-message branch April 3, 2023 15:43
jtibshirani added a commit that referenced this pull request Apr 3, 2023
jtibshirani added a commit that referenced this pull request Apr 3, 2023
jtibshirani added a commit that referenced this pull request Apr 3, 2023
Slack requires that text blocks be no longer than 3000 characters. We
already limit the number of lines in the match blocks, but if the lines
are long then the block can still exceed the character limit. With this
change, we also truncate lines if they will push the block over the
character limit.

Fixes #49979.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Code monitors: Slack webhooks fail with 400 Bad Request: invalid blocks.

3 participants