diff options
author | Aaron F <mail@aaron-fischer.net> | 2022-09-04 21:54:23 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-04 20:54:23 +0100 |
commit | 3963625b6ef6f4d4f16959466c077593a1f908db (patch) | |
tree | f509433e0ca198759452c54a65b943c28bd6e2c3 /services/webhook | |
parent | 8b0aaa5f86e40a4699f278d09d116add63f8e4a0 (diff) | |
download | gitea-3963625b6ef6f4d4f16959466c077593a1f908db.tar.gz gitea-3963625b6ef6f4d4f16959466c077593a1f908db.zip |
Webhook for Wiki changes (#20219)
Add support for triggering webhook notifications on wiki changes.
This PR contains frontend and backend for webhook notifications on wiki actions (create a new page, rename a page, edit a page and delete a page). The frontend got a new checkbox under the Custom Event -> Repository Events section. There is only one checkbox for create/edit/rename/delete actions, because it makes no sense to separate it and others like releases or packages follow the same schema.
![image](https://user-images.githubusercontent.com/121972/177018803-26851196-831f-4fde-9a4c-9e639b0e0d6b.png)
The actions itself are separated, so that different notifications will be executed (with the "action" field). All the webhook receivers implement the new interface method (Wiki) and the corresponding tests.
When implementing this, I encounter a little bug on editing a wiki page. Creating and editing a wiki page is technically the same action and will be handled by the ```updateWikiPage``` function. But the function need to know if it is a new wiki page or just a change. This distinction is done by the ```action``` parameter, but this will not be sent by the frontend (on form submit). This PR will fix this by adding the ```action``` parameter with the values ```_new``` or ```_edit```, which will be used by the ```updateWikiPage``` function.
I've done integration tests with matrix and gitea (http).
![image](https://user-images.githubusercontent.com/121972/177018795-eb5cdc01-9ba3-483e-a6b7-ed0e313a71fb.png)
Fix #16457
Signed-off-by: Aaron Fischer <mail@aaron-fischer.net>
Diffstat (limited to 'services/webhook')
-rw-r--r-- | services/webhook/dingtalk.go | 8 | ||||
-rw-r--r-- | services/webhook/dingtalk_test.go | 38 | ||||
-rw-r--r-- | services/webhook/discord.go | 14 | ||||
-rw-r--r-- | services/webhook/discord_test.go | 47 | ||||
-rw-r--r-- | services/webhook/feishu.go | 7 | ||||
-rw-r--r-- | services/webhook/feishu_test.go | 29 | ||||
-rw-r--r-- | services/webhook/general.go | 29 | ||||
-rw-r--r-- | services/webhook/general_test.go | 54 | ||||
-rw-r--r-- | services/webhook/matrix.go | 7 | ||||
-rw-r--r-- | services/webhook/matrix_test.go | 32 | ||||
-rw-r--r-- | services/webhook/msteams.go | 16 | ||||
-rw-r--r-- | services/webhook/msteams_test.go | 74 | ||||
-rw-r--r-- | services/webhook/packagist.go | 5 | ||||
-rw-r--r-- | services/webhook/packagist_test.go | 20 | ||||
-rw-r--r-- | services/webhook/payloader.go | 3 | ||||
-rw-r--r-- | services/webhook/slack.go | 7 | ||||
-rw-r--r-- | services/webhook/slack_test.go | 29 | ||||
-rw-r--r-- | services/webhook/telegram.go | 7 | ||||
-rw-r--r-- | services/webhook/telegram_test.go | 29 | ||||
-rw-r--r-- | services/webhook/wechatwork.go | 7 |
20 files changed, 462 insertions, 0 deletions
diff --git a/services/webhook/dingtalk.go b/services/webhook/dingtalk.go index 06b73c38d6..5112bdb347 100644 --- a/services/webhook/dingtalk.go +++ b/services/webhook/dingtalk.go @@ -107,6 +107,14 @@ func (d *DingtalkPayload) Issue(p *api.IssuePayload) (api.Payloader, error) { return createDingtalkPayload(issueTitle, text+"\r\n\r\n"+attachmentText, "view issue", p.Issue.HTMLURL), nil } +// Wiki implements PayloadConvertor Wiki method +func (d *DingtalkPayload) Wiki(p *api.WikiPayload) (api.Payloader, error) { + text, _, _ := getWikiPayloadInfo(p, noneLinkFormatter, true) + url := p.Repository.HTMLURL + "/wiki/" + url.PathEscape(p.Page) + + return createDingtalkPayload(text, text, "view wiki", url), nil +} + // IssueComment implements PayloadConvertor IssueComment method func (d *DingtalkPayload) IssueComment(p *api.IssueCommentPayload) (api.Payloader, error) { text, issueTitle, _ := getIssueCommentPayloadInfo(p, noneLinkFormatter, true) diff --git a/services/webhook/dingtalk_test.go b/services/webhook/dingtalk_test.go index b66b5e43a8..efc9601c12 100644 --- a/services/webhook/dingtalk_test.go +++ b/services/webhook/dingtalk_test.go @@ -189,6 +189,44 @@ func TestDingTalkPayload(t *testing.T) { assert.Equal(t, "http://localhost:3000/test/repo", parseRealSingleURL(pl.(*DingtalkPayload).ActionCard.SingleURL)) }) + t.Run("Wiki", func(t *testing.T) { + p := wikiTestPayload() + + d := new(DingtalkPayload) + p.Action = api.HookWikiCreated + pl, err := d.Wiki(p) + require.NoError(t, err) + require.NotNil(t, pl) + require.IsType(t, &DingtalkPayload{}, pl) + + assert.Equal(t, "[test/repo] New wiki page 'index' (Wiki change comment) by user1", pl.(*DingtalkPayload).ActionCard.Text) + assert.Equal(t, "[test/repo] New wiki page 'index' (Wiki change comment) by user1", pl.(*DingtalkPayload).ActionCard.Title) + assert.Equal(t, "view wiki", pl.(*DingtalkPayload).ActionCard.SingleTitle) + assert.Equal(t, "http://localhost:3000/test/repo/wiki/index", parseRealSingleURL(pl.(*DingtalkPayload).ActionCard.SingleURL)) + + p.Action = api.HookWikiEdited + pl, err = d.Wiki(p) + require.NoError(t, err) + require.NotNil(t, pl) + require.IsType(t, &DingtalkPayload{}, pl) + + assert.Equal(t, "[test/repo] Wiki page 'index' edited (Wiki change comment) by user1", pl.(*DingtalkPayload).ActionCard.Text) + assert.Equal(t, "[test/repo] Wiki page 'index' edited (Wiki change comment) by user1", pl.(*DingtalkPayload).ActionCard.Title) + assert.Equal(t, "view wiki", pl.(*DingtalkPayload).ActionCard.SingleTitle) + assert.Equal(t, "http://localhost:3000/test/repo/wiki/index", parseRealSingleURL(pl.(*DingtalkPayload).ActionCard.SingleURL)) + + p.Action = api.HookWikiDeleted + pl, err = d.Wiki(p) + require.NoError(t, err) + require.NotNil(t, pl) + require.IsType(t, &DingtalkPayload{}, pl) + + assert.Equal(t, "[test/repo] Wiki page 'index' deleted by user1", pl.(*DingtalkPayload).ActionCard.Text) + assert.Equal(t, "[test/repo] Wiki page 'index' deleted by user1", pl.(*DingtalkPayload).ActionCard.Title) + assert.Equal(t, "view wiki", pl.(*DingtalkPayload).ActionCard.SingleTitle) + assert.Equal(t, "http://localhost:3000/test/repo/wiki/index", parseRealSingleURL(pl.(*DingtalkPayload).ActionCard.SingleURL)) + }) + t.Run("Release", func(t *testing.T) { p := pullReleaseTestPayload() diff --git a/services/webhook/discord.go b/services/webhook/discord.go index ae5460b9a7..7b6f0f0b1d 100644 --- a/services/webhook/discord.go +++ b/services/webhook/discord.go @@ -7,6 +7,7 @@ package webhook import ( "errors" "fmt" + "net/url" "strconv" "strings" @@ -235,6 +236,19 @@ func (d *DiscordPayload) Repository(p *api.RepositoryPayload) (api.Payloader, er return d.createPayload(p.Sender, title, "", url, color), nil } +// Wiki implements PayloadConvertor Wiki method +func (d *DiscordPayload) Wiki(p *api.WikiPayload) (api.Payloader, error) { + text, color, _ := getWikiPayloadInfo(p, noneLinkFormatter, false) + htmlLink := p.Repository.HTMLURL + "/wiki/" + url.PathEscape(p.Page) + + var description string + if p.Action != api.HookWikiDeleted { + description = p.Comment + } + + return d.createPayload(p.Sender, text, description, htmlLink, color), nil +} + // Release implements PayloadConvertor Release method func (d *DiscordPayload) Release(p *api.ReleasePayload) (api.Payloader, error) { text, color := getReleasePayloadInfo(p, noneLinkFormatter, false) diff --git a/services/webhook/discord_test.go b/services/webhook/discord_test.go index 8fe20c0102..8e4e60a7ff 100644 --- a/services/webhook/discord_test.go +++ b/services/webhook/discord_test.go @@ -212,6 +212,53 @@ func TestDiscordPayload(t *testing.T) { assert.Equal(t, p.Sender.AvatarURL, pl.(*DiscordPayload).Embeds[0].Author.IconURL) }) + t.Run("Wiki", func(t *testing.T) { + p := wikiTestPayload() + + d := new(DiscordPayload) + p.Action = api.HookWikiCreated + pl, err := d.Wiki(p) + require.NoError(t, err) + require.NotNil(t, pl) + require.IsType(t, &DiscordPayload{}, pl) + + assert.Len(t, pl.(*DiscordPayload).Embeds, 1) + assert.Equal(t, "[test/repo] New wiki page 'index' (Wiki change comment)", pl.(*DiscordPayload).Embeds[0].Title) + assert.Equal(t, "Wiki change comment", pl.(*DiscordPayload).Embeds[0].Description) + assert.Equal(t, "http://localhost:3000/test/repo/wiki/index", pl.(*DiscordPayload).Embeds[0].URL) + assert.Equal(t, p.Sender.UserName, pl.(*DiscordPayload).Embeds[0].Author.Name) + assert.Equal(t, setting.AppURL+p.Sender.UserName, pl.(*DiscordPayload).Embeds[0].Author.URL) + assert.Equal(t, p.Sender.AvatarURL, pl.(*DiscordPayload).Embeds[0].Author.IconURL) + + p.Action = api.HookWikiEdited + pl, err = d.Wiki(p) + require.NoError(t, err) + require.NotNil(t, pl) + require.IsType(t, &DiscordPayload{}, pl) + + assert.Len(t, pl.(*DiscordPayload).Embeds, 1) + assert.Equal(t, "[test/repo] Wiki page 'index' edited (Wiki change comment)", pl.(*DiscordPayload).Embeds[0].Title) + assert.Equal(t, "Wiki change comment", pl.(*DiscordPayload).Embeds[0].Description) + assert.Equal(t, "http://localhost:3000/test/repo/wiki/index", pl.(*DiscordPayload).Embeds[0].URL) + assert.Equal(t, p.Sender.UserName, pl.(*DiscordPayload).Embeds[0].Author.Name) + assert.Equal(t, setting.AppURL+p.Sender.UserName, pl.(*DiscordPayload).Embeds[0].Author.URL) + assert.Equal(t, p.Sender.AvatarURL, pl.(*DiscordPayload).Embeds[0].Author.IconURL) + + p.Action = api.HookWikiDeleted + pl, err = d.Wiki(p) + require.NoError(t, err) + require.NotNil(t, pl) + require.IsType(t, &DiscordPayload{}, pl) + + assert.Len(t, pl.(*DiscordPayload).Embeds, 1) + assert.Equal(t, "[test/repo] Wiki page 'index' deleted", pl.(*DiscordPayload).Embeds[0].Title) + assert.Empty(t, pl.(*DiscordPayload).Embeds[0].Description) + assert.Equal(t, "http://localhost:3000/test/repo/wiki/index", pl.(*DiscordPayload).Embeds[0].URL) + assert.Equal(t, p.Sender.UserName, pl.(*DiscordPayload).Embeds[0].Author.Name) + assert.Equal(t, setting.AppURL+p.Sender.UserName, pl.(*DiscordPayload).Embeds[0].Author.URL) + assert.Equal(t, p.Sender.AvatarURL, pl.(*DiscordPayload).Embeds[0].Author.IconURL) + }) + t.Run("Release", func(t *testing.T) { p := pullReleaseTestPayload() diff --git a/services/webhook/feishu.go b/services/webhook/feishu.go index 5b20c7dda7..782684d0ff 100644 --- a/services/webhook/feishu.go +++ b/services/webhook/feishu.go @@ -145,6 +145,13 @@ func (f *FeishuPayload) Repository(p *api.RepositoryPayload) (api.Payloader, err return nil, nil } +// Wiki implements PayloadConvertor Wiki method +func (f *FeishuPayload) Wiki(p *api.WikiPayload) (api.Payloader, error) { + text, _, _ := getWikiPayloadInfo(p, noneLinkFormatter, true) + + return newFeishuTextPayload(text), nil +} + // Release implements PayloadConvertor Release method func (f *FeishuPayload) Release(p *api.ReleasePayload) (api.Payloader, error) { text, _ := getReleasePayloadInfo(p, noneLinkFormatter, true) diff --git a/services/webhook/feishu_test.go b/services/webhook/feishu_test.go index d862b015e7..85cfb759fe 100644 --- a/services/webhook/feishu_test.go +++ b/services/webhook/feishu_test.go @@ -145,6 +145,35 @@ func TestFeishuPayload(t *testing.T) { assert.Equal(t, "[test/repo] Repository created", pl.(*FeishuPayload).Content.Text) }) + t.Run("Wiki", func(t *testing.T) { + p := wikiTestPayload() + + d := new(FeishuPayload) + p.Action = api.HookWikiCreated + pl, err := d.Wiki(p) + require.NoError(t, err) + require.NotNil(t, pl) + require.IsType(t, &FeishuPayload{}, pl) + + assert.Equal(t, "[test/repo] New wiki page 'index' (Wiki change comment) by user1", pl.(*FeishuPayload).Content.Text) + + p.Action = api.HookWikiEdited + pl, err = d.Wiki(p) + require.NoError(t, err) + require.NotNil(t, pl) + require.IsType(t, &FeishuPayload{}, pl) + + assert.Equal(t, "[test/repo] Wiki page 'index' edited (Wiki change comment) by user1", pl.(*FeishuPayload).Content.Text) + + p.Action = api.HookWikiDeleted + pl, err = d.Wiki(p) + require.NoError(t, err) + require.NotNil(t, pl) + require.IsType(t, &FeishuPayload{}, pl) + + assert.Equal(t, "[test/repo] Wiki page 'index' deleted by user1", pl.(*FeishuPayload).Content.Text) + }) + t.Run("Release", func(t *testing.T) { p := pullReleaseTestPayload() diff --git a/services/webhook/general.go b/services/webhook/general.go index e8006fabae..5be177d339 100644 --- a/services/webhook/general.go +++ b/services/webhook/general.go @@ -161,6 +161,35 @@ func getReleasePayloadInfo(p *api.ReleasePayload, linkFormatter linkFormatter, w return text, color } +func getWikiPayloadInfo(p *api.WikiPayload, linkFormatter linkFormatter, withSender bool) (string, int, string) { + repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName) + pageLink := linkFormatter(p.Repository.HTMLURL+"/wiki/"+url.PathEscape(p.Page), p.Page) + + var text string + color := greenColor + + switch p.Action { + case api.HookWikiCreated: + text = fmt.Sprintf("[%s] New wiki page '%s'", repoLink, pageLink) + case api.HookWikiEdited: + text = fmt.Sprintf("[%s] Wiki page '%s' edited", repoLink, pageLink) + color = yellowColor + case api.HookWikiDeleted: + text = fmt.Sprintf("[%s] Wiki page '%s' deleted", repoLink, pageLink) + color = redColor + } + + if p.Action != api.HookWikiDeleted && p.Comment != "" { + text += fmt.Sprintf(" (%s)", p.Comment) + } + + if withSender { + text += fmt.Sprintf(" by %s", linkFormatter(setting.AppURL+url.PathEscape(p.Sender.UserName), p.Sender.UserName)) + } + + return text, color, pageLink +} + func getIssueCommentPayloadInfo(p *api.IssueCommentPayload, linkFormatter linkFormatter, withSender bool) (string, string, int) { repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName) issueTitle := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title) diff --git a/services/webhook/general_test.go b/services/webhook/general_test.go index 4d73afe060..4acf51ac71 100644 --- a/services/webhook/general_test.go +++ b/services/webhook/general_test.go @@ -195,6 +195,22 @@ func pullRequestCommentTestPayload() *api.IssueCommentPayload { } } +func wikiTestPayload() *api.WikiPayload { + return &api.WikiPayload{ + Repository: &api.Repository{ + HTMLURL: "http://localhost:3000/test/repo", + Name: "repo", + FullName: "test/repo", + }, + Sender: &api.User{ + UserName: "user1", + AvatarURL: "http://localhost:3000/user1/avatar", + }, + Page: "index", + Comment: "Wiki change comment", + } +} + func pullReleaseTestPayload() *api.ReleasePayload { return &api.ReleasePayload{ Action: api.HookReleasePublished, @@ -469,6 +485,44 @@ func TestGetPullRequestPayloadInfo(t *testing.T) { } } +func TestGetWikiPayloadInfo(t *testing.T) { + p := wikiTestPayload() + + cases := []struct { + action api.HookWikiAction + text string + color int + link string + }{ + { + api.HookWikiCreated, + "[test/repo] New wiki page 'index' (Wiki change comment) by user1", + greenColor, + "index", + }, + { + api.HookWikiEdited, + "[test/repo] Wiki page 'index' edited (Wiki change comment) by user1", + yellowColor, + "index", + }, + { + api.HookWikiDeleted, + "[test/repo] Wiki page 'index' deleted by user1", + redColor, + "index", + }, + } + + for i, c := range cases { + p.Action = c.action + text, color, link := getWikiPayloadInfo(p, noneLinkFormatter, true) + assert.Equal(t, c.text, text, "case %d", i) + assert.Equal(t, c.color, color, "case %d", i) + assert.Equal(t, c.link, link, "case %d", i) + } +} + func TestGetReleasePayloadInfo(t *testing.T) { p := pullReleaseTestPayload() diff --git a/services/webhook/matrix.go b/services/webhook/matrix.go index a42ab2a93e..f4c4cf0e9a 100644 --- a/services/webhook/matrix.go +++ b/services/webhook/matrix.go @@ -143,6 +143,13 @@ func (m *MatrixPayloadUnsafe) IssueComment(p *api.IssueCommentPayload) (api.Payl return getMatrixPayloadUnsafe(text, nil, m.AccessToken, m.MsgType), nil } +// Wiki implements PayloadConvertor Wiki method +func (m *MatrixPayloadUnsafe) Wiki(p *api.WikiPayload) (api.Payloader, error) { + text, _, _ := getWikiPayloadInfo(p, MatrixLinkFormatter, true) + + return getMatrixPayloadUnsafe(text, nil, m.AccessToken, m.MsgType), nil +} + // Release implements PayloadConvertor Release method func (m *MatrixPayloadUnsafe) Release(p *api.ReleasePayload) (api.Payloader, error) { text, _ := getReleasePayloadInfo(p, MatrixLinkFormatter, true) diff --git a/services/webhook/matrix_test.go b/services/webhook/matrix_test.go index 34196aedf0..4cab845330 100644 --- a/services/webhook/matrix_test.go +++ b/services/webhook/matrix_test.go @@ -156,6 +156,38 @@ func TestMatrixPayload(t *testing.T) { assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Repository created by <a href="https://try.gitea.io/user1">user1</a>`, pl.(*MatrixPayloadUnsafe).FormattedBody) }) + t.Run("Wiki", func(t *testing.T) { + p := wikiTestPayload() + + d := new(MatrixPayloadUnsafe) + p.Action = api.HookWikiCreated + pl, err := d.Wiki(p) + require.NoError(t, err) + require.NotNil(t, pl) + require.IsType(t, &MatrixPayloadUnsafe{}, pl) + + assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] New wiki page '[index](http://localhost:3000/test/repo/wiki/index)' (Wiki change comment) by [user1](https://try.gitea.io/user1)", pl.(*MatrixPayloadUnsafe).Body) + 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.(*MatrixPayloadUnsafe).FormattedBody) + + p.Action = api.HookWikiEdited + pl, err = d.Wiki(p) + require.NoError(t, err) + require.NotNil(t, pl) + require.IsType(t, &MatrixPayloadUnsafe{}, pl) + + assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] Wiki page '[index](http://localhost:3000/test/repo/wiki/index)' edited (Wiki change comment) by [user1](https://try.gitea.io/user1)", pl.(*MatrixPayloadUnsafe).Body) + 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.(*MatrixPayloadUnsafe).FormattedBody) + + p.Action = api.HookWikiDeleted + pl, err = d.Wiki(p) + require.NoError(t, err) + require.NotNil(t, pl) + require.IsType(t, &MatrixPayloadUnsafe{}, pl) + + assert.Equal(t, "[[test/repo](http://localhost:3000/test/repo)] Wiki page '[index](http://localhost:3000/test/repo/wiki/index)' deleted by [user1](https://try.gitea.io/user1)", pl.(*MatrixPayloadUnsafe).Body) + 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.(*MatrixPayloadUnsafe).FormattedBody) + }) + t.Run("Release", func(t *testing.T) { p := pullReleaseTestPayload() diff --git a/services/webhook/msteams.go b/services/webhook/msteams.go index 59e2e93493..1406004781 100644 --- a/services/webhook/msteams.go +++ b/services/webhook/msteams.go @@ -6,6 +6,7 @@ package webhook import ( "fmt" + "net/url" "strings" webhook_model "code.gitea.io/gitea/models/webhook" @@ -266,6 +267,21 @@ func (m *MSTeamsPayload) Repository(p *api.RepositoryPayload) (api.Payloader, er ), nil } +// Wiki implements PayloadConvertor Wiki method +func (m *MSTeamsPayload) Wiki(p *api.WikiPayload) (api.Payloader, error) { + title, color, _ := getWikiPayloadInfo(p, noneLinkFormatter, false) + + return createMSTeamsPayload( + p.Repository, + p.Sender, + title, + "", + p.Repository.HTMLURL+"/wiki/"+url.PathEscape(p.Page), + color, + &MSTeamsFact{"Repository:", p.Repository.FullName}, + ), nil +} + // Release implements PayloadConvertor Release method func (m *MSTeamsPayload) Release(p *api.ReleasePayload) (api.Payloader, error) { title, color := getReleasePayloadInfo(p, noneLinkFormatter, false) diff --git a/services/webhook/msteams_test.go b/services/webhook/msteams_test.go index 3fdf47c1ae..8292beed7f 100644 --- a/services/webhook/msteams_test.go +++ b/services/webhook/msteams_test.go @@ -330,6 +330,80 @@ func TestMSTeamsPayload(t *testing.T) { assert.Equal(t, "http://localhost:3000/test/repo", pl.(*MSTeamsPayload).PotentialAction[0].Targets[0].URI) }) + t.Run("Wiki", func(t *testing.T) { + p := wikiTestPayload() + + d := new(MSTeamsPayload) + p.Action = api.HookWikiCreated + pl, err := d.Wiki(p) + require.NoError(t, err) + require.NotNil(t, pl) + require.IsType(t, &MSTeamsPayload{}, pl) + + assert.Equal(t, "[test/repo] New wiki page 'index' (Wiki change comment)", pl.(*MSTeamsPayload).Title) + assert.Equal(t, "[test/repo] New wiki page 'index' (Wiki change comment)", pl.(*MSTeamsPayload).Summary) + assert.Len(t, pl.(*MSTeamsPayload).Sections, 1) + assert.Equal(t, "user1", pl.(*MSTeamsPayload).Sections[0].ActivitySubtitle) + assert.Equal(t, "", pl.(*MSTeamsPayload).Sections[0].Text) + assert.Len(t, pl.(*MSTeamsPayload).Sections[0].Facts, 2) + for _, fact := range pl.(*MSTeamsPayload).Sections[0].Facts { + if fact.Name == "Repository:" { + assert.Equal(t, p.Repository.FullName, fact.Value) + } else { + t.Fail() + } + } + assert.Len(t, pl.(*MSTeamsPayload).PotentialAction, 1) + assert.Len(t, pl.(*MSTeamsPayload).PotentialAction[0].Targets, 1) + assert.Equal(t, "http://localhost:3000/test/repo/wiki/index", pl.(*MSTeamsPayload).PotentialAction[0].Targets[0].URI) + + p.Action = api.HookWikiEdited + pl, err = d.Wiki(p) + require.NoError(t, err) + require.NotNil(t, pl) + require.IsType(t, &MSTeamsPayload{}, pl) + + assert.Equal(t, "[test/repo] Wiki page 'index' edited (Wiki change comment)", pl.(*MSTeamsPayload).Title) + assert.Equal(t, "[test/repo] Wiki page 'index' edited (Wiki change comment)", pl.(*MSTeamsPayload).Summary) + assert.Len(t, pl.(*MSTeamsPayload).Sections, 1) + assert.Equal(t, "user1", pl.(*MSTeamsPayload).Sections[0].ActivitySubtitle) + assert.Equal(t, "", pl.(*MSTeamsPayload).Sections[0].Text) + assert.Len(t, pl.(*MSTeamsPayload).Sections[0].Facts, 2) + for _, fact := range pl.(*MSTeamsPayload).Sections[0].Facts { + if fact.Name == "Repository:" { + assert.Equal(t, p.Repository.FullName, fact.Value) + } else { + t.Fail() + } + } + assert.Len(t, pl.(*MSTeamsPayload).PotentialAction, 1) + assert.Len(t, pl.(*MSTeamsPayload).PotentialAction[0].Targets, 1) + assert.Equal(t, "http://localhost:3000/test/repo/wiki/index", pl.(*MSTeamsPayload).PotentialAction[0].Targets[0].URI) + + p.Action = api.HookWikiDeleted + pl, err = d.Wiki(p) + require.NoError(t, err) + require.NotNil(t, pl) + require.IsType(t, &MSTeamsPayload{}, pl) + + assert.Equal(t, "[test/repo] Wiki page 'index' deleted", pl.(*MSTeamsPayload).Title) + assert.Equal(t, "[test/repo] Wiki page 'index' deleted", pl.(*MSTeamsPayload).Summary) + assert.Len(t, pl.(*MSTeamsPayload).Sections, 1) + assert.Equal(t, "user1", pl.(*MSTeamsPayload).Sections[0].ActivitySubtitle) + assert.Empty(t, pl.(*MSTeamsPayload).Sections[0].Text) + assert.Len(t, pl.(*MSTeamsPayload).Sections[0].Facts, 2) + for _, fact := range pl.(*MSTeamsPayload).Sections[0].Facts { + if fact.Name == "Repository:" { + assert.Equal(t, p.Repository.FullName, fact.Value) + } else { + t.Fail() + } + } + assert.Len(t, pl.(*MSTeamsPayload).PotentialAction, 1) + assert.Len(t, pl.(*MSTeamsPayload).PotentialAction[0].Targets, 1) + assert.Equal(t, "http://localhost:3000/test/repo/wiki/index", pl.(*MSTeamsPayload).PotentialAction[0].Targets[0].URI) + }) + t.Run("Release", func(t *testing.T) { p := pullReleaseTestPayload() diff --git a/services/webhook/packagist.go b/services/webhook/packagist.go index ace93b13ff..5badc7462a 100644 --- a/services/webhook/packagist.go +++ b/services/webhook/packagist.go @@ -94,6 +94,11 @@ func (f *PackagistPayload) Repository(p *api.RepositoryPayload) (api.Payloader, return nil, nil } +// Wiki implements PayloadConvertor Wiki method +func (f *PackagistPayload) Wiki(p *api.WikiPayload) (api.Payloader, error) { + return nil, nil +} + // Release implements PayloadConvertor Release method func (f *PackagistPayload) Release(p *api.ReleasePayload) (api.Payloader, error) { return nil, nil diff --git a/services/webhook/packagist_test.go b/services/webhook/packagist_test.go index 08912924d2..4a24b76523 100644 --- a/services/webhook/packagist_test.go +++ b/services/webhook/packagist_test.go @@ -116,6 +116,26 @@ func TestPackagistPayload(t *testing.T) { require.Nil(t, pl) }) + t.Run("Wiki", func(t *testing.T) { + p := wikiTestPayload() + + d := new(PackagistPayload) + p.Action = api.HookWikiCreated + pl, err := d.Wiki(p) + require.NoError(t, err) + require.Nil(t, pl) + + p.Action = api.HookWikiEdited + pl, err = d.Wiki(p) + require.NoError(t, err) + require.Nil(t, pl) + + p.Action = api.HookWikiDeleted + pl, err = d.Wiki(p) + require.NoError(t, err) + require.Nil(t, pl) + }) + t.Run("Release", func(t *testing.T) { p := pullReleaseTestPayload() diff --git a/services/webhook/payloader.go b/services/webhook/payloader.go index 0e09dd1b1e..a9d01c9891 100644 --- a/services/webhook/payloader.go +++ b/services/webhook/payloader.go @@ -22,6 +22,7 @@ type PayloadConvertor interface { Review(*api.PullRequestPayload, webhook_model.HookEventType) (api.Payloader, error) Repository(*api.RepositoryPayload) (api.Payloader, error) Release(*api.ReleasePayload) (api.Payloader, error) + Wiki(*api.WikiPayload) (api.Payloader, error) } func convertPayloader(s PayloadConvertor, p api.Payloader, event webhook_model.HookEventType) (api.Payloader, error) { @@ -51,6 +52,8 @@ func convertPayloader(s PayloadConvertor, p api.Payloader, event webhook_model.H return s.Repository(p.(*api.RepositoryPayload)) case webhook_model.HookEventRelease: return s.Release(p.(*api.ReleasePayload)) + case webhook_model.HookEventWiki: + return s.Wiki(p.(*api.WikiPayload)) } return s, nil } diff --git a/services/webhook/slack.go b/services/webhook/slack.go index e3d0d406de..63cc6f8ca2 100644 --- a/services/webhook/slack.go +++ b/services/webhook/slack.go @@ -157,6 +157,13 @@ func (s *SlackPayload) IssueComment(p *api.IssueCommentPayload) (api.Payloader, }}), nil } +// Wiki implements PayloadConvertor Wiki method +func (s *SlackPayload) Wiki(p *api.WikiPayload) (api.Payloader, error) { + text, _, _ := getWikiPayloadInfo(p, SlackLinkFormatter, true) + + return s.createPayload(text, nil), nil +} + // Release implements PayloadConvertor Release method func (s *SlackPayload) Release(p *api.ReleasePayload) (api.Payloader, error) { text, _ := getReleasePayloadInfo(p, SlackLinkFormatter, true) diff --git a/services/webhook/slack_test.go b/services/webhook/slack_test.go index 0f08785d25..3af38b5158 100644 --- a/services/webhook/slack_test.go +++ b/services/webhook/slack_test.go @@ -145,6 +145,35 @@ func TestSlackPayload(t *testing.T) { assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Repository created by <https://try.gitea.io/user1|user1>", pl.(*SlackPayload).Text) }) + t.Run("Wiki", func(t *testing.T) { + p := wikiTestPayload() + + d := new(SlackPayload) + p.Action = api.HookWikiCreated + pl, err := d.Wiki(p) + require.NoError(t, err) + require.NotNil(t, pl) + require.IsType(t, &SlackPayload{}, pl) + + assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] New wiki page '<http://localhost:3000/test/repo/wiki/index|index>' (Wiki change comment) by <https://try.gitea.io/user1|user1>", pl.(*SlackPayload).Text) + + p.Action = api.HookWikiEdited + pl, err = d.Wiki(p) + require.NoError(t, err) + require.NotNil(t, pl) + require.IsType(t, &SlackPayload{}, pl) + + assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Wiki page '<http://localhost:3000/test/repo/wiki/index|index>' edited (Wiki change comment) by <https://try.gitea.io/user1|user1>", pl.(*SlackPayload).Text) + + p.Action = api.HookWikiDeleted + pl, err = d.Wiki(p) + require.NoError(t, err) + require.NotNil(t, pl) + require.IsType(t, &SlackPayload{}, pl) + + assert.Equal(t, "[<http://localhost:3000/test/repo|test/repo>] Wiki page '<http://localhost:3000/test/repo/wiki/index|index>' deleted by <https://try.gitea.io/user1|user1>", pl.(*SlackPayload).Text) + }) + t.Run("Release", func(t *testing.T) { p := pullReleaseTestPayload() diff --git a/services/webhook/telegram.go b/services/webhook/telegram.go index 64211493ec..13471d864e 100644 --- a/services/webhook/telegram.go +++ b/services/webhook/telegram.go @@ -171,6 +171,13 @@ func (t *TelegramPayload) Repository(p *api.RepositoryPayload) (api.Payloader, e return nil, nil } +// Wiki implements PayloadConvertor Wiki method +func (t *TelegramPayload) Wiki(p *api.WikiPayload) (api.Payloader, error) { + text, _, _ := getWikiPayloadInfo(p, htmlLinkFormatter, true) + + return createTelegramPayload(text), nil +} + // Release implements PayloadConvertor Release method func (t *TelegramPayload) Release(p *api.ReleasePayload) (api.Payloader, error) { text, _ := getReleasePayloadInfo(p, htmlLinkFormatter, true) diff --git a/services/webhook/telegram_test.go b/services/webhook/telegram_test.go index 2f83090166..5ca78d0508 100644 --- a/services/webhook/telegram_test.go +++ b/services/webhook/telegram_test.go @@ -145,6 +145,35 @@ func TestTelegramPayload(t *testing.T) { assert.Equal(t, `[<a href="http://localhost:3000/test/repo">test/repo</a>] Repository created`, pl.(*TelegramPayload).Message) }) + t.Run("Wiki", func(t *testing.T) { + p := wikiTestPayload() + + d := new(TelegramPayload) + p.Action = api.HookWikiCreated + pl, err := d.Wiki(p) + require.NoError(t, err) + require.NotNil(t, pl) + require.IsType(t, &TelegramPayload{}, pl) + + 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.(*TelegramPayload).Message) + + p.Action = api.HookWikiEdited + pl, err = d.Wiki(p) + require.NoError(t, err) + require.NotNil(t, pl) + require.IsType(t, &TelegramPayload{}, pl) + + 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.(*TelegramPayload).Message) + + p.Action = api.HookWikiDeleted + pl, err = d.Wiki(p) + require.NoError(t, err) + require.NotNil(t, pl) + require.IsType(t, &TelegramPayload{}, pl) + + 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.(*TelegramPayload).Message) + }) + t.Run("Release", func(t *testing.T) { p := pullReleaseTestPayload() diff --git a/services/webhook/wechatwork.go b/services/webhook/wechatwork.go index de8b777066..bd4c3e0851 100644 --- a/services/webhook/wechatwork.go +++ b/services/webhook/wechatwork.go @@ -166,6 +166,13 @@ func (f *WechatworkPayload) Repository(p *api.RepositoryPayload) (api.Payloader, return nil, nil } +// Wiki implements PayloadConvertor Wiki method +func (f *WechatworkPayload) Wiki(p *api.WikiPayload) (api.Payloader, error) { + text, _, _ := getWikiPayloadInfo(p, noneLinkFormatter, true) + + return newWechatworkMarkdownPayload(text), nil +} + // Release implements PayloadConvertor Release method func (f *WechatworkPayload) Release(p *api.ReleasePayload) (api.Payloader, error) { text, _ := getReleasePayloadInfo(p, noneLinkFormatter, true) |