diff options
author | techknowlogick <matti@mdranta.net> | 2019-04-18 22:45:02 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-18 22:45:02 -0400 |
commit | 56da256853001cf3538b8d4ae99798e084935a90 (patch) | |
tree | cdbd686d31750aa89365635eb96705c5a9fc1efc /routers | |
parent | 6dbd26185203d464c4a4e32e7af04a34f37ae4f2 (diff) | |
download | gitea-56da256853001cf3538b8d4ae99798e084935a90.tar.gz gitea-56da256853001cf3538b8d4ae99798e084935a90.zip |
Telegram webhook (#4227)
Diffstat (limited to 'routers')
-rw-r--r-- | routers/repo/webhook.go | 91 | ||||
-rw-r--r-- | routers/routes/routes.go | 4 |
2 files changed, 95 insertions, 0 deletions
diff --git a/routers/repo/webhook.go b/routers/repo/webhook.go index 8510cb7ba2..bad109cd53 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 e42222be88..874d6494cb 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) |