Skip to content

Commit aadbe04

Browse files
authored
Truncate commit message during Discord webhook push events (#31970)
Resolves #31668.
1 parent 2c6fa6c commit aadbe04

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

services/webhook/discord.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net/url"
1212
"strconv"
1313
"strings"
14+
"unicode/utf8"
1415

1516
webhook_model "code.gitea.io/gitea/models/webhook"
1617
"code.gitea.io/gitea/modules/git"
@@ -154,8 +155,14 @@ func (d discordConvertor) Push(p *api.PushPayload) (DiscordPayload, error) {
154155
var text string
155156
// for each commit, generate attachment text
156157
for i, commit := range p.Commits {
157-
text += fmt.Sprintf("[%s](%s) %s - %s", commit.ID[:7], commit.URL,
158-
strings.TrimRight(commit.Message, "\r\n"), commit.Author.Name)
158+
// limit the commit message display to just the summary, otherwise it would be hard to read
159+
message := strings.TrimRight(strings.SplitN(commit.Message, "\n", 1)[0], "\r")
160+
161+
// a limit of 50 is set because GitHub does the same
162+
if utf8.RuneCountInString(message) > 50 {
163+
message = fmt.Sprintf("%.47s...", message)
164+
}
165+
text += fmt.Sprintf("[%s](%s) %s - %s", commit.ID[:7], commit.URL, message, commit.Author.Name)
159166
// add linebreak to each commit but the last
160167
if i < len(p.Commits)-1 {
161168
text += "\n"

services/webhook/discord_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ func TestDiscordPayload(t *testing.T) {
8080
assert.Equal(t, p.Sender.AvatarURL, pl.Embeds[0].Author.IconURL)
8181
})
8282

83+
t.Run("PushWithLongCommitMessage", func(t *testing.T) {
84+
p := pushTestMultilineCommitMessagePayload()
85+
86+
pl, err := dc.Push(p)
87+
require.NoError(t, err)
88+
89+
assert.Len(t, pl.Embeds, 1)
90+
assert.Equal(t, "[test/repo:test] 2 new commits", pl.Embeds[0].Title)
91+
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)
92+
assert.Equal(t, p.Sender.UserName, pl.Embeds[0].Author.Name)
93+
assert.Equal(t, setting.AppURL+p.Sender.UserName, pl.Embeds[0].Author.URL)
94+
assert.Equal(t, p.Sender.AvatarURL, pl.Embeds[0].Author.IconURL)
95+
})
96+
8397
t.Run("Issue", func(t *testing.T) {
8498
p := issueTestPayload()
8599

services/webhook/general_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,17 @@ func forkTestPayload() *api.ForkPayload {
6464
}
6565

6666
func pushTestPayload() *api.PushPayload {
67+
return pushTestPayloadWithCommitMessage("commit message")
68+
}
69+
70+
func pushTestMultilineCommitMessagePayload() *api.PushPayload {
71+
return pushTestPayloadWithCommitMessage("This is a commit summary ⚠️⚠️⚠️⚠️ containing 你好 ⚠️⚠️️\n\nThis is the message body.")
72+
}
73+
74+
func pushTestPayloadWithCommitMessage(message string) *api.PushPayload {
6775
commit := &api.PayloadCommit{
6876
ID: "2020558fe2e34debb818a514715839cabd25e778",
69-
Message: "commit message",
77+
Message: message,
7078
URL: "http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778",
7179
Author: &api.PayloadUser{
7280
Name: "user1",

0 commit comments

Comments
 (0)