diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2020-12-12 00:04:04 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-11 17:04:04 +0100 |
commit | ea914d0f7a65a95229fb769ae7d7d5d98bf72263 (patch) | |
tree | 86a23ab3f373571360af4a1a0548e02e83e15a7a /services/webhook | |
parent | b35546631e1efecbdae905d3925c5d76f9b96ded (diff) | |
download | gitea-ea914d0f7a65a95229fb769ae7d7d5d98bf72263.tar.gz gitea-ea914d0f7a65a95229fb769ae7d7d5d98bf72263.zip |
Fix bug for webhook and feishu caused by API changed (#13937)
* Fix bug for webhook
* Fix bug for feishu
Diffstat (limited to 'services/webhook')
-rw-r--r-- | services/webhook/feishu.go | 88 | ||||
-rw-r--r-- | services/webhook/webhook.go | 2 |
2 files changed, 34 insertions, 56 deletions
diff --git a/services/webhook/feishu.go b/services/webhook/feishu.go index 8e60dbba13..3d789e6c1f 100644 --- a/services/webhook/feishu.go +++ b/services/webhook/feishu.go @@ -17,11 +17,24 @@ import ( type ( // FeishuPayload represents FeishuPayload struct { - Title string `json:"title"` - Text string `json:"text"` + MsgType string `json:"msg_type"` // text / post / image / share_chat / interactive + Content struct { + Text string `json:"text"` + } `json:"content"` } ) +func newFeishuTextPayload(text string) *FeishuPayload { + return &FeishuPayload{ + MsgType: "text", + Content: struct { + Text string `json:"text"` + }{ + Text: text, + }, + } +} + // SetSecret sets the Feishu secret func (f *FeishuPayload) SetSecret(_ string) {} @@ -42,34 +55,25 @@ var ( func (f *FeishuPayload) Create(p *api.CreatePayload) (api.Payloader, error) { // created tag/branch refName := git.RefEndName(p.Ref) - title := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName) + text := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName) - return &FeishuPayload{ - Text: title, - Title: title, - }, nil + return newFeishuTextPayload(text), nil } // Delete implements PayloadConvertor Delete method func (f *FeishuPayload) Delete(p *api.DeletePayload) (api.Payloader, error) { // created tag/branch refName := git.RefEndName(p.Ref) - title := fmt.Sprintf("[%s] %s %s deleted", p.Repo.FullName, p.RefType, refName) + text := fmt.Sprintf("[%s] %s %s deleted", p.Repo.FullName, p.RefType, refName) - return &FeishuPayload{ - Text: title, - Title: title, - }, nil + return newFeishuTextPayload(text), nil } // Fork implements PayloadConvertor Fork method func (f *FeishuPayload) Fork(p *api.ForkPayload) (api.Payloader, error) { - title := fmt.Sprintf("%s is forked to %s", p.Forkee.FullName, p.Repo.FullName) + text := fmt.Sprintf("%s is forked to %s", p.Forkee.FullName, p.Repo.FullName) - return &FeishuPayload{ - Text: title, - Title: title, - }, nil + return newFeishuTextPayload(text), nil } // Push implements PayloadConvertor Push method @@ -79,9 +83,7 @@ func (f *FeishuPayload) Push(p *api.PushPayload) (api.Payloader, error) { commitDesc string ) - title := fmt.Sprintf("[%s:%s] %s", p.Repo.FullName, branchName, commitDesc) - - var text string + var text = fmt.Sprintf("[%s:%s] %s\n", p.Repo.FullName, branchName, commitDesc) // for each commit, generate attachment text for i, commit := range p.Commits { var authorName string @@ -96,40 +98,28 @@ func (f *FeishuPayload) Push(p *api.PushPayload) (api.Payloader, error) { } } - return &FeishuPayload{ - Text: text, - Title: title, - }, nil + return newFeishuTextPayload(text), nil } // Issue implements PayloadConvertor Issue method func (f *FeishuPayload) Issue(p *api.IssuePayload) (api.Payloader, error) { text, issueTitle, attachmentText, _ := getIssuesPayloadInfo(p, noneLinkFormatter, true) - return &FeishuPayload{ - Text: text + "\r\n\r\n" + attachmentText, - Title: issueTitle, - }, nil + return newFeishuTextPayload(issueTitle + "\r\n" + text + "\r\n\r\n" + attachmentText), nil } // IssueComment implements PayloadConvertor IssueComment method func (f *FeishuPayload) IssueComment(p *api.IssueCommentPayload) (api.Payloader, error) { text, issueTitle, _ := getIssueCommentPayloadInfo(p, noneLinkFormatter, true) - return &FeishuPayload{ - Text: text + "\r\n\r\n" + p.Comment.Body, - Title: issueTitle, - }, nil + return newFeishuTextPayload(issueTitle + "\r\n" + text + "\r\n\r\n" + p.Comment.Body), nil } // PullRequest implements PayloadConvertor PullRequest method func (f *FeishuPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, error) { text, issueTitle, attachmentText, _ := getPullRequestPayloadInfo(p, noneLinkFormatter, true) - return &FeishuPayload{ - Text: text + "\r\n\r\n" + attachmentText, - Title: issueTitle, - }, nil + return newFeishuTextPayload(issueTitle + "\r\n" + text + "\r\n\r\n" + attachmentText), nil } // Review implements PayloadConvertor Review method @@ -147,28 +137,19 @@ func (f *FeishuPayload) Review(p *api.PullRequestPayload, event models.HookEvent } - return &FeishuPayload{ - Text: title + "\r\n\r\n" + text, - Title: title, - }, nil + return newFeishuTextPayload(title + "\r\n\r\n" + text), nil } // Repository implements PayloadConvertor Repository method func (f *FeishuPayload) Repository(p *api.RepositoryPayload) (api.Payloader, error) { - var title string + var text string switch p.Action { case api.HookRepoCreated: - title = fmt.Sprintf("[%s] Repository created", p.Repository.FullName) - return &FeishuPayload{ - Text: title, - Title: title, - }, nil + text = fmt.Sprintf("[%s] Repository created", p.Repository.FullName) + return newFeishuTextPayload(text), nil case api.HookRepoDeleted: - title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName) - return &FeishuPayload{ - Title: title, - Text: title, - }, nil + text = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName) + return newFeishuTextPayload(text), nil } return nil, nil @@ -178,10 +159,7 @@ func (f *FeishuPayload) Repository(p *api.RepositoryPayload) (api.Payloader, err func (f *FeishuPayload) Release(p *api.ReleasePayload) (api.Payloader, error) { text, _ := getReleasePayloadInfo(p, noneLinkFormatter, true) - return &FeishuPayload{ - Text: text, - Title: text, - }, nil + return newFeishuTextPayload(text), nil } // GetFeishuPayload converts a ding talk webhook into a FeishuPayload diff --git a/services/webhook/webhook.go b/services/webhook/webhook.go index 88dec6bd40..a86d638ab5 100644 --- a/services/webhook/webhook.go +++ b/services/webhook/webhook.go @@ -144,7 +144,7 @@ func prepareWebhook(w *models.Webhook, repo *models.Repository, event models.Hoo var payloader api.Payloader var err error - webhook, ok := webhooks[w.Type] + webhook, ok := webhooks[strings.TrimSpace(w.Type)] // NOTICE: w.Type maynot be trimmed before store into database if ok { payloader, err = webhook.payloadCreator(p, event, w.Meta) if err != nil { |