2020-02-12 09:48:28 +01:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-02-12 09:48:28 +01:00
|
|
|
|
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
2024-03-07 23:18:38 +01:00
|
|
|
"context"
|
2020-02-12 09:48:28 +01:00
|
|
|
"fmt"
|
2024-03-07 23:18:38 +01:00
|
|
|
"net/http"
|
2020-02-12 09:48:28 +01:00
|
|
|
"strings"
|
|
|
|
|
2024-03-07 23:18:38 +01:00
|
|
|
webhook_model "code.gitea.io/gitea/models/webhook"
|
2020-02-12 09:48:28 +01:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2023-01-01 16:23:15 +01:00
|
|
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
2020-02-12 09:48:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// FeishuPayload represents
|
|
|
|
FeishuPayload struct {
|
2023-10-31 05:43:38 +01:00
|
|
|
MsgType string `json:"msg_type"` // text / post / image / share_chat / interactive / file /audio / media
|
2020-12-11 17:04:04 +01:00
|
|
|
Content struct {
|
|
|
|
Text string `json:"text"`
|
|
|
|
} `json:"content"`
|
2020-02-12 09:48:28 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-03-07 23:18:38 +01:00
|
|
|
func newFeishuTextPayload(text string) FeishuPayload {
|
|
|
|
return FeishuPayload{
|
2020-12-11 17:04:04 +01:00
|
|
|
MsgType: "text",
|
|
|
|
Content: struct {
|
|
|
|
Text string `json:"text"`
|
|
|
|
}{
|
2021-06-21 04:12:19 +02:00
|
|
|
Text: strings.TrimSpace(text),
|
2020-12-11 17:04:04 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-05 04:57:13 +02:00
|
|
|
// Create implements PayloadConvertor Create method
|
2024-03-07 23:18:38 +01:00
|
|
|
func (fc feishuConvertor) Create(p *api.CreatePayload) (FeishuPayload, error) {
|
2020-02-12 09:48:28 +01:00
|
|
|
// created tag/branch
|
2023-05-26 03:04:48 +02:00
|
|
|
refName := git.RefName(p.Ref).ShortName()
|
2020-12-11 17:04:04 +01:00
|
|
|
text := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName)
|
2020-02-12 09:48:28 +01:00
|
|
|
|
2020-12-11 17:04:04 +01:00
|
|
|
return newFeishuTextPayload(text), nil
|
2020-02-12 09:48:28 +01:00
|
|
|
}
|
|
|
|
|
2020-09-05 04:57:13 +02:00
|
|
|
// Delete implements PayloadConvertor Delete method
|
2024-03-07 23:18:38 +01:00
|
|
|
func (fc feishuConvertor) Delete(p *api.DeletePayload) (FeishuPayload, error) {
|
2020-02-12 09:48:28 +01:00
|
|
|
// created tag/branch
|
2023-05-26 03:04:48 +02:00
|
|
|
refName := git.RefName(p.Ref).ShortName()
|
2020-12-11 17:04:04 +01:00
|
|
|
text := fmt.Sprintf("[%s] %s %s deleted", p.Repo.FullName, p.RefType, refName)
|
2020-02-12 09:48:28 +01:00
|
|
|
|
2020-12-11 17:04:04 +01:00
|
|
|
return newFeishuTextPayload(text), nil
|
2020-02-12 09:48:28 +01:00
|
|
|
}
|
|
|
|
|
2020-09-05 04:57:13 +02:00
|
|
|
// Fork implements PayloadConvertor Fork method
|
2024-03-07 23:18:38 +01:00
|
|
|
func (fc feishuConvertor) Fork(p *api.ForkPayload) (FeishuPayload, error) {
|
2020-12-11 17:04:04 +01:00
|
|
|
text := fmt.Sprintf("%s is forked to %s", p.Forkee.FullName, p.Repo.FullName)
|
2020-02-12 09:48:28 +01:00
|
|
|
|
2020-12-11 17:04:04 +01:00
|
|
|
return newFeishuTextPayload(text), nil
|
2020-02-12 09:48:28 +01:00
|
|
|
}
|
|
|
|
|
2020-09-05 04:57:13 +02:00
|
|
|
// Push implements PayloadConvertor Push method
|
2024-03-07 23:18:38 +01:00
|
|
|
func (fc feishuConvertor) Push(p *api.PushPayload) (FeishuPayload, error) {
|
2020-02-12 09:48:28 +01:00
|
|
|
var (
|
2023-05-26 03:04:48 +02:00
|
|
|
branchName = git.RefName(p.Ref).ShortName()
|
2020-02-12 09:48:28 +01:00
|
|
|
commitDesc string
|
|
|
|
)
|
|
|
|
|
2022-01-20 18:46:10 +01:00
|
|
|
text := fmt.Sprintf("[%s:%s] %s\r\n", p.Repo.FullName, branchName, commitDesc)
|
2020-02-12 09:48:28 +01: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 04:12:19 +02:00
|
|
|
text += "\r\n"
|
2020-02-12 09:48:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 17:04:04 +01:00
|
|
|
return newFeishuTextPayload(text), nil
|
2020-02-12 09:48:28 +01:00
|
|
|
}
|
|
|
|
|
2020-09-05 04:57:13 +02:00
|
|
|
// Issue implements PayloadConvertor Issue method
|
2024-03-07 23:18:38 +01:00
|
|
|
func (fc feishuConvertor) Issue(p *api.IssuePayload) (FeishuPayload, error) {
|
2023-08-24 03:00:11 +02:00
|
|
|
title, link, by, operator, result, assignees := getIssuesInfo(p)
|
|
|
|
if assignees != "" {
|
|
|
|
if p.Action == api.HookIssueAssigned || p.Action == api.HookIssueUnassigned || p.Action == api.HookIssueMilestoned {
|
2024-03-07 23:18:38 +01:00
|
|
|
return newFeishuTextPayload(fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n%s\n\n%s", title, link, by, operator, result, assignees, p.Issue.Body)), nil
|
2023-08-24 03:00:11 +02:00
|
|
|
}
|
2024-03-07 23:18:38 +01:00
|
|
|
return newFeishuTextPayload(fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n\n%s", title, link, by, operator, assignees, p.Issue.Body)), nil
|
2023-08-24 03:00:11 +02:00
|
|
|
}
|
2024-03-07 23:18:38 +01:00
|
|
|
return newFeishuTextPayload(fmt.Sprintf("%s\n%s\n%s\n%s\n\n%s", title, link, by, operator, p.Issue.Body)), nil
|
2020-02-12 09:48:28 +01:00
|
|
|
}
|
|
|
|
|
2020-09-05 04:57:13 +02:00
|
|
|
// IssueComment implements PayloadConvertor IssueComment method
|
2024-03-07 23:18:38 +01:00
|
|
|
func (fc feishuConvertor) IssueComment(p *api.IssueCommentPayload) (FeishuPayload, error) {
|
2023-08-24 03:00:11 +02:00
|
|
|
title, link, by, operator := getIssuesCommentInfo(p)
|
|
|
|
return newFeishuTextPayload(fmt.Sprintf("%s\n%s\n%s\n%s\n\n%s", title, link, by, operator, p.Comment.Body)), nil
|
2020-02-12 09:48:28 +01:00
|
|
|
}
|
|
|
|
|
2020-09-05 04:57:13 +02:00
|
|
|
// PullRequest implements PayloadConvertor PullRequest method
|
2024-03-07 23:18:38 +01:00
|
|
|
func (fc feishuConvertor) PullRequest(p *api.PullRequestPayload) (FeishuPayload, error) {
|
2023-08-24 03:00:11 +02:00
|
|
|
title, link, by, operator, result, assignees := getPullRequestInfo(p)
|
|
|
|
if assignees != "" {
|
|
|
|
if p.Action == api.HookIssueAssigned || p.Action == api.HookIssueUnassigned || p.Action == api.HookIssueMilestoned {
|
2024-03-07 23:18:38 +01:00
|
|
|
return newFeishuTextPayload(fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n%s\n\n%s", title, link, by, operator, result, assignees, p.PullRequest.Body)), nil
|
2023-08-24 03:00:11 +02:00
|
|
|
}
|
2024-03-07 23:18:38 +01:00
|
|
|
return newFeishuTextPayload(fmt.Sprintf("%s\n%s\n%s\n%s\n%s\n\n%s", title, link, by, operator, assignees, p.PullRequest.Body)), nil
|
2023-08-24 03:00:11 +02:00
|
|
|
}
|
2024-03-07 23:18:38 +01:00
|
|
|
return newFeishuTextPayload(fmt.Sprintf("%s\n%s\n%s\n%s\n\n%s", title, link, by, operator, p.PullRequest.Body)), nil
|
2020-02-12 09:48:28 +01:00
|
|
|
}
|
|
|
|
|
2020-09-05 04:57:13 +02:00
|
|
|
// Review implements PayloadConvertor Review method
|
2024-03-07 23:18:38 +01:00
|
|
|
func (fc feishuConvertor) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (FeishuPayload, error) {
|
2021-06-21 04:12:19 +02:00
|
|
|
action, err := parseHookPullRequestEventType(event)
|
|
|
|
if err != nil {
|
2024-03-07 23:18:38 +01:00
|
|
|
return FeishuPayload{}, err
|
2020-02-12 09:48:28 +01:00
|
|
|
}
|
|
|
|
|
2021-06-21 04:12:19 +02: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 17:04:04 +01:00
|
|
|
return newFeishuTextPayload(title + "\r\n\r\n" + text), nil
|
2020-02-12 09:48:28 +01:00
|
|
|
}
|
|
|
|
|
2020-09-05 04:57:13 +02:00
|
|
|
// Repository implements PayloadConvertor Repository method
|
2024-03-07 23:18:38 +01:00
|
|
|
func (fc feishuConvertor) Repository(p *api.RepositoryPayload) (FeishuPayload, error) {
|
2020-12-11 17:04:04 +01:00
|
|
|
var text string
|
2020-02-12 09:48:28 +01:00
|
|
|
switch p.Action {
|
|
|
|
case api.HookRepoCreated:
|
2020-12-11 17:04:04 +01:00
|
|
|
text = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
|
|
|
|
return newFeishuTextPayload(text), nil
|
2020-02-12 09:48:28 +01:00
|
|
|
case api.HookRepoDeleted:
|
2020-12-11 17:04:04 +01:00
|
|
|
text = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
|
|
|
|
return newFeishuTextPayload(text), nil
|
2020-02-12 09:48:28 +01:00
|
|
|
}
|
|
|
|
|
2024-03-07 23:18:38 +01:00
|
|
|
return FeishuPayload{}, nil
|
2020-02-12 09:48:28 +01:00
|
|
|
}
|
|
|
|
|
2022-09-04 21:54:23 +02:00
|
|
|
// Wiki implements PayloadConvertor Wiki method
|
2024-03-07 23:18:38 +01:00
|
|
|
func (fc feishuConvertor) Wiki(p *api.WikiPayload) (FeishuPayload, error) {
|
2022-09-04 21:54:23 +02:00
|
|
|
text, _, _ := getWikiPayloadInfo(p, noneLinkFormatter, true)
|
|
|
|
|
|
|
|
return newFeishuTextPayload(text), nil
|
|
|
|
}
|
|
|
|
|
2020-09-05 04:57:13 +02:00
|
|
|
// Release implements PayloadConvertor Release method
|
2024-03-07 23:18:38 +01:00
|
|
|
func (fc feishuConvertor) Release(p *api.ReleasePayload) (FeishuPayload, error) {
|
2020-02-12 09:48:28 +01:00
|
|
|
text, _ := getReleasePayloadInfo(p, noneLinkFormatter, true)
|
|
|
|
|
2020-12-11 17:04:04 +01:00
|
|
|
return newFeishuTextPayload(text), nil
|
2020-02-12 09:48:28 +01:00
|
|
|
}
|
|
|
|
|
2024-03-07 23:18:38 +01:00
|
|
|
func (fc feishuConvertor) Package(p *api.PackagePayload) (FeishuPayload, error) {
|
2023-10-31 05:43:38 +01:00
|
|
|
text, _ := getPackagePayloadInfo(p, noneLinkFormatter, true)
|
|
|
|
|
|
|
|
return newFeishuTextPayload(text), nil
|
|
|
|
}
|
|
|
|
|
2024-03-07 23:18:38 +01:00
|
|
|
type feishuConvertor struct{}
|
|
|
|
|
|
|
|
var _ payloadConvertor[FeishuPayload] = feishuConvertor{}
|
|
|
|
|
|
|
|
func newFeishuRequest(ctx context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
|
|
|
|
return newJSONRequest(feishuConvertor{}, w, t, true)
|
2020-02-12 09:48:28 +01:00
|
|
|
}
|