summaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2017-08-28 13:06:45 +0800
committerGitHub <noreply@github.com>2017-08-28 13:06:45 +0800
commitced50e0ec13085504fa19c82f018a2eecb70ff68 (patch)
tree10dff3ff685a86bad683c731f8329743f217617e /routers
parente41da3845d72e3adc7ae2cd3a02fc1f3943ebee6 (diff)
downloadgitea-ced50e0ec13085504fa19c82f018a2eecb70ff68.tar.gz
gitea-ced50e0ec13085504fa19c82f018a2eecb70ff68.zip
Implementation of discord webhook (#2402)
* implementation of discord webhook * fix webhooks * fix typo and unnecessary color values * fix typo * fix imports and revert changes to webhook_slack.go
Diffstat (limited to 'routers')
-rw-r--r--routers/repo/webhook.go111
-rw-r--r--routers/routes/routes.go2
2 files changed, 108 insertions, 5 deletions
diff --git a/routers/repo/webhook.go b/routers/repo/webhook.go
index 5489d71857..ade40649b6 100644
--- a/routers/repo/webhook.go
+++ b/routers/repo/webhook.go
@@ -11,16 +11,15 @@ import (
"fmt"
"strings"
- "github.com/Unknwon/com"
-
"code.gitea.io/git"
- api "code.gitea.io/sdk/gitea"
-
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/auth"
"code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/setting"
+ api "code.gitea.io/sdk/gitea"
+
+ "github.com/Unknwon/com"
)
const (
@@ -96,10 +95,18 @@ func WebhooksNew(ctx *context.Context) {
return
}
- ctx.Data["HookType"] = checkHookType(ctx)
+ hookType := checkHookType(ctx)
+ ctx.Data["HookType"] = hookType
if ctx.Written() {
return
}
+ if hookType == "discord" {
+ ctx.Data["DiscordHook"] = map[string]interface{}{
+ "Username": "Gitea",
+ "IconURL": setting.AppURL + "img/favicon.png",
+ "Color": 16724530,
+ }
+ }
ctx.Data["BaseLink"] = orCtx.Link
ctx.HTML(200, orCtx.NewTemplate)
@@ -213,6 +220,55 @@ func GogsHooksNewPost(ctx *context.Context, form auth.NewWebhookForm) {
ctx.Redirect(orCtx.Link + "/settings/hooks")
}
+// DiscordHooksNewPost response for creating discord hook
+func DiscordHooksNewPost(ctx *context.Context, form auth.NewDiscordHookForm) {
+ 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.Handle(500, "getOrgRepoCtx", err)
+ return
+ }
+
+ if ctx.HasError() {
+ ctx.HTML(200, orCtx.NewTemplate)
+ return
+ }
+
+ meta, err := json.Marshal(&models.DiscordMeta{
+ Username: form.Username,
+ IconURL: form.IconURL,
+ })
+ if err != nil {
+ ctx.Handle(500, "Marshal", err)
+ return
+ }
+
+ w := &models.Webhook{
+ RepoID: orCtx.RepoID,
+ URL: form.PayloadURL,
+ ContentType: models.ContentTypeJSON,
+ HookEvent: ParseHookEvent(form.WebhookForm),
+ IsActive: form.Active,
+ HookTaskType: models.DISCORD,
+ Meta: string(meta),
+ OrgID: orCtx.OrgID,
+ }
+ if err := w.UpdateEvent(); err != nil {
+ ctx.Handle(500, "UpdateEvent", err)
+ return
+ } else if err := models.CreateWebhook(w); err != nil {
+ ctx.Handle(500, "CreateWebhook", err)
+ return
+ }
+
+ ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
+ ctx.Redirect(orCtx.Link + "/settings/hooks")
+}
+
// SlackHooksNewPost response for creating slack hook
func SlackHooksNewPost(ctx *context.Context, form auth.NewSlackHookForm) {
ctx.Data["Title"] = ctx.Tr("repo.settings")
@@ -295,6 +351,9 @@ func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) {
ctx.Data["HookType"] = "slack"
case models.GOGS:
ctx.Data["HookType"] = "gogs"
+ case models.DISCORD:
+ ctx.Data["DiscordHook"] = w.GetDiscordHook()
+ ctx.Data["HookType"] = "discord"
default:
ctx.Data["HookType"] = "gitea"
}
@@ -443,6 +502,48 @@ func SlackHooksEditPost(ctx *context.Context, form auth.NewSlackHookForm) {
ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
}
+// DiscordHooksEditPost response for editing discord hook
+func DiscordHooksEditPost(ctx *context.Context, form auth.NewDiscordHookForm) {
+ 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.DiscordMeta{
+ Username: form.Username,
+ IconURL: form.IconURL,
+ })
+ if err != nil {
+ ctx.Handle(500, "Marshal", err)
+ return
+ }
+
+ w.URL = form.PayloadURL
+ w.Meta = string(meta)
+ w.HookEvent = ParseHookEvent(form.WebhookForm)
+ w.IsActive = form.Active
+ if err := w.UpdateEvent(); err != nil {
+ ctx.Handle(500, "UpdateEvent", err)
+ return
+ } else if err := models.UpdateWebhook(w); err != nil {
+ ctx.Handle(500, "UpdateWebhook", err)
+ return
+ }
+
+ ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
+ ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
+}
+
// TestWebhook test if web hook is work fine
func TestWebhook(ctx *context.Context) {
// Grab latest commit or fake one if it's empty repository.
diff --git a/routers/routes/routes.go b/routers/routes/routes.go
index d765c4c03b..c619c8b5af 100644
--- a/routers/routes/routes.go
+++ b/routers/routes/routes.go
@@ -442,11 +442,13 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Post("/gitea/new", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksNewPost)
m.Post("/gogs/new", bindIgnErr(auth.NewGogshookForm{}), repo.GogsHooksNewPost)
m.Post("/slack/new", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksNewPost)
+ m.Post("/discord/new", bindIgnErr(auth.NewDiscordHookForm{}), repo.DiscordHooksNewPost)
m.Get("/:id", repo.WebHooksEdit)
m.Post("/:id/test", repo.TestWebhook)
m.Post("/gitea/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost)
m.Post("/gogs/:id", bindIgnErr(auth.NewGogshookForm{}), repo.GogsHooksNewPost)
m.Post("/slack/:id", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksEditPost)
+ m.Post("/discord/:id", bindIgnErr(auth.NewDiscordHookForm{}), repo.DiscordHooksEditPost)
m.Group("/git", func() {
m.Get("", repo.GitHooks)