Skip to content

Commit 6eee9f0

Browse files
authored
Merge default and system webhooks under one menu (#14244)
1 parent 84b147c commit 6eee9f0

File tree

12 files changed

+148
-141
lines changed

12 files changed

+148
-141
lines changed

models/webhook.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -400,20 +400,6 @@ func GetWebhooksByOrgID(orgID int64, listOptions ListOptions) ([]*Webhook, error
400400
return ws, sess.Find(&ws, &Webhook{OrgID: orgID})
401401
}
402402

403-
// GetDefaultWebhook returns admin-default webhook by given ID.
404-
func GetDefaultWebhook(id int64) (*Webhook, error) {
405-
webhook := &Webhook{ID: id}
406-
has, err := x.
407-
Where("repo_id=? AND org_id=? AND is_system_webhook=?", 0, 0, false).
408-
Get(webhook)
409-
if err != nil {
410-
return nil, err
411-
} else if !has {
412-
return nil, ErrWebhookNotExist{id}
413-
}
414-
return webhook, nil
415-
}
416-
417403
// GetDefaultWebhooks returns all admin-default webhooks.
418404
func GetDefaultWebhooks() ([]*Webhook, error) {
419405
return getDefaultWebhooks(x)
@@ -426,11 +412,11 @@ func getDefaultWebhooks(e Engine) ([]*Webhook, error) {
426412
Find(&webhooks)
427413
}
428414

429-
// GetSystemWebhook returns admin system webhook by given ID.
430-
func GetSystemWebhook(id int64) (*Webhook, error) {
415+
// GetSystemOrDefaultWebhook returns admin system or default webhook by given ID.
416+
func GetSystemOrDefaultWebhook(id int64) (*Webhook, error) {
431417
webhook := &Webhook{ID: id}
432418
has, err := x.
433-
Where("repo_id=? AND org_id=? AND is_system_webhook=?", 0, 0, true).
419+
Where("repo_id=? AND org_id=?", 0, 0).
434420
Get(webhook)
435421
if err != nil {
436422
return nil, err

options/locale/locale_en-US.ini

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,8 +2001,7 @@ dashboard = Dashboard
20012001
users = User Accounts
20022002
organizations = Organizations
20032003
repositories = Repositories
2004-
hooks = Default Webhooks
2005-
systemhooks = System Webhooks
2004+
hooks = Webhooks
20062005
authentication = Authentication Sources
20072006
emails = User Emails
20082007
config = Configuration
@@ -2152,11 +2151,13 @@ repos.forks = Forks
21522151
repos.issues = Issues
21532152
repos.size = Size
21542153

2155-
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 <a target="_blank" rel="noopener" href="https://docs.gitea.io/en-us/webhooks/">webhooks guide</a>.
2156-
hooks.add_webhook = Add Default Webhook
2157-
hooks.update_webhook = Update Default Webhook
2154+
defaulthooks = Default Webhooks
2155+
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 <a target="_blank" rel="noopener" href="https://docs.gitea.io/en-us/webhooks/">webhooks guide</a>.
2156+
defaulthooks.add_webhook = Add Default Webhook
2157+
defaulthooks.update_webhook = Update Default Webhook
21582158

2159-
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 <a target="_blank" rel="noopener" href="https://docs.gitea.io/en-us/webhooks/">webhooks guide</a>.
2159+
systemhooks = System Webhooks
2160+
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 <a target="_blank" rel="noopener" href="https://docs.gitea.io/en-us/webhooks/">webhooks guide</a>.
21602161
systemhooks.add_webhook = Add System Webhook
21612162
systemhooks.update_webhook = Update System Webhook
21622163

routers/admin/hooks.go

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,41 @@ const (
1818

1919
// DefaultOrSystemWebhooks renders both admin default and system webhook list pages
2020
func DefaultOrSystemWebhooks(ctx *context.Context) {
21-
var ws []*models.Webhook
2221
var err error
2322

24-
// Are we looking at default webhooks?
25-
if ctx.Params(":configType") == "hooks" {
26-
ctx.Data["Title"] = ctx.Tr("admin.hooks")
27-
ctx.Data["Description"] = ctx.Tr("admin.hooks.desc")
28-
ctx.Data["PageIsAdminHooks"] = true
29-
ctx.Data["BaseLink"] = setting.AppSubURL + "/admin/hooks"
30-
ws, err = models.GetDefaultWebhooks()
31-
} else {
32-
ctx.Data["Title"] = ctx.Tr("admin.systemhooks")
33-
ctx.Data["Description"] = ctx.Tr("admin.systemhooks.desc")
34-
ctx.Data["PageIsAdminSystemHooks"] = true
35-
ctx.Data["BaseLink"] = setting.AppSubURL + "/admin/system-hooks"
36-
ws, err = models.GetSystemWebhooks()
23+
ctx.Data["PageIsAdminSystemHooks"] = true
24+
ctx.Data["PageIsAdminDefaultHooks"] = true
25+
26+
def := make(map[string]interface{}, len(ctx.Data))
27+
sys := make(map[string]interface{}, len(ctx.Data))
28+
for k, v := range ctx.Data {
29+
def[k] = v
30+
sys[k] = v
31+
}
32+
33+
sys["Title"] = ctx.Tr("admin.systemhooks")
34+
sys["Description"] = ctx.Tr("admin.systemhooks.desc")
35+
sys["Webhooks"], err = models.GetSystemWebhooks()
36+
sys["BaseLink"] = setting.AppSubURL + "/admin/hooks"
37+
sys["BaseLinkNew"] = setting.AppSubURL + "/admin/system-hooks"
38+
if err != nil {
39+
ctx.ServerError("GetWebhooksAdmin", err)
40+
return
3741
}
3842

43+
def["Title"] = ctx.Tr("admin.defaulthooks")
44+
def["Description"] = ctx.Tr("admin.defaulthooks.desc")
45+
def["Webhooks"], err = models.GetDefaultWebhooks()
46+
def["BaseLink"] = setting.AppSubURL + "/admin/hooks"
47+
def["BaseLinkNew"] = setting.AppSubURL + "/admin/default-hooks"
3948
if err != nil {
4049
ctx.ServerError("GetWebhooksAdmin", err)
4150
return
4251
}
4352

44-
ctx.Data["Webhooks"] = ws
53+
ctx.Data["DefaultWebhooks"] = def
54+
ctx.Data["SystemWebhooks"] = sys
55+
4556
ctx.HTML(200, tplAdminHooks)
4657
}
4758

@@ -53,14 +64,7 @@ func DeleteDefaultOrSystemWebhook(ctx *context.Context) {
5364
ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
5465
}
5566

56-
// Are we looking at default webhooks?
57-
if ctx.Params(":configType") == "hooks" {
58-
ctx.JSON(200, map[string]interface{}{
59-
"redirect": setting.AppSubURL + "/admin/hooks",
60-
})
61-
} else {
62-
ctx.JSON(200, map[string]interface{}{
63-
"redirect": setting.AppSubURL + "/admin/system-hooks",
64-
})
65-
}
67+
ctx.JSON(200, map[string]interface{}{
68+
"redirect": setting.AppSubURL + "/admin/hooks",
69+
})
6670
}

routers/org/setting.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ func Webhooks(ctx *context.Context) {
173173
ctx.Data["Title"] = ctx.Tr("org.settings")
174174
ctx.Data["PageIsSettingsHooks"] = true
175175
ctx.Data["BaseLink"] = ctx.Org.OrgLink + "/settings/hooks"
176+
ctx.Data["BaseLinkNew"] = ctx.Org.OrgLink + "/settings/hooks"
176177
ctx.Data["Description"] = ctx.Tr("org.settings.hooks_desc")
177178

178179
ws, err := models.GetWebhooksByOrgID(ctx.Org.Organization.ID, models.ListOptions{})

routers/repo/webhook.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func Webhooks(ctx *context.Context) {
3636
ctx.Data["Title"] = ctx.Tr("repo.settings.hooks")
3737
ctx.Data["PageIsSettingsHooks"] = true
3838
ctx.Data["BaseLink"] = ctx.Repo.RepoLink + "/settings/hooks"
39+
ctx.Data["BaseLinkNew"] = ctx.Repo.RepoLink + "/settings/hooks"
3940
ctx.Data["Description"] = ctx.Tr("repo.settings.hooks_desc", "https://docs.gitea.io/en-us/webhooks/")
4041

4142
ws, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID, models.ListOptions{})
@@ -54,6 +55,7 @@ type orgRepoCtx struct {
5455
IsAdmin bool
5556
IsSystemWebhook bool
5657
Link string
58+
LinkNew string
5759
NewTemplate base.TplName
5860
}
5961

@@ -63,6 +65,7 @@ func getOrgRepoCtx(ctx *context.Context) (*orgRepoCtx, error) {
6365
return &orgRepoCtx{
6466
RepoID: ctx.Repo.Repository.ID,
6567
Link: path.Join(ctx.Repo.RepoLink, "settings/hooks"),
68+
LinkNew: path.Join(ctx.Repo.RepoLink, "settings/hooks"),
6669
NewTemplate: tplHookNew,
6770
}, nil
6871
}
@@ -71,16 +74,18 @@ func getOrgRepoCtx(ctx *context.Context) (*orgRepoCtx, error) {
7174
return &orgRepoCtx{
7275
OrgID: ctx.Org.Organization.ID,
7376
Link: path.Join(ctx.Org.OrgLink, "settings/hooks"),
77+
LinkNew: path.Join(ctx.Org.OrgLink, "settings/hooks"),
7478
NewTemplate: tplOrgHookNew,
7579
}, nil
7680
}
7781

7882
if ctx.User.IsAdmin {
7983
// Are we looking at default webhooks?
80-
if ctx.Params(":configType") == "hooks" {
84+
if ctx.Params(":configType") == "default-hooks" {
8185
return &orgRepoCtx{
8286
IsAdmin: true,
8387
Link: path.Join(setting.AppSubURL, "/admin/hooks"),
88+
LinkNew: path.Join(setting.AppSubURL, "/admin/default-hooks"),
8489
NewTemplate: tplAdminHookNew,
8590
}, nil
8691
}
@@ -89,7 +94,8 @@ func getOrgRepoCtx(ctx *context.Context) (*orgRepoCtx, error) {
8994
return &orgRepoCtx{
9095
IsAdmin: true,
9196
IsSystemWebhook: true,
92-
Link: path.Join(setting.AppSubURL, "/admin/system-hooks"),
97+
Link: path.Join(setting.AppSubURL, "/admin/hooks"),
98+
LinkNew: path.Join(setting.AppSubURL, "/admin/system-hooks"),
9399
NewTemplate: tplAdminHookNew,
94100
}, nil
95101
}
@@ -121,8 +127,8 @@ func WebhooksNew(ctx *context.Context) {
121127
ctx.Data["PageIsAdminSystemHooks"] = true
122128
ctx.Data["PageIsAdminSystemHooksNew"] = true
123129
} else if orCtx.IsAdmin {
124-
ctx.Data["PageIsAdminHooks"] = true
125-
ctx.Data["PageIsAdminHooksNew"] = true
130+
ctx.Data["PageIsAdminDefaultHooks"] = true
131+
ctx.Data["PageIsAdminDefaultHooksNew"] = true
126132
} else {
127133
ctx.Data["PageIsSettingsHooks"] = true
128134
ctx.Data["PageIsSettingsHooksNew"] = true
@@ -139,7 +145,7 @@ func WebhooksNew(ctx *context.Context) {
139145
"IconURL": setting.AppURL + "img/favicon.png",
140146
}
141147
}
142-
ctx.Data["BaseLink"] = orCtx.Link
148+
ctx.Data["BaseLink"] = orCtx.LinkNew
143149

144150
ctx.HTML(200, orCtx.NewTemplate)
145151
}
@@ -187,7 +193,7 @@ func GiteaHooksNewPost(ctx *context.Context, form auth.NewWebhookForm) {
187193
ctx.ServerError("getOrgRepoCtx", err)
188194
return
189195
}
190-
ctx.Data["BaseLink"] = orCtx.Link
196+
ctx.Data["BaseLink"] = orCtx.LinkNew
191197

192198
if ctx.HasError() {
193199
ctx.HTML(200, orCtx.NewTemplate)
@@ -241,7 +247,7 @@ func newGogsWebhookPost(ctx *context.Context, form auth.NewGogshookForm, kind mo
241247
ctx.ServerError("getOrgRepoCtx", err)
242248
return
243249
}
244-
ctx.Data["BaseLink"] = orCtx.Link
250+
ctx.Data["BaseLink"] = orCtx.LinkNew
245251

246252
if ctx.HasError() {
247253
ctx.HTML(200, orCtx.NewTemplate)
@@ -537,7 +543,7 @@ func SlackHooksNewPost(ctx *context.Context, form auth.NewSlackHookForm) {
537543

538544
if form.HasInvalidChannel() {
539545
ctx.Flash.Error(ctx.Tr("repo.settings.add_webhook.invalid_channel_name"))
540-
ctx.Redirect(orCtx.Link + "/slack/new")
546+
ctx.Redirect(orCtx.LinkNew + "/slack/new")
541547
return
542548
}
543549

@@ -632,12 +638,10 @@ func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) {
632638
w, err = models.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
633639
} else if orCtx.OrgID > 0 {
634640
w, err = models.GetWebhookByOrgID(ctx.Org.Organization.ID, ctx.ParamsInt64(":id"))
635-
} else if orCtx.IsSystemWebhook {
636-
w, err = models.GetSystemWebhook(ctx.ParamsInt64(":id"))
637-
} else {
638-
w, err = models.GetDefaultWebhook(ctx.ParamsInt64(":id"))
641+
} else if orCtx.IsAdmin {
642+
w, err = models.GetSystemOrDefaultWebhook(ctx.ParamsInt64(":id"))
639643
}
640-
if err != nil {
644+
if err != nil || w == nil {
641645
if models.IsErrWebhookNotExist(err) {
642646
ctx.NotFound("GetWebhookByID", nil)
643647
} else {

routers/routes/macaron.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -370,19 +370,9 @@ func RegisterMacaronRoutes(m *macaron.Macaron) {
370370
m.Post("/delete", admin.DeleteRepo)
371371
})
372372

373-
m.Group("/^:configType(hooks|system-hooks)$", func() {
373+
m.Group("/hooks", func() {
374374
m.Get("", admin.DefaultOrSystemWebhooks)
375375
m.Post("/delete", admin.DeleteDefaultOrSystemWebhook)
376-
m.Get("/:type/new", repo.WebhooksNew)
377-
m.Post("/gitea/new", bindIgnErr(auth.NewWebhookForm{}), repo.GiteaHooksNewPost)
378-
m.Post("/gogs/new", bindIgnErr(auth.NewGogshookForm{}), repo.GogsHooksNewPost)
379-
m.Post("/slack/new", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksNewPost)
380-
m.Post("/discord/new", bindIgnErr(auth.NewDiscordHookForm{}), repo.DiscordHooksNewPost)
381-
m.Post("/dingtalk/new", bindIgnErr(auth.NewDingtalkHookForm{}), repo.DingtalkHooksNewPost)
382-
m.Post("/telegram/new", bindIgnErr(auth.NewTelegramHookForm{}), repo.TelegramHooksNewPost)
383-
m.Post("/matrix/new", bindIgnErr(auth.NewMatrixHookForm{}), repo.MatrixHooksNewPost)
384-
m.Post("/msteams/new", bindIgnErr(auth.NewMSTeamsHookForm{}), repo.MSTeamsHooksNewPost)
385-
m.Post("/feishu/new", bindIgnErr(auth.NewFeishuHookForm{}), repo.FeishuHooksNewPost)
386376
m.Get("/:id", repo.WebHooksEdit)
387377
m.Post("/gitea/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost)
388378
m.Post("/gogs/:id", bindIgnErr(auth.NewGogshookForm{}), repo.GogsHooksEditPost)
@@ -395,6 +385,19 @@ func RegisterMacaronRoutes(m *macaron.Macaron) {
395385
m.Post("/feishu/:id", bindIgnErr(auth.NewFeishuHookForm{}), repo.FeishuHooksEditPost)
396386
})
397387

388+
m.Group("/^:configType(default-hooks|system-hooks)$", func() {
389+
m.Get("/:type/new", repo.WebhooksNew)
390+
m.Post("/gitea/new", bindIgnErr(auth.NewWebhookForm{}), repo.GiteaHooksNewPost)
391+
m.Post("/gogs/new", bindIgnErr(auth.NewGogshookForm{}), repo.GogsHooksNewPost)
392+
m.Post("/slack/new", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksNewPost)
393+
m.Post("/discord/new", bindIgnErr(auth.NewDiscordHookForm{}), repo.DiscordHooksNewPost)
394+
m.Post("/dingtalk/new", bindIgnErr(auth.NewDingtalkHookForm{}), repo.DingtalkHooksNewPost)
395+
m.Post("/telegram/new", bindIgnErr(auth.NewTelegramHookForm{}), repo.TelegramHooksNewPost)
396+
m.Post("/matrix/new", bindIgnErr(auth.NewMatrixHookForm{}), repo.MatrixHooksNewPost)
397+
m.Post("/msteams/new", bindIgnErr(auth.NewMSTeamsHookForm{}), repo.MSTeamsHooksNewPost)
398+
m.Post("/feishu/new", bindIgnErr(auth.NewFeishuHookForm{}), repo.FeishuHooksNewPost)
399+
})
400+
398401
m.Group("/auths", func() {
399402
m.Get("", admin.Authentications)
400403
m.Combo("/new").Get(admin.NewAuthSource).Post(bindIgnErr(auth.AuthenticationForm{}), admin.NewAuthSourcePost)

templates/admin/hook_new.tmpl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
<div class="ui container">
55
{{template "base/alert" .}}
66
<h4 class="ui top attached header">
7-
{{if .PageIsAdminHooksNew}}
8-
{{.i18n.Tr "admin.hooks.add_webhook"}}
7+
{{if .PageIsAdminDefaultHooksNew}}
8+
{{.i18n.Tr "admin.defaulthooks.add_webhook"}}
9+
{{else if .PageIsAdminSystemHooksNew}}
10+
{{.i18n.Tr "admin.systemhooks.add_webhook"}}
11+
{{else if .Webhook.IsSystemWebhook}}
12+
{{.i18n.Tr "admin.systemhooks.update_webhook"}}
913
{{else}}
10-
{{.i18n.Tr "admin.hooks.update_webhook"}}
14+
{{.i18n.Tr "admin.defaulthooks.update_webhook"}}
1115
{{end}}
1216
<div class="ui right">
1317
{{if eq .HookType "gitea"}}

templates/admin/hooks.tmpl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
<div class="page-content admin hooks">
33
{{template "admin/navbar" .}}
44
<div class="ui container">
5-
{{template "repo/settings/webhook/list" .}}
5+
{{template "base/alert" .}}
6+
7+
{{template "repo/settings/webhook/base_list" .SystemWebhooks}}
8+
{{template "repo/settings/webhook/base_list" .DefaultWebhooks}}
9+
10+
{{template "repo/settings/webhook/delete_modal" .}}
611
</div>
712
</div>
813
{{template "base/footer" .}}

templates/admin/navbar.tmpl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,9 @@
1212
<a class="{{if .PageIsAdminRepositories}}active{{end}} item" href="{{AppSubUrl}}/admin/repos">
1313
{{.i18n.Tr "admin.repositories"}}
1414
</a>
15-
<a class="{{if .PageIsAdminHooks}}active{{end}} item" href="{{AppSubUrl}}/admin/hooks">
15+
<a class="{{if or .PageIsAdminDefaultHooks .PageIsAdminSystemHooks}}active{{end}} item" href="{{AppSubUrl}}/admin/hooks">
1616
{{.i18n.Tr "admin.hooks"}}
1717
</a>
18-
<a class="{{if .PageIsAdminSystemHooks}}active{{end}} item" href="{{AppSubUrl}}/admin/system-hooks">
19-
{{.i18n.Tr "admin.systemhooks"}}
20-
</a>
2118
<a class="{{if .PageIsAdminAuthentications}}active{{end}} item" href="{{AppSubUrl}}/admin/auths">
2219
{{.i18n.Tr "admin.authentication"}}
2320
</a>

0 commit comments

Comments
 (0)