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-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"
|
2019-05-11 12:21:34 +02:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
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
|
|
|
|
}
|
|
|
|
|
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()
|
2019-04-19 04:45:02 +02: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 04:12:19 +02:00
|
|
|
return createTelegramPayload(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()
|
2019-04-19 04:45:02 +02: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 04:12:19 +02:00
|
|
|
return createTelegramPayload(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) {
|
2019-04-19 04:45:02 +02: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 04:12:19 +02:00
|
|
|
return createTelegramPayload(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) {
|
2019-04-19 04:45:02 +02:00
|
|
|
var (
|
2023-05-26 03:04:48 +02:00
|
|
|
branchName = git.RefName(p.Ref).ShortName()
|
2019-04-19 04:45:02 +02:00
|
|
|
commitDesc string
|
|
|
|
)
|
|
|
|
|
|
|
|
var titleLink 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 == "" {
|
|
|
|
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 04:12:19 +02:00
|
|
|
return createTelegramPayload(title + "\n" + text), 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) {
|
2020-01-04 23:20:15 +01:00
|
|
|
text, _, attachmentText, _ := getIssuesPayloadInfo(p, htmlLinkFormatter, true)
|
2019-04-19 04:45:02 +02:00
|
|
|
|
2021-06-21 04:12:19 +02:00
|
|
|
return createTelegramPayload(text + "\n\n" + attachmentText), 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)
|
2019-04-19 04:45:02 +02:00
|
|
|
|
2021-06-21 04:12:19 +02:00
|
|
|
return createTelegramPayload(text + "\n" + 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) {
|
2020-01-04 23:20:15 +01:00
|
|
|
text, _, attachmentText, _ := getPullRequestPayloadInfo(p, htmlLinkFormatter, true)
|
2019-04-19 04:45:02 +02:00
|
|
|
|
2021-06-21 04:12:19 +02:00
|
|
|
return createTelegramPayload(text + "\n" + attachmentText), 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) {
|
2020-01-08 18:45:24 +01:00
|
|
|
var text, attachmentText string
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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 04:12:19 +02:00
|
|
|
return createTelegramPayload(text + "\n" + attachmentText), 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:
|
|
|
|
title = fmt.Sprintf(`[<a href="%s">%s</a>] Repository created`, p.Repository.HTMLURL, p.Repository.FullName)
|
2021-06-21 04:12:19 +02:00
|
|
|
return createTelegramPayload(title), nil
|
2019-04-19 04:45:02 +02:00
|
|
|
case api.HookRepoDeleted:
|
|
|
|
title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName)
|
2021-06-21 04:12:19 +02:00
|
|
|
return createTelegramPayload(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)
|
|
|
|
|
|
|
|
return createTelegramPayload(text), nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-06-21 04:12:19 +02:00
|
|
|
return createTelegramPayload(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)
|
|
|
|
|
|
|
|
return createTelegramPayload(text), nil
|
|
|
|
}
|
|
|
|
|
2024-03-07 23:18:38 +01:00
|
|
|
func createTelegramPayload(message string) TelegramPayload {
|
|
|
|
return TelegramPayload{
|
2024-03-17 15:11:28 +01:00
|
|
|
Message: strings.TrimSpace(message),
|
|
|
|
ParseMode: "HTML",
|
|
|
|
DisableWebPreview: true,
|
2021-06-21 04:12:19 +02:00
|
|
|
}
|
|
|
|
}
|
2024-03-07 23:18:38 +01:00
|
|
|
|
|
|
|
type telegramConvertor struct{}
|
|
|
|
|
|
|
|
var _ payloadConvertor[TelegramPayload] = telegramConvertor{}
|
|
|
|
|
|
|
|
func newTelegramRequest(ctx context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
|
|
|
|
return newJSONRequest(telegramConvertor{}, w, t, true)
|
|
|
|
}
|