From aa583d327c5991893048e30a6bf0062bc041f79a Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Mon, 4 Jan 2021 15:23:28 +0200 Subject: [PATCH 1/3] Merge default and system webhooks under one menu --- models/webhook.go | 20 +----- options/locale/locale_en-US.ini | 11 ++-- routers/admin/hooks.go | 54 ++++++++-------- routers/org/setting.go | 1 + routers/repo/webhook.go | 32 +++++----- routers/routes/macaron.go | 25 ++++---- templates/admin/hook_new.tmpl | 10 ++- templates/admin/hooks.tmpl | 7 ++- templates/admin/navbar.tmpl | 5 +- .../repo/settings/webhook/base_list.tmpl | 60 ++++++++++++++++++ templates/repo/settings/webhook/list.tmpl | 62 +------------------ templates/repo/settings/webhook/settings.tmpl | 2 +- 12 files changed, 148 insertions(+), 141 deletions(-) create mode 100644 templates/repo/settings/webhook/base_list.tmpl diff --git a/models/webhook.go b/models/webhook.go index 9f1b6131afc54..14e8f28f7c001 100644 --- a/models/webhook.go +++ b/models/webhook.go @@ -400,20 +400,6 @@ func GetWebhooksByOrgID(orgID int64, listOptions ListOptions) ([]*Webhook, error return ws, sess.Find(&ws, &Webhook{OrgID: orgID}) } -// GetDefaultWebhook returns admin-default webhook by given ID. -func GetDefaultWebhook(id int64) (*Webhook, error) { - webhook := &Webhook{ID: id} - has, err := x. - Where("repo_id=? AND org_id=? AND is_system_webhook=?", 0, 0, false). - Get(webhook) - if err != nil { - return nil, err - } else if !has { - return nil, ErrWebhookNotExist{id} - } - return webhook, nil -} - // GetDefaultWebhooks returns all admin-default webhooks. func GetDefaultWebhooks() ([]*Webhook, error) { return getDefaultWebhooks(x) @@ -426,11 +412,11 @@ func getDefaultWebhooks(e Engine) ([]*Webhook, error) { Find(&webhooks) } -// GetSystemWebhook returns admin system webhook by given ID. -func GetSystemWebhook(id int64) (*Webhook, error) { +// GetSystemOrDefaultWebhook returns admin system or default webhook by given ID. +func GetSystemOrDefaultWebhook(id int64) (*Webhook, error) { webhook := &Webhook{ID: id} has, err := x. - Where("repo_id=? AND org_id=? AND is_system_webhook=?", 0, 0, true). + Where("repo_id=? AND org_id=?", 0, 0). Get(webhook) if err != nil { return nil, err diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index ae72dff3ad210..338ad89573d0a 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1998,8 +1998,7 @@ dashboard = Dashboard users = User Accounts organizations = Organizations repositories = Repositories -hooks = Default Webhooks -systemhooks = System Webhooks +hooks = Webhooks authentication = Authentication Sources emails = User Emails config = Configuration @@ -2148,10 +2147,12 @@ repos.forks = Forks repos.issues = Issues repos.size = Size -hooks.desc = Webhooks automatically make HTTP POST requests to a server when certain Gitea events trigger. Webhooks defined here are defaults and will be copied into all new repositories. Read more in the webhooks guide. -hooks.add_webhook = Add Default Webhook -hooks.update_webhook = Update Default Webhook +defaulthooks = Default Webhooks +defaulthooks.desc = Webhooks automatically make HTTP POST requests to a server when certain Gitea events trigger. Webhooks defined here are defaults and will be copied into all new repositories. Read more in the webhooks guide. +defaulthooks.add_webhook = Add Default Webhook +defaulthooks.update_webhook = Update Default Webhook +systemhooks = System Webhooks systemhooks.desc = Webhooks automatically make HTTP POST requests to a server when certain Gitea events trigger. Webhooks defined will act on all repositories on the system, so please consider any performance implications this may have. Read more in the webhooks guide. systemhooks.add_webhook = Add System Webhook systemhooks.update_webhook = Update System Webhook diff --git a/routers/admin/hooks.go b/routers/admin/hooks.go index 4697c4d933f5a..e233e8ac009fc 100644 --- a/routers/admin/hooks.go +++ b/routers/admin/hooks.go @@ -18,30 +18,41 @@ const ( // DefaultOrSystemWebhooks renders both admin default and system webhook list pages func DefaultOrSystemWebhooks(ctx *context.Context) { - var ws []*models.Webhook var err error - // Are we looking at default webhooks? - if ctx.Params(":configType") == "hooks" { - ctx.Data["Title"] = ctx.Tr("admin.hooks") - ctx.Data["Description"] = ctx.Tr("admin.hooks.desc") - ctx.Data["PageIsAdminHooks"] = true - ctx.Data["BaseLink"] = setting.AppSubURL + "/admin/hooks" - ws, err = models.GetDefaultWebhooks() - } else { - ctx.Data["Title"] = ctx.Tr("admin.systemhooks") - ctx.Data["Description"] = ctx.Tr("admin.systemhooks.desc") - ctx.Data["PageIsAdminSystemHooks"] = true - ctx.Data["BaseLink"] = setting.AppSubURL + "/admin/system-hooks" - ws, err = models.GetSystemWebhooks() + ctx.Data["PageIsAdminSystemHooks"] = true + ctx.Data["PageIsAdminDefaultHooks"] = true + + def := make(map[string]interface{}, len(ctx.Data)) + sys := make(map[string]interface{}, len(ctx.Data)) + for k, v := range ctx.Data { + def[k] = v + sys[k] = v + } + + sys["Title"] = ctx.Tr("admin.systemhooks") + sys["Description"] = ctx.Tr("admin.systemhooks.desc") + sys["Webhooks"], err = models.GetSystemWebhooks() + sys["BaseLink"] = setting.AppSubURL + "/admin/hooks" + sys["BaseLinkNew"] = setting.AppSubURL + "/admin/system-hooks" + if err != nil { + ctx.ServerError("GetWebhooksAdmin", err) + return } + def["Title"] = ctx.Tr("admin.defaulthooks") + def["Description"] = ctx.Tr("admin.defaulthooks.desc") + def["Webhooks"], err = models.GetDefaultWebhooks() + def["BaseLink"] = setting.AppSubURL + "/admin/hooks" + def["BaseLinkNew"] = setting.AppSubURL + "/admin/default-hooks" if err != nil { ctx.ServerError("GetWebhooksAdmin", err) return } - ctx.Data["Webhooks"] = ws + ctx.Data["DefaultWebhooks"] = def + ctx.Data["SystemWebhooks"] = sys + ctx.HTML(200, tplAdminHooks) } @@ -53,14 +64,7 @@ func DeleteDefaultOrSystemWebhook(ctx *context.Context) { ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success")) } - // Are we looking at default webhooks? - if ctx.Params(":configType") == "hooks" { - ctx.JSON(200, map[string]interface{}{ - "redirect": setting.AppSubURL + "/admin/hooks", - }) - } else { - ctx.JSON(200, map[string]interface{}{ - "redirect": setting.AppSubURL + "/admin/system-hooks", - }) - } + ctx.JSON(200, map[string]interface{}{ + "redirect": setting.AppSubURL + "/admin/hooks", + }) } diff --git a/routers/org/setting.go b/routers/org/setting.go index 454714c7eb8eb..05075ca8202cc 100644 --- a/routers/org/setting.go +++ b/routers/org/setting.go @@ -173,6 +173,7 @@ func Webhooks(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("org.settings") ctx.Data["PageIsSettingsHooks"] = true ctx.Data["BaseLink"] = ctx.Org.OrgLink + "/settings/hooks" + ctx.Data["BaseLinkNew"] = ctx.Org.OrgLink + "/settings/hooks" ctx.Data["Description"] = ctx.Tr("org.settings.hooks_desc") ws, err := models.GetWebhooksByOrgID(ctx.Org.Organization.ID, models.ListOptions{}) diff --git a/routers/repo/webhook.go b/routers/repo/webhook.go index 0c3fd1267d684..5ce1289432d63 100644 --- a/routers/repo/webhook.go +++ b/routers/repo/webhook.go @@ -36,6 +36,7 @@ func Webhooks(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("repo.settings.hooks") ctx.Data["PageIsSettingsHooks"] = true ctx.Data["BaseLink"] = ctx.Repo.RepoLink + "/settings/hooks" + ctx.Data["BaseLinkNew"] = 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, models.ListOptions{}) @@ -54,6 +55,7 @@ type orgRepoCtx struct { IsAdmin bool IsSystemWebhook bool Link string + LinkNew string NewTemplate base.TplName } @@ -63,6 +65,7 @@ func getOrgRepoCtx(ctx *context.Context) (*orgRepoCtx, error) { return &orgRepoCtx{ RepoID: ctx.Repo.Repository.ID, Link: path.Join(ctx.Repo.RepoLink, "settings/hooks"), + LinkNew: path.Join(ctx.Repo.RepoLink, "settings/hooks"), NewTemplate: tplHookNew, }, nil } @@ -71,16 +74,18 @@ func getOrgRepoCtx(ctx *context.Context) (*orgRepoCtx, error) { return &orgRepoCtx{ OrgID: ctx.Org.Organization.ID, Link: path.Join(ctx.Org.OrgLink, "settings/hooks"), + LinkNew: path.Join(ctx.Org.OrgLink, "settings/hooks"), NewTemplate: tplOrgHookNew, }, nil } if ctx.User.IsAdmin { // Are we looking at default webhooks? - if ctx.Params(":configType") == "hooks" { + if ctx.Params(":configType") == "default-hooks" { return &orgRepoCtx{ IsAdmin: true, Link: path.Join(setting.AppSubURL, "/admin/hooks"), + LinkNew: path.Join(setting.AppSubURL, "/admin/default-hooks"), NewTemplate: tplAdminHookNew, }, nil } @@ -89,7 +94,8 @@ func getOrgRepoCtx(ctx *context.Context) (*orgRepoCtx, error) { return &orgRepoCtx{ IsAdmin: true, IsSystemWebhook: true, - Link: path.Join(setting.AppSubURL, "/admin/system-hooks"), + Link: path.Join(setting.AppSubURL, "/admin/hooks"), + LinkNew: path.Join(setting.AppSubURL, "/admin/system-hooks"), NewTemplate: tplAdminHookNew, }, nil } @@ -121,8 +127,8 @@ func WebhooksNew(ctx *context.Context) { ctx.Data["PageIsAdminSystemHooks"] = true ctx.Data["PageIsAdminSystemHooksNew"] = true } else if orCtx.IsAdmin { - ctx.Data["PageIsAdminHooks"] = true - ctx.Data["PageIsAdminHooksNew"] = true + ctx.Data["PageIsAdminDefaultHooks"] = true + ctx.Data["PageIsAdminDefaultHooksNew"] = true } else { ctx.Data["PageIsSettingsHooks"] = true ctx.Data["PageIsSettingsHooksNew"] = true @@ -139,7 +145,7 @@ func WebhooksNew(ctx *context.Context) { "IconURL": setting.AppURL + "img/favicon.png", } } - ctx.Data["BaseLink"] = orCtx.Link + ctx.Data["BaseLink"] = orCtx.LinkNew ctx.HTML(200, orCtx.NewTemplate) } @@ -187,7 +193,7 @@ func GiteaHooksNewPost(ctx *context.Context, form auth.NewWebhookForm) { ctx.ServerError("getOrgRepoCtx", err) return } - ctx.Data["BaseLink"] = orCtx.Link + ctx.Data["BaseLink"] = orCtx.LinkNew if ctx.HasError() { ctx.HTML(200, orCtx.NewTemplate) @@ -241,7 +247,7 @@ func newGogsWebhookPost(ctx *context.Context, form auth.NewGogshookForm, kind mo ctx.ServerError("getOrgRepoCtx", err) return } - ctx.Data["BaseLink"] = orCtx.Link + ctx.Data["BaseLink"] = orCtx.LinkNew if ctx.HasError() { ctx.HTML(200, orCtx.NewTemplate) @@ -537,7 +543,7 @@ func SlackHooksNewPost(ctx *context.Context, form auth.NewSlackHookForm) { if form.HasInvalidChannel() { ctx.Flash.Error(ctx.Tr("repo.settings.add_webhook.invalid_channel_name")) - ctx.Redirect(orCtx.Link + "/slack/new") + ctx.Redirect(orCtx.LinkNew + "/slack/new") return } @@ -632,12 +638,10 @@ func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) { w, err = models.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) } else if orCtx.OrgID > 0 { w, err = models.GetWebhookByOrgID(ctx.Org.Organization.ID, ctx.ParamsInt64(":id")) - } else if orCtx.IsSystemWebhook { - w, err = models.GetSystemWebhook(ctx.ParamsInt64(":id")) - } else { - w, err = models.GetDefaultWebhook(ctx.ParamsInt64(":id")) + } else if orCtx.IsAdmin { + w, err = models.GetSystemOrDefaultWebhook(ctx.ParamsInt64(":id")) } - if err != nil { + if err != nil || w == nil { if models.IsErrWebhookNotExist(err) { ctx.NotFound("GetWebhookByID", nil) } else { @@ -646,7 +650,7 @@ func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) { return nil, nil } - ctx.Data["HookType"] = w.Type + ctx.Data["HookType"] = strings.TrimSpace(w.Type) switch w.Type { case models.SLACK: ctx.Data["SlackHook"] = webhook.GetSlackHook(w) diff --git a/routers/routes/macaron.go b/routers/routes/macaron.go index 019b476e71769..37fe01e138e52 100644 --- a/routers/routes/macaron.go +++ b/routers/routes/macaron.go @@ -367,19 +367,9 @@ func RegisterMacaronRoutes(m *macaron.Macaron) { m.Post("/delete", admin.DeleteRepo) }) - m.Group("/^:configType(hooks|system-hooks)$", func() { + m.Group("/hooks", func() { m.Get("", admin.DefaultOrSystemWebhooks) m.Post("/delete", admin.DeleteDefaultOrSystemWebhook) - m.Get("/:type/new", repo.WebhooksNew) - m.Post("/gitea/new", bindIgnErr(auth.NewWebhookForm{}), repo.GiteaHooksNewPost) - 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.Post("/telegram/new", bindIgnErr(auth.NewTelegramHookForm{}), repo.TelegramHooksNewPost) - m.Post("/matrix/new", bindIgnErr(auth.NewMatrixHookForm{}), repo.MatrixHooksNewPost) - m.Post("/msteams/new", bindIgnErr(auth.NewMSTeamsHookForm{}), repo.MSTeamsHooksNewPost) - m.Post("/feishu/new", bindIgnErr(auth.NewFeishuHookForm{}), repo.FeishuHooksNewPost) m.Get("/:id", repo.WebHooksEdit) m.Post("/gitea/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost) m.Post("/gogs/:id", bindIgnErr(auth.NewGogshookForm{}), repo.GogsHooksEditPost) @@ -392,6 +382,19 @@ func RegisterMacaronRoutes(m *macaron.Macaron) { m.Post("/feishu/:id", bindIgnErr(auth.NewFeishuHookForm{}), repo.FeishuHooksEditPost) }) + m.Group("/^:configType(default-hooks|system-hooks)$", func() { + m.Get("/:type/new", repo.WebhooksNew) + m.Post("/gitea/new", bindIgnErr(auth.NewWebhookForm{}), repo.GiteaHooksNewPost) + 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.Post("/telegram/new", bindIgnErr(auth.NewTelegramHookForm{}), repo.TelegramHooksNewPost) + m.Post("/matrix/new", bindIgnErr(auth.NewMatrixHookForm{}), repo.MatrixHooksNewPost) + m.Post("/msteams/new", bindIgnErr(auth.NewMSTeamsHookForm{}), repo.MSTeamsHooksNewPost) + m.Post("/feishu/new", bindIgnErr(auth.NewFeishuHookForm{}), repo.FeishuHooksNewPost) + }) + m.Group("/auths", func() { m.Get("", admin.Authentications) m.Combo("/new").Get(admin.NewAuthSource).Post(bindIgnErr(auth.AuthenticationForm{}), admin.NewAuthSourcePost) diff --git a/templates/admin/hook_new.tmpl b/templates/admin/hook_new.tmpl index c6f02ee20eb2b..9b251ec4cbc1c 100644 --- a/templates/admin/hook_new.tmpl +++ b/templates/admin/hook_new.tmpl @@ -4,10 +4,14 @@
{{template "base/alert" .}}

