Fix feishu webhook caused by API changed (#13937) (#13938)

fix #13858
This commit is contained in:
Lunny Xiao 2020-12-11 23:11:32 +08:00 committed by GitHub
parent 5cdffc2b0c
commit 0d7cb2323f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 33 additions and 55 deletions

View File

@ -17,11 +17,24 @@ import (
type ( type (
// FeishuPayload represents // FeishuPayload represents
FeishuPayload struct { FeishuPayload struct {
Title string `json:"title"` MsgType string `json:"msg_type"` // text / post / image / share_chat / interactive
Content struct {
Text string `json:"text"` 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 // SetSecret sets the Feishu secret
func (f *FeishuPayload) SetSecret(_ string) {} func (f *FeishuPayload) SetSecret(_ string) {}
@ -42,34 +55,25 @@ var (
func (f *FeishuPayload) Create(p *api.CreatePayload) (api.Payloader, error) { func (f *FeishuPayload) Create(p *api.CreatePayload) (api.Payloader, error) {
// created tag/branch // created tag/branch
refName := git.RefEndName(p.Ref) 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{ return newFeishuTextPayload(text), nil
Text: title,
Title: title,
}, nil
} }
// Delete implements PayloadConvertor Delete method // Delete implements PayloadConvertor Delete method
func (f *FeishuPayload) Delete(p *api.DeletePayload) (api.Payloader, error) { func (f *FeishuPayload) Delete(p *api.DeletePayload) (api.Payloader, error) {
// created tag/branch // created tag/branch
refName := git.RefEndName(p.Ref) 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{ return newFeishuTextPayload(text), nil
Text: title,
Title: title,
}, nil
} }
// Fork implements PayloadConvertor Fork method // Fork implements PayloadConvertor Fork method
func (f *FeishuPayload) Fork(p *api.ForkPayload) (api.Payloader, error) { 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{ return newFeishuTextPayload(text), nil
Text: title,
Title: title,
}, nil
} }
// Push implements PayloadConvertor Push method // Push implements PayloadConvertor Push method
@ -79,9 +83,7 @@ func (f *FeishuPayload) Push(p *api.PushPayload) (api.Payloader, error) {
commitDesc string commitDesc string
) )
title := fmt.Sprintf("[%s:%s] %s", p.Repo.FullName, branchName, commitDesc) var text = fmt.Sprintf("[%s:%s] %s\n", p.Repo.FullName, branchName, commitDesc)
var text string
// for each commit, generate attachment text // for each commit, generate attachment text
for i, commit := range p.Commits { for i, commit := range p.Commits {
var authorName string var authorName string
@ -96,40 +98,28 @@ func (f *FeishuPayload) Push(p *api.PushPayload) (api.Payloader, error) {
} }
} }
return &FeishuPayload{ return newFeishuTextPayload(text), nil
Text: text,
Title: title,
}, nil
} }
// Issue implements PayloadConvertor Issue method // Issue implements PayloadConvertor Issue method
func (f *FeishuPayload) Issue(p *api.IssuePayload) (api.Payloader, error) { func (f *FeishuPayload) Issue(p *api.IssuePayload) (api.Payloader, error) {
text, issueTitle, attachmentText, _ := getIssuesPayloadInfo(p, noneLinkFormatter, true) text, issueTitle, attachmentText, _ := getIssuesPayloadInfo(p, noneLinkFormatter, true)
return &FeishuPayload{ return newFeishuTextPayload(issueTitle + "\r\n" + text + "\r\n\r\n" + attachmentText), nil
Text: text + "\r\n\r\n" + attachmentText,
Title: issueTitle,
}, nil
} }
// IssueComment implements PayloadConvertor IssueComment method // IssueComment implements PayloadConvertor IssueComment method
func (f *FeishuPayload) IssueComment(p *api.IssueCommentPayload) (api.Payloader, error) { func (f *FeishuPayload) IssueComment(p *api.IssueCommentPayload) (api.Payloader, error) {
text, issueTitle, _ := getIssueCommentPayloadInfo(p, noneLinkFormatter, true) text, issueTitle, _ := getIssueCommentPayloadInfo(p, noneLinkFormatter, true)
return &FeishuPayload{ return newFeishuTextPayload(issueTitle + "\r\n" + text + "\r\n\r\n" + p.Comment.Body), nil
Text: text + "\r\n\r\n" + p.Comment.Body,
Title: issueTitle,
}, nil
} }
// PullRequest implements PayloadConvertor PullRequest method // PullRequest implements PayloadConvertor PullRequest method
func (f *FeishuPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, error) { func (f *FeishuPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, error) {
text, issueTitle, attachmentText, _ := getPullRequestPayloadInfo(p, noneLinkFormatter, true) text, issueTitle, attachmentText, _ := getPullRequestPayloadInfo(p, noneLinkFormatter, true)
return &FeishuPayload{ return newFeishuTextPayload(issueTitle + "\r\n" + text + "\r\n\r\n" + attachmentText), nil
Text: text + "\r\n\r\n" + attachmentText,
Title: issueTitle,
}, nil
} }
// Review implements PayloadConvertor Review method // Review implements PayloadConvertor Review method
@ -147,28 +137,19 @@ func (f *FeishuPayload) Review(p *api.PullRequestPayload, event models.HookEvent
} }
return &FeishuPayload{ return newFeishuTextPayload(title + "\r\n\r\n" + text), nil
Text: title + "\r\n\r\n" + text,
Title: title,
}, nil
} }
// Repository implements PayloadConvertor Repository method // Repository implements PayloadConvertor Repository method
func (f *FeishuPayload) Repository(p *api.RepositoryPayload) (api.Payloader, error) { func (f *FeishuPayload) Repository(p *api.RepositoryPayload) (api.Payloader, error) {
var title string var text string
switch p.Action { switch p.Action {
case api.HookRepoCreated: case api.HookRepoCreated:
title = fmt.Sprintf("[%s] Repository created", p.Repository.FullName) text = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
return &FeishuPayload{ return newFeishuTextPayload(text), nil
Text: title,
Title: title,
}, nil
case api.HookRepoDeleted: case api.HookRepoDeleted:
title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName) text = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
return &FeishuPayload{ return newFeishuTextPayload(text), nil
Title: title,
Text: title,
}, nil
} }
return nil, 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) { func (f *FeishuPayload) Release(p *api.ReleasePayload) (api.Payloader, error) {
text, _ := getReleasePayloadInfo(p, noneLinkFormatter, true) text, _ := getReleasePayloadInfo(p, noneLinkFormatter, true)
return &FeishuPayload{ return newFeishuTextPayload(text), nil
Text: text,
Title: text,
}, nil
} }
// GetFeishuPayload converts a ding talk webhook into a FeishuPayload // GetFeishuPayload converts a ding talk webhook into a FeishuPayload