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