2019-04-19 05:45:02 +03:00
// Copyright 2019 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2019-04-19 05:45:02 +03:00
2019-11-04 01:13:25 +03:00
package webhook
2019-04-19 05:45:02 +03:00
import (
2024-03-08 01:18:38 +03:00
"context"
2019-04-19 05:45:02 +03:00
"fmt"
2024-07-10 14:37:16 +03:00
"html"
2024-03-08 01:18:38 +03:00
"net/http"
2019-04-19 05:45:02 +03:00
"strings"
2021-11-10 08:13:16 +03:00
webhook_model "code.gitea.io/gitea/models/webhook"
2019-04-19 05:45:02 +03:00
"code.gitea.io/gitea/modules/git"
2021-07-24 19:03:58 +03:00
"code.gitea.io/gitea/modules/json"
2019-11-04 01:13:25 +03:00
"code.gitea.io/gitea/modules/log"
2024-07-10 14:37:16 +03:00
"code.gitea.io/gitea/modules/markup"
2019-05-11 13:21:34 +03:00
api "code.gitea.io/gitea/modules/structs"
2024-07-10 14:37:16 +03:00
"code.gitea.io/gitea/modules/util"
2023-01-01 18:23:15 +03:00
webhook_module "code.gitea.io/gitea/modules/webhook"
2019-04-19 05:45:02 +03:00
)
type (
// TelegramPayload represents
TelegramPayload struct {
2019-04-23 19:12:00 +03:00
Message string ` json:"text" `
ParseMode string ` json:"parse_mode" `
DisableWebPreview bool ` json:"disable_web_page_preview" `
2019-04-19 05:45:02 +03:00
}
// TelegramMeta contains the telegram metadata
TelegramMeta struct {
BotToken string ` json:"bot_token" `
ChatID string ` json:"chat_id" `
2023-08-13 17:00:06 +03:00
ThreadID string ` json:"thread_id" `
2019-04-19 05:45:02 +03:00
}
)
2019-11-04 01:13:25 +03:00
// GetTelegramHook returns telegram metadata
2021-11-10 08:13:16 +03:00
func GetTelegramHook ( w * webhook_model . Webhook ) * TelegramMeta {
2019-11-04 01:13:25 +03:00
s := & TelegramMeta { }
if err := json . Unmarshal ( [ ] byte ( w . Meta ) , s ) ; err != nil {
log . Error ( "webhook.GetTelegramHook(%d): %v" , w . ID , err )
}
return s
}
2024-07-10 14:37:16 +03:00
type telegramConvertor struct { }
2020-09-05 05:57:13 +03:00
// Create implements PayloadConvertor Create method
2024-03-08 01:18:38 +03:00
func ( t telegramConvertor ) Create ( p * api . CreatePayload ) ( TelegramPayload , error ) {
2019-04-19 05:45:02 +03:00
// created tag/branch
2023-05-26 04:04:48 +03:00
refName := git . RefName ( p . Ref ) . ShortName ( )
2024-07-10 14:37:16 +03:00
title := fmt . Sprintf ( ` [%s] %s %s created ` ,
htmlLinkFormatter ( p . Repo . HTMLURL , p . Repo . FullName ) ,
html . EscapeString ( p . RefType ) ,
htmlLinkFormatter ( p . Repo . HTMLURL + "/src/" + util . PathEscapeSegments ( refName ) , refName ) ,
)
2019-04-19 05:45:02 +03:00
2024-07-10 14:37:16 +03:00
return createTelegramPayloadHTML ( title ) , nil
2019-04-19 05:45:02 +03:00
}
2020-09-05 05:57:13 +03:00
// Delete implements PayloadConvertor Delete method
2024-03-08 01:18:38 +03:00
func ( t telegramConvertor ) Delete ( p * api . DeletePayload ) ( TelegramPayload , error ) {
2019-04-19 05:45:02 +03:00
// created tag/branch
2023-05-26 04:04:48 +03:00
refName := git . RefName ( p . Ref ) . ShortName ( )
2024-07-10 14:37:16 +03:00
title := fmt . Sprintf ( ` [%s] %s %s deleted ` ,
htmlLinkFormatter ( p . Repo . HTMLURL , p . Repo . FullName ) ,
html . EscapeString ( p . RefType ) ,
htmlLinkFormatter ( p . Repo . HTMLURL + "/src/" + util . PathEscapeSegments ( refName ) , refName ) ,
)
return createTelegramPayloadHTML ( title ) , nil
2019-04-19 05:45:02 +03:00
}
2020-09-05 05:57:13 +03:00
// Fork implements PayloadConvertor Fork method
2024-03-08 01:18:38 +03:00
func ( t telegramConvertor ) Fork ( p * api . ForkPayload ) ( TelegramPayload , error ) {
2024-07-10 14:37:16 +03:00
title := fmt . Sprintf ( ` %s is forked to %s ` , html . EscapeString ( p . Forkee . FullName ) , htmlLinkFormatter ( p . Repo . HTMLURL , p . Repo . FullName ) )
return createTelegramPayloadHTML ( title ) , nil
2019-04-19 05:45:02 +03:00
}
2020-09-05 05:57:13 +03:00
// Push implements PayloadConvertor Push method
2024-03-08 01:18:38 +03:00
func ( t telegramConvertor ) Push ( p * api . PushPayload ) ( TelegramPayload , error ) {
2024-07-10 14:37:16 +03:00
branchName := git . RefName ( p . Ref ) . ShortName ( )
var titleLink , commitDesc string
2022-10-16 19:22:34 +03:00
if p . TotalCommits == 1 {
2019-04-19 05:45:02 +03:00
commitDesc = "1 new commit"
titleLink = p . Commits [ 0 ] . URL
} else {
2022-10-16 19:22:34 +03:00
commitDesc = fmt . Sprintf ( "%d new commits" , p . TotalCommits )
2019-04-19 05:45:02 +03:00
titleLink = p . CompareURL
}
if titleLink == "" {
2024-07-10 14:37:16 +03:00
titleLink = p . Repo . HTMLURL + "/src/" + util . PathEscapeSegments ( branchName )
2019-04-19 05:45:02 +03:00
}
2024-07-10 14:37:16 +03:00
title := fmt . Sprintf ( ` [%s:%s] %s ` , htmlLinkFormatter ( p . Repo . HTMLURL , p . Repo . FullName ) , htmlLinkFormatter ( titleLink , branchName ) , html . EscapeString ( commitDesc ) )
2019-04-19 05:45:02 +03:00
2024-07-10 14:37:16 +03:00
var htmlCommits string
for _ , commit := range p . Commits {
htmlCommits += fmt . Sprintf ( "\n[%s] %s" , htmlLinkFormatter ( commit . URL , commit . ID [ : 7 ] ) , html . EscapeString ( strings . TrimRight ( commit . Message , "\r\n" ) ) )
2019-04-19 05:45:02 +03:00
if commit . Author != nil {
2024-07-10 14:37:16 +03:00
htmlCommits += " - " + html . EscapeString ( commit . Author . Name )
2019-04-19 05:45:02 +03:00
}
}
2024-07-10 14:37:16 +03:00
return createTelegramPayloadHTML ( title + htmlCommits ) , nil
2019-04-19 05:45:02 +03:00
}
2020-09-05 05:57:13 +03:00
// Issue implements PayloadConvertor Issue method
2024-03-08 01:18:38 +03:00
func ( t telegramConvertor ) Issue ( p * api . IssuePayload ) ( TelegramPayload , error ) {
2024-07-10 14:37:16 +03:00
text , _ , extraMarkdown , _ := getIssuesPayloadInfo ( p , htmlLinkFormatter , true )
// TODO: at the moment the markdown can't be rendered easily because it has context-aware links (eg: attachments)
return createTelegramPayloadHTML ( text + "\n\n" + html . EscapeString ( extraMarkdown ) ) , nil
2019-04-19 05:45:02 +03:00
}
2020-09-05 05:57:13 +03:00
// IssueComment implements PayloadConvertor IssueComment method
2024-03-08 01:18:38 +03:00
func ( t telegramConvertor ) IssueComment ( p * api . IssueCommentPayload ) ( TelegramPayload , error ) {
2020-01-05 01:20:15 +03:00
text , _ , _ := getIssueCommentPayloadInfo ( p , htmlLinkFormatter , true )
2024-07-10 14:37:16 +03:00
return createTelegramPayloadHTML ( text + "\n" + html . EscapeString ( p . Comment . Body ) ) , nil
2019-04-19 05:45:02 +03:00
}
2020-09-05 05:57:13 +03:00
// PullRequest implements PayloadConvertor PullRequest method
2024-03-08 01:18:38 +03:00
func ( t telegramConvertor ) PullRequest ( p * api . PullRequestPayload ) ( TelegramPayload , error ) {
2024-07-10 14:37:16 +03:00
text , _ , extraMarkdown , _ := getPullRequestPayloadInfo ( p , htmlLinkFormatter , true )
return createTelegramPayloadHTML ( text + "\n" + html . EscapeString ( extraMarkdown ) ) , nil
2019-04-19 05:45:02 +03:00
}
2020-09-05 05:57:13 +03:00
// Review implements PayloadConvertor Review method
2024-03-08 01:18:38 +03:00
func ( t telegramConvertor ) Review ( p * api . PullRequestPayload , event webhook_module . HookEventType ) ( TelegramPayload , error ) {
2024-07-10 14:37:16 +03:00
var text , extraMarkdown string
2020-01-08 20:45:24 +03:00
switch p . Action {
2020-03-06 08:10:48 +03:00
case api . HookIssueReviewed :
2020-01-08 20:45:24 +03:00
action , err := parseHookPullRequestEventType ( event )
if err != nil {
2024-03-08 01:18:38 +03:00
return TelegramPayload { } , err
2020-01-08 20:45:24 +03:00
}
2024-07-10 14:37:16 +03:00
text = fmt . Sprintf ( "[%s] Pull request review %s: #%d %s" , html . EscapeString ( p . Repository . FullName ) , html . EscapeString ( action ) , p . Index , html . EscapeString ( p . PullRequest . Title ) )
extraMarkdown = p . Review . Content
2020-01-08 20:45:24 +03:00
}
2024-07-10 14:37:16 +03:00
return createTelegramPayloadHTML ( text + "\n" + html . EscapeString ( extraMarkdown ) ) , nil
2020-01-08 20:45:24 +03:00
}
2020-09-05 05:57:13 +03:00
// Repository implements PayloadConvertor Repository method
2024-03-08 01:18:38 +03:00
func ( t telegramConvertor ) Repository ( p * api . RepositoryPayload ) ( TelegramPayload , error ) {
2019-04-19 05:45:02 +03:00
var title string
switch p . Action {
case api . HookRepoCreated :
2024-07-10 14:37:16 +03:00
title = fmt . Sprintf ( ` [%s] Repository created ` , htmlLinkFormatter ( p . Repository . HTMLURL , p . Repository . FullName ) )
return createTelegramPayloadHTML ( title ) , nil
2019-04-19 05:45:02 +03:00
case api . HookRepoDeleted :
2024-07-10 14:37:16 +03:00
title = fmt . Sprintf ( "[%s] Repository deleted" , html . EscapeString ( p . Repository . FullName ) )
return createTelegramPayloadHTML ( title ) , nil
2019-04-19 05:45:02 +03:00
}
2024-03-08 01:18:38 +03:00
return TelegramPayload { } , nil
2019-04-19 05:45:02 +03:00
}
2022-09-04 22:54:23 +03:00
// Wiki implements PayloadConvertor Wiki method
2024-03-08 01:18:38 +03:00
func ( t telegramConvertor ) Wiki ( p * api . WikiPayload ) ( TelegramPayload , error ) {
2022-09-04 22:54:23 +03:00
text , _ , _ := getWikiPayloadInfo ( p , htmlLinkFormatter , true )
2024-07-10 14:37:16 +03:00
return createTelegramPayloadHTML ( text ) , nil
2022-09-04 22:54:23 +03:00
}
2020-09-05 05:57:13 +03:00
// Release implements PayloadConvertor Release method
2024-03-08 01:18:38 +03:00
func ( t telegramConvertor ) Release ( p * api . ReleasePayload ) ( TelegramPayload , error ) {
2020-01-05 01:20:15 +03:00
text , _ := getReleasePayloadInfo ( p , htmlLinkFormatter , true )
2019-04-19 05:45:02 +03:00
2024-07-10 14:37:16 +03:00
return createTelegramPayloadHTML ( text ) , nil
2019-04-19 05:45:02 +03:00
}
2024-03-08 01:18:38 +03:00
func ( t telegramConvertor ) Package ( p * api . PackagePayload ) ( TelegramPayload , error ) {
2023-10-31 07:43:38 +03:00
text , _ := getPackagePayloadInfo ( p , htmlLinkFormatter , true )
2024-07-10 14:37:16 +03:00
return createTelegramPayloadHTML ( text ) , nil
2023-10-31 07:43:38 +03:00
}
2024-07-10 14:37:16 +03:00
func createTelegramPayloadHTML ( msgHTML string ) TelegramPayload {
// https://core.telegram.org/bots/api#formatting-options
2024-03-08 01:18:38 +03:00
return TelegramPayload {
2024-07-10 14:37:16 +03:00
Message : strings . TrimSpace ( markup . Sanitize ( msgHTML ) ) ,
2024-03-17 17:11:28 +03:00
ParseMode : "HTML" ,
DisableWebPreview : true ,
2021-06-21 05:12:19 +03:00
}
}
2024-03-08 01:18:38 +03:00
2024-06-11 21:47:45 +03:00
func newTelegramRequest ( _ context . Context , w * webhook_model . Webhook , t * webhook_model . HookTask ) ( * http . Request , [ ] byte , error ) {
2024-07-10 14:37:16 +03:00
var pc payloadConvertor [ TelegramPayload ] = telegramConvertor { }
return newJSONRequest ( pc , w , t , true )
2024-03-08 01:18:38 +03:00
}