Refactor the webhook logic, to have the type-dependent processing happen
only in one place.
---
## Current webhook flow
1. An event happens
2. It is pre-processed (depending on the webhook type) and its body is
added to a task queue
3. When the task is processed, some more logic (depending on the webhook
type as well) is applied to make an HTTP request
This means that webhook-type dependant logic is needed in step 2 and 3.
This is cumbersome and brittle to maintain.
Updated webhook flow with this PR:
1. An event happens
2. It is stored as-is and added to a task queue
3. When the task is processed, the event is processed (depending on the
webhook type) to make an HTTP request
So the only webhook-type dependent logic happens in one place (step 3)
which should be much more robust.
## Consequences of the refactor
- the raw event must be stored in the hooktask (until now, the
pre-processed body was stored)
- to ensure that previous hooktasks are correctly sent, a
`payload_version` is added (version 1: the body has already been
pre-process / version 2: the body is the raw event)
So future webhook additions will only have to deal with creating an
http.Request based on the raw event (no need to adjust the code in
multiple places, like currently).
Moreover since this processing happens when fetching from the task
queue, it ensures that the queuing of new events (upon a `git push` for
instance) does not get slowed down by a slow webhook.
As a concrete example, the PR #19307 for custom webhooks, should be
substantially smaller:
- no need to change `services/webhook/deliver.go`
- minimal change in `services/webhook/webhook.go` (add the new webhook
to the map)
- no need to change all the individual webhook files (since with this
refactor the `*webhook_model.Webhook` is provided as argument)
assert.Equal(t,"[Issue-test/repo #2]: opened\ncrash\nhttp://localhost:3000/test/repo/issues/2\nIssue by user1\nOperator: user1\nAssignees: user1\n\nissue body",pl.(*FeishuPayload).Content.Text)
assert.Equal(t,"[Issue-test/repo #2]: opened\ncrash\nhttp://localhost:3000/test/repo/issues/2\nIssue by user1\nOperator: user1\nAssignees: user1\n\nissue body",pl.Content.Text)
p.Action=api.HookIssueClosed
pl,err=d.Issue(p)
pl,err=fc.Issue(p)
require.NoError(t,err)
require.NotNil(t,pl)
require.IsType(t,&FeishuPayload{},pl)
assert.Equal(t,"[Issue-test/repo #2]: closed\ncrash\nhttp://localhost:3000/test/repo/issues/2\nIssue by user1\nOperator: user1\nAssignees: user1\n\nissue body",pl.(*FeishuPayload).Content.Text)
assert.Equal(t,"[Issue-test/repo #2]: closed\ncrash\nhttp://localhost:3000/test/repo/issues/2\nIssue by user1\nOperator: user1\nAssignees: user1\n\nissue body",pl.Content.Text)
})
t.Run("IssueComment",func(t*testing.T){
p:=issueCommentTestPayload()
d:=new(FeishuPayload)
pl,err:=d.IssueComment(p)
pl,err:=fc.IssueComment(p)
require.NoError(t,err)
require.NotNil(t,pl)
require.IsType(t,&FeishuPayload{},pl)
assert.Equal(t,"[Comment-test/repo #2]: created\ncrash\nhttp://localhost:3000/test/repo/issues/2\nIssue by user1\nOperator: user1\n\nmore info needed",pl.(*FeishuPayload).Content.Text)
assert.Equal(t,"[Comment-test/repo #2]: created\ncrash\nhttp://localhost:3000/test/repo/issues/2\nIssue by user1\nOperator: user1\n\nmore info needed",pl.Content.Text)
})
t.Run("PullRequest",func(t*testing.T){
p:=pullRequestTestPayload()
d:=new(FeishuPayload)
pl,err:=d.PullRequest(p)
pl,err:=fc.PullRequest(p)
require.NoError(t,err)
require.NotNil(t,pl)
require.IsType(t,&FeishuPayload{},pl)
assert.Equal(t,"[PullRequest-test/repo #12]: opened\nFix bug\nhttp://localhost:3000/test/repo/pulls/12\nPullRequest by user1\nOperator: user1\nAssignees: user1\n\nfixes bug #2",pl.(*FeishuPayload).Content.Text)
assert.Equal(t,"[PullRequest-test/repo #12]: opened\nFix bug\nhttp://localhost:3000/test/repo/pulls/12\nPullRequest by user1\nOperator: user1\nAssignees: user1\n\nfixes bug #2",pl.Content.Text)
})
t.Run("PullRequestComment",func(t*testing.T){
p:=pullRequestCommentTestPayload()
d:=new(FeishuPayload)
pl,err:=d.IssueComment(p)
pl,err:=fc.IssueComment(p)
require.NoError(t,err)
require.NotNil(t,pl)
require.IsType(t,&FeishuPayload{},pl)
assert.Equal(t,"[Comment-test/repo #12]: created\nFix bug\nhttp://localhost:3000/test/repo/pulls/12\nPullRequest by user1\nOperator: user1\n\nchanges requested",pl.(*FeishuPayload).Content.Text)
assert.Equal(t,"[Comment-test/repo #12]: created\nFix bug\nhttp://localhost:3000/test/repo/pulls/12\nPullRequest by user1\nOperator: user1\n\nchanges requested",pl.Content.Text)
assert.Equal(t,"[[test/repo](http://localhost:3000/test/repo):[test](http://localhost:3000/test/repo/src/branch/test)] branch created by user1",pl.(*MatrixPayload).Body)
assert.Equal(t,`[<a href="http://localhost:3000/test/repo">test/repo</a>:<a href="http://localhost:3000/test/repo/src/branch/test">test</a>] branch created by user1`,pl.(*MatrixPayload).FormattedBody)
assert.Equal(t,"[[test/repo](http://localhost:3000/test/repo):[test](http://localhost:3000/test/repo/src/branch/test)] branch created by user1",pl.Body)
assert.Equal(t,`[<a href="http://localhost:3000/test/repo">test/repo</a>:<a href="http://localhost:3000/test/repo/src/branch/test">test</a>] branch created by user1`,pl.FormattedBody)
})
t.Run("Delete",func(t*testing.T){
p:=deleteTestPayload()
d:=new(MatrixPayload)
pl,err:=d.Delete(p)
pl,err:=mc.Delete(p)
require.NoError(t,err)
require.NotNil(t,pl)
require.IsType(t,&MatrixPayload{},pl)
assert.Equal(t,"[[test/repo](http://localhost:3000/test/repo):test] branch deleted by user1",pl.(*MatrixPayload).Body)
assert.Equal(t,`[<a href="http://localhost:3000/test/repo">test/repo</a>:test] branch deleted by user1`,pl.(*MatrixPayload).FormattedBody)
assert.Equal(t,"[[test/repo](http://localhost:3000/test/repo):test] branch deleted by user1",pl.Body)
assert.Equal(t,`[<a href="http://localhost:3000/test/repo">test/repo</a>:test] branch deleted by user1`,pl.FormattedBody)
})
t.Run("Fork",func(t*testing.T){
p:=forkTestPayload()
d:=new(MatrixPayload)
pl,err:=d.Fork(p)
pl,err:=mc.Fork(p)
require.NoError(t,err)
require.NotNil(t,pl)
require.IsType(t,&MatrixPayload{},pl)
assert.Equal(t,"[test/repo2](http://localhost:3000/test/repo2) is forked to [test/repo](http://localhost:3000/test/repo)",pl.(*MatrixPayload).Body)
assert.Equal(t,`<a href="http://localhost:3000/test/repo2">test/repo2</a> is forked to <a href="http://localhost:3000/test/repo">test/repo</a>`,pl.(*MatrixPayload).FormattedBody)
assert.Equal(t,"[test/repo2](http://localhost:3000/test/repo2) is forked to [test/repo](http://localhost:3000/test/repo)",pl.Body)
assert.Equal(t,`<a href="http://localhost:3000/test/repo2">test/repo2</a> is forked to <a href="http://localhost:3000/test/repo">test/repo</a>`,pl.FormattedBody)
assert.Equal(t,"[[test/repo](http://localhost:3000/test/repo)] Issue opened: [#2 crash](http://localhost:3000/test/repo/issues/2) by [user1](https://try.gitea.io/user1)",pl.(*MatrixPayload).Body)
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>`,pl.(*MatrixPayload).FormattedBody)
assert.Equal(t,"[[test/repo](http://localhost:3000/test/repo)] Issue opened: [#2 crash](http://localhost:3000/test/repo/issues/2) by [user1](https://try.gitea.io/user1)",pl.Body)
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>`,pl.FormattedBody)
p.Action=api.HookIssueClosed
pl,err=d.Issue(p)
pl,err=mc.Issue(p)
require.NoError(t,err)
require.NotNil(t,pl)
require.IsType(t,&MatrixPayload{},pl)
assert.Equal(t,"[[test/repo](http://localhost:3000/test/repo)] Issue closed: [#2 crash](http://localhost:3000/test/repo/issues/2) by [user1](https://try.gitea.io/user1)",pl.(*MatrixPayload).Body)
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.(*MatrixPayload).FormattedBody)
assert.Equal(t,"[[test/repo](http://localhost:3000/test/repo)] Issue closed: [#2 crash](http://localhost:3000/test/repo/issues/2) by [user1](https://try.gitea.io/user1)",pl.Body)
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.FormattedBody)
})
t.Run("IssueComment",func(t*testing.T){
p:=issueCommentTestPayload()
d:=new(MatrixPayload)
pl,err:=d.IssueComment(p)
pl,err:=mc.IssueComment(p)
require.NoError(t,err)
require.NotNil(t,pl)
require.IsType(t,&MatrixPayload{},pl)
assert.Equal(t,"[[test/repo](http://localhost:3000/test/repo)] New comment on issue [#2 crash](http://localhost:3000/test/repo/issues/2) by [user1](https://try.gitea.io/user1)",pl.(*MatrixPayload).Body)
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>`,pl.(*MatrixPayload).FormattedBody)
assert.Equal(t,"[[test/repo](http://localhost:3000/test/repo)] New comment on issue [#2 crash](http://localhost:3000/test/repo/issues/2) by [user1](https://try.gitea.io/user1)",pl.Body)
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>`,pl.FormattedBody)
})
t.Run("PullRequest",func(t*testing.T){
p:=pullRequestTestPayload()
d:=new(MatrixPayload)
pl,err:=d.PullRequest(p)
pl,err:=mc.PullRequest(p)
require.NoError(t,err)
require.NotNil(t,pl)
require.IsType(t,&MatrixPayload{},pl)
assert.Equal(t,"[[test/repo](http://localhost:3000/test/repo)] Pull request opened: [#12 Fix bug](http://localhost:3000/test/repo/pulls/12) by [user1](https://try.gitea.io/user1)",pl.(*MatrixPayload).Body)
assert.Equal(t,"[[test/repo](http://localhost:3000/test/repo)] New comment on pull request [#12 Fix bug](http://localhost:3000/test/repo/pulls/12) by [user1](https://try.gitea.io/user1)",pl.(*MatrixPayload).Body)
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>`,pl.(*MatrixPayload).FormattedBody)
assert.Equal(t,"[[test/repo](http://localhost:3000/test/repo)] New comment on pull request [#12 Fix bug](http://localhost:3000/test/repo/pulls/12) by [user1](https://try.gitea.io/user1)",pl.Body)
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>`,pl.FormattedBody)
assert.Equal(t,`[[test/repo](http://localhost:3000/test/repo)] Repository created by [user1](https://try.gitea.io/user1)`,pl.(*MatrixPayload).Body)
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.(*MatrixPayload).FormattedBody)
assert.Equal(t,`[[test/repo](http://localhost:3000/test/repo)] Repository created by [user1](https://try.gitea.io/user1)`,pl.Body)
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.FormattedBody)
})
t.Run("Package",func(t*testing.T){
p:=packageTestPayload()
d:=new(MatrixPayload)
pl,err:=d.Package(p)
pl,err:=mc.Package(p)
require.NoError(t,err)
require.NotNil(t,pl)
require.IsType(t,&MatrixPayload{},pl)
assert.Equal(t,`[[GiteaContainer](http://localhost:3000/user1/-/packages/container/GiteaContainer/latest)] Package published by [user1](https://try.gitea.io/user1)`,pl.(*MatrixPayload).Body)
assert.Equal(t,`[<a href="http://localhost:3000/user1/-/packages/container/GiteaContainer/latest">GiteaContainer</a>] Package published by <a href="https://try.gitea.io/user1">user1</a>`,pl.(*MatrixPayload).FormattedBody)
assert.Equal(t,`[[GiteaContainer](http://localhost:3000/user1/-/packages/container/GiteaContainer/latest)] Package published by [user1](https://try.gitea.io/user1)`,pl.Body)
assert.Equal(t,`[<a href="http://localhost:3000/user1/-/packages/container/GiteaContainer/latest">GiteaContainer</a>] Package published by <a href="https://try.gitea.io/user1">user1</a>`,pl.FormattedBody)
})
t.Run("Wiki",func(t*testing.T){
p:=wikiTestPayload()
d:=new(MatrixPayload)
p.Action=api.HookWikiCreated
pl,err:=d.Wiki(p)
pl,err:=mc.Wiki(p)
require.NoError(t,err)
require.NotNil(t,pl)
require.IsType(t,&MatrixPayload{},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.(*MatrixPayload).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.(*MatrixPayload).FormattedBody)
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.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.FormattedBody)
p.Action=api.HookWikiEdited
pl,err=d.Wiki(p)
pl,err=mc.Wiki(p)
require.NoError(t,err)
require.NotNil(t,pl)
require.IsType(t,&MatrixPayload{},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.(*MatrixPayload).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.(*MatrixPayload).FormattedBody)
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.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.FormattedBody)
p.Action=api.HookWikiDeleted
pl,err=d.Wiki(p)
pl,err=mc.Wiki(p)
require.NoError(t,err)
require.NotNil(t,pl)
require.IsType(t,&MatrixPayload{},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.(*MatrixPayload).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.(*MatrixPayload).FormattedBody)
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.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.FormattedBody)
})
t.Run("Release",func(t*testing.T){
p:=pullReleaseTestPayload()
d:=new(MatrixPayload)
pl,err:=d.Release(p)
pl,err:=mc.Release(p)
require.NoError(t,err)
require.NotNil(t,pl)
require.IsType(t,&MatrixPayload{},pl)
assert.Equal(t,"[[test/repo](http://localhost:3000/test/repo)] Release created: [v1.0](http://localhost:3000/test/repo/releases/tag/v1.0) by [user1](https://try.gitea.io/user1)",pl.(*MatrixPayload).Body)
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.(*MatrixPayload).FormattedBody)
assert.Equal(t,"[[test/repo](http://localhost:3000/test/repo)] Release created: [v1.0](http://localhost:3000/test/repo/releases/tag/v1.0) by [user1](https://try.gitea.io/user1)",pl.Body)
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.FormattedBody)
assert.Equal(t,"[<http://localhost:3000/test/repo|test/repo>:<http://localhost:3000/test/repo/src/branch/test|test>] branch created by user1",pl.(*SlackPayload).Text)
assert.Equal(t,"[<http://localhost:3000/test/repo|test/repo>:<http://localhost:3000/test/repo/src/branch/test|test>] branch created by user1",pl.Text)
})
t.Run("Delete",func(t*testing.T){
p:=deleteTestPayload()
d:=new(SlackPayload)
pl,err:=d.Delete(p)
pl,err:=sc.Delete(p)
require.NoError(t,err)
require.NotNil(t,pl)
require.IsType(t,&SlackPayload{},pl)
assert.Equal(t,"[<http://localhost:3000/test/repo|test/repo>:test] branch deleted by user1",pl.(*SlackPayload).Text)
assert.Equal(t,"[<http://localhost:3000/test/repo|test/repo>:test] branch deleted by user1",pl.Text)
})
t.Run("Fork",func(t*testing.T){
p:=forkTestPayload()
d:=new(SlackPayload)
pl,err:=d.Fork(p)
pl,err:=sc.Fork(p)
require.NoError(t,err)
require.NotNil(t,pl)
require.IsType(t,&SlackPayload{},pl)
assert.Equal(t,"<http://localhost:3000/test/repo2|test/repo2> is forked to <http://localhost:3000/test/repo|test/repo>",pl.(*SlackPayload).Text)
assert.Equal(t,"<http://localhost:3000/test/repo2|test/repo2> is forked to <http://localhost:3000/test/repo|test/repo>",pl.Text)
})
t.Run("Push",func(t*testing.T){
p:=pushTestPayload()
d:=new(SlackPayload)
pl,err:=d.Push(p)
pl,err:=sc.Push(p)
require.NoError(t,err)
require.NotNil(t,pl)
require.IsType(t,&SlackPayload{},pl)
assert.Equal(t,"[<http://localhost:3000/test/repo|test/repo>:<http://localhost:3000/test/repo/src/branch/test|test>] 2 new commits pushed by user1",pl.(*SlackPayload).Text)
assert.Equal(t,"[<http://localhost:3000/test/repo|test/repo>:<http://localhost:3000/test/repo/src/branch/test|test>] 2 new commits pushed by user1",pl.Text)
})
t.Run("Issue",func(t*testing.T){
p:=issueTestPayload()
d:=new(SlackPayload)
p.Action=api.HookIssueOpened
pl,err:=d.Issue(p)
pl,err:=sc.Issue(p)
require.NoError(t,err)
require.NotNil(t,pl)
require.IsType(t,&SlackPayload{},pl)
assert.Equal(t,"[<http://localhost:3000/test/repo|test/repo>] Issue opened: <http://localhost:3000/test/repo/issues/2|#2 crash> by <https://try.gitea.io/user1|user1>",pl.(*SlackPayload).Text)
assert.Equal(t,"[<http://localhost:3000/test/repo|test/repo>] Issue opened: <http://localhost:3000/test/repo/issues/2|#2 crash> by <https://try.gitea.io/user1|user1>",pl.Text)
p.Action=api.HookIssueClosed
pl,err=d.Issue(p)
pl,err=sc.Issue(p)
require.NoError(t,err)
require.NotNil(t,pl)
require.IsType(t,&SlackPayload{},pl)
assert.Equal(t,"[<http://localhost:3000/test/repo|test/repo>] Issue closed: <http://localhost:3000/test/repo/issues/2|#2 crash> by <https://try.gitea.io/user1|user1>",pl.(*SlackPayload).Text)
assert.Equal(t,"[<http://localhost:3000/test/repo|test/repo>] Issue closed: <http://localhost:3000/test/repo/issues/2|#2 crash> by <https://try.gitea.io/user1|user1>",pl.Text)
})
t.Run("IssueComment",func(t*testing.T){
p:=issueCommentTestPayload()
d:=new(SlackPayload)
pl,err:=d.IssueComment(p)
pl,err:=sc.IssueComment(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 comment on issue <http://localhost:3000/test/repo/issues/2|#2 crash> by <https://try.gitea.io/user1|user1>",pl.(*SlackPayload).Text)
assert.Equal(t,"[<http://localhost:3000/test/repo|test/repo>] New comment on issue <http://localhost:3000/test/repo/issues/2|#2 crash> by <https://try.gitea.io/user1|user1>",pl.Text)
})
t.Run("PullRequest",func(t*testing.T){
p:=pullRequestTestPayload()
d:=new(SlackPayload)
pl,err:=d.PullRequest(p)
pl,err:=sc.PullRequest(p)
require.NoError(t,err)
require.NotNil(t,pl)
require.IsType(t,&SlackPayload{},pl)
assert.Equal(t,"[<http://localhost:3000/test/repo|test/repo>] Pull request opened: <http://localhost:3000/test/repo/pulls/12|#12 Fix bug> by <https://try.gitea.io/user1|user1>",pl.(*SlackPayload).Text)
assert.Equal(t,"[<http://localhost:3000/test/repo|test/repo>] Pull request opened: <http://localhost:3000/test/repo/pulls/12|#12 Fix bug> by <https://try.gitea.io/user1|user1>",pl.Text)
})
t.Run("PullRequestComment",func(t*testing.T){
p:=pullRequestCommentTestPayload()
d:=new(SlackPayload)
pl,err:=d.IssueComment(p)
pl,err:=sc.IssueComment(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 comment on pull request <http://localhost:3000/test/repo/pulls/12|#12 Fix bug> by <https://try.gitea.io/user1|user1>",pl.(*SlackPayload).Text)
assert.Equal(t,"[<http://localhost:3000/test/repo|test/repo>] New comment on pull request <http://localhost:3000/test/repo/pulls/12|#12 Fix bug> by <https://try.gitea.io/user1|user1>",pl.Text)
assert.Equal(t,"[<http://localhost:3000/test/repo|test/repo>] Pull request review approved: [#12 Fix bug](http://localhost:3000/test/repo/pulls/12) by <https://try.gitea.io/user1|user1>",pl.(*SlackPayload).Text)
assert.Equal(t,"[<http://localhost:3000/test/repo|test/repo>] Pull request review approved: [#12 Fix bug](http://localhost:3000/test/repo/pulls/12) by <https://try.gitea.io/user1|user1>",pl.Text)
})
t.Run("Repository",func(t*testing.T){
p:=repositoryTestPayload()
d:=new(SlackPayload)
pl,err:=d.Repository(p)
pl,err:=sc.Repository(p)
require.NoError(t,err)
require.NotNil(t,pl)
require.IsType(t,&SlackPayload{},pl)
assert.Equal(t,"[<http://localhost:3000/test/repo|test/repo>] Repository created by <https://try.gitea.io/user1|user1>",pl.(*SlackPayload).Text)
assert.Equal(t,"[<http://localhost:3000/test/repo|test/repo>] Repository created by <https://try.gitea.io/user1|user1>",pl.Text)
})
t.Run("Package",func(t*testing.T){
p:=packageTestPayload()
d:=new(SlackPayload)
pl,err:=d.Package(p)
pl,err:=sc.Package(p)
require.NoError(t,err)
require.NotNil(t,pl)
require.IsType(t,&SlackPayload{},pl)
assert.Equal(t,"Package created: <http://localhost:3000/user1/-/packages/container/GiteaContainer/latest|GiteaContainer:latest> by <https://try.gitea.io/user1|user1>",pl.(*SlackPayload).Text)
assert.Equal(t,"Package created: <http://localhost:3000/user1/-/packages/container/GiteaContainer/latest|GiteaContainer:latest> by <https://try.gitea.io/user1|user1>",pl.Text)
})
t.Run("Wiki",func(t*testing.T){
p:=wikiTestPayload()
d:=new(SlackPayload)
p.Action=api.HookWikiCreated
pl,err:=d.Wiki(p)
pl,err:=sc.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)
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.Text)
p.Action=api.HookWikiEdited
pl,err=d.Wiki(p)
pl,err=sc.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)
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.Text)
p.Action=api.HookWikiDeleted
pl,err=d.Wiki(p)
pl,err=sc.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)
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.Text)
})
t.Run("Release",func(t*testing.T){
p:=pullReleaseTestPayload()
d:=new(SlackPayload)
pl,err:=d.Release(p)
pl,err:=sc.Release(p)
require.NoError(t,err)
require.NotNil(t,pl)
require.IsType(t,&SlackPayload{},pl)
assert.Equal(t,"[<http://localhost:3000/test/repo|test/repo>] Release created: <http://localhost:3000/test/repo/releases/tag/v1.0|v1.0> by <https://try.gitea.io/user1|user1>",pl.(*SlackPayload).Text)
assert.Equal(t,"[<http://localhost:3000/test/repo|test/repo>] Release created: <http://localhost:3000/test/repo/releases/tag/v1.0|v1.0> by <https://try.gitea.io/user1|user1>",pl.Text)
assert.Equal(t,"[<http://localhost:3000/test/repo|test/repo>:<http://localhost:3000/test/repo/src/branch/test|test>] 2 new commits pushed by user1",body.Text)
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.(*TelegramPayload).Message)
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)
})
t.Run("IssueComment",func(t*testing.T){
p:=issueCommentTestPayload()
d:=new(TelegramPayload)
pl,err:=d.IssueComment(p)
pl,err:=tc.IssueComment(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 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.(*TelegramPayload).Message)
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)
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.(*TelegramPayload).Message)
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)
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.(*TelegramPayload).Message)
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)
})
t.Run("Wiki",func(t*testing.T){
p:=wikiTestPayload()
d:=new(TelegramPayload)
p.Action=api.HookWikiCreated
pl,err:=d.Wiki(p)
pl,err:=tc.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)
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)
p.Action=api.HookWikiEdited
pl,err=d.Wiki(p)
pl,err=tc.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)
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)
p.Action=api.HookWikiDeleted
pl,err=d.Wiki(p)
pl,err=tc.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)
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)
})
t.Run("Release",func(t*testing.T){
p:=pullReleaseTestPayload()
d:=new(TelegramPayload)
pl,err:=d.Release(p)
pl,err:=tc.Release(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>] 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.(*TelegramPayload).Message)
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)