- {{if .PageIsAdminHooksNew}} - {{.i18n.Tr "admin.hooks.add_webhook"}} + {{if .PageIsAdminDefaultHooksNew}} + {{.i18n.Tr "admin.defaulthooks.add_webhook"}} + {{else if .PageIsAdminSystemHooksNew}} + {{.i18n.Tr "admin.systemhooks.add_webhook"}} + {{else if .Webhook.IsSystemWebhook}} + {{.i18n.Tr "admin.systemhooks.update_webhook"}} {{else}} - {{.i18n.Tr "admin.hooks.update_webhook"}} + {{.i18n.Tr "admin.defaulthooks.update_webhook"}} {{end}}
{{if eq .HookType "gitea"}} diff --git a/templates/admin/hooks.tmpl b/templates/admin/hooks.tmpl index c09c6fcafe830..a23cff4342697 100644 --- a/templates/admin/hooks.tmpl +++ b/templates/admin/hooks.tmpl @@ -2,7 +2,12 @@
{{template "admin/navbar" .}}
- {{template "repo/settings/webhook/list" .}} + {{template "base/alert" .}} + + {{template "repo/settings/webhook/base_list" .SystemWebhooks}} + {{template "repo/settings/webhook/base_list" .DefaultWebhooks}} + + {{template "repo/settings/webhook/delete_modal" .}}
{{template "base/footer" .}} diff --git a/templates/admin/navbar.tmpl b/templates/admin/navbar.tmpl index 8c895b8c2735a..953076d808d8e 100644 --- a/templates/admin/navbar.tmpl +++ b/templates/admin/navbar.tmpl @@ -12,12 +12,9 @@ {{.i18n.Tr "admin.repositories"}} - + {{.i18n.Tr "admin.hooks"}} - - {{.i18n.Tr "admin.systemhooks"}} - {{.i18n.Tr "admin.authentication"}} diff --git a/templates/repo/settings/webhook/base_list.tmpl b/templates/repo/settings/webhook/base_list.tmpl new file mode 100644 index 0000000000000..b978a6a19ce13 --- /dev/null +++ b/templates/repo/settings/webhook/base_list.tmpl @@ -0,0 +1,60 @@ +

