Fix discord webhook 400 status code when description limit is exceeded (#34084)

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 <wxiaoguang@gmail.com>
This commit is contained in:
Mopcho 2025-04-04 21:09:40 +03:00 committed by GitHub
parent 6cee3bfa96
commit 013b2686fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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
@ -313,7 +320,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{