summaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
authorRussell Aunger <rba@live.com>2019-03-18 22:33:20 -0400
committertechknowlogick <matti@mdranta.net>2019-03-18 22:33:20 -0400
commitb34996a62937b23121d19912b37ed2b1023f1479 (patch)
tree9a080ef145d738c71956279980bd7f56b70b6825 /routers
parentcac9e6e7605184f5679b1ebfbe5b5805191d9a53 (diff)
downloadgitea-b34996a62937b23121d19912b37ed2b1023f1479.tar.gz
gitea-b34996a62937b23121d19912b37ed2b1023f1479.zip
Implement Default Webhooks (#4299)
Partially implement #770. Add "Default Webhooks" page in site admin UI. Persist to the existing webhooks table, but store with RepoID=0 and OrgID=0. Upon repo creation, copy the set of default webhooks into the new repo.
Diffstat (limited to 'routers')
-rw-r--r--routers/admin/hooks.go47
-rw-r--r--routers/org/setting.go2
-rw-r--r--routers/repo/webhook.go59
-rw-r--r--routers/routes/routes.go17
4 files changed, 104 insertions, 21 deletions
diff --git a/routers/admin/hooks.go b/routers/admin/hooks.go
new file mode 100644
index 0000000000..b80ed3cc3c
--- /dev/null
+++ b/routers/admin/hooks.go
@@ -0,0 +1,47 @@
+// Copyright 2018 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 admin
+
+import (
+ "code.gitea.io/gitea/models"
+ "code.gitea.io/gitea/modules/base"
+ "code.gitea.io/gitea/modules/context"
+ "code.gitea.io/gitea/modules/setting"
+)
+
+const (
+ // tplAdminHooks template path for render hook settings
+ tplAdminHooks base.TplName = "admin/hooks"
+)
+
+// DefaultWebhooks render admin-default webhook list page
+func DefaultWebhooks(ctx *context.Context) {
+ ctx.Data["Title"] = ctx.Tr("admin.hooks")
+ ctx.Data["PageIsAdminHooks"] = true
+ ctx.Data["BaseLink"] = setting.AppSubURL + "/admin/hooks"
+ ctx.Data["Description"] = ctx.Tr("admin.hooks.desc")
+
+ ws, err := models.GetDefaultWebhooks()
+ if err != nil {
+ ctx.ServerError("GetWebhooksDefaults", err)
+ return
+ }
+
+ ctx.Data["Webhooks"] = ws
+ ctx.HTML(200, tplAdminHooks)
+}
+
+// DeleteDefaultWebhook response for delete admin-default webhook
+func DeleteDefaultWebhook(ctx *context.Context) {
+ if err := models.DeleteDefaultWebhook(ctx.QueryInt64("id")); err != nil {
+ ctx.Flash.Error("DeleteDefaultWebhook: " + err.Error())
+ } else {
+ ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
+ }
+
+ ctx.JSON(200, map[string]interface{}{
+ "redirect": setting.AppSubURL + "/admin/hooks",
+ })
+}
diff --git a/routers/org/setting.go b/routers/org/setting.go
index 1c4e75288a..0526a96e6f 100644
--- a/routers/org/setting.go
+++ b/routers/org/setting.go
@@ -150,7 +150,7 @@ func SettingsDelete(ctx *context.Context) {
func Webhooks(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("org.settings")
ctx.Data["PageIsSettingsHooks"] = true
- ctx.Data["BaseLink"] = ctx.Org.OrgLink
+ ctx.Data["BaseLink"] = ctx.Org.OrgLink + "/settings/hooks"
ctx.Data["Description"] = ctx.Tr("org.settings.hooks_desc")
ws, err := models.GetWebhooksByOrgID(ctx.Org.Organization.ID)
diff --git a/routers/repo/webhook.go b/routers/repo/webhook.go
index 6c69354c70..cea5145353 100644
--- a/routers/repo/webhook.go
+++ b/routers/repo/webhook.go
@@ -9,6 +9,7 @@ import (
"encoding/json"
"errors"
"fmt"
+ "path"
"strings"
"code.gitea.io/git"
@@ -23,16 +24,17 @@ import (
)
const (
- tplHooks base.TplName = "repo/settings/webhook/base"
- tplHookNew base.TplName = "repo/settings/webhook/new"
- tplOrgHookNew base.TplName = "org/settings/hook_new"
+ tplHooks base.TplName = "repo/settings/webhook/base"
+ tplHookNew base.TplName = "repo/settings/webhook/new"
+ tplOrgHookNew base.TplName = "org/settings/hook_new"
+ tplAdminHookNew base.TplName = "admin/hook_new"
)
// Webhooks render web hooks list page
func Webhooks(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.settings.hooks")
ctx.Data["PageIsSettingsHooks"] = true
- ctx.Data["BaseLink"] = ctx.Repo.RepoLink
+ ctx.Data["BaseLink"] = ctx.Repo.RepoLink + "/settings/hooks"
ctx.Data["Description"] = ctx.Tr("repo.settings.hooks_desc", "https://docs.gitea.io/en-us/webhooks/")
ws, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID)
@@ -48,16 +50,17 @@ func Webhooks(ctx *context.Context) {
type orgRepoCtx struct {
OrgID int64
RepoID int64
+ IsAdmin bool
Link string
NewTemplate base.TplName
}
-// getOrgRepoCtx determines whether this is a repo context or organization context.
+// getOrgRepoCtx determines whether this is a repo, organization, or admin context.
func getOrgRepoCtx(ctx *context.Context) (*orgRepoCtx, error) {
if len(ctx.Repo.RepoLink) > 0 {
return &orgRepoCtx{
RepoID: ctx.Repo.Repository.ID,
- Link: ctx.Repo.RepoLink,
+ Link: path.Join(ctx.Repo.RepoLink, "settings/hooks"),
NewTemplate: tplHookNew,
}, nil
}
@@ -65,11 +68,19 @@ func getOrgRepoCtx(ctx *context.Context) (*orgRepoCtx, error) {
if len(ctx.Org.OrgLink) > 0 {
return &orgRepoCtx{
OrgID: ctx.Org.Organization.ID,
- Link: ctx.Org.OrgLink,
+ Link: path.Join(ctx.Org.OrgLink, "settings/hooks"),
NewTemplate: tplOrgHookNew,
}, nil
}
+ if ctx.User.IsAdmin {
+ return &orgRepoCtx{
+ IsAdmin: true,
+ Link: path.Join(setting.AppSubURL, "/admin/hooks"),
+ NewTemplate: tplAdminHookNew,
+ }, nil
+ }
+
return nil, errors.New("Unable to set OrgRepo context")
}
@@ -85,8 +96,6 @@ func checkHookType(ctx *context.Context) string {
// WebhooksNew render creating webhook page
func WebhooksNew(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
- ctx.Data["PageIsSettingsHooks"] = true
- ctx.Data["PageIsSettingsHooksNew"] = true
ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
orCtx, err := getOrgRepoCtx(ctx)
@@ -95,6 +104,14 @@ func WebhooksNew(ctx *context.Context) {
return
}
+ if orCtx.IsAdmin {
+ ctx.Data["PageIsAdminHooks"] = true
+ ctx.Data["PageIsAdminHooksNew"] = true
+ } else {
+ ctx.Data["PageIsSettingsHooks"] = true
+ ctx.Data["PageIsSettingsHooksNew"] = true
+ }
+
hookType := checkHookType(ctx)
ctx.Data["HookType"] = hookType
if ctx.Written() {
@@ -175,7 +192,7 @@ func WebHooksNewPost(ctx *context.Context, form auth.NewWebhookForm) {
}
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
- ctx.Redirect(orCtx.Link + "/settings/hooks")
+ ctx.Redirect(orCtx.Link)
}
// GogsHooksNewPost response for creating webhook
@@ -222,7 +239,7 @@ func GogsHooksNewPost(ctx *context.Context, form auth.NewGogshookForm) {
}
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
- ctx.Redirect(orCtx.Link + "/settings/hooks")
+ ctx.Redirect(orCtx.Link)
}
// DiscordHooksNewPost response for creating discord hook
@@ -271,7 +288,7 @@ func DiscordHooksNewPost(ctx *context.Context, form auth.NewDiscordHookForm) {
}
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
- ctx.Redirect(orCtx.Link + "/settings/hooks")
+ ctx.Redirect(orCtx.Link)
}
// DingtalkHooksNewPost response for creating dingtalk hook
@@ -311,7 +328,7 @@ func DingtalkHooksNewPost(ctx *context.Context, form auth.NewDingtalkHookForm) {
}
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
- ctx.Redirect(orCtx.Link + "/settings/hooks")
+ ctx.Redirect(orCtx.Link)
}
// SlackHooksNewPost response for creating slack hook
@@ -368,7 +385,7 @@ func SlackHooksNewPost(ctx *context.Context, form auth.NewSlackHookForm) {
}
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
- ctx.Redirect(orCtx.Link + "/settings/hooks")
+ ctx.Redirect(orCtx.Link)
}
func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) {
@@ -384,8 +401,10 @@ func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) {
var w *models.Webhook
if orCtx.RepoID > 0 {
w, err = models.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
- } else {
+ } else if orCtx.OrgID > 0 {
w, err = models.GetWebhookByOrgID(ctx.Org.Organization.ID, ctx.ParamsInt64(":id"))
+ } else {
+ w, err = models.GetDefaultWebhook(ctx.ParamsInt64(":id"))
}
if err != nil {
if models.IsErrWebhookNotExist(err) {
@@ -462,7 +481,7 @@ func WebHooksEditPost(ctx *context.Context, form auth.NewWebhookForm) {
}
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
- ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
+ ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
}
// GogsHooksEditPost response for editing gogs hook
@@ -501,7 +520,7 @@ func GogsHooksEditPost(ctx *context.Context, form auth.NewGogshookForm) {
}
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
- ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
+ ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
}
// SlackHooksEditPost response for editing slack hook
@@ -551,7 +570,7 @@ func SlackHooksEditPost(ctx *context.Context, form auth.NewSlackHookForm) {
}
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
- ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
+ ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
}
// DiscordHooksEditPost response for editing discord hook
@@ -593,7 +612,7 @@ func DiscordHooksEditPost(ctx *context.Context, form auth.NewDiscordHookForm) {
}
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
- ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
+ ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
}
// DingtalkHooksEditPost response for editing discord hook
@@ -625,7 +644,7 @@ func DingtalkHooksEditPost(ctx *context.Context, form auth.NewDingtalkHookForm)
}
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
- ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID))
+ ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
}
// TestWebhook test if web hook is work fine
diff --git a/routers/routes/routes.go b/routers/routes/routes.go
index 5f55d6cf9d..fafef0830e 100644
--- a/routers/routes/routes.go
+++ b/routers/routes/routes.go
@@ -373,6 +373,23 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Post("/delete", admin.DeleteRepo)
})
+ m.Group("/hooks", func() {
+ m.Get("", admin.DefaultWebhooks)
+ m.Post("/delete", admin.DeleteDefaultWebhook)
+ m.Get("/:type/new", repo.WebhooksNew)
+ 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.Post("/dingtalk/new", bindIgnErr(auth.NewDingtalkHookForm{}), repo.DingtalkHooksNewPost)
+ 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.Group("/auths", func() {
m.Get("", admin.Authentications)
m.Combo("/new").Get(admin.NewAuthSource).Post(bindIgnErr(auth.AuthenticationForm{}), admin.NewAuthSourcePost)