From f69d71e332cc43befdde1b5ce5c1ce50886f9da4 Mon Sep 17 00:00:00 2001 From: Mopcho <47301161+Mopcho@users.noreply.github.com> Date: Fri, 4 Apr 2025 21:09:40 +0300 Subject: [PATCH 1/2] Fix discord webhook 400 status code when description limit is exceeded (#34084) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: wxiaoguang --- services/webhook/discord.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/services/webhook/discord.go b/services/webhook/discord.go index 43e5e533bff65..38484651bd9ed 100644 --- a/services/webhook/discord.go +++ b/services/webhook/discord.go @@ -101,6 +101,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 +314,7 @@ func (d discordConvertor) createPayload(s *api.User, title, text, url string, co Embeds: []DiscordEmbed{ { Title: title, - Description: text, + Description: util.TruncateRunes(text, discordDescriptionCharactersLimit), URL: url, Color: color, Author: DiscordEmbedAuthor{ From 6468ec2569b345e21068e4e85de47651f984614e Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 4 Apr 2025 18:42:33 -0700 Subject: [PATCH 2/2] Fix bug --- services/webhook/discord.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/services/webhook/discord.go b/services/webhook/discord.go index 38484651bd9ed..d8d8e984374dd 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" @@ -314,7 +315,7 @@ func (d discordConvertor) createPayload(s *api.User, title, text, url string, co Embeds: []DiscordEmbed{ { Title: title, - Description: util.TruncateRunes(text, discordDescriptionCharactersLimit), + Description: base.TruncateString(text, discordDescriptionCharactersLimit), URL: url, Color: color, Author: DiscordEmbedAuthor{