Skip to content

Commit 43c63c3

Browse files
authored
Use old behavior for telegram webhook (#31588)
Fix #31182
1 parent 2dc6993 commit 43c63c3

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

services/webhook/telegram.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"code.gitea.io/gitea/modules/git"
1414
"code.gitea.io/gitea/modules/json"
1515
"code.gitea.io/gitea/modules/log"
16+
"code.gitea.io/gitea/modules/markup"
1617
api "code.gitea.io/gitea/modules/structs"
1718
webhook_module "code.gitea.io/gitea/modules/webhook"
1819
)
@@ -181,7 +182,7 @@ func (t telegramConvertor) Package(p *api.PackagePayload) (TelegramPayload, erro
181182

182183
func createTelegramPayload(message string) TelegramPayload {
183184
return TelegramPayload{
184-
Message: strings.TrimSpace(message),
185+
Message: markup.Sanitize(strings.TrimSpace(message)),
185186
ParseMode: "HTML",
186187
DisableWebPreview: true,
187188
}

services/webhook/telegram_test.go

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestTelegramPayload(t *testing.T) {
3333
pl, err := tc.Create(p)
3434
require.NoError(t, err)
3535

36-
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] branch <a href="http://localhost:3000/test/repo/src/test">test</a> created`, pl.Message)
36+
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] branch <a href="http://localhost:3000/test/repo/src/test" rel="nofollow">test</a> created`, pl.Message)
3737
})
3838

3939
t.Run("Delete", func(t *testing.T) {
@@ -42,7 +42,7 @@ func TestTelegramPayload(t *testing.T) {
4242
pl, err := tc.Delete(p)
4343
require.NoError(t, err)
4444

45-
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] branch <a href="http://localhost:3000/test/repo/src/test">test</a> deleted`, pl.Message)
45+
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] branch <a href="http://localhost:3000/test/repo/src/test" rel="nofollow">test</a> deleted`, pl.Message)
4646
})
4747

