diff --git a/models/webhook.go b/models/webhook.go index eb22e959754..8db281a15c0 100644 --- a/models/webhook.go +++ b/models/webhook.go @@ -145,6 +145,15 @@ func (w *Webhook) GetDiscordHook() *DiscordMeta { return s } +// GetTelegramHook returns telegram metadata +func (w *Webhook) GetTelegramHook() *TelegramMeta { + s := &TelegramMeta{} + if err := json.Unmarshal([]byte(w.Meta), s); err != nil { + log.Error("webhook.GetTelegramHook(%d): %v", w.ID, err) + } + return s +} + // History returns history of webhook by given conditions. func (w *Webhook) History(page int) ([]*HookTask, error) { return HookTasks(w.ID, page) @@ -456,6 +465,7 @@ const ( GITEA DISCORD DINGTALK + TELEGRAM ) var hookTaskTypes = map[string]HookTaskType{ @@ -464,6 +474,7 @@ var hookTaskTypes = map[string]HookTaskType{ "slack": SLACK, "discord": DISCORD, "dingtalk": DINGTALK, + "telegram": TELEGRAM, } // ToHookTaskType returns HookTaskType by given name. @@ -484,6 +495,8 @@ func (t HookTaskType) Name() string { return "discord" case DINGTALK: return "dingtalk" + case TELEGRAM: + return "telegram" } return "" } @@ -657,6 +670,11 @@ func prepareWebhook(e Engine, w *Webhook, repo *Repository, event HookEventType, if err != nil { return fmt.Errorf("GetDingtalkPayload: %v", err) } + case TELEGRAM: + payloader, err = GetTelegramPayload(p, event, w.Meta) + if err != nil { + return fmt.Errorf("GetTelegramPayload: %v", err) + } default: p.SetSecret(w.Secret) payloader = p diff --git a/models/webhook_telegram.go b/models/webhook_telegram.go new file mode 100644 index 00000000000..5680c48b85a --- /dev/null +++ b/models/webhook_telegram.go @@ -0,0 +1,322 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package models + +import ( + "encoding/json" + "fmt" + "strings" + + "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/markup" + api "code.gitea.io/sdk/gitea" +) + +type ( + // TelegramPayload represents + TelegramPayload struct { + Message string `json:"text"` + ParseMode string `json:"parse_mode"` + } + + // TelegramMeta contains the telegram metadata + TelegramMeta struct { + BotToken string `json:"bot_token"` + ChatID string `json:"chat_id"` + } +) + +// SetSecret sets the telegram secret +func (p *TelegramPayload) SetSecret(_ string) {} + +// JSONPayload Marshals the TelegramPayload to json +func (p *TelegramPayload) JSONPayload() ([]byte, error) { + p.ParseMode = "HTML" + p.Message = markup.Sanitize(p.Message) + data, err := json.MarshalIndent(p, "", " ") + if err != nil { + return []byte{}, err + } + return data, nil +} + +func getTelegramCreatePayload(p *api.CreatePayload) (*TelegramPayload, error) { + // created tag/branch + refName := git.RefEndName(p.Ref) + title := fmt.Sprintf(`[%s] %s %s created`, p.Repo.HTMLURL, p.Repo.FullName, p.RefType, + p.Repo.HTMLURL+"/src/"+refName, refName) + + return &TelegramPayload{ + Message: title, + }, nil +} + +func getTelegramDeletePayload(p *api.DeletePayload) (*TelegramPayload, error) { + // created tag/branch + refName := git.RefEndName(p.Ref) + title := fmt.Sprintf(`[%s] %s %s deleted`, p.Repo.HTMLURL, p.Repo.FullName, p.RefType, + p.Repo.HTMLURL+"/src/"+refName, refName) + + return &TelegramPayload{ + Message: title, + }, nil +} + +func getTelegramForkPayload(p *api.ForkPayload) (*TelegramPayload, error) { + title := fmt.Sprintf(`%s is forked to %s`, p.Forkee.FullName, p.Repo.HTMLURL, p.Repo.FullName) + + return &TelegramPayload{ + Message: title, + }, nil +} + +func getTelegramPushPayload(p *api.PushPayload) (*TelegramPayload, error) { + var ( + branchName = git.RefEndName(p.Ref) + commitDesc string + ) + + var titleLink string + if len(p.Commits) == 1 { + commitDesc = "1 new commit" + titleLink = p.Commits[0].URL + } else { + commitDesc = fmt.Sprintf("%d new commits", len(p.Commits)) + titleLink = p.CompareURL + } + if titleLink == "" { + titleLink = p.Repo.HTMLURL + "/src/" + branchName + } + title := fmt.Sprintf(`[%s:%s] %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(`[%s] %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" + } + } + + return &TelegramPayload{ + Message: title + "\n" + text, + }, nil +} + +func getTelegramIssuesPayload(p *api.IssuePayload) (*TelegramPayload, error) { + var text, title string + switch p.Action { + case api.HookIssueOpened: + title = fmt.Sprintf(`[%s] Issue opened: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, + p.Issue.URL, p.Index, p.Issue.Title) + text = p.Issue.Body + case api.HookIssueClosed: + title = fmt.Sprintf(`[%s] Issue closed: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, + p.Issue.URL, p.Index, p.Issue.Title) + text = p.Issue.Body + case api.HookIssueReOpened: + title = fmt.Sprintf(`[%s] Issue re-opened: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, + p.Issue.URL, p.Index, p.Issue.Title) + text = p.Issue.Body + case api.HookIssueEdited: + title = fmt.Sprintf(`[%s] Issue edited: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, + p.Issue.URL, p.Index, p.Issue.Title) + text = p.Issue.Body + case api.HookIssueAssigned: + title = fmt.Sprintf(`[%s] Issue assigned to %s: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, + p.Issue.Assignee.UserName, p.Issue.URL, p.Index, p.Issue.Title) + text = p.Issue.Body + case api.HookIssueUnassigned: + title = fmt.Sprintf(`[%s] Issue unassigned: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, + p.Issue.URL, p.Index, p.Issue.Title) + text = p.Issue.Body + case api.HookIssueLabelUpdated: + title = fmt.Sprintf(`[%s] Issue labels updated: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, + p.Issue.URL, p.Index, p.Issue.Title) + text = p.Issue.Body + case api.HookIssueLabelCleared: + title = fmt.Sprintf(`[%s] Issue labels cleared: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, + p.Issue.URL, p.Index, p.Issue.Title) + text = p.Issue.Body + case api.HookIssueSynchronized: + title = fmt.Sprintf(`[%s] Issue synchronized: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, + p.Issue.URL, p.Index, p.Issue.Title) + text = p.Issue.Body + case api.HookIssueMilestoned: + title = fmt.Sprintf(`[%s] Issue milestone: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, + p.Issue.URL, p.Index, p.Issue.Title) + text = p.Issue.Body + case api.HookIssueDemilestoned: + title = fmt.Sprintf(`[%s] Issue clear milestone: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, + p.Issue.URL, p.Index, p.Issue.Title) + text = p.Issue.Body + } + + return &TelegramPayload{ + Message: title + "\n\n" + text, + }, nil +} + +func getTelegramIssueCommentPayload(p *api.IssueCommentPayload) (*TelegramPayload, error) { + url := fmt.Sprintf("%s/issues/%d#%s", p.Repository.HTMLURL, p.Issue.Index, CommentHashTag(p.Comment.ID)) + title := fmt.Sprintf(`#%d %s`, url, p.Issue.Index, p.Issue.Title) + var text string + switch p.Action { + case api.HookIssueCommentCreated: + text = "New comment: " + title + text += p.Comment.Body + case api.HookIssueCommentEdited: + text = "Comment edited: " + title + text += p.Comment.Body + case api.HookIssueCommentDeleted: + text = "Comment deleted: " + title + text += p.Comment.Body + } + + return &TelegramPayload{ + Message: title + "\n" + text, + }, nil +} + +func getTelegramPullRequestPayload(p *api.PullRequestPayload) (*TelegramPayload, error) { + var text, title string + switch p.Action { + case api.HookIssueOpened: + title = fmt.Sprintf(`[%s] Pull request opened: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, + p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) + text = p.PullRequest.Body + case api.HookIssueClosed: + if p.PullRequest.HasMerged { + title = fmt.Sprintf(`[%s] Pull request merged: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, + p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) + } else { + title = fmt.Sprintf(`[%s] Pull request closed: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, + p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) + } + text = p.PullRequest.Body + case api.HookIssueReOpened: + title = fmt.Sprintf(`[%s] Pull request re-opened: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, + p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) + text = p.PullRequest.Body + case api.HookIssueEdited: + title = fmt.Sprintf(`[%s] Pull request edited: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, + p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) + text = p.PullRequest.Body + case api.HookIssueAssigned: + list, err := MakeAssigneeList(&Issue{ID: p.PullRequest.ID}) + if err != nil { + return &TelegramPayload{}, err + } + title = fmt.Sprintf(`[%s] Pull request assigned to %s: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, + list, p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) + text = p.PullRequest.Body + case api.HookIssueUnassigned: + title = fmt.Sprintf(`[%s] Pull request unassigned: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, + p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) + text = p.PullRequest.Body + case api.HookIssueLabelUpdated: + title = fmt.Sprintf(`[%s] Pull request labels updated: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, + p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) + text = p.PullRequest.Body + case api.HookIssueLabelCleared: + title = fmt.Sprintf(`[%s] Pull request labels cleared: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, + p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) + text = p.PullRequest.Body + case api.HookIssueSynchronized: + title = fmt.Sprintf(`[%s] Pull request synchronized: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, + p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) + text = p.PullRequest.Body + case api.HookIssueMilestoned: + title = fmt.Sprintf(`[%s] Pull request milestone: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, + p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) + text = p.PullRequest.Body + case api.HookIssueDemilestoned: + title = fmt.Sprintf(`[%s] Pull request clear milestone: #%d %s`, p.Repository.HTMLURL, p.Repository.FullName, + p.PullRequest.HTMLURL, p.Index, p.PullRequest.Title) + text = p.PullRequest.Body + } + + return &TelegramPayload{ + Message: title + "\n" + text, + }, nil +} + +func getTelegramRepositoryPayload(p *api.RepositoryPayload) (*TelegramPayload, error) { + var title string + switch p.Action { + case api.HookRepoCreated: + title = fmt.Sprintf(`[%s] Repository created`, p.Repository.HTMLURL, p.Repository.FullName) + return &TelegramPayload{ + Message: title, + }, nil + case api.HookRepoDeleted: + title = fmt.Sprintf("[%s] Repository deleted", p.Repository.FullName) + return &TelegramPayload{ + Message: title, + }, nil + } + return nil, nil +} + +func getTelegramReleasePayload(p *api.ReleasePayload) (*TelegramPayload, error) { + var title, url string + switch p.Action { + case api.HookReleasePublished: + title = fmt.Sprintf("[%s] Release created", p.Release.TagName) + url = p.Release.URL + return &TelegramPayload{ + Message: title + "\n" + url, + }, nil + case api.HookReleaseUpdated: + title = fmt.Sprintf("[%s] Release updated", p.Release.TagName) + url = p.Release.URL + return &TelegramPayload{ + Message: title + "\n" + url, + }, nil + + case api.HookReleaseDeleted: + title = fmt.Sprintf("[%s] Release deleted", p.Release.TagName) + url = p.Release.URL + return &TelegramPayload{ + Message: title + "\n" + url, + }, nil + } + + return nil, nil +} + +// GetTelegramPayload converts a telegram webhook into a TelegramPayload +func GetTelegramPayload(p api.Payloader, event HookEventType, meta string) (*TelegramPayload, error) { + s := new(TelegramPayload) + + switch event { + case HookEventCreate: + return getTelegramCreatePayload(p.(*api.CreatePayload)) + case HookEventDelete: + return getTelegramDeletePayload(p.(*api.DeletePayload)) + case HookEventFork: + return getTelegramForkPayload(p.(*api.ForkPayload)) + case HookEventIssues: + return getTelegramIssuesPayload(p.(*api.IssuePayload)) + case HookEventIssueComment: + return getTelegramIssueCommentPayload(p.(*api.IssueCommentPayload)) + case HookEventPush: + return getTelegramPushPayload(p.(*api.PushPayload)) + case HookEventPullRequest: + return getTelegramPullRequestPayload(p.(*api.PullRequestPayload)) + case HookEventRepository: + return getTelegramRepositoryPayload(p.(*api.RepositoryPayload)) + case HookEventRelease: + return getTelegramReleasePayload(p.(*api.ReleasePayload)) + } + + return s, nil +} diff --git a/models/webhook_test.go b/models/webhook_test.go index 50106a37923..518be8be8a3 100644 --- a/models/webhook_test.go +++ b/models/webhook_test.go @@ -197,18 +197,21 @@ func TestToHookTaskType(t *testing.T) { assert.Equal(t, GOGS, ToHookTaskType("gogs")) assert.Equal(t, SLACK, ToHookTaskType("slack")) assert.Equal(t, GITEA, ToHookTaskType("gitea")) + assert.Equal(t, TELEGRAM, ToHookTaskType("telegram")) } func TestHookTaskType_Name(t *testing.T) { assert.Equal(t, "gogs", GOGS.Name()) assert.Equal(t, "slack", SLACK.Name()) assert.Equal(t, "gitea", GITEA.Name()) + assert.Equal(t, "telegram", TELEGRAM.Name()) } func TestIsValidHookTaskType(t *testing.T) { assert.True(t, IsValidHookTaskType("gogs")) assert.True(t, IsValidHookTaskType("slack")) assert.True(t, IsValidHookTaskType("gitea")) + assert.True(t, IsValidHookTaskType("telegram")) assert.False(t, IsValidHookTaskType("invalid")) } diff --git a/modules/auth/repo_form.go b/modules/auth/repo_form.go index 990a94dd63c..d37a5b94d8a 100644 --- a/modules/auth/repo_form.go +++ b/modules/auth/repo_form.go @@ -263,6 +263,18 @@ func (f *NewDingtalkHookForm) Validate(ctx *macaron.Context, errs binding.Errors return validate(errs, ctx.Data, f, ctx.Locale) } +// NewTelegramHookForm form for creating telegram hook +type NewTelegramHookForm struct { + BotToken string `binding:"Required"` + ChatID string `binding:"Required"` + WebhookForm +} + +// Validate validates the fields +func (f *NewTelegramHookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) +} + // .___ // | | ______ ________ __ ____ // | |/ ___// ___/ | \_/ __ \ diff --git a/modules/setting/webhook.go b/modules/setting/webhook.go index 741963e545f..0d91e7d9e78 100644 --- a/modules/setting/webhook.go +++ b/modules/setting/webhook.go @@ -25,6 +25,6 @@ func newWebhookService() { Webhook.QueueLength = sec.Key("QUEUE_LENGTH").MustInt(1000) Webhook.DeliverTimeout = sec.Key("DELIVER_TIMEOUT").MustInt(5) Webhook.SkipTLSVerify = sec.Key("SKIP_TLS_VERIFY").MustBool() - Webhook.Types = []string{"gitea", "gogs", "slack", "discord", "dingtalk"} + Webhook.Types = []string{"gitea", "gogs", "slack", "discord", "dingtalk", "telegram"} Webhook.PagingNum = sec.Key("PAGING_NUM").MustInt(10) } diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index b53a72da9be..5c391cf566d 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1211,6 +1211,7 @@ settings.slack_domain = Domain settings.slack_channel = Channel settings.add_discord_hook_desc = Integrate Discord into your repository. settings.add_dingtalk_hook_desc = Integrate Dingtalk into your repository. +settings.add_telegram_hook_desc = Integrate Telegram into your repository. settings.deploy_keys = Deploy Keys settings.add_deploy_key = Add Deploy Key settings.deploy_key_desc = Deploy keys have read-only pull access to the repository. @@ -1258,6 +1259,8 @@ settings.choose_branch = Choose a branch… settings.no_protected_branch = There are no protected branches. settings.edit_protected_branch = Edit settings.protected_branch_required_approvals_min = Required approvals cannot be negative. +settings.bot_token = Bot Token +settings.chat_id = Chat ID settings.archive.button = Archive Repo settings.archive.header = Archive This Repo settings.archive.text = Archiving the repo will make it entirely read-only. It is hidden from the dashboard, cannot be committed to and no issues or pull-requests can be created. diff --git a/public/img/telegram.png b/public/img/telegram.png new file mode 100644 index 00000000000..ee0756db5e5 Binary files /dev/null and b/public/img/telegram.png differ diff --git a/routers/repo/webhook.go b/routers/repo/webhook.go index 8510cb7ba21..bad109cd532 100644 --- a/routers/repo/webhook.go +++ b/routers/repo/webhook.go @@ -331,6 +331,55 @@ func DingtalkHooksNewPost(ctx *context.Context, form auth.NewDingtalkHookForm) { ctx.Redirect(orCtx.Link) } +// TelegramHooksNewPost response for creating telegram hook +func TelegramHooksNewPost(ctx *context.Context, form auth.NewTelegramHookForm) { + ctx.Data["Title"] = ctx.Tr("repo.settings") + ctx.Data["PageIsSettingsHooks"] = true + ctx.Data["PageIsSettingsHooksNew"] = true + ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}} + + orCtx, err := getOrgRepoCtx(ctx) + if err != nil { + ctx.ServerError("getOrgRepoCtx", err) + return + } + + if ctx.HasError() { + ctx.HTML(200, orCtx.NewTemplate) + return + } + + meta, err := json.Marshal(&models.TelegramMeta{ + BotToken: form.BotToken, + ChatID: form.ChatID, + }) + if err != nil { + ctx.ServerError("Marshal", err) + return + } + + w := &models.Webhook{ + RepoID: orCtx.RepoID, + URL: fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s", form.BotToken, form.ChatID), + ContentType: models.ContentTypeJSON, + HookEvent: ParseHookEvent(form.WebhookForm), + IsActive: form.Active, + HookTaskType: models.TELEGRAM, + Meta: string(meta), + OrgID: orCtx.OrgID, + } + if err := w.UpdateEvent(); err != nil { + ctx.ServerError("UpdateEvent", err) + return + } else if err := models.CreateWebhook(w); err != nil { + ctx.ServerError("CreateWebhook", err) + return + } + + ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success")) + ctx.Redirect(orCtx.Link) +} + // SlackHooksNewPost response for creating slack hook func SlackHooksNewPost(ctx *context.Context, form auth.NewSlackHookForm) { ctx.Data["Title"] = ctx.Tr("repo.settings") @@ -421,6 +470,8 @@ func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) { ctx.Data["SlackHook"] = w.GetSlackHook() case models.DISCORD: ctx.Data["DiscordHook"] = w.GetDiscordHook() + case models.TELEGRAM: + ctx.Data["TelegramHook"] = w.GetTelegramHook() } ctx.Data["History"], err = w.History(1) @@ -647,6 +698,46 @@ func DingtalkHooksEditPost(ctx *context.Context, form auth.NewDingtalkHookForm) ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID)) } +// TelegramHooksEditPost response for editing discord hook +func TelegramHooksEditPost(ctx *context.Context, form auth.NewTelegramHookForm) { + ctx.Data["Title"] = ctx.Tr("repo.settings") + ctx.Data["PageIsSettingsHooks"] = true + ctx.Data["PageIsSettingsHooksEdit"] = true + + orCtx, w := checkWebhook(ctx) + if ctx.Written() { + return + } + ctx.Data["Webhook"] = w + + if ctx.HasError() { + ctx.HTML(200, orCtx.NewTemplate) + return + } + meta, err := json.Marshal(&models.TelegramMeta{ + BotToken: form.BotToken, + ChatID: form.ChatID, + }) + if err != nil { + ctx.ServerError("Marshal", err) + return + } + w.Meta = string(meta) + w.URL = fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s", form.BotToken, form.ChatID) + w.HookEvent = ParseHookEvent(form.WebhookForm) + w.IsActive = form.Active + if err := w.UpdateEvent(); err != nil { + ctx.ServerError("UpdateEvent", err) + return + } else if err := models.UpdateWebhook(w); err != nil { + ctx.ServerError("UpdateWebhook", err) + return + } + + ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success")) + ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID)) +} + // TestWebhook test if web hook is work fine func TestWebhook(ctx *context.Context) { hookID := ctx.ParamsInt64(":id") diff --git a/routers/routes/routes.go b/routers/routes/routes.go index e42222be886..874d6494cb7 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -559,12 +559,14 @@ func RegisterRoutes(m *macaron.Macaron) { m.Post("/slack/new", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksNewPost) m.Post("/discord/new", bindIgnErr(auth.NewDiscordHookForm{}), repo.DiscordHooksNewPost) m.Post("/dingtalk/new", bindIgnErr(auth.NewDingtalkHookForm{}), repo.DingtalkHooksNewPost) + m.Post("/telegram/new", bindIgnErr(auth.NewTelegramHookForm{}), repo.TelegramHooksNewPost) m.Get("/:id", repo.WebHooksEdit) m.Post("/gitea/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost) m.Post("/gogs/:id", bindIgnErr(auth.NewGogshookForm{}), repo.GogsHooksEditPost) m.Post("/slack/:id", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksEditPost) m.Post("/discord/:id", bindIgnErr(auth.NewDiscordHookForm{}), repo.DiscordHooksEditPost) m.Post("/dingtalk/:id", bindIgnErr(auth.NewDingtalkHookForm{}), repo.DingtalkHooksEditPost) + m.Post("/telegram/:id", bindIgnErr(auth.NewTelegramHookForm{}), repo.TelegramHooksEditPost) }) m.Route("/delete", "GET,POST", org.SettingsDelete) @@ -612,6 +614,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Post("/slack/new", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksNewPost) m.Post("/discord/new", bindIgnErr(auth.NewDiscordHookForm{}), repo.DiscordHooksNewPost) m.Post("/dingtalk/new", bindIgnErr(auth.NewDingtalkHookForm{}), repo.DingtalkHooksNewPost) + m.Post("/telegram/new", bindIgnErr(auth.NewTelegramHookForm{}), repo.TelegramHooksNewPost) m.Get("/:id", repo.WebHooksEdit) m.Post("/:id/test", repo.TestWebhook) m.Post("/gitea/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost) @@ -619,6 +622,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Post("/slack/:id", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksEditPost) m.Post("/discord/:id", bindIgnErr(auth.NewDiscordHookForm{}), repo.DiscordHooksEditPost) m.Post("/dingtalk/:id", bindIgnErr(auth.NewDingtalkHookForm{}), repo.DingtalkHooksEditPost) + m.Post("/telegram/:id", bindIgnErr(auth.NewTelegramHookForm{}), repo.TelegramHooksEditPost) m.Group("/git", func() { m.Get("", repo.GitHooks) diff --git a/templates/org/settings/hook_new.tmpl b/templates/org/settings/hook_new.tmpl index 809009b66b7..5b216c466b7 100644 --- a/templates/org/settings/hook_new.tmpl +++ b/templates/org/settings/hook_new.tmpl @@ -19,6 +19,8 @@ {{else if eq .HookType "dingtalk"}} + {{else if eq .HookType "telegram"}} + {{end}} @@ -28,6 +30,7 @@ {{template "repo/settings/webhook/slack" .}} {{template "repo/settings/webhook/discord" .}} {{template "repo/settings/webhook/dingtalk" .}} + {{template "repo/settings/webhook/telegram" .}} {{template "repo/settings/webhook/history" .}} diff --git a/templates/repo/settings/webhook/list.tmpl b/templates/repo/settings/webhook/list.tmpl index d2985c36769..ddba0f863cc 100644 --- a/templates/repo/settings/webhook/list.tmpl +++ b/templates/repo/settings/webhook/list.tmpl @@ -20,6 +20,9 @@ Dingtalk + + Telegram + diff --git a/templates/repo/settings/webhook/new.tmpl b/templates/repo/settings/webhook/new.tmpl index 1b3d114577a..8c8b3280e55 100644 --- a/templates/repo/settings/webhook/new.tmpl +++ b/templates/repo/settings/webhook/new.tmpl @@ -17,6 +17,8 @@ {{else if eq .HookType "dingtalk"}} + {{else if eq .HookType "telegram"}} + {{end}} @@ -26,6 +28,7 @@ {{template "repo/settings/webhook/slack" .}} {{template "repo/settings/webhook/discord" .}} {{template "repo/settings/webhook/dingtalk" .}} + {{template "repo/settings/webhook/telegram" .}} {{template "repo/settings/webhook/history" .}} diff --git a/templates/repo/settings/webhook/telegram.tmpl b/templates/repo/settings/webhook/telegram.tmpl new file mode 100644 index 00000000000..598ac448223 --- /dev/null +++ b/templates/repo/settings/webhook/telegram.tmpl @@ -0,0 +1,15 @@ +{{if eq .HookType "telegram"}} +
{{.i18n.Tr "repo.settings.add_telegram_hook_desc" "https://core.telegram.org/bots" | Str2html}}
+ +{{end}}