mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-19 08:20:13 +00:00
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>
This commit is contained in:
parent
8601440e81
commit
4fcae3d06d
15 changed files with 2127 additions and 908 deletions
|
@ -111,12 +111,7 @@ func (s *SlackPayload) Create(p *api.CreatePayload) (api.Payloader, error) {
|
|||
refLink := SlackLinkToRef(p.Repo.HTMLURL, p.Ref)
|
||||
text := fmt.Sprintf("[%s:%s] %s created by %s", repoLink, refLink, p.RefType, p.Sender.UserName)
|
||||
|
||||
return &SlackPayload{
|
||||
Channel: s.Channel,
|
||||
Text: text,
|
||||
Username: s.Username,
|
||||
IconURL: s.IconURL,
|
||||
}, nil
|
||||
return s.createPayload(text, nil), nil
|
||||
}
|
||||
|
||||
// Delete composes Slack payload for delete a branch or tag.
|
||||
|
@ -124,12 +119,8 @@ func (s *SlackPayload) Delete(p *api.DeletePayload) (api.Payloader, error) {
|
|||
refName := git.RefEndName(p.Ref)
|
||||
repoLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.FullName)
|
||||
text := fmt.Sprintf("[%s:%s] %s deleted by %s", repoLink, refName, p.RefType, p.Sender.UserName)
|
||||
return &SlackPayload{
|
||||
Channel: s.Channel,
|
||||
Text: text,
|
||||
Username: s.Username,
|
||||
IconURL: s.IconURL,
|
||||
}, nil
|
||||
|
||||
return s.createPayload(text, nil), nil
|
||||
}
|
||||
|
||||
// Fork composes Slack payload for forked by a repository.
|
||||
|
@ -137,66 +128,46 @@ func (s *SlackPayload) Fork(p *api.ForkPayload) (api.Payloader, error) {
|
|||
baseLink := SlackLinkFormatter(p.Forkee.HTMLURL, p.Forkee.FullName)
|
||||
forkLink := SlackLinkFormatter(p.Repo.HTMLURL, p.Repo.FullName)
|
||||
text := fmt.Sprintf("%s is forked to %s", baseLink, forkLink)
|
||||
return &SlackPayload{
|
||||
Channel: s.Channel,
|
||||
Text: text,
|
||||
Username: s.Username,
|
||||
IconURL: s.IconURL,
|
||||
}, nil
|
||||
|
||||
return s.createPayload(text, nil), nil
|
||||
}
|
||||
|
||||
// Issue implements PayloadConvertor Issue method
|
||||
func (s *SlackPayload) Issue(p *api.IssuePayload) (api.Payloader, error) {
|
||||
text, issueTitle, attachmentText, color := getIssuesPayloadInfo(p, SlackLinkFormatter, true)
|
||||
|
||||
pl := &SlackPayload{
|
||||
Channel: s.Channel,
|
||||
Text: text,
|
||||
Username: s.Username,
|
||||
IconURL: s.IconURL,
|
||||
}
|
||||
var attachments []SlackAttachment
|
||||
if attachmentText != "" {
|
||||
attachmentText = SlackTextFormatter(attachmentText)
|
||||
issueTitle = SlackTextFormatter(issueTitle)
|
||||
pl.Attachments = []SlackAttachment{{
|
||||
attachments = append(attachments, SlackAttachment{
|
||||
Color: fmt.Sprintf("%x", color),
|
||||
Title: issueTitle,
|
||||
TitleLink: p.Issue.HTMLURL,
|
||||
Text: attachmentText,
|
||||
}}
|
||||
})
|
||||
}
|
||||
|
||||
return pl, nil
|
||||
return s.createPayload(text, attachments), nil
|
||||
}
|
||||
|
||||
// IssueComment implements PayloadConvertor IssueComment method
|
||||
func (s *SlackPayload) IssueComment(p *api.IssueCommentPayload) (api.Payloader, error) {
|
||||
text, issueTitle, color := getIssueCommentPayloadInfo(p, SlackLinkFormatter, true)
|
||||
|
||||
return &SlackPayload{
|
||||
Channel: s.Channel,
|
||||
Text: text,
|
||||
Username: s.Username,
|
||||
IconURL: s.IconURL,
|
||||
Attachments: []SlackAttachment{{
|
||||
Color: fmt.Sprintf("%x", color),
|
||||
Title: issueTitle,
|
||||
TitleLink: p.Comment.HTMLURL,
|
||||
Text: SlackTextFormatter(p.Comment.Body),
|
||||
}},
|
||||
}, nil
|
||||
return s.createPayload(text, []SlackAttachment{{
|
||||
Color: fmt.Sprintf("%x", color),
|
||||
Title: issueTitle,
|
||||
TitleLink: p.Comment.HTMLURL,
|
||||
Text: SlackTextFormatter(p.Comment.Body),
|
||||
}}), nil
|
||||
}
|
||||
|
||||
// Release implements PayloadConvertor Release method
|
||||
func (s *SlackPayload) Release(p *api.ReleasePayload) (api.Payloader, error) {
|
||||
text, _ := getReleasePayloadInfo(p, SlackLinkFormatter, true)
|
||||
|
||||
return &SlackPayload{
|
||||
Channel: s.Channel,
|
||||
Text: text,
|
||||
Username: s.Username,
|
||||
IconURL: s.IconURL,
|
||||
}, nil
|
||||
return s.createPayload(text, nil), nil
|
||||
}
|
||||
|
||||
// Push implements PayloadConvertor Push method
|
||||
|
@ -232,42 +203,31 @@ func (s *SlackPayload) Push(p *api.PushPayload) (api.Payloader, error) {
|
|||
}
|
||||
}
|
||||
|
||||
return &SlackPayload{
|
||||
Channel: s.Channel,
|
||||
Text: text,
|
||||
Username: s.Username,
|
||||
IconURL: s.IconURL,
|
||||
Attachments: []SlackAttachment{{
|
||||
Color: s.Color,
|
||||
Title: p.Repo.HTMLURL,
|
||||
TitleLink: p.Repo.HTMLURL,
|
||||
Text: attachmentText,
|
||||
}},
|
||||
}, nil
|
||||
return s.createPayload(text, []SlackAttachment{{
|
||||
Color: s.Color,
|
||||
Title: p.Repo.HTMLURL,
|
||||
TitleLink: p.Repo.HTMLURL,
|
||||
Text: attachmentText,
|
||||
}}), nil
|
||||
}
|
||||
|
||||
// PullRequest implements PayloadConvertor PullRequest method
|
||||
func (s *SlackPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, error) {
|
||||
text, issueTitle, attachmentText, color := getPullRequestPayloadInfo(p, SlackLinkFormatter, true)
|
||||
|
||||
pl := &SlackPayload{
|
||||
Channel: s.Channel,
|
||||
Text: text,
|
||||
Username: s.Username,
|
||||
IconURL: s.IconURL,
|
||||
}
|
||||
var attachments []SlackAttachment
|
||||
if attachmentText != "" {
|
||||
attachmentText = SlackTextFormatter(p.PullRequest.Body)
|
||||
issueTitle = SlackTextFormatter(issueTitle)
|
||||
pl.Attachments = []SlackAttachment{{
|
||||
attachments = append(attachments, SlackAttachment{
|
||||
Color: fmt.Sprintf("%x", color),
|
||||
Title: issueTitle,
|
||||
TitleLink: p.PullRequest.URL,
|
||||
Text: attachmentText,
|
||||
}}
|
||||
})
|
||||
}
|
||||
|
||||
return pl, nil
|
||||
return s.createPayload(text, attachments), nil
|
||||
}
|
||||
|
||||
// Review implements PayloadConvertor Review method
|
||||
|
@ -288,12 +248,7 @@ func (s *SlackPayload) Review(p *api.PullRequestPayload, event models.HookEventT
|
|||
text = fmt.Sprintf("[%s] Pull request review %s: [%s](%s) by %s", repoLink, action, title, titleLink, senderLink)
|
||||
}
|
||||
|
||||
return &SlackPayload{
|
||||
Channel: s.Channel,
|
||||
Text: text,
|
||||
Username: s.Username,
|
||||
IconURL: s.IconURL,
|
||||
}, nil
|
||||
return s.createPayload(text, nil), nil
|
||||
}
|
||||
|
||||
// Repository implements PayloadConvertor Repository method
|
||||
|
@ -309,12 +264,17 @@ func (s *SlackPayload) Repository(p *api.RepositoryPayload) (api.Payloader, erro
|
|||
text = fmt.Sprintf("[%s] Repository deleted by %s", repoLink, senderLink)
|
||||
}
|
||||
|
||||
return s.createPayload(text, nil), nil
|
||||
}
|
||||
|
||||
func (s *SlackPayload) createPayload(text string, attachments []SlackAttachment) *SlackPayload {
|
||||
return &SlackPayload{
|
||||
Channel: s.Channel,
|
||||
Text: text,
|
||||
Username: s.Username,
|
||||
IconURL: s.IconURL,
|
||||
}, nil
|
||||
Channel: s.Channel,
|
||||
Text: text,
|
||||
Username: s.Username,
|
||||
IconURL: s.IconURL,
|
||||
Attachments: attachments,
|
||||
}
|
||||
}
|
||||
|
||||
// GetSlackPayload converts a slack webhook into a SlackPayload
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue