2021-07-23 12:41:27 +08:00
// Copyright 2021 The Gitea Authors. All rights reserved.
2022-11-27 13:20:29 -05:00
// SPDX-License-Identifier: MIT
2021-07-23 12:41:27 +08:00
package webhook
import (
2024-03-07 23:18:38 +01:00
"context"
2021-07-23 12:41:27 +08:00
"fmt"
2024-03-07 23:18:38 +01:00
"net/http"
2021-07-23 12:41:27 +08:00
"strings"
2024-03-07 23:18:38 +01:00
webhook_model "code.gitea.io/gitea/models/webhook"
2021-07-23 12:41:27 +08: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"
2021-07-23 12:41:27 +08: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 12:41:27 +08: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 12:41:27 +08:00
// created tag/branch
2023-05-26 09:04:48 +08:00
refName := git . RefName ( p . Ref ) . ShortName ( )
2021-07-23 12:41:27 +08: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 12:41:27 +08:00
// created tag/branch
2023-05-26 09:04:48 +08:00
refName := git . RefName ( p . Ref ) . ShortName ( )
2021-07-23 12:41:27 +08: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 12:41:27 +08: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 12:41:27 +08:00
var (
2023-05-26 09:04:48 +08:00
branchName = git . RefName ( p . Ref ) . ShortName ( )
2021-07-23 12:41:27 +08: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 12:41:27 +08: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 12:41:27 +08:00
text , issueTitle , attachmentText , _ := getIssuesPayloadInfo ( p , noneLinkFormatter , true )
var content string
2021-12-25 21:30:09 +08: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 12:41:27 +08: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 12:41:27 +08:00
text , issueTitle , _ := getIssueCommentPayloadInfo ( p , noneLinkFormatter , true )
var content string
2021-12-25 21:30:09 +08: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 12:41:27 +08: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 12:41:27 +08: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 12:41:27 +08:00
var text , title string
switch p . Action {
2022-11-19 23:19:14 +08:00
case api . HookIssueReviewed :
2021-07-23 12:41:27 +08:00
action , err := parseHookPullRequestEventType ( event )
if err != nil {
2024-03-07 23:18:38 +01:00
return WechatworkPayload { } , err
2021-07-23 12:41:27 +08: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 12:41:27 +08: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 12:41:27 +08: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 12:41:27 +08: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 12:41:27 +08: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 12:43:38 +08:00
text , _ := getPackagePayloadInfo ( p , noneLinkFormatter , true )
return newWechatworkMarkdownPayload ( text ) , nil
}
2024-03-07 23:18:38 +01:00
type wechatworkConvertor struct { }
var _ payloadConvertor [ WechatworkPayload ] = wechatworkConvertor { }
func newWechatworkRequest ( ctx context . Context , w * webhook_model . Webhook , t * webhook_model . HookTask ) ( * http . Request , [ ] byte , error ) {
return newJSONRequest ( wechatworkConvertor { } , w , t , true )
2021-07-23 12:41:27 +08:00
}