2017-11-21 04:26:43 +00:00
// Copyright 2017 The Gitea Authors. All rights reserved.
2022-11-27 18:20:29 +00:00
// SPDX-License-Identifier: MIT
2017-11-21 04:26:43 +00:00
2019-11-03 22:13:25 +00:00
package webhook
2017-11-21 04:26:43 +00:00
import (
2024-03-07 22:18:38 +00:00
"context"
2017-11-21 04:26:43 +00:00
"fmt"
2024-03-07 22:18:38 +00:00
"net/http"
2021-09-18 19:35:23 +00:00
"net/url"
2017-11-21 04:26:43 +00:00
"strings"
2024-03-07 22:18:38 +00:00
webhook_model "code.gitea.io/gitea/models/webhook"
2019-03-27 09:33:00 +00:00
"code.gitea.io/gitea/modules/git"
2019-05-11 10:21:34 +00:00
api "code.gitea.io/gitea/modules/structs"
2021-11-16 18:18:25 +00:00
"code.gitea.io/gitea/modules/util"
2023-01-01 15:23:15 +00:00
webhook_module "code.gitea.io/gitea/modules/webhook"
2019-03-27 09:33:00 +00:00
2022-09-01 21:13:17 +00:00
dingtalk "gitea.com/lunny/dingtalk_webhook"
2017-11-21 04:26:43 +00:00
)
type (
2024-07-10 11:37:16 +00:00
DingtalkPayload dingtalk . Payload
dingtalkConvertor struct { }
2017-11-21 04:26:43 +00:00
)
2020-09-05 02:57:13 +00:00
// Create implements PayloadConvertor Create method
2024-03-07 22:18:38 +00:00
func ( dc dingtalkConvertor ) Create ( p * api . CreatePayload ) ( DingtalkPayload , error ) {
2017-11-21 04:26:43 +00:00
// created tag/branch
2023-05-26 01:04:48 +00:00
refName := git . RefName ( p . Ref ) . ShortName ( )
2017-11-21 04:26:43 +00:00
title := fmt . Sprintf ( "[%s] %s %s created" , p . Repo . FullName , p . RefType , refName )
2021-11-16 18:18:25 +00:00
return createDingtalkPayload ( title , title , fmt . Sprintf ( "view ref %s" , refName ) , p . Repo . HTMLURL + "/src/" + util . PathEscapeSegments ( refName ) ) , nil
2017-11-21 04:26:43 +00:00
}
2020-09-05 02:57:13 +00:00
// Delete implements PayloadConvertor Delete method
2024-03-07 22:18:38 +00:00
func ( dc dingtalkConvertor ) Delete ( p * api . DeletePayload ) ( DingtalkPayload , error ) {
2018-05-16 14:01:55 +00:00
// created tag/branch
2023-05-26 01:04:48 +00:00
refName := git . RefName ( p . Ref ) . ShortName ( )
2018-05-16 14:01:55 +00:00
title := fmt . Sprintf ( "[%s] %s %s deleted" , p . Repo . FullName , p . RefType , refName )
2021-11-16 18:18:25 +00:00
return createDingtalkPayload ( title , title , fmt . Sprintf ( "view ref %s" , refName ) , p . Repo . HTMLURL + "/src/" + util . PathEscapeSegments ( refName ) ) , nil
2018-05-16 14:01:55 +00:00
}
2020-09-05 02:57:13 +00:00
// Fork implements PayloadConvertor Fork method
2024-03-07 22:18:38 +00:00
func ( dc dingtalkConvertor ) Fork ( p * api . ForkPayload ) ( DingtalkPayload , error ) {
2018-05-16 14:01:55 +00:00
title := fmt . Sprintf ( "%s is forked to %s" , p . Forkee . FullName , p . Repo . FullName )
2021-06-21 02:12:19 +00:00
return createDingtalkPayload ( title , title , fmt . Sprintf ( "view forked repo %s" , p . Repo . FullName ) , p . Repo . HTMLURL ) , nil
2018-05-16 14:01:55 +00:00
}
2020-09-05 02:57:13 +00:00
// Push implements PayloadConvertor Push method
2024-03-07 22:18:38 +00:00
func ( dc dingtalkConvertor ) Push ( p * api . PushPayload ) ( DingtalkPayload , error ) {
2017-11-21 04:26:43 +00:00
var (
2023-05-26 01:04:48 +00:00
branchName = git . RefName ( p . Ref ) . ShortName ( )
2017-11-21 04:26:43 +00:00
commitDesc string
)
var titleLink , linkText string
2022-10-16 16:22:34 +00:00
if p . TotalCommits == 1 {
2017-11-21 04:26:43 +00:00
commitDesc = "1 new commit"
titleLink = p . Commits [ 0 ] . URL
2022-10-16 16:22:34 +00:00
linkText = "view commit"
2017-11-21 04:26:43 +00:00
} else {
2022-10-16 16:22:34 +00:00
commitDesc = fmt . Sprintf ( "%d new commits" , p . TotalCommits )
2017-11-21 04:26:43 +00:00
titleLink = p . CompareURL
2022-10-16 16:22:34 +00:00
linkText = "view commits"
2017-11-21 04:26:43 +00:00
}
if titleLink == "" {
2021-11-16 18:18:25 +00:00
titleLink = p . Repo . HTMLURL + "/src/" + util . PathEscapeSegments ( branchName )
2017-11-21 04:26:43 +00: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 02:12:19 +00:00
text += "\r\n"
2017-11-21 04:26:43 +00:00
}
}
2021-06-21 02:12:19 +00:00
return createDingtalkPayload ( title , text , linkText , titleLink ) , nil
2017-11-21 04:26:43 +00:00
}
2020-09-05 02:57:13 +00:00
// Issue implements PayloadConvertor Issue method
2024-03-07 22:18:38 +00:00
func ( dc dingtalkConvertor ) Issue ( p * api . IssuePayload ) ( DingtalkPayload , error ) {
2024-07-10 11:37:16 +00:00
text , issueTitle , extraMarkdown , _ := getIssuesPayloadInfo ( p , noneLinkFormatter , true )
2018-05-16 14:01:55 +00:00
2024-07-10 11:37:16 +00:00
return createDingtalkPayload ( issueTitle , text + "\r\n\r\n" + extraMarkdown , "view issue" , p . Issue . HTMLURL ) , nil
2018-05-16 14:01:55 +00:00
}
2022-09-04 19:54:23 +00:00
// Wiki implements PayloadConvertor Wiki method
2024-03-07 22:18:38 +00:00
func ( dc dingtalkConvertor ) Wiki ( p * api . WikiPayload ) ( DingtalkPayload , error ) {
2022-09-04 19:54:23 +00:00
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 02:57:13 +00:00
// IssueComment implements PayloadConvertor IssueComment method
2024-03-07 22:18:38 +00:00
func ( dc dingtalkConvertor ) IssueComment ( p * api . IssueCommentPayload ) ( DingtalkPayload , error ) {
2020-01-04 22:20:15 +00:00
text , issueTitle , _ := getIssueCommentPayloadInfo ( p , noneLinkFormatter , true )
2019-10-18 22:42:04 +00:00
2021-06-21 02:12:19 +00:00
return createDingtalkPayload ( issueTitle , text + "\r\n\r\n" + p . Comment . Body , "view issue comment" , p . Comment . HTMLURL ) , nil
2018-05-16 14:01:55 +00:00
}
2020-09-05 02:57:13 +00:00
// PullRequest implements PayloadConvertor PullRequest method
2024-03-07 22:18:38 +00:00
func ( dc dingtalkConvertor ) PullRequest ( p * api . PullRequestPayload ) ( DingtalkPayload , error ) {
2024-07-10 11:37:16 +00:00
text , issueTitle , extraMarkdown , _ := getPullRequestPayloadInfo ( p , noneLinkFormatter , true )
2017-11-21 04:26:43 +00:00
2024-07-10 11:37:16 +00:00
return createDingtalkPayload ( issueTitle , text + "\r\n\r\n" + extraMarkdown , "view pull request" , p . PullRequest . HTMLURL ) , nil
2017-11-21 04:26:43 +00:00
}
2020-09-05 02:57:13 +00:00
// Review implements PayloadConvertor Review method
2024-03-07 22:18:38 +00:00
func ( dc dingtalkConvertor ) Review ( p * api . PullRequestPayload , event webhook_module . HookEventType ) ( DingtalkPayload , error ) {
2018-12-27 18:04:30 +00:00
var text , title string
switch p . Action {
2020-03-06 05:10:48 +00:00
case api . HookIssueReviewed :
2018-12-27 18:04:30 +00:00
action , err := parseHookPullRequestEventType ( event )
if err != nil {
2024-03-07 22:18:38 +00:00
return DingtalkPayload { } , err
2018-12-27 18:04:30 +00:00
}
title = fmt . Sprintf ( "[%s] Pull request review %s : #%d %s" , p . Repository . FullName , action , p . Index , p . PullRequest . Title )
2019-10-18 22:42:04 +00:00
text = p . Review . Content
2018-12-27 18:04:30 +00:00
}
2021-06-21 02:12:19 +00:00
return createDingtalkPayload ( title , title + "\r\n\r\n" + text , "view pull request" , p . PullRequest . HTMLURL ) , nil
2018-12-27 18:04:30 +00:00
}
2020-09-05 02:57:13 +00:00
// Repository implements PayloadConvertor Repository method
2024-03-07 22:18:38 +00:00
func ( dc dingtalkConvertor ) Repository ( p * api . RepositoryPayload ) ( DingtalkPayload , error ) {
2017-11-21 04:26:43 +00:00
switch p . Action {
case api . HookRepoCreated :
2021-06-21 02:12:19 +00:00
title := fmt . Sprintf ( "[%s] Repository created" , p . Repository . FullName )
return createDingtalkPayload ( title , title , "view repository" , p . Repository . HTMLURL ) , nil
2017-11-21 04:26:43 +00:00
case api . HookRepoDeleted :
2021-06-21 02:12:19 +00:00
title := fmt . Sprintf ( "[%s] Repository deleted" , p . Repository . FullName )
2024-03-07 22:18:38 +00:00
return DingtalkPayload {
2017-11-21 04:26:43 +00:00
MsgType : "text" ,
Text : struct {
Content string ` json:"content" `
} {
Content : title ,
} ,
} , nil
}
2024-03-07 22:18:38 +00:00
return DingtalkPayload { } , nil
2017-11-21 04:26:43 +00:00
}
2020-09-05 02:57:13 +00:00
// Release implements PayloadConvertor Release method
2024-03-07 22:18:38 +00:00
func ( dc dingtalkConvertor ) Release ( p * api . ReleasePayload ) ( DingtalkPayload , error ) {
2020-01-04 22:20:15 +00:00
text , _ := getReleasePayloadInfo ( p , noneLinkFormatter , true )
2018-05-21 02:28:29 +00:00
2023-09-21 22:55:09 +00:00
return createDingtalkPayload ( text , text , "view release" , p . Release . HTMLURL ) , nil
2021-06-21 02:12:19 +00:00
}
2024-03-07 22:18:38 +00:00
func ( dc dingtalkConvertor ) Package ( p * api . PackagePayload ) ( DingtalkPayload , error ) {
2023-10-31 04:43:38 +00:00
text , _ := getPackagePayloadInfo ( p , noneLinkFormatter , true )
return createDingtalkPayload ( text , text , "view package" , p . Package . HTMLURL ) , nil
}
2024-03-07 22:18:38 +00:00
func createDingtalkPayload ( title , text , singleTitle , singleURL string ) DingtalkPayload {
return DingtalkPayload {
2019-12-28 08:55:09 +00:00
MsgType : "actionCard" ,
ActionCard : dingtalk . ActionCard {
2021-06-21 02:12:19 +00:00
Text : strings . TrimSpace ( text ) ,
Title : strings . TrimSpace ( title ) ,
2019-12-28 08:55:09 +00:00
HideAvatar : "0" ,
2021-06-21 02:12:19 +00:00
SingleTitle : singleTitle ,
2021-09-18 19:35:23 +00: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 08:55:09 +00:00
} ,
2021-06-21 02:12:19 +00:00
}
2018-05-16 14:01:55 +00:00
}
2024-06-11 18:47:45 +00:00
func newDingtalkRequest ( _ context . Context , w * webhook_model . Webhook , t * webhook_model . HookTask ) ( * http . Request , [ ] byte , error ) {
2024-07-10 11:37:16 +00:00
var pc payloadConvertor [ DingtalkPayload ] = dingtalkConvertor { }
return newJSONRequest ( pc , w , t , true )
2017-11-21 04:26:43 +00:00
}