+ {{.Title}} + +

+
+
+
+ {{.Description | Str2html}} +
+ {{range .Webhooks}} +
+ {{if eq .LastStatus 1}} + {{svg "octicon-check"}} + {{else if eq .LastStatus 2}} + {{svg "octicon-alert"}} + {{else}} + {{svg "octicon-dot-fill"}} + {{end}} + {{.URL}} + +
+ {{end}} +
+
diff --git a/templates/repo/settings/webhook/list.tmpl b/templates/repo/settings/webhook/list.tmpl index 5efd6d4a3c2ff..507f5e1f675b7 100644 --- a/templates/repo/settings/webhook/list.tmpl +++ b/templates/repo/settings/webhook/list.tmpl @@ -1,63 +1,5 @@ {{template "base/alert" .}} -

- {{.i18n.Tr "repo.settings.hooks"}} - -

-
-
-
- {{.Description | Str2html}} -
- {{range .Webhooks}} -
- {{if eq .LastStatus 1}} - {{svg "octicon-check"}} - {{else if eq .LastStatus 2}} - {{svg "octicon-alert"}} - {{else}} - {{svg "octicon-dot-fill"}} - {{end}} - {{.URL}} - -
- {{end}} -
-
+ +{{template "repo/settings/webhook/base_list" .}} {{template "repo/settings/webhook/delete_modal" .}} diff --git a/templates/repo/settings/webhook/settings.tmpl b/templates/repo/settings/webhook/settings.tmpl index de74dab05d482..934794b539b07 100644 --- a/templates/repo/settings/webhook/settings.tmpl +++ b/templates/repo/settings/webhook/settings.tmpl @@ -1,4 +1,4 @@ -{{$isNew:=or .PageIsSettingsHooksNew .PageIsAdminHooksNew}} +{{$isNew:=or .PageIsSettingsHooksNew .PageIsAdminDefaultHooksNew .PageIsAdminSystemHooksNew}}

