From 3df1cd326cca61e166906a683879e422f1fbf430 Mon Sep 17 00:00:00 2001 From: Gitea Date: Thu, 19 Dec 2019 11:07:55 +0800 Subject: [PATCH 1/9] Start webhook refactoring to reduce code duplication --- modules/webhook/discord.go | 51 +++------------ modules/webhook/general.go | 109 +++++++++++++++++++++++++++++++ modules/webhook/msteams.go | 53 +++------------ modules/webhook/slack.go | 90 ++++--------------------- modules/webhook/slack_test.go | 44 +++++++++++++ modules/webhook/telegram.go | 97 ++++----------------------- modules/webhook/telegram_test.go | 41 ++++++++++++ 7 files changed, 235 insertions(+), 250 deletions(-) create mode 100644 modules/webhook/general.go create mode 100644 modules/webhook/slack_test.go create mode 100644 modules/webhook/telegram_test.go diff --git a/modules/webhook/discord.go b/modules/webhook/discord.go index 99444851bc1b9..bf505bafc2592 100644 --- a/modules/webhook/discord.go +++ b/modules/webhook/discord.go @@ -227,46 +227,11 @@ func getDiscordPushPayload(p *api.PushPayload, meta *DiscordMeta) (*DiscordPaylo } func getDiscordIssuesPayload(p *api.IssuePayload, meta *DiscordMeta) (*DiscordPayload, error) { - var text, title string - var color int - url := fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index) - switch p.Action { - case api.HookIssueOpened: - title = fmt.Sprintf("[%s] Issue opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - text = p.Issue.Body - color = orangeColor - case api.HookIssueClosed: - title = fmt.Sprintf("[%s] Issue closed: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - color = redColor - case api.HookIssueReOpened: - title = fmt.Sprintf("[%s] Issue re-opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - color = yellowColor - case api.HookIssueEdited: - title = fmt.Sprintf("[%s] Issue edited: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - text = p.Issue.Body - color = yellowColor - case api.HookIssueAssigned: - title = fmt.Sprintf("[%s] Issue assigned to %s: #%d %s", p.Repository.FullName, - p.Issue.Assignee.UserName, p.Index, p.Issue.Title) - color = greenColor - case api.HookIssueUnassigned: - title = fmt.Sprintf("[%s] Issue unassigned: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - color = yellowColor - case api.HookIssueLabelUpdated: - title = fmt.Sprintf("[%s] Issue labels updated: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - color = yellowColor - case api.HookIssueLabelCleared: - title = fmt.Sprintf("[%s] Issue labels cleared: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - color = yellowColor - case api.HookIssueSynchronized: - title = fmt.Sprintf("[%s] Issue synchronized: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - color = yellowColor - case api.HookIssueMilestoned: - title = fmt.Sprintf("[%s] Issue milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - color = yellowColor - case api.HookIssueDemilestoned: - title = fmt.Sprintf("[%s] Issue clear milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - color = yellowColor + text, _, color := getIssuesPayloadInfo(p, noneLinkFormatter) + + var attachmentText string + if p.Action == api.HookIssueOpened || p.Action == api.HookIssueEdited { + attachmentText = p.Issue.Body } return &DiscordPayload{ @@ -274,9 +239,9 @@ func getDiscordIssuesPayload(p *api.IssuePayload, meta *DiscordMeta) (*DiscordPa AvatarURL: meta.IconURL, Embeds: []DiscordEmbed{ { - Title: title, - Description: text, - URL: url, + Title: text, + Description: attachmentText, + URL: p.Issue.URL, Color: color, Author: DiscordEmbedAuthor{ Name: p.Sender.UserName, diff --git a/modules/webhook/general.go b/modules/webhook/general.go new file mode 100644 index 0000000000000..8ea6b2169cf37 --- /dev/null +++ b/modules/webhook/general.go @@ -0,0 +1,109 @@ +package webhook + +import ( + "fmt" + "strings" + + "code.gitea.io/gitea/modules/setting" + api "code.gitea.io/gitea/modules/structs" +) + +type linkFormatter = func(string, string) string + +func noneLinkFormatter(url string, text string) string { + return text +} + +func htmlLinkFormatter(url string, text string) string { + return fmt.Sprintf(`%s`, url, text) +} + +func getIssuesPayloadInfo(p *api.IssuePayload, linkFormatter linkFormatter) (string, string, int) { + senderLink := linkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName) + repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName) + issueTitle := fmt.Sprintf("#%d %s", p.Index, p.Issue.Title) + titleLink := linkFormatter(p.Issue.URL, issueTitle) + var text string + color := yellowColor + + switch p.Action { + case api.HookIssueOpened: + text = fmt.Sprintf("[%s] Issue opened by %s", repoLink, senderLink) + color = orangeColor + case api.HookIssueClosed: + text = fmt.Sprintf("[%s] Issue closed: %s by %s", repoLink, titleLink, senderLink) + color = redColor + case api.HookIssueReOpened: + text = fmt.Sprintf("[%s] Issue re-opened: %s by %s", repoLink, titleLink, senderLink) + case api.HookIssueEdited: + text = fmt.Sprintf("[%s] Issue edited: %s by %s", repoLink, titleLink, senderLink) + case api.HookIssueAssigned: + text = fmt.Sprintf("[%s] Issue assigned to %s: %s by %s", repoLink, + linkFormatter(setting.AppURL+p.Issue.Assignee.UserName, p.Issue.Assignee.UserName), + titleLink, senderLink) + color = greenColor + case api.HookIssueUnassigned: + text = fmt.Sprintf("[%s] Issue unassigned: %s by %s", repoLink, titleLink, senderLink) + case api.HookIssueLabelUpdated: + text = fmt.Sprintf("[%s] Issue labels updated: %s by %s", repoLink, titleLink, senderLink) + case api.HookIssueLabelCleared: + text = fmt.Sprintf("[%s] Issue labels cleared: %s by %s", repoLink, titleLink, senderLink) + case api.HookIssueSynchronized: + text = fmt.Sprintf("[%s] Issue synchronized: %s by %s", repoLink, 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 by %s", repoLink, + linkFormatter(mileStoneLink, p.Issue.Milestone.Title), titleLink, senderLink) + case api.HookIssueDemilestoned: + text = fmt.Sprintf("[%s] Issue milestone cleared: %s by %s", repoLink, titleLink, senderLink) + } + + return text, issueTitle, color +} + +func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkFormatter) (string, string) { + senderLink := linkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName) + repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName) + issueTitle := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title) + titleLink := linkFormatter(p.PullRequest.URL, issueTitle) + var text string + + switch p.Action { + case api.HookIssueOpened: + text = fmt.Sprintf("[%s] Pull request opened by %s", repoLink, senderLink) + case api.HookIssueClosed: + if p.PullRequest.HasMerged { + text = fmt.Sprintf("[%s] Pull request merged: %s by %s", repoLink, titleLink, senderLink) + } else { + text = fmt.Sprintf("[%s] Pull request closed: %s by %s", repoLink, titleLink, senderLink) + } + case api.HookIssueReOpened: + text = fmt.Sprintf("[%s] Pull request re-opened: %s by %s", repoLink, titleLink, senderLink) + case api.HookIssueEdited: + text = fmt.Sprintf("[%s] Pull request edited: %s by %s", repoLink, titleLink, senderLink) + case api.HookIssueAssigned: + list := make([]string, len(p.PullRequest.Assignees)) + for i, user := range p.PullRequest.Assignees { + list[i] = linkFormatter(setting.AppURL+user.UserName, user.UserName) + } + text = fmt.Sprintf("[%s] Pull request assigned to %s: %s by %s", repoLink, + strings.Join(list, ", "), + titleLink, senderLink) + case api.HookIssueUnassigned: + text = fmt.Sprintf("[%s] Pull request unassigned: %s by %s", repoLink, titleLink, senderLink) + case api.HookIssueLabelUpdated: + text = fmt.Sprintf("[%s] Pull request labels updated: %s by %s", repoLink, titleLink, senderLink) + case api.HookIssueLabelCleared: + text = fmt.Sprintf("[%s] Pull request labels cleared: %s by %s", repoLink, titleLink, senderLink) + case api.HookIssueSynchronized: + text = fmt.Sprintf("[%s] Pull request synchronized: %s by %s", repoLink, 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 by %s", repoLink, + linkFormatter(mileStoneLink, p.PullRequest.Milestone.Title), titleLink, senderLink) + case api.HookIssueDemilestoned: + text = fmt.Sprintf("[%s] Pull request milestone cleared: %s %s", repoLink, titleLink, senderLink) + } + + return text, issueTitle +} diff --git a/modules/webhook/msteams.go b/modules/webhook/msteams.go index 8ff9bb5ba452c..4910ec3e5b672 100644 --- a/modules/webhook/msteams.go +++ b/modules/webhook/msteams.go @@ -266,60 +266,25 @@ func getMSTeamsPushPayload(p *api.PushPayload) (*MSTeamsPayload, error) { } func getMSTeamsIssuesPayload(p *api.IssuePayload) (*MSTeamsPayload, error) { - var text, title string - var color int - url := fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index) - switch p.Action { - case api.HookIssueOpened: - title = fmt.Sprintf("[%s] Issue opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - text = p.Issue.Body - color = orangeColor - case api.HookIssueClosed: - title = fmt.Sprintf("[%s] Issue closed: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - color = redColor - case api.HookIssueReOpened: - title = fmt.Sprintf("[%s] Issue re-opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - color = yellowColor - case api.HookIssueEdited: - title = fmt.Sprintf("[%s] Issue edited: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - text = p.Issue.Body - color = yellowColor - case api.HookIssueAssigned: - title = fmt.Sprintf("[%s] Issue assigned to %s: #%d %s", p.Repository.FullName, - p.Issue.Assignee.UserName, p.Index, p.Issue.Title) - color = greenColor - case api.HookIssueUnassigned: - title = fmt.Sprintf("[%s] Issue unassigned: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - color = yellowColor - case api.HookIssueLabelUpdated: - title = fmt.Sprintf("[%s] Issue labels updated: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - color = yellowColor - case api.HookIssueLabelCleared: - title = fmt.Sprintf("[%s] Issue labels cleared: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - color = yellowColor - case api.HookIssueSynchronized: - title = fmt.Sprintf("[%s] Issue synchronized: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - color = yellowColor - case api.HookIssueMilestoned: - title = fmt.Sprintf("[%s] Issue milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - color = yellowColor - case api.HookIssueDemilestoned: - title = fmt.Sprintf("[%s] Issue clear milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - color = yellowColor + text, _, color := getIssuesPayloadInfo(p, noneLinkFormatter) + + var attachmentText string + if p.Action == api.HookIssueOpened || p.Action == api.HookIssueEdited { + attachmentText = p.Issue.Body } return &MSTeamsPayload{ Type: "MessageCard", Context: "https://schema.org/extensions", ThemeColor: fmt.Sprintf("%x", color), - Title: title, - Summary: title, + Title: text, + Summary: text, Sections: []MSTeamsSection{ { ActivityTitle: p.Sender.FullName, ActivitySubtitle: p.Sender.UserName, ActivityImage: p.Sender.AvatarURL, - Text: text, + Text: attachmentText, Facts: []MSTeamsFact{ { Name: "Repository:", @@ -339,7 +304,7 @@ func getMSTeamsIssuesPayload(p *api.IssuePayload) (*MSTeamsPayload, error) { Targets: []MSTeamsActionTarget{ { Os: "default", - URI: url, + URI: p.Issue.URL, }, }, }, diff --git a/modules/webhook/slack.go b/modules/webhook/slack.go index 41872f940fe5f..e380d970c52e4 100644 --- a/modules/webhook/slack.go +++ b/modules/webhook/slack.go @@ -144,41 +144,11 @@ 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 + text, issueTitle, _ := getIssuesPayloadInfo(p, SlackLinkFormatter) - 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) + var attachmentText string + if p.Action == api.HookIssueOpened || p.Action == api.HookIssueEdited { 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) } pl := &SlackPayload{ @@ -188,10 +158,11 @@ func getSlackIssuesPayload(p *api.IssuePayload, slack *SlackMeta) (*SlackPayload IconURL: slack.IconURL, } if attachmentText != "" { + issueTitle = SlackTextFormatter(issueTitle) pl.Attachments = []SlackAttachment{{ Color: slack.Color, - Title: title, - TitleLink: titleLink, + Title: issueTitle, + TitleLink: p.Issue.URL, Text: attachmentText, }} } @@ -301,49 +272,11 @@ 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 + text, issueTitle := getPullRequestPayloadInfo(p, SlackLinkFormatter) - 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) + var attachmentText string + if p.Action == api.HookIssueOpened || p.Action == api.HookIssueEdited { 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) } pl := &SlackPayload{ @@ -353,10 +286,11 @@ func getSlackPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) (*S IconURL: slack.IconURL, } if attachmentText != "" { + issueTitle = SlackTextFormatter(issueTitle) pl.Attachments = []SlackAttachment{{ Color: slack.Color, - Title: title, - TitleLink: titleLink, + Title: issueTitle, + TitleLink: p.PullRequest.URL, Text: attachmentText, }} } diff --git a/modules/webhook/slack_test.go b/modules/webhook/slack_test.go new file mode 100644 index 0000000000000..58abd312cdbaa --- /dev/null +++ b/modules/webhook/slack_test.go @@ -0,0 +1,44 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package webhook + +import ( + "testing" + + api "code.gitea.io/gitea/modules/structs" + _ "github.com/mattn/go-sqlite3" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestSlackIssuesPayload(t *testing.T) { + p := &api.IssuePayload{ + Index: 1, + Action: api.HookIssueClosed, + Sender: &api.User{ + UserName: "user1", + }, + Repository: &api.Repository{ + HTMLURL: "http://localhost:3000/test/repo", + Name: "repo", + FullName: "test/repo", + }, + Issue: &api.Issue{ + ID: 2, + URL: "http://localhost:3000/api/v1/repos/test/repo/issues/2", + Title: "crash", + }, + } + + sl := &SlackMeta{ + Username: p.Sender.UserName, + } + + pl, err := getSlackIssuesPayload(p, sl) + require.Nil(t, err) + assert.NotNil(t, pl) + + assert.Equal(t, "[] Issue closed: by ", pl.Text) +} diff --git a/modules/webhook/telegram.go b/modules/webhook/telegram.go index d95ee0f73fed1..49e88eee2046f 100644 --- a/modules/webhook/telegram.go +++ b/modules/webhook/telegram.go @@ -126,47 +126,15 @@ func getTelegramPushPayload(p *api.PushPayload) (*TelegramPayload, error) { } func getTelegramIssuesPayload(p *api.IssuePayload) (*TelegramPayload, error) { - var text, title string - switch p.Action { - case api.HookIssueOpened: - title = fmt.Sprintf(`[%s] Issue opened: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, - p.Issue.URL, p.Index, p.Issue.Title) - text = p.Issue.Body - case api.HookIssueClosed: - title = fmt.Sprintf(`[%s] Issue closed: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, - p.Issue.URL, p.Index, p.Issue.Title) - case api.HookIssueReOpened: - title = fmt.Sprintf(`[%s] Issue re-opened: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, - p.Issue.URL, p.Index, p.Issue.Title) - case api.HookIssueEdited: - title = fmt.Sprintf(`[%s] Issue edited: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, - p.Issue.URL, p.Index, p.Issue.Title) - text = p.Issue.Body - case api.HookIssueAssigned: - title = fmt.Sprintf(`[%s] Issue assigned to %s: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, - p.Issue.Assignee.UserName, p.Issue.URL, p.Index, p.Issue.Title) - case api.HookIssueUnassigned: - title = fmt.Sprintf(`[%s] Issue unassigned: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, - p.Issue.URL, p.Index, p.Issue.Title) - case api.HookIssueLabelUpdated: - title = fmt.Sprintf(`[%s] Issue labels updated: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, - p.Issue.URL, p.Index, p.Issue.Title) - case api.HookIssueLabelCleared: - title = fmt.Sprintf(`[%s] Issue labels cleared: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, - p.Issue.URL, p.Index, p.Issue.Title) - case api.HookIssueSynchronized: - title = fmt.Sprintf(`[%s] Issue synchronized: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, - p.Issue.URL, p.Index, p.Issue.Title) - case api.HookIssueMilestoned: - title = fmt.Sprintf(`[%s] Issue milestone: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, - p.Issue.URL, p.Index, p.Issue.Title) - case api.HookIssueDemilestoned: - title = fmt.Sprintf(`[%s] Issue clear milestone: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, - p.Issue.URL, p.Index, p.Issue.Title) + text, _, _ := getIssuesPayloadInfo(p, htmlLinkFormatter) + + var attachmentText string + if p.Action == api.HookIssueOpened || p.Action == api.HookIssueEdited { + attachmentText = p.Issue.Body } return &TelegramPayload{ - Message: title + "\n\n" + text, + Message: text + "\n\n" + attachmentText, }, nil } @@ -192,56 +160,15 @@ func getTelegramIssueCommentPayload(p *api.IssueCommentPayload) (*TelegramPayloa } func getTelegramPullRequestPayload(p *api.PullRequestPayload) (*TelegramPayload, error) { - var text, title string - switch p.Action { - case api.HookIssueOpened: - title = fmt.Sprintf(`[%s] Pull request opened: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, - p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) - text = p.PullRequest.Body - case api.HookIssueClosed: - if p.PullRequest.HasMerged { - title = fmt.Sprintf(`[%s] Pull request merged: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, - p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) - } else { - title = fmt.Sprintf(`[%s] Pull request closed: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, - p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) - } - case api.HookIssueReOpened: - title = fmt.Sprintf(`[%s] Pull request re-opened: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, - p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) - case api.HookIssueEdited: - title = fmt.Sprintf(`[%s] Pull request edited: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, - p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) - text = p.PullRequest.Body - case api.HookIssueAssigned: - list, err := models.MakeAssigneeList(&models.Issue{ID: p.PullRequest.ID}) - if err != nil { - return &TelegramPayload{}, err - } - title = fmt.Sprintf(`[%s] Pull request assigned to %s: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, - list, p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) - case api.HookIssueUnassigned: - title = fmt.Sprintf(`[%s] Pull request unassigned: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, - p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) - case api.HookIssueLabelUpdated: - title = fmt.Sprintf(`[%s] Pull request labels updated: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, - p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) - case api.HookIssueLabelCleared: - title = fmt.Sprintf(`[%s] Pull request labels cleared: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, - p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) - case api.HookIssueSynchronized: - title = fmt.Sprintf(`[%s] Pull request synchronized: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, - p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) - case api.HookIssueMilestoned: - title = fmt.Sprintf(`[%s] Pull request milestone: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, - p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) - case api.HookIssueDemilestoned: - title = fmt.Sprintf(`[%s] Pull request clear milestone: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, - p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) + text, _ := getPullRequestPayloadInfo(p, htmlLinkFormatter) + + var attachmentText string + if p.Action == api.HookIssueOpened || p.Action == api.HookIssueEdited { + attachmentText = p.PullRequest.Body } return &TelegramPayload{ - Message: title + "\n" + text, + Message: text + "\n" + attachmentText, }, nil } diff --git a/modules/webhook/telegram_test.go b/modules/webhook/telegram_test.go new file mode 100644 index 0000000000000..dc0e2fdbad5fb --- /dev/null +++ b/modules/webhook/telegram_test.go @@ -0,0 +1,41 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package webhook + +import ( + "testing" + + api "code.gitea.io/gitea/modules/structs" + _ "github.com/mattn/go-sqlite3" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestGetTelegramIssuesPayload(t *testing.T) { + p := &api.IssuePayload{ + Index: 1, + Action: api.HookIssueClosed, + Sender: &api.User{ + UserName: "user1", + }, + Repository: &api.Repository{ + HTMLURL: "http://localhost:3000/test/repo", + Name: "repo", + FullName: "test/repo", + }, + Issue: &api.Issue{ + ID: 2, + URL: "http://localhost:3000/api/v1/repos/test/repo/issues/2", + Title: "crash", + }, + } + + pl, err := getTelegramIssuesPayload(p) + require.Nil(t, err) + assert.NotNil(t, pl) + + assert.Equal(t, "[test/repo] Issue closed: #1 crash by user1\n\n", pl.Message) + +} From 096095e73dbea6ebad14a020e998478549c33967 Mon Sep 17 00:00:00 2001 From: Gitea Date: Thu, 19 Dec 2019 15:46:34 +0800 Subject: [PATCH 2/9] More webhook refactoring --- modules/webhook/dingtalk.go | 75 +++----------------------------- modules/webhook/discord.go | 62 ++------------------------ modules/webhook/general.go | 29 ++++++++++-- modules/webhook/msteams.go | 64 +++------------------------ modules/webhook/slack.go | 16 ++----- modules/webhook/slack_test.go | 2 +- modules/webhook/telegram.go | 14 +----- modules/webhook/telegram_test.go | 2 +- 8 files changed, 48 insertions(+), 216 deletions(-) diff --git a/modules/webhook/dingtalk.go b/modules/webhook/dingtalk.go index 5ec8db9649db6..7239e84bfac73 100644 --- a/modules/webhook/dingtalk.go +++ b/modules/webhook/dingtalk.go @@ -132,41 +132,14 @@ func getDingtalkPushPayload(p *api.PushPayload) (*DingtalkPayload, error) { } func getDingtalkIssuesPayload(p *api.IssuePayload) (*DingtalkPayload, error) { - var text, title string - switch p.Action { - case api.HookIssueOpened: - title = fmt.Sprintf("[%s] Issue opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - text = p.Issue.Body - case api.HookIssueClosed: - title = fmt.Sprintf("[%s] Issue closed: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - case api.HookIssueReOpened: - title = fmt.Sprintf("[%s] Issue re-opened: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - case api.HookIssueEdited: - title = fmt.Sprintf("[%s] Issue edited: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - text = p.Issue.Body - case api.HookIssueAssigned: - title = fmt.Sprintf("[%s] Issue assigned to %s: #%d %s", p.Repository.FullName, - p.Issue.Assignee.UserName, p.Index, p.Issue.Title) - case api.HookIssueUnassigned: - title = fmt.Sprintf("[%s] Issue unassigned: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - case api.HookIssueLabelUpdated: - title = fmt.Sprintf("[%s] Issue labels updated: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - case api.HookIssueLabelCleared: - title = fmt.Sprintf("[%s] Issue labels cleared: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - case api.HookIssueSynchronized: - title = fmt.Sprintf("[%s] Issue synchronized: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - case api.HookIssueMilestoned: - title = fmt.Sprintf("[%s] Issue milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - case api.HookIssueDemilestoned: - title = fmt.Sprintf("[%s] Issue clear milestone: #%d %s", p.Repository.FullName, p.Index, p.Issue.Title) - } + text, _, attachmentText, _ := getIssuesPayloadInfo(p, noneLinkFormatter) return &DingtalkPayload{ MsgType: "actionCard", ActionCard: dingtalk.ActionCard{ - Text: title + "\r\n\r\n" + text, + Text: text + "\r\n\r\n" + attachmentText, //Markdown: "# " + title + "\n" + text, - Title: title, + Title: text, HideAvatar: "0", SingleTitle: "view issue", SingleURL: p.Issue.URL, @@ -218,50 +191,14 @@ func getDingtalkIssueCommentPayload(p *api.IssueCommentPayload) (*DingtalkPayloa } func getDingtalkPullRequestPayload(p *api.PullRequestPayload) (*DingtalkPayload, error) { - var text, title string - switch p.Action { - case api.HookIssueOpened: - title = fmt.Sprintf("[%s] Pull request opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - text = p.PullRequest.Body - case api.HookIssueClosed: - if p.PullRequest.HasMerged { - title = fmt.Sprintf("[%s] Pull request merged: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - } else { - title = fmt.Sprintf("[%s] Pull request closed: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - } - case api.HookIssueReOpened: - title = fmt.Sprintf("[%s] Pull request re-opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - case api.HookIssueEdited: - title = fmt.Sprintf("[%s] Pull request edited: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - text = p.PullRequest.Body - case api.HookIssueAssigned: - list := make([]string, len(p.PullRequest.Assignees)) - for i, user := range p.PullRequest.Assignees { - list[i] = user.UserName - } - title = fmt.Sprintf("[%s] Pull request assigned to %s: #%d %s", p.Repository.FullName, - strings.Join(list, ", "), - p.Index, p.PullRequest.Title) - case api.HookIssueUnassigned: - title = fmt.Sprintf("[%s] Pull request unassigned: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - case api.HookIssueLabelUpdated: - title = fmt.Sprintf("[%s] Pull request labels updated: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - case api.HookIssueLabelCleared: - title = fmt.Sprintf("[%s] Pull request labels cleared: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - case api.HookIssueSynchronized: - title = fmt.Sprintf("[%s] Pull request synchronized: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - case api.HookIssueMilestoned: - title = fmt.Sprintf("[%s] Pull request milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - case api.HookIssueDemilestoned: - title = fmt.Sprintf("[%s] Pull request clear milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - } + text, _, attachmentText, _ := getPullRequestPayloadInfo(p, noneLinkFormatter) return &DingtalkPayload{ MsgType: "actionCard", ActionCard: dingtalk.ActionCard{ - Text: title + "\r\n\r\n" + text, + Text: text + "\r\n\r\n" + attachmentText, //Markdown: "# " + title + "\n" + text, - Title: title, + Title: text, HideAvatar: "0", SingleTitle: "view pull request", SingleURL: p.PullRequest.HTMLURL, diff --git a/modules/webhook/discord.go b/modules/webhook/discord.go index bf505bafc2592..6e3a5a4a91c43 100644 --- a/modules/webhook/discord.go +++ b/modules/webhook/discord.go @@ -227,12 +227,7 @@ func getDiscordPushPayload(p *api.PushPayload, meta *DiscordMeta) (*DiscordPaylo } func getDiscordIssuesPayload(p *api.IssuePayload, meta *DiscordMeta) (*DiscordPayload, error) { - text, _, color := getIssuesPayloadInfo(p, noneLinkFormatter) - - var attachmentText string - if p.Action == api.HookIssueOpened || p.Action == api.HookIssueEdited { - attachmentText = p.Issue.Body - } + text, _, attachmentText, color := getIssuesPayloadInfo(p, noneLinkFormatter) return &DiscordPayload{ Username: meta.Username, @@ -309,64 +304,15 @@ func getDiscordIssueCommentPayload(p *api.IssueCommentPayload, discord *DiscordM } func getDiscordPullRequestPayload(p *api.PullRequestPayload, meta *DiscordMeta) (*DiscordPayload, error) { - var text, title string - var color int - switch p.Action { - case api.HookIssueOpened: - title = fmt.Sprintf("[%s] Pull request opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - text = p.PullRequest.Body - color = greenColor - case api.HookIssueClosed: - if p.PullRequest.HasMerged { - title = fmt.Sprintf("[%s] Pull request merged: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - color = purpleColor - } else { - title = fmt.Sprintf("[%s] Pull request closed: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - color = redColor - } - case api.HookIssueReOpened: - title = fmt.Sprintf("[%s] Pull request re-opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - color = yellowColor - case api.HookIssueEdited: - title = fmt.Sprintf("[%s] Pull request edited: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - text = p.PullRequest.Body - color = yellowColor - case api.HookIssueAssigned: - list := make([]string, len(p.PullRequest.Assignees)) - for i, user := range p.PullRequest.Assignees { - list[i] = user.UserName - } - title = fmt.Sprintf("[%s] Pull request assigned to %s: #%d by %s", p.Repository.FullName, - strings.Join(list, ", "), - p.Index, p.PullRequest.Title) - color = greenColor - case api.HookIssueUnassigned: - title = fmt.Sprintf("[%s] Pull request unassigned: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - color = yellowColor - case api.HookIssueLabelUpdated: - title = fmt.Sprintf("[%s] Pull request labels updated: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - color = yellowColor - case api.HookIssueLabelCleared: - title = fmt.Sprintf("[%s] Pull request labels cleared: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - color = yellowColor - case api.HookIssueSynchronized: - title = fmt.Sprintf("[%s] Pull request synchronized: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - color = yellowColor - case api.HookIssueMilestoned: - title = fmt.Sprintf("[%s] Pull request milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - color = yellowColor - case api.HookIssueDemilestoned: - title = fmt.Sprintf("[%s] Pull request clear milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - color = yellowColor - } + text, _, attachmentText, color := getPullRequestPayloadInfo(p, noneLinkFormatter) return &DiscordPayload{ Username: meta.Username, AvatarURL: meta.IconURL, Embeds: []DiscordEmbed{ { - Title: title, - Description: text, + Title: text, + Description: attachmentText, URL: p.PullRequest.HTMLURL, Color: color, Author: DiscordEmbedAuthor{ diff --git a/modules/webhook/general.go b/modules/webhook/general.go index 8ea6b2169cf37..d151fc563f0e8 100644 --- a/modules/webhook/general.go +++ b/modules/webhook/general.go @@ -1,3 +1,7 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + package webhook import ( @@ -10,15 +14,17 @@ import ( type linkFormatter = func(string, string) string +// noneLinkFormatter does not create a link but just returns the text func noneLinkFormatter(url string, text string) string { return text } +// htmlLinkFormatter creates a HTML link func htmlLinkFormatter(url string, text string) string { return fmt.Sprintf(`%s`, url, text) } -func getIssuesPayloadInfo(p *api.IssuePayload, linkFormatter linkFormatter) (string, string, int) { +func getIssuesPayloadInfo(p *api.IssuePayload, linkFormatter linkFormatter) (string, string, string, int) { senderLink := linkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName) repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName) issueTitle := fmt.Sprintf("#%d %s", p.Index, p.Issue.Title) @@ -58,24 +64,33 @@ func getIssuesPayloadInfo(p *api.IssuePayload, linkFormatter linkFormatter) (str text = fmt.Sprintf("[%s] Issue milestone cleared: %s by %s", repoLink, titleLink, senderLink) } - return text, issueTitle, color + var attachmentText string + if p.Action == api.HookIssueOpened || p.Action == api.HookIssueEdited { + attachmentText = p.Issue.Body + } + + return text, issueTitle, attachmentText, color } -func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkFormatter) (string, string) { +func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkFormatter) (string, string, string, int) { senderLink := linkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName) repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName) issueTitle := fmt.Sprintf("#%d %s", p.Index, p.PullRequest.Title) titleLink := linkFormatter(p.PullRequest.URL, issueTitle) var text string + color := yellowColor switch p.Action { case api.HookIssueOpened: text = fmt.Sprintf("[%s] Pull request opened by %s", repoLink, senderLink) + color = greenColor case api.HookIssueClosed: if p.PullRequest.HasMerged { text = fmt.Sprintf("[%s] Pull request merged: %s by %s", repoLink, titleLink, senderLink) + color = purpleColor } else { text = fmt.Sprintf("[%s] Pull request closed: %s by %s", repoLink, titleLink, senderLink) + color = redColor } case api.HookIssueReOpened: text = fmt.Sprintf("[%s] Pull request re-opened: %s by %s", repoLink, titleLink, senderLink) @@ -89,6 +104,7 @@ func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkForm text = fmt.Sprintf("[%s] Pull request assigned to %s: %s by %s", repoLink, strings.Join(list, ", "), titleLink, senderLink) + color = greenColor case api.HookIssueUnassigned: text = fmt.Sprintf("[%s] Pull request unassigned: %s by %s", repoLink, titleLink, senderLink) case api.HookIssueLabelUpdated: @@ -105,5 +121,10 @@ func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkForm text = fmt.Sprintf("[%s] Pull request milestone cleared: %s %s", repoLink, titleLink, senderLink) } - return text, issueTitle + var attachmentText string + if p.Action == api.HookIssueOpened || p.Action == api.HookIssueEdited { + attachmentText = p.PullRequest.Body + } + + return text, issueTitle, attachmentText, color } diff --git a/modules/webhook/msteams.go b/modules/webhook/msteams.go index 4910ec3e5b672..4aff38a01ac76 100644 --- a/modules/webhook/msteams.go +++ b/modules/webhook/msteams.go @@ -266,12 +266,7 @@ func getMSTeamsPushPayload(p *api.PushPayload) (*MSTeamsPayload, error) { } func getMSTeamsIssuesPayload(p *api.IssuePayload) (*MSTeamsPayload, error) { - text, _, color := getIssuesPayloadInfo(p, noneLinkFormatter) - - var attachmentText string - if p.Action == api.HookIssueOpened || p.Action == api.HookIssueEdited { - attachmentText = p.Issue.Body - } + text, _, attachmentText, color := getIssuesPayloadInfo(p, noneLinkFormatter) return &MSTeamsPayload{ Type: "MessageCard", @@ -388,69 +383,20 @@ func getMSTeamsIssueCommentPayload(p *api.IssueCommentPayload) (*MSTeamsPayload, } func getMSTeamsPullRequestPayload(p *api.PullRequestPayload) (*MSTeamsPayload, error) { - var text, title string - var color int - switch p.Action { - case api.HookIssueOpened: - title = fmt.Sprintf("[%s] Pull request opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - text = p.PullRequest.Body - color = greenColor - case api.HookIssueClosed: - if p.PullRequest.HasMerged { - title = fmt.Sprintf("[%s] Pull request merged: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - color = purpleColor - } else { - title = fmt.Sprintf("[%s] Pull request closed: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - color = redColor - } - case api.HookIssueReOpened: - title = fmt.Sprintf("[%s] Pull request re-opened: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - color = yellowColor - case api.HookIssueEdited: - title = fmt.Sprintf("[%s] Pull request edited: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - text = p.PullRequest.Body - color = yellowColor - case api.HookIssueAssigned: - list := make([]string, len(p.PullRequest.Assignees)) - for i, user := range p.PullRequest.Assignees { - list[i] = user.UserName - } - title = fmt.Sprintf("[%s] Pull request assigned to %s: #%d by %s", p.Repository.FullName, - strings.Join(list, ", "), - p.Index, p.PullRequest.Title) - color = greenColor - case api.HookIssueUnassigned: - title = fmt.Sprintf("[%s] Pull request unassigned: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - color = yellowColor - case api.HookIssueLabelUpdated: - title = fmt.Sprintf("[%s] Pull request labels updated: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - color = yellowColor - case api.HookIssueLabelCleared: - title = fmt.Sprintf("[%s] Pull request labels cleared: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - color = yellowColor - case api.HookIssueSynchronized: - title = fmt.Sprintf("[%s] Pull request synchronized: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - color = yellowColor - case api.HookIssueMilestoned: - title = fmt.Sprintf("[%s] Pull request milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - color = yellowColor - case api.HookIssueDemilestoned: - title = fmt.Sprintf("[%s] Pull request clear milestone: #%d %s", p.Repository.FullName, p.Index, p.PullRequest.Title) - color = yellowColor - } + text, _, attachmentText, color := getPullRequestPayloadInfo(p, noneLinkFormatter) return &MSTeamsPayload{ Type: "MessageCard", Context: "https://schema.org/extensions", ThemeColor: fmt.Sprintf("%x", color), - Title: title, - Summary: title, + Title: text, + Summary: text, Sections: []MSTeamsSection{ { ActivityTitle: p.Sender.FullName, ActivitySubtitle: p.Sender.UserName, ActivityImage: p.Sender.AvatarURL, - Text: text, + Text: attachmentText, Facts: []MSTeamsFact{ { Name: "Repository:", diff --git a/modules/webhook/slack.go b/modules/webhook/slack.go index e380d970c52e4..f29be54e5a633 100644 --- a/modules/webhook/slack.go +++ b/modules/webhook/slack.go @@ -144,12 +144,7 @@ func getSlackForkPayload(p *api.ForkPayload, slack *SlackMeta) (*SlackPayload, e } func getSlackIssuesPayload(p *api.IssuePayload, slack *SlackMeta) (*SlackPayload, error) { - text, issueTitle, _ := getIssuesPayloadInfo(p, SlackLinkFormatter) - - var attachmentText string - if p.Action == api.HookIssueOpened || p.Action == api.HookIssueEdited { - attachmentText = SlackTextFormatter(p.Issue.Body) - } + text, issueTitle, attachmentText, _ := getIssuesPayloadInfo(p, SlackLinkFormatter) pl := &SlackPayload{ Channel: slack.Channel, @@ -158,6 +153,7 @@ 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, @@ -272,12 +268,7 @@ func getSlackPushPayload(p *api.PushPayload, slack *SlackMeta) (*SlackPayload, e } func getSlackPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) (*SlackPayload, error) { - text, issueTitle := getPullRequestPayloadInfo(p, SlackLinkFormatter) - - var attachmentText string - if p.Action == api.HookIssueOpened || p.Action == api.HookIssueEdited { - attachmentText = SlackTextFormatter(p.PullRequest.Body) - } + text, issueTitle, attachmentText, _ := getPullRequestPayloadInfo(p, SlackLinkFormatter) pl := &SlackPayload{ Channel: slack.Channel, @@ -286,6 +277,7 @@ 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, diff --git a/modules/webhook/slack_test.go b/modules/webhook/slack_test.go index 58abd312cdbaa..4d8a5c91f8468 100644 --- a/modules/webhook/slack_test.go +++ b/modules/webhook/slack_test.go @@ -38,7 +38,7 @@ func TestSlackIssuesPayload(t *testing.T) { pl, err := getSlackIssuesPayload(p, sl) require.Nil(t, err) - assert.NotNil(t, pl) + require.NotNil(t, pl) assert.Equal(t, "[] Issue closed: by ", pl.Text) } diff --git a/modules/webhook/telegram.go b/modules/webhook/telegram.go index 49e88eee2046f..616f77db705e6 100644 --- a/modules/webhook/telegram.go +++ b/modules/webhook/telegram.go @@ -126,12 +126,7 @@ func getTelegramPushPayload(p *api.PushPayload) (*TelegramPayload, error) { } func getTelegramIssuesPayload(p *api.IssuePayload) (*TelegramPayload, error) { - text, _, _ := getIssuesPayloadInfo(p, htmlLinkFormatter) - - var attachmentText string - if p.Action == api.HookIssueOpened || p.Action == api.HookIssueEdited { - attachmentText = p.Issue.Body - } + text, _, attachmentText, _ := getIssuesPayloadInfo(p, htmlLinkFormatter) return &TelegramPayload{ Message: text + "\n\n" + attachmentText, @@ -160,12 +155,7 @@ func getTelegramIssueCommentPayload(p *api.IssueCommentPayload) (*TelegramPayloa } func getTelegramPullRequestPayload(p *api.PullRequestPayload) (*TelegramPayload, error) { - text, _ := getPullRequestPayloadInfo(p, htmlLinkFormatter) - - var attachmentText string - if p.Action == api.HookIssueOpened || p.Action == api.HookIssueEdited { - attachmentText = p.PullRequest.Body - } + text, _, attachmentText, _ := getPullRequestPayloadInfo(p, htmlLinkFormatter) return &TelegramPayload{ Message: text + "\n" + attachmentText, diff --git a/modules/webhook/telegram_test.go b/modules/webhook/telegram_test.go index dc0e2fdbad5fb..2ff3a0e4ddc3d 100644 --- a/modules/webhook/telegram_test.go +++ b/modules/webhook/telegram_test.go @@ -34,7 +34,7 @@ func TestGetTelegramIssuesPayload(t *testing.T) { pl, err := getTelegramIssuesPayload(p) require.Nil(t, err) - assert.NotNil(t, pl) + require.NotNil(t, pl) assert.Equal(t, "[test/repo] Issue closed: #1 crash by user1\n\n", pl.Message) From 41b527aa087ec79432b70150031512096a039dc1 Mon Sep 17 00:00:00 2001 From: Gitea Date: Fri, 20 Dec 2019 12:08:24 +0800 Subject: [PATCH 3/9] Unify webhook release messages --- modules/webhook/dingtalk.go | 55 ++++++++----------------------------- modules/webhook/discord.go | 21 ++------------ modules/webhook/general.go | 20 ++++++++++++++ modules/webhook/msteams.go | 23 +++------------- modules/webhook/slack.go | 13 +-------- modules/webhook/telegram.go | 27 +++--------------- 6 files changed, 43 insertions(+), 116 deletions(-) diff --git a/modules/webhook/dingtalk.go b/modules/webhook/dingtalk.go index 7239e84bfac73..b8946927b5db8 100644 --- a/modules/webhook/dingtalk.go +++ b/modules/webhook/dingtalk.go @@ -264,51 +264,18 @@ func getDingtalkRepositoryPayload(p *api.RepositoryPayload) (*DingtalkPayload, e } func getDingtalkReleasePayload(p *api.ReleasePayload) (*DingtalkPayload, error) { - var title, url string - switch p.Action { - case api.HookReleasePublished: - title = fmt.Sprintf("[%s] Release created", p.Release.TagName) - url = p.Release.URL - return &DingtalkPayload{ - MsgType: "actionCard", - ActionCard: dingtalk.ActionCard{ - Text: title, - Title: title, - HideAvatar: "0", - SingleTitle: "view release", - SingleURL: url, - }, - }, nil - case api.HookReleaseUpdated: - title = fmt.Sprintf("[%s] Release updated", p.Release.TagName) - url = p.Release.URL - return &DingtalkPayload{ - MsgType: "actionCard", - ActionCard: dingtalk.ActionCard{ - Text: title, - Title: title, - HideAvatar: "0", - SingleTitle: "view release", - SingleURL: url, - }, - }, nil + text, _ := getReleasePayloadInfo(p, noneLinkFormatter) - case api.HookReleaseDeleted: - title = fmt.Sprintf("[%s] Release deleted", p.Release.TagName) - url = p.Release.URL - return &DingtalkPayload{ - MsgType: "actionCard", - ActionCard: dingtalk.ActionCard{ - Text: title, - Title: title, - HideAvatar: "0", - SingleTitle: "view release", - SingleURL: url, - }, - }, nil - } - - return nil, nil + return &DingtalkPayload{ + MsgType: "actionCard", + ActionCard: dingtalk.ActionCard{ + Text: text, + Title: text, + HideAvatar: "0", + SingleTitle: "view release", + SingleURL: p.Release.URL, + }, + }, nil } // GetDingtalkPayload converts a ding talk webhook into a DingtalkPayload diff --git a/modules/webhook/discord.go b/modules/webhook/discord.go index 6e3a5a4a91c43..a59c97ce7a2f0 100644 --- a/modules/webhook/discord.go +++ b/modules/webhook/discord.go @@ -401,31 +401,16 @@ func getDiscordRepositoryPayload(p *api.RepositoryPayload, meta *DiscordMeta) (* } func getDiscordReleasePayload(p *api.ReleasePayload, meta *DiscordMeta) (*DiscordPayload, error) { - var title, url string - var color int - switch p.Action { - case api.HookReleasePublished: - title = fmt.Sprintf("[%s] Release created", p.Release.TagName) - url = p.Release.URL - color = greenColor - case api.HookReleaseUpdated: - title = fmt.Sprintf("[%s] Release updated", p.Release.TagName) - url = p.Release.URL - color = yellowColor - case api.HookReleaseDeleted: - title = fmt.Sprintf("[%s] Release deleted", p.Release.TagName) - url = p.Release.URL - color = redColor - } + text, color := getReleasePayloadInfo(p, noneLinkFormatter) return &DiscordPayload{ Username: meta.Username, AvatarURL: meta.IconURL, Embeds: []DiscordEmbed{ { - Title: title, + Title: text, Description: p.Release.Note, - URL: url, + URL: p.Release.URL, Color: color, Author: DiscordEmbedAuthor{ Name: p.Sender.UserName, diff --git a/modules/webhook/general.go b/modules/webhook/general.go index d151fc563f0e8..a8a6f837f62c6 100644 --- a/modules/webhook/general.go +++ b/modules/webhook/general.go @@ -128,3 +128,23 @@ func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkForm return text, issueTitle, attachmentText, color } + +func getReleasePayloadInfo(p *api.ReleasePayload, linkFormatter linkFormatter) (text string, color int) { + senderLink := linkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName) + repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName) + relLink := linkFormatter(p.Release.URL, p.Release.TagName) + + switch p.Action { + case api.HookReleasePublished: + text = fmt.Sprintf("[%s] Release %s created by %s", repoLink, relLink, senderLink) + color = greenColor + case api.HookReleaseUpdated: + text = fmt.Sprintf("[%s] Release %s updated by %s", repoLink, relLink, senderLink) + color = yellowColor + case api.HookReleaseDeleted: + text = fmt.Sprintf("[%s] Release %s deleted by %s", repoLink, relLink, senderLink) + color = redColor + } + + return text, color +} diff --git a/modules/webhook/msteams.go b/modules/webhook/msteams.go index 4aff38a01ac76..0fb048dcdac96 100644 --- a/modules/webhook/msteams.go +++ b/modules/webhook/msteams.go @@ -536,29 +536,14 @@ func getMSTeamsRepositoryPayload(p *api.RepositoryPayload) (*MSTeamsPayload, err } func getMSTeamsReleasePayload(p *api.ReleasePayload) (*MSTeamsPayload, error) { - var title, url string - var color int - switch p.Action { - case api.HookReleasePublished: - title = fmt.Sprintf("[%s] Release created", p.Release.TagName) - url = p.Release.URL - color = greenColor - case api.HookReleaseUpdated: - title = fmt.Sprintf("[%s] Release updated", p.Release.TagName) - url = p.Release.URL - color = greenColor - case api.HookReleaseDeleted: - title = fmt.Sprintf("[%s] Release deleted", p.Release.TagName) - url = p.Release.URL - color = greenColor - } + text, color := getReleasePayloadInfo(p, noneLinkFormatter) return &MSTeamsPayload{ Type: "MessageCard", Context: "https://schema.org/extensions", ThemeColor: fmt.Sprintf("%x", color), - Title: title, - Summary: title, + Title: text, + Summary: text, Sections: []MSTeamsSection{ { ActivityTitle: p.Sender.FullName, @@ -584,7 +569,7 @@ func getMSTeamsReleasePayload(p *api.ReleasePayload) (*MSTeamsPayload, error) { Targets: []MSTeamsActionTarget{ { Os: "default", - URI: url, + URI: p.Release.URL, }, }, }, diff --git a/modules/webhook/slack.go b/modules/webhook/slack.go index f29be54e5a633..5e880ad4a5d7a 100644 --- a/modules/webhook/slack.go +++ b/modules/webhook/slack.go @@ -202,18 +202,7 @@ func getSlackIssueCommentPayload(p *api.IssueCommentPayload, slack *SlackMeta) ( } 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, diff --git a/modules/webhook/telegram.go b/modules/webhook/telegram.go index 616f77db705e6..5245ff990ba9d 100644 --- a/modules/webhook/telegram.go +++ b/modules/webhook/telegram.go @@ -180,30 +180,11 @@ func getTelegramRepositoryPayload(p *api.RepositoryPayload) (*TelegramPayload, e } func getTelegramReleasePayload(p *api.ReleasePayload) (*TelegramPayload, error) { - var title, url string - switch p.Action { - case api.HookReleasePublished: - title = fmt.Sprintf("[%s] Release created", p.Release.TagName) - url = p.Release.URL - return &TelegramPayload{ - Message: title + "\n" + url, - }, nil - case api.HookReleaseUpdated: - title = fmt.Sprintf("[%s] Release updated", p.Release.TagName) - url = p.Release.URL - return &TelegramPayload{ - Message: title + "\n" + url, - }, nil - - case api.HookReleaseDeleted: - title = fmt.Sprintf("[%s] Release deleted", p.Release.TagName) - url = p.Release.URL - return &TelegramPayload{ - Message: title + "\n" + url, - }, nil - } + text, _ := getReleasePayloadInfo(p, htmlLinkFormatter) - return nil, nil + return &TelegramPayload{ + Message: text + "\n", + }, nil } // GetTelegramPayload converts a telegram webhook into a TelegramPayload From aebc450fb936388ee22814fac073433d639c1001 Mon Sep 17 00:00:00 2001 From: Gitea Date: Fri, 20 Dec 2019 12:25:11 +0800 Subject: [PATCH 4/9] Fix webhook release link --- modules/webhook/general.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/webhook/general.go b/modules/webhook/general.go index a8a6f837f62c6..5249638ee0f72 100644 --- a/modules/webhook/general.go +++ b/modules/webhook/general.go @@ -132,17 +132,17 @@ func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkForm func getReleasePayloadInfo(p *api.ReleasePayload, linkFormatter linkFormatter) (text string, color int) { senderLink := linkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName) repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName) - relLink := linkFormatter(p.Release.URL, p.Release.TagName) + refLink := linkFormatter(p.Repository.HTMLURL+"/src/"+p.Release.TagName, p.Release.TagName) switch p.Action { case api.HookReleasePublished: - text = fmt.Sprintf("[%s] Release %s created by %s", repoLink, relLink, senderLink) + text = fmt.Sprintf("[%s] Release %s created by %s", repoLink, refLink, senderLink) color = greenColor case api.HookReleaseUpdated: - text = fmt.Sprintf("[%s] Release %s updated by %s", repoLink, relLink, senderLink) + text = fmt.Sprintf("[%s] Release %s updated by %s", repoLink, refLink, senderLink) color = yellowColor case api.HookReleaseDeleted: - text = fmt.Sprintf("[%s] Release %s deleted by %s", repoLink, relLink, senderLink) + text = fmt.Sprintf("[%s] Release %s deleted by %s", repoLink, refLink, senderLink) color = redColor } From 35bb3d7e049e42fef5a3c109ea661309ecedbeff Mon Sep 17 00:00:00 2001 From: Gitea Date: Fri, 20 Dec 2019 12:31:42 +0800 Subject: [PATCH 5/9] Remove sql import --- modules/webhook/slack_test.go | 1 - modules/webhook/telegram_test.go | 1 - 2 files changed, 2 deletions(-) diff --git a/modules/webhook/slack_test.go b/modules/webhook/slack_test.go index 4d8a5c91f8468..006fe6a265973 100644 --- a/modules/webhook/slack_test.go +++ b/modules/webhook/slack_test.go @@ -8,7 +8,6 @@ import ( "testing" api "code.gitea.io/gitea/modules/structs" - _ "github.com/mattn/go-sqlite3" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/modules/webhook/telegram_test.go b/modules/webhook/telegram_test.go index 2ff3a0e4ddc3d..96345931157e9 100644 --- a/modules/webhook/telegram_test.go +++ b/modules/webhook/telegram_test.go @@ -8,7 +8,6 @@ import ( "testing" api "code.gitea.io/gitea/modules/structs" - _ "github.com/mattn/go-sqlite3" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) From 611bfe0d2e970db3270408d620e64330ef29e80f Mon Sep 17 00:00:00 2001 From: Gitea Date: Sun, 22 Dec 2019 17:02:31 +0800 Subject: [PATCH 6/9] More webhook refactoring --- modules/webhook/dingtalk.go | 44 +++++++------------------------------ modules/webhook/discord.go | 41 ++++------------------------------ modules/webhook/general.go | 34 +++++++++++++++++++++++++++- modules/webhook/msteams.go | 43 +++++------------------------------- modules/webhook/slack.go | 26 ++++------------------ modules/webhook/telegram.go | 18 ++------------- 6 files changed, 56 insertions(+), 150 deletions(-) diff --git a/modules/webhook/dingtalk.go b/modules/webhook/dingtalk.go index b8946927b5db8..1cf9248f0c5d6 100644 --- a/modules/webhook/dingtalk.go +++ b/modules/webhook/dingtalk.go @@ -132,14 +132,14 @@ func getDingtalkPushPayload(p *api.PushPayload) (*DingtalkPayload, error) { } func getDingtalkIssuesPayload(p *api.IssuePayload) (*DingtalkPayload, error) { - text, _, attachmentText, _ := getIssuesPayloadInfo(p, noneLinkFormatter) + text, issueTitle, attachmentText, _ := getIssuesPayloadInfo(p, noneLinkFormatter) return &DingtalkPayload{ MsgType: "actionCard", ActionCard: dingtalk.ActionCard{ Text: text + "\r\n\r\n" + attachmentText, //Markdown: "# " + title + "\n" + text, - Title: text, + Title: issueTitle, HideAvatar: "0", SingleTitle: "view issue", SingleURL: p.Issue.URL, @@ -148,57 +148,29 @@ func getDingtalkIssuesPayload(p *api.IssuePayload) (*DingtalkPayload, error) { } func getDingtalkIssueCommentPayload(p *api.IssueCommentPayload) (*DingtalkPayload, error) { - title := fmt.Sprintf("#%d: %s", p.Issue.Index, p.Issue.Title) - url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, models.CommentHashTag(p.Comment.ID)) - var content string - switch p.Action { - case api.HookIssueCommentCreated: - if p.IsPull { - title = "New comment on pull request " + title - } else { - title = "New comment on issue " + title - } - content = p.Comment.Body - case api.HookIssueCommentEdited: - if p.IsPull { - title = "Comment edited on pull request " + title - } else { - title = "Comment edited on issue " + title - } - content = p.Comment.Body - case api.HookIssueCommentDeleted: - if p.IsPull { - title = "Comment deleted on pull request " + title - } else { - title = "Comment deleted on issue " + title - } - url = fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index) - content = p.Comment.Body - } - - title = fmt.Sprintf("[%s] %s", p.Repository.FullName, title) + text, issueTitle, _ := getIssueCommentPayloadInfo(p, noneLinkFormatter) return &DingtalkPayload{ MsgType: "actionCard", ActionCard: dingtalk.ActionCard{ - Text: title + "\r\n\r\n" + content, - Title: title, + Text: text + "\r\n\r\n" + p.Comment.Body, + Title: issueTitle, HideAvatar: "0", SingleTitle: "view issue comment", - SingleURL: url, + SingleURL: p.Issue.URL, }, }, nil } func getDingtalkPullRequestPayload(p *api.PullRequestPayload) (*DingtalkPayload, error) { - text, _, attachmentText, _ := getPullRequestPayloadInfo(p, noneLinkFormatter) + text, issueTitle, attachmentText, _ := getPullRequestPayloadInfo(p, noneLinkFormatter) return &DingtalkPayload{ MsgType: "actionCard", ActionCard: dingtalk.ActionCard{ Text: text + "\r\n\r\n" + attachmentText, //Markdown: "# " + title + "\n" + text, - Title: text, + Title: issueTitle, HideAvatar: "0", SingleTitle: "view pull request", SingleURL: p.PullRequest.HTMLURL, diff --git a/modules/webhook/discord.go b/modules/webhook/discord.go index a59c97ce7a2f0..62d976d1076aa 100644 --- a/modules/webhook/discord.go +++ b/modules/webhook/discord.go @@ -249,49 +249,16 @@ func getDiscordIssuesPayload(p *api.IssuePayload, meta *DiscordMeta) (*DiscordPa } func getDiscordIssueCommentPayload(p *api.IssueCommentPayload, discord *DiscordMeta) (*DiscordPayload, error) { - title := fmt.Sprintf("#%d: %s", p.Issue.Index, p.Issue.Title) - url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, models.CommentHashTag(p.Comment.ID)) - content := "" - var color int - switch p.Action { - case api.HookIssueCommentCreated: - if p.IsPull { - title = "New comment on pull request " + title - color = greenColorLight - } else { - title = "New comment on issue " + title - color = orangeColorLight - } - content = p.Comment.Body - case api.HookIssueCommentEdited: - if p.IsPull { - title = "Comment edited on pull request " + title - } else { - title = "Comment edited on issue " + title - } - content = p.Comment.Body - color = yellowColor - case api.HookIssueCommentDeleted: - if p.IsPull { - title = "Comment deleted on pull request " + title - } else { - title = "Comment deleted on issue " + title - } - url = fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index) - content = p.Comment.Body - color = redColor - } - - title = fmt.Sprintf("[%s] %s", p.Repository.FullName, title) + text, _, color := getIssueCommentPayloadInfo(p, noneLinkFormatter) return &DiscordPayload{ Username: discord.Username, AvatarURL: discord.IconURL, Embeds: []DiscordEmbed{ { - Title: title, - Description: content, - URL: url, + Title: text, + Description: p.Comment.Body, + URL: p.Issue.URL, Color: color, Author: DiscordEmbedAuthor{ Name: p.Sender.UserName, diff --git a/modules/webhook/general.go b/modules/webhook/general.go index 5249638ee0f72..64b96a315b13d 100644 --- a/modules/webhook/general.go +++ b/modules/webhook/general.go @@ -6,6 +6,7 @@ package webhook import ( "fmt" + "html" "strings" "code.gitea.io/gitea/modules/setting" @@ -21,7 +22,7 @@ func noneLinkFormatter(url string, text string) string { // htmlLinkFormatter creates a HTML link func htmlLinkFormatter(url string, text string) string { - return fmt.Sprintf(`%s`, url, text) + return fmt.Sprintf(`%s`, url, html.EscapeString(text)) } func getIssuesPayloadInfo(p *api.IssuePayload, linkFormatter linkFormatter) (string, string, string, int) { @@ -148,3 +149,34 @@ func getReleasePayloadInfo(p *api.ReleasePayload, linkFormatter linkFormatter) ( return text, color } + +func getIssueCommentPayloadInfo(p *api.IssueCommentPayload, linkFormatter linkFormatter) (string, string, int) { + senderLink := linkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName) + repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName) + issueTitle := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title) + var text, typ string + color := yellowColor + + if p.IsPull { + typ = "pull request" + } else { + typ = "issue" + } + + switch p.Action { + case api.HookIssueCommentCreated: + text = fmt.Sprintf("[%s] New comment on %s by %s", repoLink, typ, senderLink) + if p.IsPull { + color = greenColorLight + } else { + color = orangeColorLight + } + case api.HookIssueCommentEdited: + text = fmt.Sprintf("[%s] Comment on %s edited by %s", repoLink, typ, senderLink) + case api.HookIssueCommentDeleted: + text = fmt.Sprintf("[%s] Comment on %s deleted by %s", repoLink, typ, senderLink) + color = redColor + } + + return text, issueTitle, color +} diff --git a/modules/webhook/msteams.go b/modules/webhook/msteams.go index 0fb048dcdac96..5ade8ddf445e8 100644 --- a/modules/webhook/msteams.go +++ b/modules/webhook/msteams.go @@ -308,53 +308,20 @@ func getMSTeamsIssuesPayload(p *api.IssuePayload) (*MSTeamsPayload, error) { } func getMSTeamsIssueCommentPayload(p *api.IssueCommentPayload) (*MSTeamsPayload, error) { - title := fmt.Sprintf("#%d: %s", p.Issue.Index, p.Issue.Title) - url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, models.CommentHashTag(p.Comment.ID)) - content := "" - var color int - switch p.Action { - case api.HookIssueCommentCreated: - if p.IsPull { - title = "New comment on pull request " + title - color = greenColorLight - } else { - title = "New comment on issue " + title - color = orangeColorLight - } - content = p.Comment.Body - case api.HookIssueCommentEdited: - if p.IsPull { - title = "Comment edited on pull request " + title - } else { - title = "Comment edited on issue " + title - } - content = p.Comment.Body - color = yellowColor - case api.HookIssueCommentDeleted: - if p.IsPull { - title = "Comment deleted on pull request " + title - } else { - title = "Comment deleted on issue " + title - } - url = fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Issue.Index) - content = p.Comment.Body - color = redColor - } - - title = fmt.Sprintf("[%s] %s", p.Repository.FullName, title) + text, _, color := getIssueCommentPayloadInfo(p, noneLinkFormatter) return &MSTeamsPayload{ Type: "MessageCard", Context: "https://schema.org/extensions", ThemeColor: fmt.Sprintf("%x", color), - Title: title, - Summary: title, + Title: text, + Summary: text, Sections: []MSTeamsSection{ { ActivityTitle: p.Sender.FullName, ActivitySubtitle: p.Sender.UserName, ActivityImage: p.Sender.AvatarURL, - Text: content, + Text: p.Comment.Body, Facts: []MSTeamsFact{ { Name: "Repository:", @@ -374,7 +341,7 @@ func getMSTeamsIssueCommentPayload(p *api.IssueCommentPayload) (*MSTeamsPayload, Targets: []MSTeamsActionTarget{ { Os: "default", - URI: url, + URI: p.Issue.URL, }, }, }, diff --git a/modules/webhook/slack.go b/modules/webhook/slack.go index 5e880ad4a5d7a..5b4f13420358c 100644 --- a/modules/webhook/slack.go +++ b/modules/webhook/slack.go @@ -167,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, _ := getIssueCommentPayloadInfo(p, SlackLinkFormatter) return &SlackPayload{ Channel: slack.Channel, @@ -194,9 +176,9 @@ func getSlackIssueCommentPayload(p *api.IssueCommentPayload, slack *SlackMeta) ( IconURL: slack.IconURL, Attachments: []SlackAttachment{{ Color: slack.Color, - Title: title, - TitleLink: titleLink, - Text: attachmentText, + Title: issueTitle, + TitleLink: p.Issue.URL, + Text: SlackTextFormatter(p.Comment.Body), }}, }, nil } diff --git a/modules/webhook/telegram.go b/modules/webhook/telegram.go index 5245ff990ba9d..a98d47d55c73c 100644 --- a/modules/webhook/telegram.go +++ b/modules/webhook/telegram.go @@ -7,7 +7,6 @@ package webhook import ( "encoding/json" "fmt" - "html" "strings" "code.gitea.io/gitea/models" @@ -134,23 +133,10 @@ func getTelegramIssuesPayload(p *api.IssuePayload) (*TelegramPayload, error) { } func getTelegramIssueCommentPayload(p *api.IssueCommentPayload) (*TelegramPayload, error) { - url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, models.CommentHashTag(p.Comment.ID)) - title := fmt.Sprintf(`#%d %s`, url, p.Issue.Index, html.EscapeString(p.Issue.Title)) - var text string - switch p.Action { - case api.HookIssueCommentCreated: - text = "New comment: " + title - text += p.Comment.Body - case api.HookIssueCommentEdited: - text = "Comment edited: " + title - text += p.Comment.Body - case api.HookIssueCommentDeleted: - text = "Comment deleted: " + title - text += p.Comment.Body - } + text, _, _ := getIssueCommentPayloadInfo(p, htmlLinkFormatter) return &TelegramPayload{ - Message: title + "\n" + text, + Message: text + "\n" + p.Comment.Body, }, nil } From 17f63e8687bbe2dc1a6c0f7e5da58f79807ebb6b Mon Sep 17 00:00:00 2001 From: Gitea Date: Mon, 23 Dec 2019 09:11:34 +0800 Subject: [PATCH 7/9] More webhook refactoring --- modules/webhook/dingtalk.go | 2 +- modules/webhook/discord.go | 2 +- modules/webhook/general.go | 37 ++++++++------- modules/webhook/general_test.go | 81 ++++++++++++++++++++++++++++++++ modules/webhook/msteams.go | 2 +- modules/webhook/slack.go | 14 +++--- modules/webhook/slack_test.go | 49 +++++++++++-------- modules/webhook/telegram_test.go | 22 +-------- 8 files changed, 143 insertions(+), 66 deletions(-) create mode 100644 modules/webhook/general_test.go diff --git a/modules/webhook/dingtalk.go b/modules/webhook/dingtalk.go index 1cf9248f0c5d6..4869c1a37c3cb 100644 --- a/modules/webhook/dingtalk.go +++ b/modules/webhook/dingtalk.go @@ -157,7 +157,7 @@ func getDingtalkIssueCommentPayload(p *api.IssueCommentPayload) (*DingtalkPayloa Title: issueTitle, HideAvatar: "0", SingleTitle: "view issue comment", - SingleURL: p.Issue.URL, + SingleURL: p.Comment.HTMLURL, }, }, nil } diff --git a/modules/webhook/discord.go b/modules/webhook/discord.go index 62d976d1076aa..ea69f36fe7919 100644 --- a/modules/webhook/discord.go +++ b/modules/webhook/discord.go @@ -258,7 +258,7 @@ func getDiscordIssueCommentPayload(p *api.IssueCommentPayload, discord *DiscordM { Title: text, Description: p.Comment.Body, - URL: p.Issue.URL, + URL: p.Comment.HTMLURL, Color: color, Author: DiscordEmbedAuthor{ Name: p.Sender.UserName, diff --git a/modules/webhook/general.go b/modules/webhook/general.go index 64b96a315b13d..e8931b09062d2 100644 --- a/modules/webhook/general.go +++ b/modules/webhook/general.go @@ -29,7 +29,7 @@ func getIssuesPayloadInfo(p *api.IssuePayload, linkFormatter linkFormatter) (str senderLink := linkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName) repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName) issueTitle := fmt.Sprintf("#%d %s", p.Index, p.Issue.Title) - titleLink := linkFormatter(p.Issue.URL, issueTitle) + titleLink := linkFormatter(fmt.Sprintf("%s/issues/%d", p.Repository.HTMLURL, p.Index), issueTitle) var text string color := yellowColor @@ -83,43 +83,43 @@ func getPullRequestPayloadInfo(p *api.PullRequestPayload, linkFormatter linkForm switch p.Action { case api.HookIssueOpened: - text = fmt.Sprintf("[%s] Pull request opened by %s", repoLink, senderLink) + text = fmt.Sprintf("[%s] Pull request %s opened by %s", repoLink, titleLink, senderLink) color = greenColor case api.HookIssueClosed: if p.PullRequest.HasMerged { - text = fmt.Sprintf("[%s] Pull request merged: %s by %s", repoLink, titleLink, senderLink) + text = fmt.Sprintf("[%s] Pull request %s merged by %s", repoLink, titleLink, senderLink) color = purpleColor } else { - text = fmt.Sprintf("[%s] Pull request closed: %s by %s", repoLink, titleLink, senderLink) + text = fmt.Sprintf("[%s] Pull request %s closed by %s", repoLink, titleLink, senderLink) color = redColor } case api.HookIssueReOpened: - text = fmt.Sprintf("[%s] Pull request re-opened: %s by %s", repoLink, titleLink, senderLink) + text = fmt.Sprintf("[%s] Pull request %s re-opened by %s", repoLink, titleLink, senderLink) case api.HookIssueEdited: - text = fmt.Sprintf("[%s] Pull request edited: %s by %s", repoLink, titleLink, senderLink) + text = fmt.Sprintf("[%s] Pull request %s edited by %s", repoLink, titleLink, senderLink) case api.HookIssueAssigned: list := make([]string, len(p.PullRequest.Assignees)) for i, user := range p.PullRequest.Assignees { list[i] = linkFormatter(setting.AppURL+user.UserName, user.UserName) } - text = fmt.Sprintf("[%s] Pull request assigned to %s: %s by %s", repoLink, + text = fmt.Sprintf("[%s] Pull request %s assigned to %s by %s", repoLink, strings.Join(list, ", "), titleLink, senderLink) color = greenColor case api.HookIssueUnassigned: - text = fmt.Sprintf("[%s] Pull request unassigned: %s by %s", repoLink, titleLink, senderLink) + text = fmt.Sprintf("[%s] Pull request %s unassigned by %s", repoLink, titleLink, senderLink) case api.HookIssueLabelUpdated: - text = fmt.Sprintf("[%s] Pull request labels updated: %s by %s", repoLink, titleLink, senderLink) + text = fmt.Sprintf("[%s] Pull request %s labels updated by %s", repoLink, titleLink, senderLink) case api.HookIssueLabelCleared: - text = fmt.Sprintf("[%s] Pull request labels cleared: %s by %s", repoLink, titleLink, senderLink) + text = fmt.Sprintf("[%s] Pull request %s labels cleared by %s", repoLink, titleLink, senderLink) case api.HookIssueSynchronized: - text = fmt.Sprintf("[%s] Pull request synchronized: %s by %s", repoLink, titleLink, senderLink) + text = fmt.Sprintf("[%s] Pull request %s synchronized by %s", repoLink, 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 by %s", repoLink, + text = fmt.Sprintf("[%s] Pull request %s milestoned to %s by %s", repoLink, linkFormatter(mileStoneLink, p.PullRequest.Milestone.Title), titleLink, senderLink) case api.HookIssueDemilestoned: - text = fmt.Sprintf("[%s] Pull request milestone cleared: %s %s", repoLink, titleLink, senderLink) + text = fmt.Sprintf("[%s] Pull request %s milestone cleared by %s", repoLink, titleLink, senderLink) } var attachmentText string @@ -154,27 +154,30 @@ func getIssueCommentPayloadInfo(p *api.IssueCommentPayload, linkFormatter linkFo senderLink := linkFormatter(setting.AppURL+p.Sender.UserName, p.Sender.UserName) repoLink := linkFormatter(p.Repository.HTMLURL, p.Repository.FullName) issueTitle := fmt.Sprintf("#%d %s", p.Issue.Index, p.Issue.Title) - var text, typ string + + var text, typ, titleLink string color := yellowColor if p.IsPull { typ = "pull request" + titleLink = linkFormatter(p.Comment.PRURL, issueTitle) } else { typ = "issue" + titleLink = linkFormatter(p.Comment.IssueURL, issueTitle) } switch p.Action { case api.HookIssueCommentCreated: - text = fmt.Sprintf("[%s] New comment on %s by %s", repoLink, typ, senderLink) + text = fmt.Sprintf("[%s] New comment on %s %s by %s", repoLink, typ, titleLink, senderLink) if p.IsPull { color = greenColorLight } else { color = orangeColorLight } case api.HookIssueCommentEdited: - text = fmt.Sprintf("[%s] Comment on %s edited by %s", repoLink, typ, senderLink) + text = fmt.Sprintf("[%s] Comment on %s %s edited by %s", repoLink, typ, titleLink, senderLink) case api.HookIssueCommentDeleted: - text = fmt.Sprintf("[%s] Comment on %s deleted by %s", repoLink, typ, senderLink) + text = fmt.Sprintf("[%s] Comment on %s %s deleted by %s", repoLink, typ, titleLink, senderLink) color = redColor } diff --git a/modules/webhook/general_test.go b/modules/webhook/general_test.go new file mode 100644 index 0000000000000..8d5a070ab4502 --- /dev/null +++ b/modules/webhook/general_test.go @@ -0,0 +1,81 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package webhook + +import api "code.gitea.io/gitea/modules/structs" + +func issueTestPayLoad() *api.IssuePayload { + return &api.IssuePayload{ + Index: 2, + Action: api.HookIssueClosed, + Sender: &api.User{ + UserName: "user1", + }, + Repository: &api.Repository{ + HTMLURL: "http://localhost:3000/test/repo", + Name: "repo", + FullName: "test/repo", + }, + Issue: &api.Issue{ + ID: 2, + Index: 2, + URL: "http://localhost:3000/api/v1/repos/test/repo/issues/2", + Title: "crash", + }, + } +} + +func issueCommentTestPayLoad() *api.IssueCommentPayload { + return &api.IssueCommentPayload{ + Action: api.HookIssueCommentCreated, + Sender: &api.User{ + UserName: "user1", + }, + Repository: &api.Repository{ + HTMLURL: "http://localhost:3000/test/repo", + Name: "repo", + FullName: "test/repo", + }, + Comment: &api.Comment{ + HTMLURL: "http://localhost:3000/test/repo/issues/2#issuecomment-4", + IssueURL: "http://localhost:3000/test/repo/issues/2", + Body: "more info needed", + }, + Issue: &api.Issue{ + ID: 2, + Index: 2, + URL: "http://localhost:3000/api/v1/repos/test/repo/issues/2", + Title: "crash", + Body: "this happened", + }, + } +} + +func pullRequestCommentTestPayLoad() *api.IssueCommentPayload { + return &api.IssueCommentPayload{ + Action: api.HookIssueCommentCreated, + Sender: &api.User{ + UserName: "user1", + }, + Repository: &api.Repository{ + HTMLURL: "http://localhost:3000/test/repo", + Name: "repo", + FullName: "test/repo", + }, + Comment: &api.Comment{ + HTMLURL: "http://localhost:3000/test/repo/pulls/2#issuecomment-4", + PRURL: "http://localhost:3000/test/repo/pulls/2", + Body: "changes requested", + }, + Issue: &api.Issue{ + ID: 2, + Index: 2, + URL: "http://localhost:3000/api/v1/repos/test/repo/issues/2", + Title: "Fix bug", + Body: "fixes bug #2", + }, + IsPull: true, + } +} diff --git a/modules/webhook/msteams.go b/modules/webhook/msteams.go index 5ade8ddf445e8..4c148421fe151 100644 --- a/modules/webhook/msteams.go +++ b/modules/webhook/msteams.go @@ -341,7 +341,7 @@ func getMSTeamsIssueCommentPayload(p *api.IssueCommentPayload) (*MSTeamsPayload, Targets: []MSTeamsActionTarget{ { Os: "default", - URI: p.Issue.URL, + URI: p.Comment.HTMLURL, }, }, }, diff --git a/modules/webhook/slack.go b/modules/webhook/slack.go index 5b4f13420358c..508cb13b8fb7e 100644 --- a/modules/webhook/slack.go +++ b/modules/webhook/slack.go @@ -144,7 +144,7 @@ func getSlackForkPayload(p *api.ForkPayload, slack *SlackMeta) (*SlackPayload, e } func getSlackIssuesPayload(p *api.IssuePayload, slack *SlackMeta) (*SlackPayload, error) { - text, issueTitle, attachmentText, _ := getIssuesPayloadInfo(p, SlackLinkFormatter) + text, issueTitle, attachmentText, color := getIssuesPayloadInfo(p, SlackLinkFormatter) pl := &SlackPayload{ Channel: slack.Channel, @@ -156,7 +156,7 @@ func getSlackIssuesPayload(p *api.IssuePayload, slack *SlackMeta) (*SlackPayload attachmentText = SlackTextFormatter(attachmentText) issueTitle = SlackTextFormatter(issueTitle) pl.Attachments = []SlackAttachment{{ - Color: slack.Color, + Color: fmt.Sprintf("%x", color), Title: issueTitle, TitleLink: p.Issue.URL, Text: attachmentText, @@ -167,7 +167,7 @@ func getSlackIssuesPayload(p *api.IssuePayload, slack *SlackMeta) (*SlackPayload } func getSlackIssueCommentPayload(p *api.IssueCommentPayload, slack *SlackMeta) (*SlackPayload, error) { - text, issueTitle, _ := getIssueCommentPayloadInfo(p, SlackLinkFormatter) + text, issueTitle, color := getIssueCommentPayloadInfo(p, SlackLinkFormatter) return &SlackPayload{ Channel: slack.Channel, @@ -175,9 +175,9 @@ func getSlackIssueCommentPayload(p *api.IssueCommentPayload, slack *SlackMeta) ( Username: slack.Username, IconURL: slack.IconURL, Attachments: []SlackAttachment{{ - Color: slack.Color, + Color: fmt.Sprintf("%x", color), Title: issueTitle, - TitleLink: p.Issue.URL, + TitleLink: p.Comment.HTMLURL, Text: SlackTextFormatter(p.Comment.Body), }}, }, nil @@ -239,7 +239,7 @@ func getSlackPushPayload(p *api.PushPayload, slack *SlackMeta) (*SlackPayload, e } func getSlackPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) (*SlackPayload, error) { - text, issueTitle, attachmentText, _ := getPullRequestPayloadInfo(p, SlackLinkFormatter) + text, issueTitle, attachmentText, color := getPullRequestPayloadInfo(p, SlackLinkFormatter) pl := &SlackPayload{ Channel: slack.Channel, @@ -251,7 +251,7 @@ func getSlackPullRequestPayload(p *api.PullRequestPayload, slack *SlackMeta) (*S attachmentText = SlackTextFormatter(p.PullRequest.Body) issueTitle = SlackTextFormatter(issueTitle) pl.Attachments = []SlackAttachment{{ - Color: slack.Color, + Color: fmt.Sprintf("%x", color), Title: issueTitle, TitleLink: p.PullRequest.URL, Text: attachmentText, diff --git a/modules/webhook/slack_test.go b/modules/webhook/slack_test.go index 006fe6a265973..b78467ba98c05 100644 --- a/modules/webhook/slack_test.go +++ b/modules/webhook/slack_test.go @@ -7,29 +7,12 @@ package webhook import ( "testing" - api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestSlackIssuesPayload(t *testing.T) { - p := &api.IssuePayload{ - Index: 1, - Action: api.HookIssueClosed, - Sender: &api.User{ - UserName: "user1", - }, - Repository: &api.Repository{ - HTMLURL: "http://localhost:3000/test/repo", - Name: "repo", - FullName: "test/repo", - }, - Issue: &api.Issue{ - ID: 2, - URL: "http://localhost:3000/api/v1/repos/test/repo/issues/2", - Title: "crash", - }, - } + p := issueTestPayLoad() sl := &SlackMeta{ Username: p.Sender.UserName, @@ -39,5 +22,33 @@ func TestSlackIssuesPayload(t *testing.T) { require.Nil(t, err) require.NotNil(t, pl) - assert.Equal(t, "[] Issue closed: by ", pl.Text) + assert.Equal(t, "[] Issue closed: by ", pl.Text) +} + +func TestSlackIssueCommentPayload(t *testing.T) { + p := issueCommentTestPayLoad() + + sl := &SlackMeta{ + Username: p.Sender.UserName, + } + + pl, err := getSlackIssueCommentPayload(p, sl) + require.Nil(t, err) + require.NotNil(t, pl) + + assert.Equal(t, "[] New comment on issue by ", pl.Text) +} + +func TestSlackPullRequestCommentPayload(t *testing.T) { + p := pullRequestCommentTestPayLoad() + + sl := &SlackMeta{ + Username: p.Sender.UserName, + } + + pl, err := getSlackIssueCommentPayload(p, sl) + require.Nil(t, err) + require.NotNil(t, pl) + + assert.Equal(t, "[] New comment on pull request by ", pl.Text) } diff --git a/modules/webhook/telegram_test.go b/modules/webhook/telegram_test.go index 96345931157e9..c3a9dececa5e1 100644 --- a/modules/webhook/telegram_test.go +++ b/modules/webhook/telegram_test.go @@ -7,34 +7,16 @@ package webhook import ( "testing" - api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestGetTelegramIssuesPayload(t *testing.T) { - p := &api.IssuePayload{ - Index: 1, - Action: api.HookIssueClosed, - Sender: &api.User{ - UserName: "user1", - }, - Repository: &api.Repository{ - HTMLURL: "http://localhost:3000/test/repo", - Name: "repo", - FullName: "test/repo", - }, - Issue: &api.Issue{ - ID: 2, - URL: "http://localhost:3000/api/v1/repos/test/repo/issues/2", - Title: "crash", - }, - } + p := issueTestPayLoad() pl, err := getTelegramIssuesPayload(p) require.Nil(t, err) require.NotNil(t, pl) - assert.Equal(t, "[test/repo] Issue closed: #1 crash by user1\n\n", pl.Message) - + assert.Equal(t, "[test/repo] Issue closed: #2 crash by user1\n\n", pl.Message) } From 4506db95f7ff148eb37175ec3bb824852fe239ee Mon Sep 17 00:00:00 2001 From: Gitea Date: Thu, 26 Dec 2019 16:24:00 +0800 Subject: [PATCH 8/9] Webhook tests extended --- modules/webhook/general_test.go | 45 ++++++++++++++++++++++++++++++++- modules/webhook/slack_test.go | 28 ++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/modules/webhook/general_test.go b/modules/webhook/general_test.go index 8d5a070ab4502..70e6d6d9e7e2d 100644 --- a/modules/webhook/general_test.go +++ b/modules/webhook/general_test.go @@ -8,8 +8,8 @@ import api "code.gitea.io/gitea/modules/structs" func issueTestPayLoad() *api.IssuePayload { return &api.IssuePayload{ - Index: 2, Action: api.HookIssueClosed, + Index: 2, Sender: &api.User{ UserName: "user1", }, @@ -79,3 +79,46 @@ func pullRequestCommentTestPayLoad() *api.IssueCommentPayload { IsPull: true, } } + +func pullReleaseTestPayLoad() *api.ReleasePayload { + return &api.ReleasePayload{ + Action: api.HookReleasePublished, + Sender: &api.User{ + UserName: "user1", + }, + Repository: &api.Repository{ + HTMLURL: "http://localhost:3000/test/repo", + Name: "repo", + FullName: "test/repo", + }, + Release: &api.Release{ + TagName: "v1.0", + Target: "master", + Title: "First stable release", + URL: "http://localhost:3000/api/v1/repos/test/repo/releases/2", + }, + } +} + +func pullRequestTestPayLoad() *api.PullRequestPayload { + return &api.PullRequestPayload{ + Action: api.HookIssueOpened, + Index: 2, + Sender: &api.User{ + UserName: "user1", + }, + Repository: &api.Repository{ + HTMLURL: "http://localhost:3000/test/repo", + Name: "repo", + FullName: "test/repo", + }, + PullRequest: &api.PullRequest{ + ID: 2, + Index: 2, + URL: "http://localhost:3000/test/repo/pulls/12", + Title: "Fix bug", + Body: "fixes bug #2", + Mergeable: true, + }, + } +} diff --git a/modules/webhook/slack_test.go b/modules/webhook/slack_test.go index b78467ba98c05..fa0945c18ff9e 100644 --- a/modules/webhook/slack_test.go +++ b/modules/webhook/slack_test.go @@ -52,3 +52,31 @@ func TestSlackPullRequestCommentPayload(t *testing.T) { assert.Equal(t, "[] New comment on pull request by ", pl.Text) } + +func TestSlackReleasePayload(t *testing.T) { + p := pullReleaseTestPayLoad() + + sl := &SlackMeta{ + Username: p.Sender.UserName, + } + + pl, err := getSlackReleasePayload(p, sl) + require.Nil(t, err) + require.NotNil(t, pl) + + assert.Equal(t, "[] Release created by ", pl.Text) +} + +func TestSlackPullRequestPayload(t *testing.T) { + p := pullRequestTestPayLoad() + + sl := &SlackMeta{ + Username: p.Sender.UserName, + } + + pl, err := getSlackPullRequestPayload(p, sl) + require.Nil(t, err) + require.NotNil(t, pl) + + assert.Equal(t, "[] Pull request opened by ", pl.Text) +} From 9eecded9405e2cfb8aabded278c64c0640b4341d Mon Sep 17 00:00:00 2001 From: Gitea Date: Sat, 28 Dec 2019 10:37:21 +0800 Subject: [PATCH 9/9] Fixed issue opened webhook --- modules/webhook/dingtalk_test.go | 31 +++++++++++++++++++++++++++++++ modules/webhook/general.go | 2 +- modules/webhook/general_test.go | 17 +++++++++-------- modules/webhook/slack_test.go | 20 +++++++++++++------- modules/webhook/telegram_test.go | 4 +++- 5 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 modules/webhook/dingtalk_test.go diff --git a/modules/webhook/dingtalk_test.go b/modules/webhook/dingtalk_test.go new file mode 100644 index 0000000000000..f50cb9a587914 --- /dev/null +++ b/modules/webhook/dingtalk_test.go @@ -0,0 +1,31 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package webhook + +import ( + "testing" + + api "code.gitea.io/gitea/modules/structs" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestGetDingTalkIssuesPayload(t *testing.T) { + p := issueTestPayload() + + p.Action = api.HookIssueOpened + pl, err := getDingtalkIssuesPayload(p) + require.Nil(t, err) + require.NotNil(t, pl) + assert.Equal(t, "#2 crash", pl.ActionCard.Title) + assert.Equal(t, "[test/repo] Issue opened: #2 crash by user1\r\n\r\n", pl.ActionCard.Text) + + p.Action = api.HookIssueClosed + pl, err = getDingtalkIssuesPayload(p) + require.Nil(t, err) + require.NotNil(t, pl) + assert.Equal(t, "#2 crash", pl.ActionCard.Title) + assert.Equal(t, "[test/repo] Issue closed: #2 crash by user1\r\n\r\n", pl.ActionCard.Text) +} diff --git a/modules/webhook/general.go b/modules/webhook/general.go index e8931b09062d2..28c3b2730d759 100644 --- a/modules/webhook/general.go +++ b/modules/webhook/general.go @@ -35,7 +35,7 @@ func getIssuesPayloadInfo(p *api.IssuePayload, linkFormatter linkFormatter) (str switch p.Action { case api.HookIssueOpened: - text = fmt.Sprintf("[%s] Issue opened by %s", repoLink, senderLink) + text = fmt.Sprintf("[%s] Issue opened: %s by %s", repoLink, titleLink, senderLink) color = orangeColor case api.HookIssueClosed: text = fmt.Sprintf("[%s] Issue closed: %s by %s", repoLink, titleLink, senderLink) diff --git a/modules/webhook/general_test.go b/modules/webhook/general_test.go index 70e6d6d9e7e2d..3033b578805b6 100644 --- a/modules/webhook/general_test.go +++ b/modules/webhook/general_test.go @@ -4,12 +4,13 @@ package webhook -import api "code.gitea.io/gitea/modules/structs" +import ( + api "code.gitea.io/gitea/modules/structs" +) -func issueTestPayLoad() *api.IssuePayload { +func issueTestPayload() *api.IssuePayload { return &api.IssuePayload{ - Action: api.HookIssueClosed, - Index: 2, + Index: 2, Sender: &api.User{ UserName: "user1", }, @@ -27,7 +28,7 @@ func issueTestPayLoad() *api.IssuePayload { } } -func issueCommentTestPayLoad() *api.IssueCommentPayload { +func issueCommentTestPayload() *api.IssueCommentPayload { return &api.IssueCommentPayload{ Action: api.HookIssueCommentCreated, Sender: &api.User{ @@ -53,7 +54,7 @@ func issueCommentTestPayLoad() *api.IssueCommentPayload { } } -func pullRequestCommentTestPayLoad() *api.IssueCommentPayload { +func pullRequestCommentTestPayload() *api.IssueCommentPayload { return &api.IssueCommentPayload{ Action: api.HookIssueCommentCreated, Sender: &api.User{ @@ -80,7 +81,7 @@ func pullRequestCommentTestPayLoad() *api.IssueCommentPayload { } } -func pullReleaseTestPayLoad() *api.ReleasePayload { +func pullReleaseTestPayload() *api.ReleasePayload { return &api.ReleasePayload{ Action: api.HookReleasePublished, Sender: &api.User{ @@ -100,7 +101,7 @@ func pullReleaseTestPayLoad() *api.ReleasePayload { } } -func pullRequestTestPayLoad() *api.PullRequestPayload { +func pullRequestTestPayload() *api.PullRequestPayload { return &api.PullRequestPayload{ Action: api.HookIssueOpened, Index: 2, diff --git a/modules/webhook/slack_test.go b/modules/webhook/slack_test.go index fa0945c18ff9e..fe4f1384fcdd6 100644 --- a/modules/webhook/slack_test.go +++ b/modules/webhook/slack_test.go @@ -7,26 +7,32 @@ package webhook import ( "testing" + api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) -func TestSlackIssuesPayload(t *testing.T) { - p := issueTestPayLoad() - +func TestSlackIssuesPayloadOpened(t *testing.T) { + p := issueTestPayload() sl := &SlackMeta{ Username: p.Sender.UserName, } + p.Action = api.HookIssueOpened pl, err := getSlackIssuesPayload(p, sl) require.Nil(t, err) require.NotNil(t, pl) + assert.Equal(t, "[] Issue opened: by ", pl.Text) + p.Action = api.HookIssueClosed + pl, err = getSlackIssuesPayload(p, sl) + require.Nil(t, err) + require.NotNil(t, pl) assert.Equal(t, "[] Issue closed: by ", pl.Text) } func TestSlackIssueCommentPayload(t *testing.T) { - p := issueCommentTestPayLoad() + p := issueCommentTestPayload() sl := &SlackMeta{ Username: p.Sender.UserName, @@ -40,7 +46,7 @@ func TestSlackIssueCommentPayload(t *testing.T) { } func TestSlackPullRequestCommentPayload(t *testing.T) { - p := pullRequestCommentTestPayLoad() + p := pullRequestCommentTestPayload() sl := &SlackMeta{ Username: p.Sender.UserName, @@ -54,7 +60,7 @@ func TestSlackPullRequestCommentPayload(t *testing.T) { } func TestSlackReleasePayload(t *testing.T) { - p := pullReleaseTestPayLoad() + p := pullReleaseTestPayload() sl := &SlackMeta{ Username: p.Sender.UserName, @@ -68,7 +74,7 @@ func TestSlackReleasePayload(t *testing.T) { } func TestSlackPullRequestPayload(t *testing.T) { - p := pullRequestTestPayLoad() + p := pullRequestTestPayload() sl := &SlackMeta{ Username: p.Sender.UserName, diff --git a/modules/webhook/telegram_test.go b/modules/webhook/telegram_test.go index c3a9dececa5e1..221dc9843c5b0 100644 --- a/modules/webhook/telegram_test.go +++ b/modules/webhook/telegram_test.go @@ -7,12 +7,14 @@ package webhook import ( "testing" + api "code.gitea.io/gitea/modules/structs" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestGetTelegramIssuesPayload(t *testing.T) { - p := issueTestPayLoad() + p := issueTestPayload() + p.Action = api.HookIssueClosed pl, err := getTelegramIssuesPayload(p) require.Nil(t, err)