diff options
author | KN4CK3R <admin@oldschoolhack.me> | 2021-06-21 04:12:19 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-20 22:12:19 -0400 |
commit | 4fcae3d06d1099ade682df96e41baf4f5ac56620 (patch) | |
tree | af3608da89c4765711d7d905773336a7ecbd68e1 /services/webhook/slack.go | |
parent | 8601440e81e0e609ab8049fb6dae8307db78fd6c (diff) | |
download | gitea-4fcae3d06d1099ade682df96e41baf4f5ac56620.tar.gz gitea-4fcae3d06d1099ade682df96e41baf4f5ac56620.zip |
Add tests for all webhooks (#16214)
* Added tests for MS Teams.
* Added tests for Dingtalk.
* Added tests for Telegram.
* Added tests for Feishu.
* Added tests for Discord.
* Added tests for closed issue and pullrequest comment.
* Added tests for Matrix.
* Trim all spaces.
* Added tests for Slack.
* Added JSONPayload tests.
* Added general tests.
* Replaced duplicated code.
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'services/webhook/slack.go')
-rw-r--r-- | services/webhook/slack.go | 114 |
1 files changed, 37 insertions, 77 deletions
diff --git a/services/webhook/slack.go b/services/webhook/slack.go index f5c857f2a9..1de9c9c37c 100644 --- a/services/webhook/slack.go +++ b/services/webhook/slack.go @@ -111,12 +111,7 @@ func (s *SlackPayload) Create(p *api.CreatePayload) (api.Payloader, error) { refLink := SlackLinkToRef(p.Repo.HTMLURL, p.Ref) text := fmt.Sprintf("[%s:%s] %s created by %s", repoLink, refLink, p.RefType, p.Sender.UserName) - return &SlackPayload{ - Channel: s.Channel, - Text: text, - Username: s.Username, - IconURL: s.IconURL, - }, nil + return s.createPayload(text, nil), nil } // Delete composes Slack payload for delete a branch or tag. @@ -124,12 +119,8 @@ func (s *SlackPayload) Delete(p *api.DeletePayload) (api.Payloader, error) { refName := git.RefEndName(p.Ref) repoLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.FullName) text := fmt.Sprintf("[%s:%s] %s deleted by %s", repoLink, refName, p.RefType, p.Sender.UserName) - return &SlackPayload{ - Channel: s.Channel, - Text: text, - Username: s.Username, - IconURL: s.IconURL, - }, nil + + return s.createPayload(text, nil), nil } // Fork composes Slack payload for forked by a repository. @@ -137,66 +128,46 @@ func (s *SlackPayload) Fork(p *api.ForkPayload) (api.Payloader, error) { baseLink := SlackLinkFormatter(p.Forkee.HTMLURL, p.Forkee.FullName) forkLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.FullName) text := fmt.Sprintf("%s is forked to %s", baseLink, forkLink) - return &SlackPayload{ - Channel: s.Channel, - Text: text, - Username: s.Username, - IconURL: s.IconURL, - }, nil + + return s.createPayload(text, nil), nil } // Issue implements PayloadConvertor Issue method func (s *SlackPayload) Issue(p *api.IssuePayload) (api.Payloader, error) { text, issueTitle, attachmentText, color := getIssuesPayloadInfo(p, SlackLinkFormatter, true) - pl := &SlackPayload{ - Channel: s.Channel, - Text: text, - Username: s.Username, - IconURL: s.IconURL, - } + var attachments []SlackAttachment if attachmentText != "" { attachmentText = SlackTextFormatter(attachmentText) issueTitle = SlackTextFormatter(issueTitle) - pl.Attachments = []SlackAttachment{{ + attachments = append(attachments, SlackAttachment{ Color: fmt.Sprintf("%x", color), Title: issueTitle, TitleLink: p.Issue.HTMLURL, Text: attachmentText, - }} + }) } - return pl, nil + return s.createPayload(text, attachments), nil } // IssueComment implements PayloadConvertor IssueComment method func (s *SlackPayload) IssueComment(p *api.IssueCommentPayload) (api.Payloader, error) { text, issueTitle, color := getIssueCommentPayloadInfo(p, SlackLinkFormatter, true) - return &SlackPayload{ - Channel: s.Channel, - Text: text, - Username: s.Username, - IconURL: s.IconURL, - Attachments: []SlackAttachment{{ - Color: fmt.Sprintf("%x", color), - Title: issueTitle, - TitleLink: p.Comment.HTMLURL, - Text: SlackTextFormatter(p.Comment.Body), - }}, - }, nil + return s.createPayload(text, []SlackAttachment{{ + Color: fmt.Sprintf("%x", color), + Title: issueTitle, + TitleLink: p.Comment.HTMLURL, + Text: SlackTextFormatter(p.Comment.Body), + }}), nil } // Release implements PayloadConvertor Release method func (s *SlackPayload) Release(p *api.ReleasePayload) (api.Payloader, error) { text, _ := getReleasePayloadInfo(p, SlackLinkFormatter, true) - return &SlackPayload{ - Channel: s.Channel, - Text: text, - Username: s.Username, - IconURL: s.IconURL, - }, nil + return s.createPayload(text, nil), nil } // Push implements PayloadConvertor Push method @@ -232,42 +203,31 @@ func (s *SlackPayload) Push(p *api.PushPayload) (api.Payloader, error) { } } - return &SlackPayload{ - Channel: s.Channel, - Text: text, - Username: s.Username, - IconURL: s.IconURL, - Attachments: []SlackAttachment{{ - Color: s.Color, - Title: p.Repo.HTMLURL, - TitleLink: p.Repo.HTMLURL, - Text: attachmentText, - }}, - }, nil + return s.createPayload(text, []SlackAttachment{{ + Color: s.Color, + Title: p.Repo.HTMLURL, + TitleLink: p.Repo.HTMLURL, + Text: attachmentText, + }}), nil } // PullRequest implements PayloadConvertor PullRequest method func (s *SlackPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, error) { text, issueTitle, attachmentText, color := getPullRequestPayloadInfo(p, SlackLinkFormatter, true) - pl := &SlackPayload{ - Channel: s.Channel, - Text: text, - Username: s.Username, - IconURL: s.IconURL, - } + var attachments []SlackAttachment if attachmentText != "" { attachmentText = SlackTextFormatter(p.PullRequest.Body) issueTitle = SlackTextFormatter(issueTitle) - pl.Attachments = []SlackAttachment{{ + attachments = append(attachments, SlackAttachment{ Color: fmt.Sprintf("%x", color), Title: issueTitle, TitleLink: p.PullRequest.URL, Text: attachmentText, - }} + }) } - return pl, nil + return s.createPayload(text, attachments), nil } // Review implements PayloadConvertor Review method @@ -288,12 +248,7 @@ func (s *SlackPayload) Review(p *api.PullRequestPayload, event models.HookEventT text = fmt.Sprintf("[%s] Pull request review %s: [%s](%s) by %s", repoLink, action, title, titleLink, senderLink) } - return &SlackPayload{ - Channel: s.Channel, - Text: text, - Username: s.Username, - IconURL: s.IconURL, - }, nil + return s.createPayload(text, nil), nil } // Repository implements PayloadConvertor Repository method @@ -309,12 +264,17 @@ func (s *SlackPayload) Repository(p *api.RepositoryPayload) (api.Payloader, erro text = fmt.Sprintf("[%s] Repository deleted by %s", repoLink, senderLink) } + return s.createPayload(text, nil), nil +} + +func (s *SlackPayload) createPayload(text string, attachments []SlackAttachment) *SlackPayload { return &SlackPayload{ - Channel: s.Channel, - Text: text, - Username: s.Username, - IconURL: s.IconURL, - }, nil + Channel: s.Channel, + Text: text, + Username: s.Username, + IconURL: s.IconURL, + Attachments: attachments, + } } // GetSlackPayload converts a slack webhook into a SlackPayload |