{{.i18n.Tr "repo.settings.event_desc"}}

From 3acc3061cc6de711ee98a071f2cc6ff0587fee9f Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Mon, 4 Jan 2021 16:50:55 +0200 Subject: [PATCH 2/3] Revert hook type spacing fix --- routers/repo/webhook.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/repo/webhook.go b/routers/repo/webhook.go index 5ce1289432d63..5d7074b339f38 100644 --- a/routers/repo/webhook.go +++ b/routers/repo/webhook.go @@ -650,7 +650,7 @@ func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) { return nil, nil } - ctx.Data["HookType"] = strings.TrimSpace(w.Type) + ctx.Data["HookType"] = w.Type switch w.Type { case models.SLACK: ctx.Data["SlackHook"] = webhook.GetSlackHook(w) From fbc8922dfc8cbe13f367c5552192f34878a27e42 Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Wed, 6 Jan 2021 09:40:39 +0200 Subject: [PATCH 3/3] Fix description text --- options/locale/locale_en-US.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 338ad89573d0a..6eeb4ec1835d6 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -2153,7 +2153,7 @@ defaulthooks.add_webhook = Add Default Webhook defaulthooks.update_webhook = Update Default Webhook systemhooks = System Webhooks -systemhooks.desc = Webhooks automatically make HTTP POST requests to a server when certain Gitea events trigger. Webhooks defined will act on all repositories on the system, so please consider any performance implications this may have. Read more in the webhooks guide. +systemhooks.desc = Webhooks automatically make HTTP POST requests to a server when certain Gitea events trigger. Webhooks defined here will act on all repositories on the system, so please consider any performance implications this may have. Read more in the webhooks guide. systemhooks.add_webhook = Add System Webhook systemhooks.update_webhook = Update System Webhook