summaryrefslogtreecommitdiffstats
path: root/services/webhook/dingtalk.go
diff options
context:
space:
mode:
authorKN4CK3R <admin@oldschoolhack.me>2021-06-21 04:12:19 +0200
committerGitHub <noreply@github.com>2021-06-20 22:12:19 -0400
commit4fcae3d06d1099ade682df96e41baf4f5ac56620 (patch)
treeaf3608da89c4765711d7d905773336a7ecbd68e1 /services/webhook/dingtalk.go
parent8601440e81e0e609ab8049fb6dae8307db78fd6c (diff)
downloadgitea-4fcae3d06d1099ade682df96e41baf4f5ac56620.tar.gz
gitea-4fcae3d06d1099ade682df96e41baf4f5ac56620.zip
Add tests for all webhooks (#16214)
* Added tests for MS Teams. * Added tests for Dingtalk. * Added tests for Telegram. * Added tests for Feishu. * Added tests for Discord. * Added tests for closed issue and pullrequest comment. * Added tests for Matrix. * Trim all spaces. * Added tests for Slack. * Added JSONPayload tests. * Added general tests. * Replaced duplicated code. Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'services/webhook/dingtalk.go')
-rw-r--r--services/webhook/dingtalk.go123
1 files changed, 21 insertions, 102 deletions
diff --git a/services/webhook/dingtalk.go b/services/webhook/dingtalk.go
index 0401464a44..d781b8c87d 100644
--- a/services/webhook/dingtalk.go
+++ b/services/webhook/dingtalk.go
@@ -44,16 +44,7 @@ func (d *DingtalkPayload) Create(p *api.CreatePayload) (api.Payloader, error) {
refName := git.RefEndName(p.Ref)
title := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName)
- return &DingtalkPayload{
- MsgType: "actionCard",
- ActionCard: dingtalk.ActionCard{
- Text: title,
- Title: title,
- HideAvatar: "0",
- SingleTitle: fmt.Sprintf("view ref %s", refName),
- SingleURL: p.Repo.HTMLURL + "/src/" + refName,
- },
- }, nil
+ return createDingtalkPayload(title, title, fmt.Sprintf("view ref %s", refName), p.Repo.HTMLURL+"/src/"+refName), nil
}
// Delete implements PayloadConvertor Delete method
@@ -62,32 +53,14 @@ func (d *DingtalkPayload) Delete(p *api.DeletePayload) (api.Payloader, error) {
refName := git.RefEndName(p.Ref)
title := fmt.Sprintf("[%s] %s %s deleted", p.Repo.FullName, p.RefType, refName)
- return &DingtalkPayload{
- MsgType: "actionCard",
- ActionCard: dingtalk.ActionCard{
- Text: title,
- Title: title,
- HideAvatar: "0",
- SingleTitle: fmt.Sprintf("view ref %s", refName),
- SingleURL: p.Repo.HTMLURL + "/src/" + refName,
- },
- }, nil
+ return createDingtalkPayload(title, title, fmt.Sprintf("view ref %s", refName), p.Repo.HTMLURL+"/src/"+refName), nil
}
// Fork implements PayloadConvertor Fork method
func (d *DingtalkPayload) Fork(p *api.ForkPayload) (api.Payloader, error) {
title := fmt.Sprintf("%s is forked to %s", p.Forkee.FullName, p.Repo.FullName)
- return &DingtalkPayload{
- MsgType: "actionCard",
- ActionCard: dingtalk.ActionCard{
- Text: title,
- Title: title,
- HideAvatar: "0",
- SingleTitle: fmt.Sprintf("view forked repo %s", p.Repo.FullName),
- SingleURL: p.Repo.HTMLURL,
- },
- }, nil
+ return createDingtalkPayload(title, title, fmt.Sprintf("view forked repo %s", p.Repo.FullName), p.Repo.HTMLURL), nil
}
// Push implements PayloadConvertor Push method
@@ -124,70 +97,32 @@ func (d *DingtalkPayload) Push(p *api.PushPayload) (api.Payloader, error) {
strings.TrimRight(commit.Message, "\r\n")) + authorName
// add linebreak to each commit but the last
if i < len(p.Commits)-1 {
- text += "\n"
+ text += "\r\n"
}
}
- return &DingtalkPayload{
- MsgType: "actionCard",
- ActionCard: dingtalk.ActionCard{
- Text: text,
- Title: title,
- HideAvatar: "0",
- SingleTitle: linkText,
- SingleURL: titleLink,
- },
- }, nil
+ return createDingtalkPayload(title, text, linkText, titleLink), nil
}
// Issue implements PayloadConvertor Issue method
func (d *DingtalkPayload) Issue(p *api.IssuePayload) (api.Payloader, error) {
text, issueTitle, attachmentText, _ := getIssuesPayloadInfo(p, noneLinkFormatter, true)
- return &DingtalkPayload{
- MsgType: "actionCard",
- ActionCard: dingtalk.ActionCard{
- Text: text + "\r\n\r\n" + attachmentText,
- //Markdown: "# " + title + "\n" + text,
- Title: issueTitle,
- HideAvatar: "0",
- SingleTitle: "view issue",
- SingleURL: p.Issue.HTMLURL,
- },
- }, nil
+ return createDingtalkPayload(issueTitle, text+"\r\n\r\n"+attachmentText, "view issue", p.Issue.HTMLURL), nil
}
// IssueComment implements PayloadConvertor IssueComment method
func (d *DingtalkPayload) IssueComment(p *api.IssueCommentPayload) (api.Payloader, error) {
text, issueTitle, _ := getIssueCommentPayloadInfo(p, noneLinkFormatter, true)
- return &DingtalkPayload{
- MsgType: "actionCard",
- ActionCard: dingtalk.ActionCard{
- Text: text + "\r\n\r\n" + p.Comment.Body,
- Title: issueTitle,
- HideAvatar: "0",
- SingleTitle: "view issue comment",
- SingleURL: p.Comment.HTMLURL,
- },
- }, nil
+ return createDingtalkPayload(issueTitle, text+"\r\n\r\n"+p.Comment.Body, "view issue comment", p.Comment.HTMLURL), nil
}
// PullRequest implements PayloadConvertor PullRequest method
func (d *DingtalkPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, error) {
text, issueTitle, attachmentText, _ := getPullRequestPayloadInfo(p, noneLinkFormatter, true)
- return &DingtalkPayload{
- MsgType: "actionCard",
- ActionCard: dingtalk.ActionCard{
- Text: text + "\r\n\r\n" + attachmentText,
- //Markdown: "# " + title + "\n" + text,
- Title: issueTitle,
- HideAvatar: "0",
- SingleTitle: "view pull request",
- SingleURL: p.PullRequest.HTMLURL,
- },
- }, nil
+ return createDingtalkPayload(issueTitle, text+"\r\n\r\n"+attachmentText, "view pull request", p.PullRequest.HTMLURL), nil
}
// Review implements PayloadConvertor Review method
@@ -205,37 +140,17 @@ func (d *DingtalkPayload) Review(p *api.PullRequestPayload, event models.HookEve
}
- return &DingtalkPayload{
- MsgType: "actionCard",
- ActionCard: dingtalk.ActionCard{
- Text: title + "\r\n\r\n" + text,
- Title: title,
- HideAvatar: "0",
- SingleTitle: "view pull request",
- SingleURL: p.PullRequest.HTMLURL,
- },
- }, nil
+ return createDingtalkPayload(title, title+"\r\n\r\n"+text, "view pull request", p.PullRequest.HTMLURL), nil
}
// Repository implements PayloadConvertor Repository method
func (d *DingtalkPayload) Repository(p *api.RepositoryPayload) (api.Payloader, error) {
- var title, url string
switch p.Action {
case api.HookRepoCreated:
- title = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
- url = p.Repository.HTMLURL
- return &DingtalkPayload{
- MsgType: "actionCard",
- ActionCard: dingtalk.ActionCard{
- Text: title,
- Title: title,
- HideAvatar: "0",
- SingleTitle: "view repository",
- SingleURL: url,
- },
- }, nil
+ title := fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
+ return createDingtalkPayload(title, title, "view repository", p.Repository.HTMLURL), nil
case api.HookRepoDeleted:
- title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
+ title := fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
return &DingtalkPayload{
MsgType: "text",
Text: struct {
@@ -253,16 +168,20 @@ func (d *DingtalkPayload) Repository(p *api.RepositoryPayload) (api.Payloader, e
func (d *DingtalkPayload) Release(p *api.ReleasePayload) (api.Payloader, error) {
text, _ := getReleasePayloadInfo(p, noneLinkFormatter, true)
+ return createDingtalkPayload(text, text, "view release", p.Release.URL), nil
+}
+
+func createDingtalkPayload(title, text, singleTitle, singleURL string) *DingtalkPayload {
return &DingtalkPayload{
MsgType: "actionCard",
ActionCard: dingtalk.ActionCard{
- Text: text,
- Title: text,
+ Text: strings.TrimSpace(text),
+ Title: strings.TrimSpace(title),
HideAvatar: "0",
- SingleTitle: "view release",
- SingleURL: p.Release.URL,
+ SingleTitle: singleTitle,
+ SingleURL: singleURL,
},
- }, nil
+ }
}
// GetDingtalkPayload converts a ding talk webhook into a DingtalkPayload