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