2017-11-21 12:26:43 +08:00
// Copyright 2017 The Gitea Authors. All rights reserved.
2022-11-28 02:20:29 +08:00
// SPDX-License-Identifier: MIT
2017-11-21 12:26:43 +08:00
2019-11-04 06:13:25 +08:00
package webhook
2017-11-21 12:26:43 +08:00
import (
"fmt"
2021-09-19 03:35:23 +08:00
"net/url"
2017-11-21 12:26:43 +08:00
"strings"
2019-03-27 17:33:00 +08:00
"code.gitea.io/gitea/modules/git"
2021-07-25 00:03:58 +08:00
"code.gitea.io/gitea/modules/json"
2019-05-11 18:21:34 +08:00
api "code.gitea.io/gitea/modules/structs"
2021-11-17 02:18:25 +08:00
"code.gitea.io/gitea/modules/util"
2023-01-01 23:23:15 +08:00
webhook_module "code.gitea.io/gitea/modules/webhook"
2019-03-27 17:33:00 +08:00
2022-09-02 05:13:17 +08:00
dingtalk "gitea.com/lunny/dingtalk_webhook"
2017-11-21 12:26:43 +08:00
)
type (
// DingtalkPayload represents
DingtalkPayload dingtalk . Payload
)
2022-01-21 01:46:10 +08:00
var _ PayloadConvertor = & DingtalkPayload { }
2020-09-05 10:57:13 +08:00
2017-11-21 12:26:43 +08:00
// JSONPayload Marshals the DingtalkPayload to json
2020-09-05 10:57:13 +08:00
func ( d * DingtalkPayload ) JSONPayload ( ) ( [ ] byte , error ) {
data , err := json . MarshalIndent ( d , "" , " " )
2017-11-21 12:26:43 +08:00
if err != nil {
return [ ] byte { } , err
}
return data , nil
}
2020-09-05 10:57:13 +08:00
// Create implements PayloadConvertor Create method
func ( d * DingtalkPayload ) Create ( p * api . CreatePayload ) ( api . Payloader , error ) {
2017-11-21 12:26:43 +08:00
// created tag/branch
2023-05-26 09:04:48 +08:00
refName := git . RefName ( p . Ref ) . ShortName ( )
2017-11-21 12:26:43 +08:00
title := fmt . Sprintf ( "[%s] %s %s created" , p . Repo . FullName , p . RefType , refName )
2021-11-17 02:18:25 +08:00
return createDingtalkPayload ( title , title , fmt . Sprintf ( "view ref %s" , refName ) , p . Repo . HTMLURL + "/src/" + util . PathEscapeSegments ( refName ) ) , nil
2017-11-21 12:26:43 +08:00
}
2020-09-05 10:57:13 +08:00
// Delete implements PayloadConvertor Delete method
func ( d * DingtalkPayload ) Delete ( p * api . DeletePayload ) ( api . Payloader , error ) {
2018-05-16 22:01:55 +08:00
// created tag/branch
2023-05-26 09:04:48 +08:00
refName := git . RefName ( p . Ref ) . ShortName ( )
2018-05-16 22:01:55 +08:00
title := fmt . Sprintf ( "[%s] %s %s deleted" , p . Repo . FullName , p . RefType , refName )
2021-11-17 02:18:25 +08:00
return createDingtalkPayload ( title , title , fmt . Sprintf ( "view ref %s" , refName ) , p . Repo . HTMLURL + "/src/" + util . PathEscapeSegments ( refName ) ) , nil
2018-05-16 22:01:55 +08:00
}
2020-09-05 10:57:13 +08:00
// Fork implements PayloadConvertor Fork method
func ( d * DingtalkPayload ) Fork ( p * api . ForkPayload ) ( api . Payloader , error ) {
2018-05-16 22:01:55 +08:00
title := fmt . Sprintf ( "%s is forked to %s" , p . Forkee . FullName , p . Repo . FullName )
2021-06-21 10:12:19 +08:00
return createDingtalkPayload ( title , title , fmt . Sprintf ( "view forked repo %s" , p . Repo . FullName ) , p . Repo . HTMLURL ) , nil
2018-05-16 22:01:55 +08:00
}
2020-09-05 10:57:13 +08:00
// Push implements PayloadConvertor Push method
func ( d * DingtalkPayload ) Push ( p * api . PushPayload ) ( api . Payloader , error ) {
2017-11-21 12:26:43 +08:00
var (
2023-05-26 09:04:48 +08:00
branchName = git . RefName ( p . Ref ) . ShortName ( )
2017-11-21 12:26:43 +08:00
commitDesc string
)
var titleLink , linkText string
2022-10-17 00:22:34 +08:00
if p . TotalCommits == 1 {
2017-11-21 12:26:43 +08:00
commitDesc = "1 new commit"
titleLink = p . Commits [ 0 ] . URL
2022-10-17 00:22:34 +08:00
linkText = "view commit"
2017-11-21 12:26:43 +08:00
} else {
2022-10-17 00:22:34 +08:00
commitDesc = fmt . Sprintf ( "%d new commits" , p . TotalCommits )
2017-11-21 12:26:43 +08:00
titleLink = p . CompareURL
2022-10-17 00:22:34 +08:00
linkText = "view commits"
2017-11-21 12:26:43 +08:00
}
if titleLink == "" {
2021-11-17 02:18:25 +08:00
titleLink = p . Repo . HTMLURL + "/src/" + util . PathEscapeSegments ( branchName )
2017-11-21 12:26:43 +08:00
}
title := fmt . Sprintf ( "[%s:%s] %s" , 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 {
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 10:12:19 +08:00
text += "\r\n"
2017-11-21 12:26:43 +08:00
}
}
2021-06-21 10:12:19 +08:00
return createDingtalkPayload ( title , text , linkText , titleLink ) , nil
2017-11-21 12:26:43 +08:00
}
2020-09-05 10:57:13 +08:00
// Issue implements PayloadConvertor Issue method
func ( d * DingtalkPayload ) Issue ( p * api . IssuePayload ) ( api . Payloader , error ) {
2020-01-05 06:20:15 +08:00
text , issueTitle , attachmentText , _ := getIssuesPayloadInfo ( p , noneLinkFormatter , true )
2018-05-16 22:01:55 +08:00
2021-06-21 10:12:19 +08:00
return createDingtalkPayload ( issueTitle , text + "\r\n\r\n" + attachmentText , "view issue" , p . Issue . HTMLURL ) , nil
2018-05-16 22:01:55 +08:00
}
2022-09-05 03:54:23 +08:00
// Wiki implements PayloadConvertor Wiki method
func ( d * DingtalkPayload ) Wiki ( p * api . WikiPayload ) ( api . Payloader , error ) {
text , _ , _ := getWikiPayloadInfo ( p , noneLinkFormatter , true )
url := p . Repository . HTMLURL + "/wiki/" + url . PathEscape ( p . Page )
return createDingtalkPayload ( text , text , "view wiki" , url ) , nil
}
2020-09-05 10:57:13 +08:00
// IssueComment implements PayloadConvertor IssueComment method
func ( d * DingtalkPayload ) IssueComment ( p * api . IssueCommentPayload ) ( api . Payloader , error ) {
2020-01-05 06:20:15 +08:00
text , issueTitle , _ := getIssueCommentPayloadInfo ( p , noneLinkFormatter , true )
2019-10-19 06:42:04 +08:00
2021-06-21 10:12:19 +08:00
return createDingtalkPayload ( issueTitle , text + "\r\n\r\n" + p . Comment . Body , "view issue comment" , p . Comment . HTMLURL ) , nil
2018-05-16 22:01:55 +08:00
}
2020-09-05 10:57:13 +08:00
// PullRequest implements PayloadConvertor PullRequest method
func ( d * DingtalkPayload ) PullRequest ( p * api . PullRequestPayload ) ( api . Payloader , error ) {
2020-01-05 06:20:15 +08:00
text , issueTitle , attachmentText , _ := getPullRequestPayloadInfo ( p , noneLinkFormatter , true )
2017-11-21 12:26:43 +08:00
2021-06-21 10:12:19 +08:00
return createDingtalkPayload ( issueTitle , text + "\r\n\r\n" + attachmentText , "view pull request" , p . PullRequest . HTMLURL ) , nil
2017-11-21 12:26:43 +08:00
}
2020-09-05 10:57:13 +08:00
// Review implements PayloadConvertor Review method
2023-01-01 23:23:15 +08:00
func ( d * DingtalkPayload ) Review ( p * api . PullRequestPayload , event webhook_module . HookEventType ) ( api . Payloader , error ) {
2018-12-28 02:04:30 +08:00
var text , title string
switch p . Action {
2020-03-06 13:10:48 +08:00
case api . HookIssueReviewed :
2018-12-28 02:04:30 +08:00
action , err := parseHookPullRequestEventType ( event )
if err != nil {
return nil , err
}
title = fmt . Sprintf ( "[%s] Pull request review %s : #%d %s" , p . Repository . FullName , action , p . Index , p . PullRequest . Title )
2019-10-19 06:42:04 +08:00
text = p . Review . Content
2018-12-28 02:04:30 +08:00
}
2021-06-21 10:12:19 +08:00
return createDingtalkPayload ( title , title + "\r\n\r\n" + text , "view pull request" , p . PullRequest . HTMLURL ) , nil
2018-12-28 02:04:30 +08:00
}
2020-09-05 10:57:13 +08:00
// Repository implements PayloadConvertor Repository method
func ( d * DingtalkPayload ) Repository ( p * api . RepositoryPayload ) ( api . Payloader , error ) {
2017-11-21 12:26:43 +08:00
switch p . Action {
case api . HookRepoCreated :
2021-06-21 10:12:19 +08:00
title := fmt . Sprintf ( "[%s] Repository created" , p . Repository . FullName )
return createDingtalkPayload ( title , title , "view repository" , p . Repository . HTMLURL ) , nil
2017-11-21 12:26:43 +08:00
case api . HookRepoDeleted :
2021-06-21 10:12:19 +08:00
title := fmt . Sprintf ( "[%s] Repository deleted" , p . Repository . FullName )
2017-11-21 12:26:43 +08:00
return & DingtalkPayload {
MsgType : "text" ,
Text : struct {
Content string ` json:"content" `
} {
Content : title ,
} ,
} , nil
}
return nil , nil
}
2020-09-05 10:57:13 +08:00
// Release implements PayloadConvertor Release method
func ( d * DingtalkPayload ) Release ( p * api . ReleasePayload ) ( api . Payloader , error ) {
2020-01-05 06:20:15 +08:00
text , _ := getReleasePayloadInfo ( p , noneLinkFormatter , true )
2018-05-21 10:28:29 +08:00
2023-09-22 07:59:27 +08:00
return createDingtalkPayload ( text , text , "view release" , p . Release . HTMLURL ) , nil
2021-06-21 10:12:19 +08:00
}
2023-10-31 18:26:06 +08:00
func ( d * DingtalkPayload ) Package ( p * api . PackagePayload ) ( api . Payloader , error ) {
text , _ := getPackagePayloadInfo ( p , noneLinkFormatter , true )
return createDingtalkPayload ( text , text , "view package" , p . Package . HTMLURL ) , nil
}
2021-06-21 10:12:19 +08:00
func createDingtalkPayload ( title , text , singleTitle , singleURL string ) * DingtalkPayload {
2019-12-28 16:55:09 +08:00
return & DingtalkPayload {
MsgType : "actionCard" ,
ActionCard : dingtalk . ActionCard {
2021-06-21 10:12:19 +08:00
Text : strings . TrimSpace ( text ) ,
Title : strings . TrimSpace ( title ) ,
2019-12-28 16:55:09 +08:00
HideAvatar : "0" ,
2021-06-21 10:12:19 +08:00
SingleTitle : singleTitle ,
2021-09-19 03:35:23 +08:00
// https://developers.dingtalk.com/document/app/message-link-description
// to open the link in browser, we should use this URL, otherwise the page is displayed inside DingTalk client, very difficult to visit non-public URLs.
SingleURL : "dingtalk://dingtalkclient/page/link?pc_slide=false&url=" + url . QueryEscape ( singleURL ) ,
2019-12-28 16:55:09 +08:00
} ,
2021-06-21 10:12:19 +08:00
}
2018-05-16 22:01:55 +08:00
}
2017-11-21 12:26:43 +08:00
// GetDingtalkPayload converts a ding talk webhook into a DingtalkPayload
2023-01-01 23:23:15 +08:00
func GetDingtalkPayload ( p api . Payloader , event webhook_module . HookEventType , _ string ) ( api . Payloader , error ) {
2020-09-05 10:57:13 +08:00
return convertPayloader ( new ( DingtalkPayload ) , p , event )
2017-11-21 12:26:43 +08:00
}