diff options
author | Cornel <cornelk@users.noreply.github.com> | 2019-12-28 16:55:09 +0800 |
---|---|---|
committer | zeripath <art27@cantab.net> | 2019-12-28 08:55:09 +0000 |
commit | 81e63d0714f1539fcd688cf3e216b0eab89e88aa (patch) | |
tree | cddd201be48da86ba309e7d794f8c6becf3c1b05 /modules/webhook/slack.go | |
parent | 10455a88dc28c00f86587266a1ecd736bea63d03 (diff) | |
download | gitea-81e63d0714f1539fcd688cf3e216b0eab89e88aa.tar.gz gitea-81e63d0714f1539fcd688cf3e216b0eab89e88aa.zip |
Refactor webhooks to reduce code duplication (#9422)
* Start webhook refactoring to reduce code duplication
* More webhook refactoring
* Unify webhook release messages
* Fix webhook release link
* Remove sql import
* More webhook refactoring
* More webhook refactoring
* Webhook tests extended
* Fixed issue opened webhook
Co-authored-by: Lauris BH <lauris@nix.lv>
Co-authored-by: zeripath <art27@cantab.net>
Co-authored-by: techknowlogick <matti@mdranta.net>
Diffstat (limited to 'modules/webhook/slack.go')
-rw-r--r-- | modules/webhook/slack.go | 139 |
1 files changed, 18 insertions, 121 deletions
diff --git a/modules/webhook/slack.go b/modules/webhook/slack.go index 41872f940f..508cb13b8f 100644 --- a/modules/webhook/slack.go +++ b/modules/webhook/slack.go @@ -144,42 +144,7 @@ func getSlackForkPayload(p *api.ForkPayload, slack *SlackMeta) (*SlackPayload, e } func getSlackIssuesPayload(p *api.IssuePayload, slack *SlackMeta) (*SlackPayload, error) { - senderLink := SlackLinkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName) - title := SlackTextFormatter(fmt.Sprintf("#%d %s", p.Index, p.Issue.Title)) - titleLink := fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Index) - repoLink := SlackLinkFormatter(p.Repository.HTMLURL, p.Repository.FullName) - var text, attachmentText string - - switch p.Action { - case api.HookIssueOpened: - text = fmt.Sprintf("[%s] Issue opened by %s", repoLink, senderLink) - attachmentText = SlackTextFormatter(p.Issue.Body) - case api.HookIssueClosed: - text = fmt.Sprintf("[%s] Issue closed: [%s](%s) by %s", repoLink, title, titleLink, senderLink) - case api.HookIssueReOpened: - text = fmt.Sprintf("[%s] Issue re-opened: [%s](%s) by %s", repoLink, title, titleLink, senderLink) - case api.HookIssueEdited: - text = fmt.Sprintf("[%s] Issue edited: [%s](%s) by %s", repoLink, title, titleLink, senderLink) - attachmentText = SlackTextFormatter(p.Issue.Body) - case api.HookIssueAssigned: - text = fmt.Sprintf("[%s] Issue assigned to %s: [%s](%s) by %s", repoLink, - SlackLinkFormatter(setting.AppURL+p.Issue.Assignee.UserName, p.Issue.Assignee.UserName), - title, titleLink, senderLink) - case api.HookIssueUnassigned: - text = fmt.Sprintf("[%s] Issue unassigned: [%s](%s) by %s", repoLink, title, titleLink, senderLink) - case api.HookIssueLabelUpdated: - text = fmt.Sprintf("[%s] Issue labels updated: [%s](%s) by %s", repoLink, title, titleLink, senderLink) - case api.HookIssueLabelCleared: - text = fmt.Sprintf("[%s] Issue labels cleared: [%s](%s) by %s", repoLink, title, titleLink, senderLink) - case api.HookIssueSynchronized: - text = fmt.Sprintf("[%s] Issue synchronized: [%s](%s) by %s", repoLink, title, titleLink, senderLink) - case api.HookIssueMilestoned: - mileStoneLink := fmt.Sprintf("%s/milestone/%d", p.Repository.HTMLURL, p.Issue.Milestone.ID) - text = fmt.Sprintf("[%s] Issue milestoned to [%s](%s): [%s](%s) by %s", repoLink, - p.Issue.Milestone.Title, mileStoneLink, title, titleLink, senderLink) - case api.HookIssueDemilestoned: - text = fmt.Sprintf("[%s] Issue milestone cleared: [%s](%s) by %s", repoLink, title, titleLink, senderLink) - } + text, issueTitle, attachmentText, color := getIssuesPayloadInfo(p, SlackLinkFormatter) pl := &SlackPayload{ Channel: slack.Channel, @@ -188,10 +153,12 @@ func getSlackIssuesPayload(p *api.IssuePayload, slack *SlackMeta) (*SlackPayload IconURL: slack.IconURL, } if attachmentText != "" { + attachmentText = SlackTextFormatter(attachmentText) + issueTitle = SlackTextFormatter(issueTitle) pl.Attachments = []SlackAttachment{{ - Color: slack.Color, - Title: title, - TitleLink: titleLink, + Color: fmt.Sprintf("%x", color), + Title: issueTitle, + TitleLink: p.Issue.URL, Text: attachmentText, }} } @@ -200,25 +167,7 @@ func getSlackIssuesPayload(p *api.IssuePayload, slack *SlackMeta) (*SlackPayload } func getSlackIssueCommentPayload(p *api.IssueCommentPayload, slack *SlackMeta) (*SlackPayload, error) { - senderLink := SlackLinkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName) - title := SlackTextFormatter(fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title)) - repoLink := SlackLinkFormatter(p.Repository.HTMLURL, p.Repository.FullName) - var text, titleLink, attachmentText string - - switch p.Action { - case api.HookIssueCommentCreated: - text = fmt.Sprintf("[%s] New comment created by %s", repoLink, senderLink) - titleLink = fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, models.CommentHashTag(p.Comment.ID)) - attachmentText = SlackTextFormatter(p.Comment.Body) - case api.HookIssueCommentEdited: - text = fmt.Sprintf("[%s] Comment edited by %s", repoLink, senderLink) - titleLink = fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, models.CommentHashTag(p.Comment.ID)) - attachmentText = SlackTextFormatter(p.Comment.Body) - case api.HookIssueCommentDeleted: - text = fmt.Sprintf("[%s] Comment deleted by %s", repoLink, senderLink) - titleLink = fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index) - attachmentText = SlackTextFormatter(p.Comment.Body) - } + text, issueTitle, color := getIssueCommentPayloadInfo(p, SlackLinkFormatter) return &SlackPayload{ Channel: slack.Channel, @@ -226,27 +175,16 @@ func getSlackIssueCommentPayload(p *api.IssueCommentPayload, slack *SlackMeta) ( Username: slack.Username, IconURL: slack.IconURL, Attachments: []SlackAttachment{{ - Color: slack.Color, - Title: title, - TitleLink: titleLink, - Text: attachmentText, + Color: fmt.Sprintf("%x", color), + Title: issueTitle, + TitleLink: p.Comment.HTMLURL, + Text: SlackTextFormatter(p.Comment.Body), }}, }, nil } func getSlackReleasePayload(p *api.ReleasePayload, slack *SlackMeta) (*SlackPayload, error) { - repoLink := SlackLinkFormatter(p.Repository.HTMLURL, p.Repository.FullName) - refLink := SlackLinkFormatter(p.Repository.HTMLURL+"/src/"+p.Release.TagName, p.Release.TagName) - var text string - - switch p.Action { - case api.HookReleasePublished: - text = fmt.Sprintf("[%s] new release %s published by %s", repoLink, refLink, p.Sender.UserName) - case api.HookReleaseUpdated: - text = fmt.Sprintf("[%s] new release %s updated by %s", repoLink, refLink, p.Sender.UserName) - case api.HookReleaseDeleted: - text = fmt.Sprintf("[%s] new release %s deleted by %s", repoLink, refLink, p.Sender.UserName) - } + text, _ := getReleasePayloadInfo(p, SlackLinkFormatter) return &SlackPayload{ Channel: slack.Channel, @@ -301,50 +239,7 @@ func getSlackPushPayload(p *api.PushPayload, slack *SlackMeta) (*SlackPayload, e } func getSlackPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) (*SlackPayload, error) { - senderLink := SlackLinkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName) - title := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title) - titleLink := fmt.Sprintf("%s/pulls/%d", p.Repository.HTMLURL, p.Index) - repoLink := SlackLinkFormatter(p.Repository.HTMLURL, p.Repository.FullName) - var text, attachmentText string - - switch p.Action { - case api.HookIssueOpened: - text = fmt.Sprintf("[%s] Pull request opened by %s", repoLink, senderLink) - attachmentText = SlackTextFormatter(p.PullRequest.Body) - case api.HookIssueClosed: - if p.PullRequest.HasMerged { - text = fmt.Sprintf("[%s] Pull request merged: [%s](%s) by %s", repoLink, title, titleLink, senderLink) - } else { - text = fmt.Sprintf("[%s] Pull request closed: [%s](%s) by %s", repoLink, title, titleLink, senderLink) - } - case api.HookIssueReOpened: - text = fmt.Sprintf("[%s] Pull request re-opened: [%s](%s) by %s", repoLink, title, titleLink, senderLink) - case api.HookIssueEdited: - text = fmt.Sprintf("[%s] Pull request edited: [%s](%s) by %s", repoLink, title, titleLink, senderLink) - attachmentText = SlackTextFormatter(p.PullRequest.Body) - case api.HookIssueAssigned: - list := make([]string, len(p.PullRequest.Assignees)) - for i, user := range p.PullRequest.Assignees { - list[i] = SlackLinkFormatter(setting.AppURL+user.UserName, user.UserName) - } - text = fmt.Sprintf("[%s] Pull request assigned to %s: [%s](%s) by %s", repoLink, - strings.Join(list, ", "), - title, titleLink, senderLink) - case api.HookIssueUnassigned: - text = fmt.Sprintf("[%s] Pull request unassigned: [%s](%s) by %s", repoLink, title, titleLink, senderLink) - case api.HookIssueLabelUpdated: - text = fmt.Sprintf("[%s] Pull request labels updated: [%s](%s) by %s", repoLink, title, titleLink, senderLink) - case api.HookIssueLabelCleared: - text = fmt.Sprintf("[%s] Pull request labels cleared: [%s](%s) by %s", repoLink, title, titleLink, senderLink) - case api.HookIssueSynchronized: - text = fmt.Sprintf("[%s] Pull request synchronized: [%s](%s) by %s", repoLink, title, titleLink, senderLink) - case api.HookIssueMilestoned: - mileStoneLink := fmt.Sprintf("%s/milestone/%d", p.Repository.HTMLURL, p.PullRequest.Milestone.ID) - text = fmt.Sprintf("[%s] Pull request milestoned to [%s](%s): [%s](%s) %s", repoLink, - p.PullRequest.Milestone.Title, mileStoneLink, title, titleLink, senderLink) - case api.HookIssueDemilestoned: - text = fmt.Sprintf("[%s] Pull request milestone cleared: [%s](%s) %s", repoLink, title, titleLink, senderLink) - } + text, issueTitle, attachmentText, color := getPullRequestPayloadInfo(p, SlackLinkFormatter) pl := &SlackPayload{ Channel: slack.Channel, @@ -353,10 +248,12 @@ func getSlackPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) (*S IconURL: slack.IconURL, } if attachmentText != "" { + attachmentText = SlackTextFormatter(p.PullRequest.Body) + issueTitle = SlackTextFormatter(issueTitle) pl.Attachments = []SlackAttachment{{ - Color: slack.Color, - Title: title, - TitleLink: titleLink, + Color: fmt.Sprintf("%x", color), + Title: issueTitle, + TitleLink: p.PullRequest.URL, Text: attachmentText, }} } |