2019-04-19 02:45:02 +00:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2019-04-19 02:45:02 +00:00
|
|
|
|
2019-11-03 22:13:25 +00:00
|
|
|
package webhook
|
2019-04-19 02:45:02 +00:00
|
|
|
|
|
|
|
import (
|
2024-03-07 22:18:38 +00:00
|
|
|
"context"
|
2019-04-19 02:45:02 +00:00
|
|
|
"fmt"
|
2024-03-22 15:02:48 +00:00
|
|
|
"html/template"
|
2024-03-07 22:18:38 +00:00
|
|
|
"net/http"
|
2024-03-21 12:59:24 +00:00
|
|
|
"net/url"
|
2019-04-19 02:45:02 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-11-10 05:13:16 +00:00
|
|
|
webhook_model "code.gitea.io/gitea/models/webhook"
|
2019-04-19 02:45:02 +00:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2021-07-24 16:03:58 +00:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2019-11-03 22:13:25 +00:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-05-11 10:21:34 +00:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2023-01-01 15:23:15 +00:00
|
|
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
2024-03-21 12:59:24 +00:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
2024-04-03 12:22:36 +00:00
|
|
|
"code.gitea.io/gitea/services/webhook/shared"
|
2019-04-19 02:45:02 +00:00
|
|
|
)
|
|
|
|
|
2024-03-20 14:44:01 +00:00
|
|
|
type telegramHandler struct{}
|
|
|
|
|
|
|
|
func (telegramHandler) Type() webhook_module.HookType { return webhook_module.TELEGRAM }
|
2024-04-03 12:22:36 +00:00
|
|
|
func (telegramHandler) Icon(size int) template.HTML { return shared.ImgIcon("telegram.png", size) }
|
2024-03-20 14:44:01 +00:00
|
|
|
|
2024-04-03 12:22:36 +00:00
|
|
|
func (telegramHandler) UnmarshalForm(bind func(any)) forms.WebhookForm {
|
2024-03-21 12:59:24 +00:00
|
|
|
var form struct {
|
2024-04-03 12:22:36 +00:00
|
|
|
forms.WebhookCoreForm
|
2024-03-21 12:59:24 +00:00
|
|
|
BotToken string `binding:"Required"`
|
|
|
|
ChatID string `binding:"Required"`
|
|
|
|
ThreadID string
|
|
|
|
}
|
|
|
|
bind(&form)
|
|
|
|
|
2024-04-03 12:22:36 +00:00
|
|
|
return forms.WebhookForm{
|
|
|
|
WebhookCoreForm: form.WebhookCoreForm,
|
|
|
|
URL: fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s&message_thread_id=%s", url.PathEscape(form.BotToken), url.QueryEscape(form.ChatID), url.QueryEscape(form.ThreadID)),
|
|
|
|
ContentType: webhook_model.ContentTypeJSON,
|
|
|
|
Secret: "",
|
|
|
|
HTTPMethod: http.MethodPost,
|
2024-03-21 12:59:24 +00:00
|
|
|
Metadata: &TelegramMeta{
|
|
|
|
BotToken: form.BotToken,
|
|
|
|
ChatID: form.ChatID,
|
|
|
|
ThreadID: form.ThreadID,
|
|
|
|
},
|
|
|
|
}
|
2024-03-21 12:23:27 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 02:45:02 +00:00
|
|
|
type (
|
|
|
|
// TelegramPayload represents
|
|
|
|
TelegramPayload struct {
|
2019-04-23 16:12:00 +00:00
|
|
|
Message string `json:"text"`
|
|
|
|
ParseMode string `json:"parse_mode"`
|
|
|
|
DisableWebPreview bool `json:"disable_web_page_preview"`
|
2019-04-19 02:45:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TelegramMeta contains the telegram metadata
|
|
|
|
TelegramMeta struct {
|
|
|
|
BotToken string `json:"bot_token"`
|
|
|
|
ChatID string `json:"chat_id"`
|
2023-08-13 14:00:06 +00:00
|
|
|
ThreadID string `json:"thread_id"`
|
2019-04-19 02:45:02 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-03-20 14:44:01 +00:00
|
|
|
// Metadata returns telegram metadata
|
|
|
|
func (telegramHandler) Metadata(w *webhook_model.Webhook) any {
|
2019-11-03 22:13:25 +00:00
|
|
|
s := &TelegramMeta{}
|
|
|
|
if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
|
2024-03-20 14:44:01 +00:00
|
|
|
log.Error("telegramHandler.Metadata(%d): %v", w.ID, err)
|
2019-11-03 22:13:25 +00:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-09-05 02:57:13 +00:00
|
|
|
// Create implements PayloadConvertor Create method
|
2024-03-07 22:18:38 +00:00
|
|
|
func (t telegramConvertor) Create(p *api.CreatePayload) (TelegramPayload, error) {
|
2019-04-19 02:45:02 +00:00
|
|
|
// created tag/branch
|
2023-05-26 01:04:48 +00:00
|
|
|
refName := git.RefName(p.Ref).ShortName()
|
2019-04-19 02:45:02 +00:00
|
|
|
title := fmt.Sprintf(`[<a href="%s">%s</a>] %s <a href="%s">%s</a> created`, p.Repo.HTMLURL, p.Repo.FullName, p.RefType,
|
|
|
|
p.Repo.HTMLURL+"/src/"+refName, refName)
|
|
|
|
|
2021-06-21 02:12:19 +00:00
|
|
|
return createTelegramPayload(title), nil
|
2019-04-19 02:45:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 02:57:13 +00:00
|
|
|
// Delete implements PayloadConvertor Delete method
|
2024-03-07 22:18:38 +00:00
|
|
|
func (t telegramConvertor) Delete(p *api.DeletePayload) (TelegramPayload, error) {
|
2019-04-19 02:45:02 +00:00
|
|
|
// created tag/branch
|
2023-05-26 01:04:48 +00:00
|
|
|
refName := git.RefName(p.Ref).ShortName()
|
2019-04-19 02:45:02 +00:00
|
|
|
title := fmt.Sprintf(`[<a href="%s">%s</a>] %s <a href="%s">%s</a> deleted`, p.Repo.HTMLURL, p.Repo.FullName, p.RefType,
|
|
|
|
p.Repo.HTMLURL+"/src/"+refName, refName)
|
|
|
|
|
2021-06-21 02:12:19 +00:00
|
|
|
return createTelegramPayload(title), nil
|
2019-04-19 02:45:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 02:57:13 +00:00
|
|
|
// Fork implements PayloadConvertor Fork method
|
2024-03-07 22:18:38 +00:00
|
|
|
func (t telegramConvertor) Fork(p *api.ForkPayload) (TelegramPayload, error) {
|
2019-04-19 02:45:02 +00:00
|
|
|
title := fmt.Sprintf(`%s is forked to <a href="%s">%s</a>`, p.Forkee.FullName, p.Repo.HTMLURL, p.Repo.FullName)
|
|
|
|
|
2021-06-21 02:12:19 +00:00
|
|
|
return createTelegramPayload(title), nil
|
2019-04-19 02:45:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 02:57:13 +00:00
|
|
|
// Push implements PayloadConvertor Push method
|
2024-03-07 22:18:38 +00:00
|
|
|
func (t telegramConvertor) Push(p *api.PushPayload) (TelegramPayload, error) {
|
2019-04-19 02:45:02 +00:00
|
|
|
var (
|
2023-05-26 01:04:48 +00:00
|
|
|
branchName = git.RefName(p.Ref).ShortName()
|
2019-04-19 02:45:02 +00:00
|
|
|
commitDesc string
|
|
|
|
)
|
|
|
|
|
|
|
|
var titleLink string
|
2022-10-16 16:22:34 +00:00
|
|
|
if p.TotalCommits == 1 {
|
2019-04-19 02:45:02 +00:00
|
|
|
commitDesc = "1 new commit"
|
|
|
|
titleLink = p.Commits[0].URL
|
|
|
|
} else {
|
2022-10-16 16:22:34 +00:00
|
|
|
commitDesc = fmt.Sprintf("%d new commits", p.TotalCommits)
|
2019-04-19 02:45:02 +00:00
|
|
|
titleLink = p.CompareURL
|
|
|
|
}
|
|
|
|
if titleLink == "" {
|
|
|
|
titleLink = p.Repo.HTMLURL + "/src/" + branchName
|
|
|
|
}
|
|
|
|
title := fmt.Sprintf(`[<a href="%s">%s</a>:<a href="%s">%s</a>] %s`, p.Repo.HTMLURL, p.Repo.FullName, titleLink, 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(`[<a href="%s">%s</a>] %s`, commit.URL, commit.ID[:7],
|
|
|
|
strings.TrimRight(commit.Message, "\r\n")) + authorName
|
|
|
|
// add linebreak to each commit but the last
|
|
|
|
if i < len(p.Commits)-1 {
|
|
|
|
text += "\n"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-21 02:12:19 +00:00
|
|
|
return createTelegramPayload(title + "\n" + text), nil
|
2019-04-19 02:45:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 02:57:13 +00:00
|
|
|
// Issue implements PayloadConvertor Issue method
|
2024-03-07 22:18:38 +00:00
|
|
|
func (t telegramConvertor) Issue(p *api.IssuePayload) (TelegramPayload, error) {
|
2020-01-04 22:20:15 +00:00
|
|
|
text, _, attachmentText, _ := getIssuesPayloadInfo(p, htmlLinkFormatter, true)
|
2019-04-19 02:45:02 +00:00
|
|
|
|
2021-06-21 02:12:19 +00:00
|
|
|
return createTelegramPayload(text + "\n\n" + attachmentText), nil
|
2019-04-19 02:45:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 02:57:13 +00:00
|
|
|
// IssueComment implements PayloadConvertor IssueComment method
|
2024-03-07 22:18:38 +00:00
|
|
|
func (t telegramConvertor) IssueComment(p *api.IssueCommentPayload) (TelegramPayload, error) {
|
2020-01-04 22:20:15 +00:00
|
|
|
text, _, _ := getIssueCommentPayloadInfo(p, htmlLinkFormatter, true)
|
2019-04-19 02:45:02 +00:00
|
|
|
|
2021-06-21 02:12:19 +00:00
|
|
|
return createTelegramPayload(text + "\n" + p.Comment.Body), nil
|
2019-04-19 02:45:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 02:57:13 +00:00
|
|
|
// PullRequest implements PayloadConvertor PullRequest method
|
2024-03-07 22:18:38 +00:00
|
|
|
func (t telegramConvertor) PullRequest(p *api.PullRequestPayload) (TelegramPayload, error) {
|
2020-01-04 22:20:15 +00:00
|
|
|
text, _, attachmentText, _ := getPullRequestPayloadInfo(p, htmlLinkFormatter, true)
|
2019-04-19 02:45:02 +00:00
|
|
|
|
2021-06-21 02:12:19 +00:00
|
|
|
return createTelegramPayload(text + "\n" + attachmentText), nil
|
2019-04-19 02:45:02 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 02:57:13 +00:00
|
|
|
// Review implements PayloadConvertor Review method
|
2024-03-07 22:18:38 +00:00
|
|
|
func (t telegramConvertor) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (TelegramPayload, error) {
|
2020-01-08 17:45:24 +00:00
|
|
|
var text, attachmentText string
|
|
|
|
switch p.Action {
|
2020-03-06 05:10:48 +00:00
|
|
|
case api.HookIssueReviewed:
|
2020-01-08 17:45:24 +00:00
|
|
|
action, err := parseHookPullRequestEventType(event)
|
|
|
|
if err != nil {
|
2024-03-07 22:18:38 +00:00
|
|
|
return TelegramPayload{}, err
|
2020-01-08 17:45:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
text = fmt.Sprintf("[%s] Pull request review %s: #%d %s", p.Repository.FullName, action, p.Index, p.PullRequest.Title)
|
|
|
|
attachmentText = p.Review.Content
|
|
|
|
}
|
|
|
|
|
2021-06-21 02:12:19 +00:00
|
|
|
return createTelegramPayload(text + "\n" + attachmentText), nil
|
2020-01-08 17:45:24 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 02:57:13 +00:00
|
|
|
// Repository implements PayloadConvertor Repository method
|
2024-03-07 22:18:38 +00:00
|
|
|
func (t telegramConvertor) Repository(p *api.RepositoryPayload) (TelegramPayload, error) {
|
2019-04-19 02:45:02 +00:00
|
|
|
var title string
|
|
|
|
switch p.Action {
|
|
|
|
case api.HookRepoCreated:
|
|
|
|
title = fmt.Sprintf(`[<a href="%s">%s</a>] Repository created`, p.Repository.HTMLURL, p.Repository.FullName)
|
2021-06-21 02:12:19 +00:00
|
|
|
return createTelegramPayload(title), nil
|
2019-04-19 02:45:02 +00:00
|
|
|
case api.HookRepoDeleted:
|
|
|
|
title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
|
2021-06-21 02:12:19 +00:00
|
|
|
return createTelegramPayload(title), nil
|
2019-04-19 02:45:02 +00:00
|
|
|
}
|
2024-03-07 22:18:38 +00:00
|
|
|
return TelegramPayload{}, nil
|
2019-04-19 02:45:02 +00:00
|
|
|
}
|
|
|
|
|
2022-09-04 19:54:23 +00:00
|
|
|
// Wiki implements PayloadConvertor Wiki method
|
2024-03-07 22:18:38 +00:00
|
|
|
func (t telegramConvertor) Wiki(p *api.WikiPayload) (TelegramPayload, error) {
|
2022-09-04 19:54:23 +00:00
|
|
|
text, _, _ := getWikiPayloadInfo(p, htmlLinkFormatter, true)
|
|
|
|
|
|
|
|
return createTelegramPayload(text), nil
|
|
|
|
}
|
|
|
|
|
2020-09-05 02:57:13 +00:00
|
|
|
// Release implements PayloadConvertor Release method
|
2024-03-07 22:18:38 +00:00
|
|
|
func (t telegramConvertor) Release(p *api.ReleasePayload) (TelegramPayload, error) {
|
2020-01-04 22:20:15 +00:00
|
|
|
text, _ := getReleasePayloadInfo(p, htmlLinkFormatter, true)
|
2019-04-19 02:45:02 +00:00
|
|
|
|
2021-06-21 02:12:19 +00:00
|
|
|
return createTelegramPayload(text), nil
|
2019-04-19 02:45:02 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 22:18:38 +00:00
|
|
|
func (t telegramConvertor) Package(p *api.PackagePayload) (TelegramPayload, error) {
|
2023-10-31 04:43:38 +00:00
|
|
|
text, _ := getPackagePayloadInfo(p, htmlLinkFormatter, true)
|
|
|
|
|
|
|
|
return createTelegramPayload(text), nil
|
|
|
|
}
|
|
|
|
|
2024-03-07 22:18:38 +00:00
|
|
|
func createTelegramPayload(message string) TelegramPayload {
|
|
|
|
return TelegramPayload{
|
2024-03-17 14:11:28 +00:00
|
|
|
Message: strings.TrimSpace(message),
|
|
|
|
ParseMode: "HTML",
|
|
|
|
DisableWebPreview: true,
|
2021-06-21 02:12:19 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-07 22:18:38 +00:00
|
|
|
|
|
|
|
type telegramConvertor struct{}
|
|
|
|
|
2024-04-03 12:22:36 +00:00
|
|
|
var _ shared.PayloadConvertor[TelegramPayload] = telegramConvertor{}
|
2024-03-07 22:18:38 +00:00
|
|
|
|
2024-03-20 14:44:01 +00:00
|
|
|
func (telegramHandler) NewRequest(ctx context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
|
2024-04-03 12:22:36 +00:00
|
|
|
return shared.NewJSONRequest(telegramConvertor{}, w, t, true)
|
2024-03-07 22:18:38 +00:00
|
|
|
}
|