diff --git a/services/webhook/discord.go b/services/webhook/discord.go index 8c50dcf968..ab7faaf870 100644 --- a/services/webhook/discord.go +++ b/services/webhook/discord.go @@ -11,6 +11,7 @@ import ( "net/url" "strconv" "strings" + "unicode/utf8" webhook_model "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/git" @@ -158,7 +159,7 @@ func (d discordConvertor) Push(p *api.PushPayload) (DiscordPayload, error) { message := strings.TrimRight(strings.Split(commit.Message, "\n")[0], "\r") // a limit of 50 is set because GitHub does the same - if len(message) > 50 { + if utf8.RuneCountInString(message) > 50 { message = fmt.Sprintf("%.47s...", message) } text += fmt.Sprintf("[%s](%s) %s - %s", commit.ID[:7], commit.URL, message, commit.Author.Name) diff --git a/services/webhook/discord_test.go b/services/webhook/discord_test.go index 201f82580d..fbb4b24ef1 100644 --- a/services/webhook/discord_test.go +++ b/services/webhook/discord_test.go @@ -88,8 +88,7 @@ func TestDiscordPayload(t *testing.T) { assert.Len(t, pl.Embeds, 1) assert.Equal(t, "[test/repo:test] 2 new commits", pl.Embeds[0].Title) - assert.Equal(t, "[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778) This is a commit summary that is longer than 50... - user1\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778) This is a commit summary that is longer than 50... - user1", pl.Embeds[0].Description) - assert.Equal(t, "http://localhost:3000/test/repo/src/test", pl.Embeds[0].URL) + assert.Equal(t, "[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778) This is a commit summary ⚠️⚠️⚠️⚠️ containing 你好... - user1\n[2020558](http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778) This is a commit summary ⚠️⚠️⚠️⚠️ containing 你好... - user1", pl.Embeds[0].Description) assert.Equal(t, p.Sender.UserName, pl.Embeds[0].Author.Name) assert.Equal(t, setting.AppURL+p.Sender.UserName, pl.Embeds[0].Author.URL) assert.Equal(t, p.Sender.AvatarURL, pl.Embeds[0].Author.IconURL) diff --git a/services/webhook/general_test.go b/services/webhook/general_test.go index 5fa81050aa..d6a77c9442 100644 --- a/services/webhook/general_test.go +++ b/services/webhook/general_test.go @@ -68,7 +68,7 @@ func pushTestPayload() *api.PushPayload { } func pushTestMultilineCommitMessagePayload() *api.PushPayload { - return pushTestPayloadWithCommitMessage("This is a commit summary that is longer than 50 characters\n\nThis is the message body.") + return pushTestPayloadWithCommitMessage("This is a commit summary ⚠️⚠️⚠️⚠️ containing 你好 ⚠️⚠️️\n\nThis is the message body.") } func pushTestPayloadWithCommitMessage(message string) *api.PushPayload {