4848
t.Run("Fork", func(t *testing.T) {
@@ -51,7 +51,7 @@ func TestTelegramPayload(t *testing.T) {
5151
pl, err := tc.Fork(p)
5252
require.NoError(t, err)
5353

54-
assert.Equal(t, `test/repo2 is forked to <a href="http://localhost:3000/test/repo">test/repo</a>`, pl.Message)
54+
assert.Equal(t, `test/repo2 is forked to <a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>`, pl.Message)
5555
})
5656

5757
t.Run("Push", func(t *testing.T) {
@@ -60,7 +60,9 @@ func TestTelegramPayload(t *testing.T) {
6060
pl, err := tc.Push(p)
6161
require.NoError(t, err)
6262

63-
assert.Equal(t, "[<a href=\"http://localhost:3000/test/repo\">test/repo</a>:<a href=\"http://localhost:3000/test/repo/src/test\">test</a>] 2 new commits\n[<a href=\"http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778\">2020558</a>] commit message - user1\n[<a href=\"http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778\">2020558</a>] commit message - user1", pl.Message)
63+
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>:<a href="http://localhost:3000/test/repo/src/test" rel="nofollow">test</a>] 2 new commits
64+
[<a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778" rel="nofollow">2020558</a>] commit message - user1
65+
[<a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778" rel="nofollow">2020558</a>] commit message - user1`, pl.Message)
6466
})
6567

6668
t.Run("Issue", func(t *testing.T) {
@@ -70,13 +72,15 @@ func TestTelegramPayload(t *testing.T) {
7072
pl, err := tc.Issue(p)
7173
require.NoError(t, err)
7274

73-
assert.Equal(t, "[<a href=\"http://localhost:3000/test/repo\">test/repo</a>] Issue opened: <a href=\"http://localhost:3000/test/repo/issues/2\">#2 crash</a> by <a href=\"https://try.gitea.io/user1\">user1</a>\n\nissue body", pl.Message)
75+
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Issue opened: <a href="http://localhost:3000/test/repo/issues/2" rel="nofollow">#2 crash</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>
76+
77+
issue body`, pl.Message)
7478

7579
p.Action = api.HookIssueClosed
7680
pl, err = tc.Issue(p)
7781
require.NoError(t, err)
7882

79-
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Issue closed: <a href="http://localhost:3000/test/repo/issues/2">#2 crash</a> by <a href="https://try.gitea.io/user1">user1</a>`, pl.Message)
83+
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Issue closed: <a href="http://localhost:3000/test/repo/issues/2" rel="nofollow">#2 crash</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>`, pl.Message)
8084
})
8185

8286
t.Run("IssueComment", func(t *testing.T) {
@@ -85,7 +89,8 @@ func TestTelegramPayload(t *testing.T) {
8589
pl, err := tc.IssueComment(p)
8690
require.NoError(t, err)
8791

88-
assert.Equal(t, "[<a href=\"http://localhost:3000/test/repo\">test/repo</a>] New comment on issue <a href=\"http://localhost:3000/test/repo/issues/2\">#2 crash</a> by <a href=\"https://try.gitea.io/user1\">user1</a>\nmore info needed", pl.Message)
92+
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] New comment on issue <a href="http://localhost:3000/test/repo/issues/2" rel="nofollow">#2 crash</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>
93+
more info needed`, pl.Message)
8994
})
9095

9196
t.Run("PullRequest", func(t *testing.T) {
@@ -94,7 +99,8 @@ func TestTelegramPayload(t *testing.T) {
9499
pl, err := tc.PullRequest(p)
95100
require.NoError(t, err)
96101

97-
assert.Equal(t, "[<a href=\"http://localhost:3000/test/repo\">test/repo</a>] Pull request opened: <a href=\"http://localhost:3000/test/repo/pulls/12\">#12 Fix bug</a> by <a href=\"https://try.gitea.io/user1\">user1</a>\nfixes bug #2", pl.Message)
102+
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Pull request opened: <a href="http://localhost:3000/test/repo/pulls/12" rel="nofollow">#12 Fix bug</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>
103+
fixes bug #2`, pl.Message)
98104
})
99105

100106
t.Run("PullRequestComment", func(t *testing.T) {
@@ -103,7 +109,8 @@ func TestTelegramPayload(t *testing.T) {
103109
pl, err := tc.IssueComment(p)
104110
require.NoError(t, err)
105111

106-
assert.Equal(t, "[<a href=\"http://localhost:3000/test/repo\">test/repo</a>] New comment on pull request <a href=\"http://localhost:3000/test/repo/pulls/12\">#12 Fix bug</a> by <a href=\"https://try.gitea.io/user1\">user1</a>\nchanges requested", pl.Message)
112+
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] New comment on pull request <a href="http://localhost:3000/test/repo/pulls/12" rel="nofollow">#12 Fix bug</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>
113+
changes requested`, pl.Message)
107114
})
108115

109116
t.Run("Review", func(t *testing.T) {
@@ -113,7 +120,8 @@ func TestTelegramPayload(t *testing.T) {
113120
pl, err := tc.Review(p, webhook_module.HookEventPullRequestReviewApproved)
114121
require.NoError(t, err)
115122

116-
assert.Equal(t, "[test/repo] Pull request review approved: #12 Fix bug\ngood job", pl.Message)
123+
assert.Equal(t, `[test/repo] Pull request review approved: #12 Fix bug
124+
good job`, pl.Message)
117125
})
118126

119127
t.Run("Repository", func(t *testing.T) {
@@ -122,7 +130,7 @@ func TestTelegramPayload(t *testing.T) {
122130
pl, err := tc.Repository(p)
123131
require.NoError(t, err)
124132

125-
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Repository created`, pl.Message)
133+
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Repository created`, pl.Message)
126134
})
127135

128136
t.Run("Package", func(t *testing.T) {
@@ -131,7 +139,7 @@ func TestTelegramPayload(t *testing.T) {
131139
pl, err := tc.Package(p)
132140
require.NoError(t, err)
133141

134-
assert.Equal(t, `Package created: <a href="http://localhost:3000/user1/-/packages/container/GiteaContainer/latest">GiteaContainer:latest</a> by <a href="https://try.gitea.io/user1">user1</a>`, pl.Message)
142+
assert.Equal(t, `Package created: <a href="http://localhost:3000/user1/-/packages/container/GiteaContainer/latest" rel="nofollow">GiteaContainer:latest</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>`, pl.Message)
135143
})
136144

137145
t.Run("Wiki", func(t *testing.T) {
@@ -141,19 +149,19 @@ func TestTelegramPayload(t *testing.T) {
141149
pl, err := tc.Wiki(p)
142150
require.NoError(t, err)
143151

144-
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] New wiki page '<a href="http://localhost:3000/test/repo/wiki/index">index</a>' (Wiki change comment) by <a href="https://try.gitea.io/user1">user1</a>`, pl.Message)
152+
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] New wiki page &#39;<a href="http://localhost:3000/test/repo/wiki/index" rel="nofollow">index</a>&#39; (Wiki change comment) by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>`, pl.Message)
145153

146154
p.Action = api.HookWikiEdited
147155
pl, err = tc.Wiki(p)
148156
require.NoError(t, err)
149157

150-
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Wiki page '<a href="http://localhost:3000/test/repo/wiki/index">index</a>' edited (Wiki change comment) by <a href="https://try.gitea.io/user1">user1</a>`, pl.Message)
158+
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Wiki page &#39;<a href="http://localhost:3000/test/repo/wiki/index" rel="nofollow">index</a>&#39; edited (Wiki change comment) by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>`, pl.Message)
151159

