2020-02-12 11:48:28 +03:00
// Copyright 2020 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2020-02-12 11:48:28 +03:00
package webhook
import (
"fmt"
"strings"
"code.gitea.io/gitea/modules/git"
2021-07-24 19:03:58 +03:00
"code.gitea.io/gitea/modules/json"
2020-02-12 11:48:28 +03:00
api "code.gitea.io/gitea/modules/structs"
2023-01-01 18:23:15 +03:00
webhook_module "code.gitea.io/gitea/modules/webhook"
2020-02-12 11:48:28 +03:00
)
type (
// FeishuPayload represents
FeishuPayload struct {
2020-12-11 19:04:04 +03:00
MsgType string ` json:"msg_type" ` // text / post / image / share_chat / interactive
Content struct {
Text string ` json:"text" `
} ` json:"content" `
2020-02-12 11:48:28 +03:00
}
)
2020-12-11 19:04:04 +03:00
func newFeishuTextPayload ( text string ) * FeishuPayload {
return & FeishuPayload {
MsgType : "text" ,
Content : struct {
Text string ` json:"text" `
} {
2021-06-21 05:12:19 +03:00
Text : strings . TrimSpace ( text ) ,
2020-12-11 19:04:04 +03:00
} ,
}
}
2020-02-12 11:48:28 +03:00
// JSONPayload Marshals the FeishuPayload to json
2020-09-05 05:57:13 +03:00
func ( f * FeishuPayload ) JSONPayload ( ) ( [ ] byte , error ) {
data , err := json . MarshalIndent ( f , "" , " " )
2020-02-12 11:48:28 +03:00
if err != nil {
return [ ] byte { } , err
}
return data , nil
}
2022-01-20 20:46:10 +03:00
var _ PayloadConvertor = & FeishuPayload { }
2020-09-05 05:57:13 +03:00
// Create implements PayloadConvertor Create method
func ( f * FeishuPayload ) Create ( p * api . CreatePayload ) ( api . Payloader , error ) {
2020-02-12 11:48:28 +03:00
// created tag/branch
2023-05-26 04:04:48 +03:00
refName := git . RefName ( p . Ref ) . ShortName ( )
2020-12-11 19:04:04 +03:00
text := fmt . Sprintf ( "[%s] %s %s created" , p . Repo . FullName , p . RefType , refName )
2020-02-12 11:48:28 +03:00
2020-12-11 19:04:04 +03:00
return newFeishuTextPayload ( text ) , nil
2020-02-12 11:48:28 +03:00
}
2020-09-05 05:57:13 +03:00
// Delete implements PayloadConvertor Delete method
func ( f * FeishuPayload ) Delete ( p * api . DeletePayload ) ( api . Payloader , error ) {
2020-02-12 11:48:28 +03:00
// created tag/branch
2023-05-26 04:04:48 +03:00
refName := git . RefName ( p . Ref ) . ShortName ( )
2020-12-11 19:04:04 +03:00
text := fmt . Sprintf ( "[%s] %s %s deleted" , p . Repo . FullName , p . RefType , refName )
2020-02-12 11:48:28 +03:00
2020-12-11 19:04:04 +03:00
return newFeishuTextPayload ( text ) , nil
2020-02-12 11:48:28 +03:00
}
2020-09-05 05:57:13 +03:00
// Fork implements PayloadConvertor Fork method
func ( f * FeishuPayload ) Fork ( p * api . ForkPayload ) ( api . Payloader , error ) {
2020-12-11 19:04:04 +03:00
text := fmt . Sprintf ( "%s is forked to %s" , p . Forkee . FullName , p . Repo . FullName )
2020-02-12 11:48:28 +03:00
2020-12-11 19:04:04 +03:00
return newFeishuTextPayload ( text ) , nil
2020-02-12 11:48:28 +03:00
}
2020-09-05 05:57:13 +03:00
// Push implements PayloadConvertor Push method
func ( f * FeishuPayload ) Push ( p * api . PushPayload ) ( api . Payloader , error ) {
2020-02-12 11:48:28 +03:00
var (
2023-05-26 04:04:48 +03:00
branchName = git . RefName ( p . Ref ) . ShortName ( )
2020-02-12 11:48:28 +03:00
commitDesc string
)
2022-01-20 20:46:10 +03:00
text := fmt . Sprintf ( "[%s:%s] %s\r\n" , p . Repo . FullName , branchName , commitDesc )
2020-02-12 11:48:28 +03: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 05:12:19 +03:00
text += "\r\n"
2020-02-12 11:48:28 +03:00
}
}
2020-12-11 19:04:04 +03:00
return newFeishuTextPayload ( text ) , nil
2020-02-12 11:48:28 +03:00
}
2020-09-05 05:57:13 +03:00
// Issue implements PayloadConvertor Issue method
func ( f * FeishuPayload ) Issue ( p * api . IssuePayload ) ( api . Payloader , error ) {
2020-02-12 11:48:28 +03:00
text , issueTitle , attachmentText , _ := getIssuesPayloadInfo ( p , noneLinkFormatter , true )
2020-12-11 19:04:04 +03:00
return newFeishuTextPayload ( issueTitle + "\r\n" + text + "\r\n\r\n" + attachmentText ) , nil
2020-02-12 11:48:28 +03:00
}
2020-09-05 05:57:13 +03:00
// IssueComment implements PayloadConvertor IssueComment method
func ( f * FeishuPayload ) IssueComment ( p * api . IssueCommentPayload ) ( api . Payloader , error ) {
2020-02-12 11:48:28 +03:00
text , issueTitle , _ := getIssueCommentPayloadInfo ( p , noneLinkFormatter , true )
2020-12-11 19:04:04 +03:00
return newFeishuTextPayload ( issueTitle + "\r\n" + text + "\r\n\r\n" + p . Comment . Body ) , nil
2020-02-12 11:48:28 +03:00
}
2020-09-05 05:57:13 +03:00
// PullRequest implements PayloadConvertor PullRequest method
func ( f * FeishuPayload ) PullRequest ( p * api . PullRequestPayload ) ( api . Payloader , error ) {
2020-02-12 11:48:28 +03:00
text , issueTitle , attachmentText , _ := getPullRequestPayloadInfo ( p , noneLinkFormatter , true )
2020-12-11 19:04:04 +03:00
return newFeishuTextPayload ( issueTitle + "\r\n" + text + "\r\n\r\n" + attachmentText ) , nil
2020-02-12 11:48:28 +03:00
}
2020-09-05 05:57:13 +03:00
// Review implements PayloadConvertor Review method
2023-01-01 18:23:15 +03:00
func ( f * FeishuPayload ) Review ( p * api . PullRequestPayload , event webhook_module . HookEventType ) ( api . Payloader , error ) {
2021-06-21 05:12:19 +03:00
action , err := parseHookPullRequestEventType ( event )
if err != nil {
return nil , err
2020-02-12 11:48:28 +03:00
}
2021-06-21 05:12:19 +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
2020-12-11 19:04:04 +03:00
return newFeishuTextPayload ( title + "\r\n\r\n" + text ) , nil
2020-02-12 11:48:28 +03:00
}
2020-09-05 05:57:13 +03:00
// Repository implements PayloadConvertor Repository method
func ( f * FeishuPayload ) Repository ( p * api . RepositoryPayload ) ( api . Payloader , error ) {
2020-12-11 19:04:04 +03:00
var text string
2020-02-12 11:48:28 +03:00
switch p . Action {
case api . HookRepoCreated :
2020-12-11 19:04:04 +03:00
text = fmt . Sprintf ( "[%s] Repository created" , p . Repository . FullName )
return newFeishuTextPayload ( text ) , nil
2020-02-12 11:48:28 +03:00
case api . HookRepoDeleted :
2020-12-11 19:04:04 +03:00
text = fmt . Sprintf ( "[%s] Repository deleted" , p . Repository . FullName )
return newFeishuTextPayload ( text ) , nil
2020-02-12 11:48:28 +03:00
}
return nil , nil
}
2022-09-04 22:54:23 +03:00
// Wiki implements PayloadConvertor Wiki method
func ( f * FeishuPayload ) Wiki ( p * api . WikiPayload ) ( api . Payloader , error ) {
text , _ , _ := getWikiPayloadInfo ( p , noneLinkFormatter , true )
return newFeishuTextPayload ( text ) , nil
}
2020-09-05 05:57:13 +03:00
// Release implements PayloadConvertor Release method
func ( f * FeishuPayload ) Release ( p * api . ReleasePayload ) ( api . Payloader , error ) {
2020-02-12 11:48:28 +03:00
text , _ := getReleasePayloadInfo ( p , noneLinkFormatter , true )
2020-12-11 19:04:04 +03:00
return newFeishuTextPayload ( text ) , nil
2020-02-12 11:48:28 +03:00
}
// GetFeishuPayload converts a ding talk webhook into a FeishuPayload
2023-01-01 18:23:15 +03:00
func GetFeishuPayload ( p api . Payloader , event webhook_module . HookEventType , _ string ) ( api . Payloader , error ) {
2020-09-05 05:57:13 +03:00
return convertPayloader ( new ( FeishuPayload ) , p , event )
2020-02-12 11:48:28 +03:00
}