Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code monitors: truncate long Slack blocks #50083

Merged
merged 3 commits into from Apr 3, 2023
Merged

Conversation

jtibshirani
Copy link
Member

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
Member Author

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

@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
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
Member 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
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?

Copy link
Member 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
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
Member 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
18 checks passed
@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 join this conversation on GitHub. Already have an account? Sign in to comment
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