2021-07-23 07:41:27 +03:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-07-23 07:41:27 +03:00
|
|
|
|
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
2024-03-08 01:18:38 +03:00
|
|
|
"context"
|
2021-07-23 07:41:27 +03:00
|
|
|
"fmt"
|
2024-03-22 18:02:48 +03:00
|
|
|
"html/template"
|
2024-03-08 01:18:38 +03:00
|
|
|
"net/http"
|
2021-07-23 07:41:27 +03:00
|
|
|
"strings"
|
|
|
|
|
2024-03-08 01:18:38 +03:00
|
|
|
webhook_model "code.gitea.io/gitea/models/webhook"
|
2021-07-23 07:41:27 +03:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2023-01-01 18:23:15 +03:00
|
|
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
2024-03-21 16:03:15 +03:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
2021-07-23 07:41:27 +03:00
|
|
|
)
|
|
|
|
|
2024-03-20 17:44:01 +03: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 18:02:48 +03:00
|
|
|
func (wechatworkHandler) Icon(size int) template.HTML {
|
|
|
|
return imgIcon("wechatwork.png", size)
|
|
|
|
}
|
|
|
|
|
2024-03-21 15:23:27 +03:00
|
|
|
func (wechatworkHandler) FormFields(bind func(any)) FormFields {
|
2024-03-21 16:03:15 +03:00
|
|
|
var form struct {
|
|
|
|
forms.WebhookForm
|
|
|
|
PayloadURL string `binding:"Required;ValidUrl"`
|
|
|
|
}
|
|
|
|
bind(&form)
|
|
|
|
|
|
|
|
return FormFields{
|
|
|
|
WebhookForm: form.WebhookForm,
|
|
|
|
URL: form.PayloadURL,
|
|
|
|
ContentType: webhook_model.ContentTypeJSON,
|
|
|
|
Secret: "",
|
|
|
|
HTTPMethod: http.MethodPost,
|
|
|
|
Metadata: nil,
|
|
|
|
}
|
2024-03-21 15:23:27 +03:00
|
|
|
}
|
|
|
|
|
2021-07-23 07:41:27 +03: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-08 01:18:38 +03:00
|
|
|
func newWechatworkMarkdownPayload(title string) WechatworkPayload {
|
|
|
|
return WechatworkPayload{
|
2021-07-23 07:41:27 +03:00
|
|
|
Msgtype: "markdown",
|
|
|
|
Markdown: struct {
|
|
|
|
Content string `json:"content"`
|
|
|
|
}{
|
|
|
|
Content: title,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create implements PayloadConvertor Create method
|
2024-03-08 01:18:38 +03:00
|
|
|
func (wc wechatworkConvertor) Create(p *api.CreatePayload) (WechatworkPayload, error) {
|
2021-07-23 07:41:27 +03:00
|
|
|
// created tag/branch
|
2023-05-26 04:04:48 +03:00
|
|
|
refName := git.RefName(p.Ref).ShortName()
|
2021-07-23 07:41:27 +03: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-08 01:18:38 +03:00
|
|
|
func (wc wechatworkConvertor) Delete(p *api.DeletePayload) (WechatworkPayload, error) {
|
2021-07-23 07:41:27 +03:00
|
|
|
// created tag/branch
|
2023-05-26 04:04:48 +03:00
|
|
|
refName := git.RefName(p.Ref).ShortName()
|
2021-07-23 07:41:27 +03: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-08 01:18:38 +03:00
|
|
|
func (wc wechatworkConvertor) Fork(p *api.ForkPayload) (WechatworkPayload, error) {
|
2021-07-23 07:41:27 +03: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-08 01:18:38 +03:00
|
|
|
func (wc wechatworkConvertor) Push(p *api.PushPayload) (WechatworkPayload, error) {
|
2021-07-23 07:41:27 +03:00
|
|
|
var (
|
2023-05-26 04:04:48 +03:00
|
|
|
branchName = git.RefName(p.Ref).ShortName()
|
2021-07-23 07:41:27 +03: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 19:22:34 +03:00
|
|
|
authorName = "Author: " + commit.Author.Name
|
2021-07-23 07:41:27 +03: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-08 01:18:38 +03:00
|
|
|
func (wc wechatworkConvertor) Issue(p *api.IssuePayload) (WechatworkPayload, error) {
|
2021-07-23 07:41:27 +03:00
|
|
|
text, issueTitle, attachmentText, _ := getIssuesPayloadInfo(p, noneLinkFormatter, true)
|
|
|
|
var content string
|
2021-12-25 16:30:09 +03: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 07:41:27 +03:00
|
|
|
|
|
|
|
return newWechatworkMarkdownPayload(content), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IssueComment implements PayloadConvertor IssueComment method
|
2024-03-08 01:18:38 +03:00
|
|
|
func (wc wechatworkConvertor) IssueComment(p *api.IssueCommentPayload) (WechatworkPayload, error) {
|
2021-07-23 07:41:27 +03:00
|
|
|
text, issueTitle, _ := getIssueCommentPayloadInfo(p, noneLinkFormatter, true)
|
|
|
|
var content string
|
2021-12-25 16:30:09 +03: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 07:41:27 +03:00
|
|
|
|
|
|
|
return newWechatworkMarkdownPayload(content), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PullRequest implements PayloadConvertor PullRequest method
|
2024-03-08 01:18:38 +03:00
|
|
|
func (wc wechatworkConvertor) PullRequest(p *api.PullRequestPayload) (WechatworkPayload, error) {
|
2021-07-23 07:41:27 +03: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-08 01:18:38 +03:00
|
|
|
func (wc wechatworkConvertor) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (WechatworkPayload, error) {
|
2021-07-23 07:41:27 +03:00
|
|
|
var text, title string
|
|
|
|
switch p.Action {
|
2022-11-19 18:19:14 +03:00
|
|
|
case api.HookIssueReviewed:
|
2021-07-23 07:41:27 +03:00
|
|
|
action, err := parseHookPullRequestEventType(event)
|
|
|
|
if err != nil {
|
2024-03-08 01:18:38 +03:00
|
|
|
return WechatworkPayload{}, err
|
2021-07-23 07:41:27 +03: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-08 01:18:38 +03:00
|
|
|
func (wc wechatworkConvertor) Repository(p *api.RepositoryPayload) (WechatworkPayload, error) {
|
2021-07-23 07:41:27 +03: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-08 01:18:38 +03:00
|
|
|
return WechatworkPayload{}, nil
|
2021-07-23 07:41:27 +03:00
|
|
|
}
|
|
|
|
|
2022-09-04 22:54:23 +03:00
|
|
|
// Wiki implements PayloadConvertor Wiki method
|
2024-03-08 01:18:38 +03:00
|
|
|
func (wc wechatworkConvertor) Wiki(p *api.WikiPayload) (WechatworkPayload, error) {
|
2022-09-04 22:54:23 +03:00
|
|
|
text, _, _ := getWikiPayloadInfo(p, noneLinkFormatter, true)
|
|
|
|
|
|
|
|
return newWechatworkMarkdownPayload(text), nil
|
|
|
|
}
|
|
|
|
|
2021-07-23 07:41:27 +03:00
|
|
|
// Release implements PayloadConvertor Release method
|
2024-03-08 01:18:38 +03:00
|
|
|
func (wc wechatworkConvertor) Release(p *api.ReleasePayload) (WechatworkPayload, error) {
|
2021-07-23 07:41:27 +03:00
|
|
|
text, _ := getReleasePayloadInfo(p, noneLinkFormatter, true)
|
|
|
|
|
|
|
|
return newWechatworkMarkdownPayload(text), nil
|
|
|
|
}
|
|
|
|
|
2024-03-08 01:18:38 +03:00
|
|
|
func (wc wechatworkConvertor) Package(p *api.PackagePayload) (WechatworkPayload, error) {
|
2023-10-31 07:43:38 +03:00
|
|
|
text, _ := getPackagePayloadInfo(p, noneLinkFormatter, true)
|
|
|
|
|
|
|
|
return newWechatworkMarkdownPayload(text), nil
|
|
|
|
}
|
|
|
|
|
2024-03-08 01:18:38 +03:00
|
|
|
type wechatworkConvertor struct{}
|
|
|
|
|
|
|
|
var _ payloadConvertor[WechatworkPayload] = wechatworkConvertor{}
|
|
|
|
|
2024-03-20 17:44:01 +03:00
|
|
|
func (wechatworkHandler) NewRequest(ctx context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
|
2024-03-08 01:18:38 +03:00
|
|
|
return newJSONRequest(wechatworkConvertor{}, w, t, true)
|
2021-07-23 07:41:27 +03:00
|
|
|
}
|