2020-02-12 08:48:28 +00:00
|
|
|
// Copyright 2020 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 (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2021-11-10 05:13:16 +00:00
|
|
|
webhook_model "code.gitea.io/gitea/models/webhook"
|
2020-02-12 08:48:28 +00:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2021-07-24 16:03:58 +00:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2020-02-12 08:48:28 +00:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// FeishuPayload represents
|
|
|
|
FeishuPayload struct {
|
2020-12-11 16:04:04 +00:00
|
|
|
MsgType string `json:"msg_type"` // text / post / image / share_chat / interactive
|
|
|
|
Content struct {
|
|
|
|
Text string `json:"text"`
|
|
|
|
} `json:"content"`
|
2020-02-12 08:48:28 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-12-11 16:04:04 +00:00
|
|
|
func newFeishuTextPayload(text string) *FeishuPayload {
|
|
|
|
return &FeishuPayload{
|
|
|
|
MsgType: "text",
|
|
|
|
Content: struct {
|
|
|
|
Text string `json:"text"`
|
|
|
|
}{
|
2021-06-21 02:12:19 +00:00
|
|
|
Text: strings.TrimSpace(text),
|
2020-12-11 16:04:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 08:48:28 +00:00
|
|
|
// JSONPayload Marshals the FeishuPayload to json
|
2020-09-05 02:57:13 +00:00
|
|
|
func (f *FeishuPayload) JSONPayload() ([]byte, error) {
|
|
|
|
data, err := json.MarshalIndent(f, "", " ")
|
2020-02-12 08:48:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2020-09-05 02:57:13 +00:00
|
|
|
var (
|
|
|
|
_ PayloadConvertor = &FeishuPayload{}
|
|
|
|
)
|
|
|
|
|
|
|
|
// Create implements PayloadConvertor Create method
|
|
|
|
func (f *FeishuPayload) Create(p *api.CreatePayload) (api.Payloader, error) {
|
2020-02-12 08:48:28 +00:00
|
|
|
// created tag/branch
|
|
|
|
refName := git.RefEndName(p.Ref)
|
2020-12-11 16:04:04 +00:00
|
|
|
text := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName)
|
2020-02-12 08:48:28 +00:00
|
|
|
|
2020-12-11 16:04:04 +00:00
|
|
|
return newFeishuTextPayload(text), nil
|
2020-02-12 08:48:28 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 02:57:13 +00:00
|
|
|
// Delete implements PayloadConvertor Delete method
|
|
|
|
func (f *FeishuPayload) Delete(p *api.DeletePayload) (api.Payloader, error) {
|
2020-02-12 08:48:28 +00:00
|
|
|
// created tag/branch
|
|
|
|
refName := git.RefEndName(p.Ref)
|
2020-12-11 16:04:04 +00:00
|
|
|
text := fmt.Sprintf("[%s] %s %s deleted", p.Repo.FullName, p.RefType, refName)
|
2020-02-12 08:48:28 +00:00
|
|
|
|
2020-12-11 16:04:04 +00:00
|
|
|
return newFeishuTextPayload(text), nil
|
2020-02-12 08:48:28 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 02:57:13 +00:00
|
|
|
// Fork implements PayloadConvertor Fork method
|
|
|
|
func (f *FeishuPayload) Fork(p *api.ForkPayload) (api.Payloader, error) {
|
2020-12-11 16:04:04 +00:00
|
|
|
text := fmt.Sprintf("%s is forked to %s", p.Forkee.FullName, p.Repo.FullName)
|
2020-02-12 08:48:28 +00:00
|
|
|
|
2020-12-11 16:04:04 +00:00
|
|
|
return newFeishuTextPayload(text), nil
|
2020-02-12 08:48:28 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 02:57:13 +00:00
|
|
|
// Push implements PayloadConvertor Push method
|
|
|
|
func (f *FeishuPayload) Push(p *api.PushPayload) (api.Payloader, error) {
|
2020-02-12 08:48:28 +00:00
|
|
|
var (
|
|
|
|
branchName = git.RefEndName(p.Ref)
|
|
|
|
commitDesc string
|
|
|
|
)
|
|
|
|
|
2021-06-21 02:12:19 +00:00
|
|
|
var text = fmt.Sprintf("[%s:%s] %s\r\n", p.Repo.FullName, branchName, commitDesc)
|
2020-02-12 08:48:28 +00:00
|
|
|
// for each commit, generate attachment text
|
|
|
|
for i, commit := range p.Commits {
|
|
|
|
var authorName string
|
|
|
|
if commit.Author != nil {
|
|
|
|
authorName = " - " + commit.Author.Name
|
|
|
|
}
|
|
|
|
text += fmt.Sprintf("[%s](%s) %s", commit.ID[:7], commit.URL,
|
|
|
|
strings.TrimRight(commit.Message, "\r\n")) + authorName
|
|
|
|
// add linebreak to each commit but the last
|
|
|
|
if i < len(p.Commits)-1 {
|
2021-06-21 02:12:19 +00:00
|
|
|
text += "\r\n"
|
2020-02-12 08:48:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 16:04:04 +00:00
|
|
|
return newFeishuTextPayload(text), nil
|
2020-02-12 08:48:28 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 02:57:13 +00:00
|
|
|
// Issue implements PayloadConvertor Issue method
|
|
|
|
func (f *FeishuPayload) Issue(p *api.IssuePayload) (api.Payloader, error) {
|
2020-02-12 08:48:28 +00:00
|
|
|
text, issueTitle, attachmentText, _ := getIssuesPayloadInfo(p, noneLinkFormatter, true)
|
|
|
|
|
2020-12-11 16:04:04 +00:00
|
|
|
return newFeishuTextPayload(issueTitle + "\r\n" + text + "\r\n\r\n" + attachmentText), nil
|
2020-02-12 08:48:28 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 02:57:13 +00:00
|
|
|
// IssueComment implements PayloadConvertor IssueComment method
|
|
|
|
func (f *FeishuPayload) IssueComment(p *api.IssueCommentPayload) (api.Payloader, error) {
|
2020-02-12 08:48:28 +00:00
|
|
|
text, issueTitle, _ := getIssueCommentPayloadInfo(p, noneLinkFormatter, true)
|
|
|
|
|
2020-12-11 16:04:04 +00:00
|
|
|
return newFeishuTextPayload(issueTitle + "\r\n" + text + "\r\n\r\n" + p.Comment.Body), nil
|
2020-02-12 08:48:28 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 02:57:13 +00:00
|
|
|
// PullRequest implements PayloadConvertor PullRequest method
|
|
|
|
func (f *FeishuPayload) PullRequest(p *api.PullRequestPayload) (api.Payloader, error) {
|
2020-02-12 08:48:28 +00:00
|
|
|
text, issueTitle, attachmentText, _ := getPullRequestPayloadInfo(p, noneLinkFormatter, true)
|
|
|
|
|
2020-12-11 16:04:04 +00:00
|
|
|
return newFeishuTextPayload(issueTitle + "\r\n" + text + "\r\n\r\n" + attachmentText), nil
|
2020-02-12 08:48:28 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 02:57:13 +00:00
|
|
|
// Review implements PayloadConvertor Review method
|
2021-11-10 05:13:16 +00:00
|
|
|
func (f *FeishuPayload) Review(p *api.PullRequestPayload, event webhook_model.HookEventType) (api.Payloader, error) {
|
2021-06-21 02:12:19 +00:00
|
|
|
action, err := parseHookPullRequestEventType(event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-02-12 08:48:28 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 02:12:19 +00:00
|
|
|
title := fmt.Sprintf("[%s] Pull request review %s : #%d %s", p.Repository.FullName, action, p.Index, p.PullRequest.Title)
|
|
|
|
text := p.Review.Content
|
|
|
|
|
2020-12-11 16:04:04 +00:00
|
|
|
return newFeishuTextPayload(title + "\r\n\r\n" + text), nil
|
2020-02-12 08:48:28 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 02:57:13 +00:00
|
|
|
// Repository implements PayloadConvertor Repository method
|
|
|
|
func (f *FeishuPayload) Repository(p *api.RepositoryPayload) (api.Payloader, error) {
|
2020-12-11 16:04:04 +00:00
|
|
|
var text string
|
2020-02-12 08:48:28 +00:00
|
|
|
switch p.Action {
|
|
|
|
case api.HookRepoCreated:
|
2020-12-11 16:04:04 +00:00
|
|
|
text = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
|
|
|
|
return newFeishuTextPayload(text), nil
|
2020-02-12 08:48:28 +00:00
|
|
|
case api.HookRepoDeleted:
|
2020-12-11 16:04:04 +00:00
|
|
|
text = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
|
|
|
|
return newFeishuTextPayload(text), nil
|
2020-02-12 08:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-09-05 02:57:13 +00:00
|
|
|
// Release implements PayloadConvertor Release method
|
|
|
|
func (f *FeishuPayload) Release(p *api.ReleasePayload) (api.Payloader, error) {
|
2020-02-12 08:48:28 +00:00
|
|
|
text, _ := getReleasePayloadInfo(p, noneLinkFormatter, true)
|
|
|
|
|
2020-12-11 16:04:04 +00:00
|
|
|
return newFeishuTextPayload(text), nil
|
2020-02-12 08:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetFeishuPayload converts a ding talk webhook into a FeishuPayload
|
2021-11-10 05:13:16 +00:00
|
|
|
func GetFeishuPayload(p api.Payloader, event webhook_model.HookEventType, meta string) (api.Payloader, error) {
|
2020-09-05 02:57:13 +00:00
|
|
|
return convertPayloader(new(FeishuPayload), p, event)
|
2020-02-12 08:48:28 +00:00
|
|
|
}
|