2021-07-23 06:41:27 +02:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-07-23 06:41:27 +02:00
|
|
|
|
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
2024-03-07 23:18:38 +01:00
|
|
|
"context"
|
2021-07-23 06:41:27 +02:00
|
|
|
"fmt"
|
2024-03-22 16:02:48 +01:00
|
|
|
"html/template"
|
2024-03-07 23:18:38 +01:00
|
|
|
"net/http"
|
2021-07-23 06:41:27 +02:00
|
|
|
"strings"
|
|
|
|
|
2024-03-07 23:18:38 +01:00
|
|
|
webhook_model "code.gitea.io/gitea/models/webhook"
|
2021-07-23 06:41:27 +02: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"
|
2024-03-21 14:03:15 +01:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
2024-04-03 14:22:36 +02:00
|
|
|
"code.gitea.io/gitea/services/webhook/shared"
|
2021-07-23 06:41:27 +02:00
|
|
|
)
|
|
|
|
|
2024-03-20 15:44:01 +01:00
|
|
|
type wechatworkHandler struct{}
|
|
|
|
|
|
|
|
func (wechatworkHandler) Type() webhook_module.HookType { return webhook_module.WECHATWORK }
|
|
|
|
func (wechatworkHandler) Metadata(*webhook_model.Webhook) any { return nil }
|
|
|
|
|
2024-03-22 16:02:48 +01:00
|
|
|
func (wechatworkHandler) Icon(size int) template.HTML {
|
2024-04-03 14:22:36 +02:00
|
|
|
return shared.ImgIcon("wechatwork.png", size)
|
2024-03-22 16:02:48 +01:00
|
|
|
}
|
|
|
|
|
2024-04-03 14:22:36 +02:00
|
|
|
func (wechatworkHandler) UnmarshalForm(bind func(any)) forms.WebhookForm {
|
2024-03-21 14:03:15 +01:00
|
|
|
var form struct {
|
2024-04-03 14:22:36 +02:00
|
|
|
forms.WebhookCoreForm
|
2024-03-21 14:03:15 +01:00
|
|
|
PayloadURL string `binding:"Required;ValidUrl"`
|
|
|
|
}
|
|
|
|
bind(&form)
|
|
|
|
|
2024-04-03 14:22:36 +02:00
|
|
|
return forms.WebhookForm{
|
|
|
|
WebhookCoreForm: form.WebhookCoreForm,
|
|
|
|
URL: form.PayloadURL,
|
|
|
|
ContentType: webhook_model.ContentTypeJSON,
|
|
|
|
Secret: "",
|
|
|
|
HTTPMethod: http.MethodPost,
|
|
|
|
Metadata: nil,
|
2024-03-21 14:03:15 +01:00
|
|
|
}
|
2024-03-21 13:23:27 +01:00
|
|
|
}
|
|
|
|
|
2021-07-23 06:41:27 +02:00
|
|
|
type (
|
|
|
|
// WechatworkPayload represents
|
|
|
|
WechatworkPayload struct {
|
|
|
|
Msgtype string `json:"msgtype"`
|
|
|
|
Text struct {
|
|
|
|
Content string `json:"content"`
|
|
|
|
MentionedList []string `json:"mentioned_list"`
|
|
|
|
MentionedMobileList []string `json:"mentioned_mobile_list"`
|
|
|
|
} `json:"text"`
|
|
|
|
Markdown struct {
|
|
|
|
Content string `json:"content"`
|
|
|
|
} `json:"markdown"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-03-07 23:18:38 +01:00
|
|
|
func newWechatworkMarkdownPayload(title string) WechatworkPayload {
|
|
|
|
return WechatworkPayload{
|
2021-07-23 06:41:27 +02:00
|
|
|
Msgtype: "markdown",
|
|
|
|
Markdown: struct {
|
|
|
|
Content string `json:"content"`
|
|
|
|
}{
|
|
|
|
Content: title,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create implements PayloadConvertor Create method
|
2024-03-07 23:18:38 +01:00
|
|
|
func (wc wechatworkConvertor) Create(p *api.CreatePayload) (WechatworkPayload, error) {
|
2021-07-23 06:41:27 +02:00
|
|
|
// created tag/branch
|
2023-05-26 03:04:48 +02:00
|
|
|
refName := git.RefName(p.Ref).ShortName()
|
2021-07-23 06:41:27 +02:00
|
|
|
title := fmt.Sprintf("[%s] %s %s created", p.Repo.FullName, p.RefType, refName)
|
|
|
|
|
|
|
|
return newWechatworkMarkdownPayload(title), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete implements PayloadConvertor Delete method
|
2024-03-07 23:18:38 +01:00
|
|
|
func (wc wechatworkConvertor) Delete(p *api.DeletePayload) (WechatworkPayload, error) {
|
2021-07-23 06:41:27 +02:00
|
|
|
// created tag/branch
|
2023-05-26 03:04:48 +02:00
|
|
|
refName := git.RefName(p.Ref).ShortName()
|
2021-07-23 06:41:27 +02:00
|
|
|
title := fmt.Sprintf("[%s] %s %s deleted", p.Repo.FullName, p.RefType, refName)
|
|
|
|
|
|
|
|
return newWechatworkMarkdownPayload(title), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fork implements PayloadConvertor Fork method
|
2024-03-07 23:18:38 +01:00
|
|
|
func (wc wechatworkConvertor) Fork(p *api.ForkPayload) (WechatworkPayload, error) {
|
2021-07-23 06:41:27 +02:00
|
|
|
title := fmt.Sprintf("%s is forked to %s", p.Forkee.FullName, p.Repo.FullName)
|
|
|
|
|
|
|
|
return newWechatworkMarkdownPayload(title), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push implements PayloadConvertor Push method
|
2024-03-07 23:18:38 +01:00
|
|
|
func (wc wechatworkConvertor) Push(p *api.PushPayload) (WechatworkPayload, error) {
|
2021-07-23 06:41:27 +02:00
|
|
|
var (
|
2023-05-26 03:04:48 +02:00
|
|
|
branchName = git.RefName(p.Ref).ShortName()
|
2021-07-23 06:41:27 +02:00
|
|
|
commitDesc string
|
|
|
|
)
|
|
|
|
|
|
|
|
title := fmt.Sprintf("# %s:%s <font color=\"warning\"> %s </font>", p.Repo.FullName, branchName, commitDesc)
|
|
|
|
|
|
|
|
var text string
|
|
|
|
// for each commit, generate attachment text
|
|
|
|
for i, commit := range p.Commits {
|
|
|
|
var authorName string
|
|
|
|
if commit.Author != nil {
|
2022-10-16 18:22:34 +02:00
|
|
|
authorName = "Author: " + commit.Author.Name
|
2021-07-23 06:41:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
message := strings.ReplaceAll(commit.Message, "\n\n", "\r\n")
|
|
|
|
text += fmt.Sprintf(" > [%s](%s) \r\n ><font color=\"info\">%s</font> \n ><font color=\"warning\">%s</font>", commit.ID[:7], commit.URL,
|
|
|
|
message, authorName)
|
|
|
|
|
|
|
|
// add linebreak to each commit but the last
|
|
|
|
if i < len(p.Commits)-1 {
|
|
|
|
text += "\n"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newWechatworkMarkdownPayload(title + "\r\n\r\n" + text), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Issue implements PayloadConvertor Issue method
|
2024-03-07 23:18:38 +01:00
|
|
|
func (wc wechatworkConvertor) Issue(p *api.IssuePayload) (WechatworkPayload, error) {
|
2021-07-23 06:41:27 +02:00
|
|
|
text, issueTitle, attachmentText, _ := getIssuesPayloadInfo(p, noneLinkFormatter, true)
|
|
|
|
var content string
|
2021-12-25 14:30:09 +01:00
|
|
|
content += fmt.Sprintf(" ><font color=\"info\">%s</font>\n >%s \n ><font color=\"warning\"> %s</font> \n [%s](%s)", text, attachmentText, issueTitle, p.Issue.HTMLURL, p.Issue.HTMLURL)
|
2021-07-23 06:41:27 +02:00
|
|
|
|
|
|
|
return newWechatworkMarkdownPayload(content), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IssueComment implements PayloadConvertor IssueComment method
|
2024-03-07 23:18:38 +01:00
|
|
|
func (wc wechatworkConvertor) IssueComment(p *api.IssueCommentPayload) (WechatworkPayload, error) {
|
2021-07-23 06:41:27 +02:00
|
|
|
text, issueTitle, _ := getIssueCommentPayloadInfo(p, noneLinkFormatter, true)
|
|
|
|
var content string
|
2021-12-25 14:30:09 +01:00
|
|
|
content += fmt.Sprintf(" ><font color=\"info\">%s</font>\n >%s \n ><font color=\"warning\">%s</font> \n [%s](%s)", text, p.Comment.Body, issueTitle, p.Comment.HTMLURL, p.Comment.HTMLURL)
|
2021-07-23 06:41:27 +02:00
|
|
|
|
|
|
|
return newWechatworkMarkdownPayload(content), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PullRequest implements PayloadConvertor PullRequest method
|
2024-03-07 23:18:38 +01:00
|
|
|
func (wc wechatworkConvertor) PullRequest(p *api.PullRequestPayload) (WechatworkPayload, error) {
|
2021-07-23 06:41:27 +02:00
|
|
|
text, issueTitle, attachmentText, _ := getPullRequestPayloadInfo(p, noneLinkFormatter, true)
|
|
|
|
pr := fmt.Sprintf("> <font color=\"info\"> %s </font> \r\n > <font color=\"comment\">%s </font> \r\n > <font color=\"comment\">%s </font> \r\n",
|
|
|
|
text, issueTitle, attachmentText)
|
|
|
|
|
|
|
|
return newWechatworkMarkdownPayload(pr), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Review implements PayloadConvertor Review method
|
2024-03-07 23:18:38 +01:00
|
|
|
func (wc wechatworkConvertor) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (WechatworkPayload, error) {
|
2021-07-23 06:41:27 +02:00
|
|
|
var text, title string
|
2024-07-17 19:19:36 +02:00
|
|
|
if p.Action == api.HookIssueReviewed {
|
2021-07-23 06:41:27 +02:00
|
|
|
action, err := parseHookPullRequestEventType(event)
|
|
|
|
if err != nil {
|
2024-03-07 23:18:38 +01:00
|
|
|
return WechatworkPayload{}, err
|
2021-07-23 06:41:27 +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
|
|
|
|
}
|
|
|
|
|
|
|
|
return newWechatworkMarkdownPayload("# " + title + "\r\n\r\n >" + text), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repository implements PayloadConvertor Repository method
|
2024-03-07 23:18:38 +01:00
|
|
|
func (wc wechatworkConvertor) Repository(p *api.RepositoryPayload) (WechatworkPayload, error) {
|
2021-07-23 06:41:27 +02:00
|
|
|
var title string
|
|
|
|
switch p.Action {
|
|
|
|
case api.HookRepoCreated:
|
|
|
|
title = fmt.Sprintf("[%s] Repository created", p.Repository.FullName)
|
|
|
|
return newWechatworkMarkdownPayload(title), nil
|
|
|
|
case api.HookRepoDeleted:
|
|
|
|
title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
|
|
|
|
return newWechatworkMarkdownPayload(title), nil
|
|
|
|
}
|
|
|
|
|
2024-03-07 23:18:38 +01:00
|
|
|
return WechatworkPayload{}, nil
|
2021-07-23 06:41:27 +02:00
|
|
|
}
|
|
|
|
|
2022-09-04 21:54:23 +02:00
|
|
|
// Wiki implements PayloadConvertor Wiki method
|
2024-03-07 23:18:38 +01:00
|
|
|
func (wc wechatworkConvertor) Wiki(p *api.WikiPayload) (WechatworkPayload, error) {
|
2022-09-04 21:54:23 +02:00
|
|
|
text, _, _ := getWikiPayloadInfo(p, noneLinkFormatter, true)
|
|
|
|
|
|
|
|
return newWechatworkMarkdownPayload(text), nil
|
|
|
|
}
|
|
|
|
|
2021-07-23 06:41:27 +02:00
|
|
|
// Release implements PayloadConvertor Release method
|
2024-03-07 23:18:38 +01:00
|
|
|
func (wc wechatworkConvertor) Release(p *api.ReleasePayload) (WechatworkPayload, error) {
|
2021-07-23 06:41:27 +02:00
|
|
|
text, _ := getReleasePayloadInfo(p, noneLinkFormatter, true)
|
|
|
|
|
|
|
|
return newWechatworkMarkdownPayload(text), nil
|
|
|
|
}
|
|
|
|
|
2024-03-07 23:18:38 +01:00
|
|
|
func (wc wechatworkConvertor) Package(p *api.PackagePayload) (WechatworkPayload, error) {
|
2023-10-31 05:43:38 +01:00
|
|
|
text, _ := getPackagePayloadInfo(p, noneLinkFormatter, true)
|
|
|
|
|
|
|
|
return newWechatworkMarkdownPayload(text), nil
|
|
|
|
}
|
|
|
|
|
2024-03-07 23:18:38 +01:00
|
|
|
type wechatworkConvertor struct{}
|
|
|
|
|
2024-04-03 14:22:36 +02:00
|
|
|
var _ shared.PayloadConvertor[WechatworkPayload] = wechatworkConvertor{}
|
2024-03-07 23:18:38 +01:00
|
|
|
|
2024-03-20 15:44:01 +01:00
|
|
|
func (wechatworkHandler) NewRequest(ctx context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
|
2024-04-03 14:22:36 +02:00
|
|
|
return shared.NewJSONRequest(wechatworkConvertor{}, w, t, true)
|
2021-07-23 06:41:27 +02:00
|
|
|
}
|