aboutsummaryrefslogtreecommitdiffstats
path: root/services/webhook/discord.go
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2024-07-10 19:37:16 +0800
committerGitHub <noreply@github.com>2024-07-10 11:37:16 +0000
commit72b6bc8cafe1d64ff30481170cd68b85a710c67e (patch)
tree1156e8b92053cb588863ee1ea05db3e804a0831f /services/webhook/discord.go
parent4ea2a6de81ab6adde436d77e2743095fafa6a7a9 (diff)
downloadgitea-72b6bc8cafe1d64ff30481170cd68b85a710c67e.tar.gz
gitea-72b6bc8cafe1d64ff30481170cd68b85a710c67e.zip
Refactor webhook (#31587)
A more complete fix for #31588 1. Make "generic" code more readable 2. Clarify HTML or Markdown for the payload content
Diffstat (limited to 'services/webhook/discord.go')
-rw-r--r--services/webhook/discord.go24
1 files changed, 11 insertions, 13 deletions
diff --git a/services/webhook/discord.go b/services/webhook/discord.go
index 31332396f2..f27b39dac3 100644
--- a/services/webhook/discord.go
+++ b/services/webhook/discord.go
@@ -100,6 +100,11 @@ var (
redColor = color("ff3232")
)
+type discordConvertor struct {
+ Username string
+ AvatarURL string
+}
+
// Create implements PayloadConvertor Create method
func (d discordConvertor) Create(p *api.CreatePayload) (DiscordPayload, error) {
// created tag/branch
@@ -162,9 +167,9 @@ func (d discordConvertor) Push(p *api.PushPayload) (DiscordPayload, error) {
// Issue implements PayloadConvertor Issue method
func (d discordConvertor) Issue(p *api.IssuePayload) (DiscordPayload, error) {
- title, _, text, color := getIssuesPayloadInfo(p, noneLinkFormatter, false)
+ title, _, extraMarkdown, color := getIssuesPayloadInfo(p, noneLinkFormatter, false)
- return d.createPayload(p.Sender, title, text, p.Issue.HTMLURL, color), nil
+ return d.createPayload(p.Sender, title, extraMarkdown, p.Issue.HTMLURL, color), nil
}
// IssueComment implements PayloadConvertor IssueComment method
@@ -176,9 +181,9 @@ func (d discordConvertor) IssueComment(p *api.IssueCommentPayload) (DiscordPaylo
// PullRequest implements PayloadConvertor PullRequest method
func (d discordConvertor) PullRequest(p *api.PullRequestPayload) (DiscordPayload, error) {
- title, _, text, color := getPullRequestPayloadInfo(p, noneLinkFormatter, false)
+ title, _, extraMarkdown, color := getPullRequestPayloadInfo(p, noneLinkFormatter, false)
- return d.createPayload(p.Sender, title, text, p.PullRequest.HTMLURL, color), nil
+ return d.createPayload(p.Sender, title, extraMarkdown, p.PullRequest.HTMLURL, color), nil
}
// Review implements PayloadConvertor Review method
@@ -253,23 +258,16 @@ func (d discordConvertor) Package(p *api.PackagePayload) (DiscordPayload, error)
return d.createPayload(p.Sender, text, "", p.Package.HTMLURL, color), nil
}
-type discordConvertor struct {
- Username string
- AvatarURL string
-}
-
-var _ payloadConvertor[DiscordPayload] = discordConvertor{}
-
func newDiscordRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
meta := &DiscordMeta{}
if err := json.Unmarshal([]byte(w.Meta), meta); err != nil {
return nil, nil, fmt.Errorf("newDiscordRequest meta json: %w", err)
}
- sc := discordConvertor{
+ var pc payloadConvertor[DiscordPayload] = discordConvertor{
Username: meta.Username,
AvatarURL: meta.IconURL,
}
- return newJSONRequest(sc, w, t, true)
+ return newJSONRequest(pc, w, t, true)
}
func parseHookPullRequestEventType(event webhook_module.HookEventType) (string, error) {