From 66ccae8b906f1ed107023120508f2bba46e0c133 Mon Sep 17 00:00:00 2001 From: Giteabot Date: Sat, 5 Apr 2025 10:08:04 +0800 Subject: [PATCH] Fix discord webhook 400 status code when description limit is exceeded (#34084) (#34124) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Backport #34084 by @Mopcho Fixes [#34027](https://github.com/go-gitea/gitea/issues/34027) Discord does not allow for description bigger than 2048 bytes. If the description is bigger than that it will throw 400 and the event won't appear in discord. To fix that, in the createPayload method we now slice the description to ensure it doesn’t exceed the limit. --------- Co-authored-by: Mopcho <47301161+Mopcho@users.noreply.github.com> Co-authored-by: wxiaoguang Co-authored-by: Lunny Xiao --- services/webhook/discord.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/services/webhook/discord.go b/services/webhook/discord.go index 43e5e533bf..d8d8e98437 100644 --- a/services/webhook/discord.go +++ b/services/webhook/discord.go @@ -14,6 +14,7 @@ import ( "unicode/utf8" webhook_model "code.gitea.io/gitea/models/webhook" + "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" @@ -101,6 +102,13 @@ var ( redColor = color("ff3232") ) +// https://discord.com/developers/docs/resources/message#embed-object-embed-limits +// Discord has some limits in place for the embeds. +// According to some tests, there is no consistent limit for different character sets. +// For example: 4096 ASCII letters are allowed, but only 2490 emoji characters are allowed. +// To keep it simple, we currently truncate at 2000. +const discordDescriptionCharactersLimit = 2000 + type discordConvertor struct { Username string AvatarURL string @@ -307,7 +315,7 @@ func (d discordConvertor) createPayload(s *api.User, title, text, url string, co Embeds: []DiscordEmbed{ { Title: title, - Description: text, + Description: base.TruncateString(text, discordDescriptionCharactersLimit), URL: url, Color: color, Author: DiscordEmbedAuthor{