152160
p.Action = api.HookWikiDeleted
153161
pl, err = tc.Wiki(p)
154162
require.NoError(t, err)
155163

156-
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Wiki page '<a href="http://localhost:3000/test/repo/wiki/index">index</a>' deleted by <a href="https://try.gitea.io/user1">user1</a>`, pl.Message)
164+
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Wiki page &#39;<a href="http://localhost:3000/test/repo/wiki/index" rel="nofollow">index</a>&#39; deleted by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>`, pl.Message)
157165
})
158166

159167
t.Run("Release", func(t *testing.T) {
@@ -162,7 +170,7 @@ func TestTelegramPayload(t *testing.T) {
162170
pl, err := tc.Release(p)
163171
require.NoError(t, err)
164172

165-
assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Release created: <a href="http://localhost:3000/test/repo/releases/tag/v1.0">v1.0</a> by <a href="https://try.gitea.io/user1">user1</a>`, pl.Message)
173+
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>] Release created: <a href="http://localhost:3000/test/repo/releases/tag/v1.0" rel="nofollow">v1.0</a> by <a href="https://try.gitea.io/user1" rel="nofollow">user1</a>`, pl.Message)
166174
})
167175
}
168176

@@ -198,5 +206,7 @@ func TestTelegramJSONPayload(t *testing.T) {
198206
var body TelegramPayload
199207
err = json.NewDecoder(req.Body).Decode(&body)
200208
assert.NoError(t, err)
201-
assert.Equal(t, "[<a href=\"http://localhost:3000/test/repo\">test/repo</a>:<a href=\"http://localhost:3000/test/repo/src/test\">test</a>] 2 new commits\n[<a href=\"http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778\">2020558</a>] commit message - user1\n[<a href=\"http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778\">2020558</a>] commit message - user1", body.Message)
209+
assert.Equal(t, `[<a href="http://localhost:3000/test/repo" rel="nofollow">test/repo</a>:<a href="http://localhost:3000/test/repo/src/test" rel="nofollow">test</a>] 2 new commits
210+
[<a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778" rel="nofollow">2020558</a>] commit message - user1
211+
[<a href="http://localhost:3000/test/repo/commit/2020558fe2e34debb818a514715839cabd25e778" rel="nofollow">2020558</a>] commit message - user1`, body.Message)
202212
}

0 commit comments

Comments
 (0)