From c12857d5dda3e59180a71f6a3d7dbff75129e0c9 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 9 Jul 2024 11:23:33 +0800 Subject: [PATCH] Use old behavior for telegram webhook (#31588) Fix #31182 (cherry picked from commit 43c63c33aea1b0725ed1fe7b4a55dd85050f0beb) (cherry picked from commit 38dce65dc1e5604f93f86eb5858c08e403067ca3) --- services/webhook/telegram.go | 3 ++- services/webhook/telegram_test.go | 44 +++++++++++++++++++------------ 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/services/webhook/telegram.go b/services/webhook/telegram.go index 724c41012f..74eb133922 100644 --- a/services/webhook/telegram.go +++ b/services/webhook/telegram.go @@ -15,6 +15,7 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/markup" api "code.gitea.io/gitea/modules/structs" webhook_module "code.gitea.io/gitea/modules/webhook" "code.gitea.io/gitea/services/forms" @@ -213,7 +214,7 @@ func (t telegramConvertor) Package(p *api.PackagePayload) (TelegramPayload, erro func createTelegramPayload(message string) TelegramPayload { return TelegramPayload{ - Message: strings.TrimSpace(message), + Message: markup.Sanitize(strings.TrimSpace(message)), ParseMode: "HTML", DisableWebPreview: true, } diff --git a/services/webhook/telegram_test.go b/services/webhook/telegram_test.go index ff6455e16b..65b767f0af 100644 --- a/services/webhook/telegram_test.go +++ b/services/webhook/telegram_test.go @@ -33,7 +33,7 @@ func TestTelegramPayload(t *testing.T) { pl, err := tc.Create(p) require.NoError(t, err) - assert.Equal(t, `[test/repo] branch test created`, pl.Message) + assert.Equal(t, `[test/repo] branch test created`, pl.Message) }) t.Run("Delete", func(t *testing.T) { @@ -42,7 +42,7 @@ func TestTelegramPayload(t *testing.T) { pl, err := tc.Delete(p) require.NoError(t, err) - assert.Equal(t, `[test/repo] branch test deleted`, pl.Message) + assert.Equal(t, `[test/repo] branch test deleted`, pl.Message) }) t.Run("Fork", func(t *testing.T) { @@ -51,7 +51,7 @@ func TestTelegramPayload(t *testing.T) { pl, err := tc.Fork(p) require.NoError(t, err) - assert.Equal(t, `test/repo2 is forked to test/repo`, pl.Message) + assert.Equal(t, `test/repo2 is forked to test/repo`, pl.Message) }) t.Run("Push", func(t *testing.T) { @@ -60,7 +60,9 @@ func TestTelegramPayload(t *testing.T) { pl, err := tc.Push(p) require.NoError(t, err) - assert.Equal(t, "[test/repo:test] 2 new commits\n[2020558] commit message - user1\n[2020558] commit message - user1", pl.Message) + assert.Equal(t, `[test/repo:test] 2 new commits +[2020558] commit message - user1 +[2020558] commit message - user1`, pl.Message) }) t.Run("Issue", func(t *testing.T) { @@ -70,13 +72,15 @@ func TestTelegramPayload(t *testing.T) { pl, err := tc.Issue(p) require.NoError(t, err) - assert.Equal(t, "[test/repo] Issue opened: #2 crash by user1\n\nissue body", pl.Message) + assert.Equal(t, `[test/repo] Issue opened: #2 crash by user1 + +issue body`, pl.Message) p.Action = api.HookIssueClosed pl, err = tc.Issue(p) require.NoError(t, err) - assert.Equal(t, `[test/repo] Issue closed: #2 crash by user1`, pl.Message) + assert.Equal(t, `[test/repo] Issue closed: #2 crash by user1`, pl.Message) }) t.Run("IssueComment", func(t *testing.T) { @@ -85,7 +89,8 @@ func TestTelegramPayload(t *testing.T) { pl, err := tc.IssueComment(p) require.NoError(t, err) - assert.Equal(t, "[test/repo] New comment on issue #2 crash by user1\nmore info needed", pl.Message) + assert.Equal(t, `[test/repo] New comment on issue #2 crash by user1 +more info needed`, pl.Message) }) t.Run("PullRequest", func(t *testing.T) { @@ -94,7 +99,8 @@ func TestTelegramPayload(t *testing.T) { pl, err := tc.PullRequest(p) require.NoError(t, err) - assert.Equal(t, "[test/repo] Pull request opened: #12 Fix bug by user1\nfixes bug #2", pl.Message) + assert.Equal(t, `[test/repo] Pull request opened: #12 Fix bug by user1 +fixes bug #2`, pl.Message) }) t.Run("PullRequestComment", func(t *testing.T) { @@ -103,7 +109,8 @@ func TestTelegramPayload(t *testing.T) { pl, err := tc.IssueComment(p) require.NoError(t, err) - assert.Equal(t, "[test/repo] New comment on pull request #12 Fix bug by user1\nchanges requested", pl.Message) + assert.Equal(t, `[test/repo] New comment on pull request #12 Fix bug by user1 +changes requested`, pl.Message) }) t.Run("Review", func(t *testing.T) { @@ -113,7 +120,8 @@ func TestTelegramPayload(t *testing.T) { pl, err := tc.Review(p, webhook_module.HookEventPullRequestReviewApproved) require.NoError(t, err) - assert.Equal(t, "[test/repo] Pull request review approved: #12 Fix bug\ngood job", pl.Message) + assert.Equal(t, `[test/repo] Pull request review approved: #12 Fix bug +good job`, pl.Message) }) t.Run("Repository", func(t *testing.T) { @@ -122,7 +130,7 @@ func TestTelegramPayload(t *testing.T) { pl, err := tc.Repository(p) require.NoError(t, err) - assert.Equal(t, `[test/repo] Repository created`, pl.Message) + assert.Equal(t, `[test/repo] Repository created`, pl.Message) }) t.Run("Package", func(t *testing.T) { @@ -131,7 +139,7 @@ func TestTelegramPayload(t *testing.T) { pl, err := tc.Package(p) require.NoError(t, err) - assert.Equal(t, `Package created: GiteaContainer:latest by user1`, pl.Message) + assert.Equal(t, `Package created: GiteaContainer:latest by user1`, pl.Message) }) t.Run("Wiki", func(t *testing.T) { @@ -141,19 +149,19 @@ func TestTelegramPayload(t *testing.T) { pl, err := tc.Wiki(p) require.NoError(t, err) - assert.Equal(t, `[test/repo] New wiki page 'index' (Wiki change comment) by user1`, pl.Message) + assert.Equal(t, `[test/repo] New wiki page 'index' (Wiki change comment) by user1`, pl.Message) p.Action = api.HookWikiEdited pl, err = tc.Wiki(p) require.NoError(t, err) - assert.Equal(t, `[test/repo] Wiki page 'index' edited (Wiki change comment) by user1`, pl.Message) + assert.Equal(t, `[test/repo] Wiki page 'index' edited (Wiki change comment) by user1`, pl.Message) p.Action = api.HookWikiDeleted pl, err = tc.Wiki(p) require.NoError(t, err) - assert.Equal(t, `[test/repo] Wiki page 'index' deleted by user1`, pl.Message) + assert.Equal(t, `[test/repo] Wiki page 'index' deleted by user1`, pl.Message) }) t.Run("Release", func(t *testing.T) { @@ -162,7 +170,7 @@ func TestTelegramPayload(t *testing.T) { pl, err := tc.Release(p) require.NoError(t, err) - assert.Equal(t, `[test/repo] Release created: v1.0 by user1`, pl.Message) + assert.Equal(t, `[test/repo] Release created: v1.0 by user1`, pl.Message) }) } @@ -198,5 +206,7 @@ func TestTelegramJSONPayload(t *testing.T) { var body TelegramPayload err = json.NewDecoder(req.Body).Decode(&body) assert.NoError(t, err) - assert.Equal(t, "[test/repo:test] 2 new commits\n[2020558] commit message - user1\n[2020558] commit message - user1", body.Message) + assert.Equal(t, `[test/repo:test] 2 new commits +[2020558] commit message - user1 +[2020558] commit message - user1`, body.Message) }