9
9
"encoding/json"
10
10
"errors"
11
11
"fmt"
12
+ "path"
12
13
"strings"
13
14
14
15
"code.gitea.io/git"
@@ -23,16 +24,17 @@ import (
23
24
)
24
25
25
26
const (
26
- tplHooks base.TplName = "repo/settings/webhook/base"
27
- tplHookNew base.TplName = "repo/settings/webhook/new"
28
- tplOrgHookNew base.TplName = "org/settings/hook_new"
27
+ tplHooks base.TplName = "repo/settings/webhook/base"
28
+ tplHookNew base.TplName = "repo/settings/webhook/new"
29
+ tplOrgHookNew base.TplName = "org/settings/hook_new"
30
+ tplAdminHookNew base.TplName = "admin/hook_new"
29
31
)
30
32
31
33
// Webhooks render web hooks list page
32
34
func Webhooks (ctx * context.Context ) {
33
35
ctx .Data ["Title" ] = ctx .Tr ("repo.settings.hooks" )
34
36
ctx .Data ["PageIsSettingsHooks" ] = true
35
- ctx .Data ["BaseLink" ] = ctx .Repo .RepoLink
37
+ ctx .Data ["BaseLink" ] = ctx .Repo .RepoLink + "/settings/hooks"
36
38
ctx .Data ["Description" ] = ctx .Tr ("repo.settings.hooks_desc" , "https://docs.gitea.io/en-us/webhooks/" )
37
39
38
40
ws , err := models .GetWebhooksByRepoID (ctx .Repo .Repository .ID )
@@ -48,28 +50,37 @@ func Webhooks(ctx *context.Context) {
48
50
type orgRepoCtx struct {
49
51
OrgID int64
50
52
RepoID int64
53
+ IsAdmin bool
51
54
Link string
52
55
NewTemplate base.TplName
53
56
}
54
57
55
- // getOrgRepoCtx determines whether this is a repo context or organization context.
58
+ // getOrgRepoCtx determines whether this is a repo, organization, or admin context.
56
59
func getOrgRepoCtx (ctx * context.Context ) (* orgRepoCtx , error ) {
57
60
if len (ctx .Repo .RepoLink ) > 0 {
58
61
return & orgRepoCtx {
59
62
RepoID : ctx .Repo .Repository .ID ,
60
- Link : ctx .Repo .RepoLink ,
63
+ Link : path . Join ( ctx .Repo .RepoLink , "settings/hooks" ) ,
61
64
NewTemplate : tplHookNew ,
62
65
}, nil
63
66
}
64
67
65
68
if len (ctx .Org .OrgLink ) > 0 {
66
69
return & orgRepoCtx {
67
70
OrgID : ctx .Org .Organization .ID ,
68
- Link : ctx .Org .OrgLink ,
71
+ Link : path . Join ( ctx .Org .OrgLink , "settings/hooks" ) ,
69
72
NewTemplate : tplOrgHookNew ,
70
73
}, nil
71
74
}
72
75
76
+ if ctx .User .IsAdmin {
77
+ return & orgRepoCtx {
78
+ IsAdmin : true ,
79
+ Link : path .Join (setting .AppSubURL , "/admin/hooks" ),
80
+ NewTemplate : tplAdminHookNew ,
81
+ }, nil
82
+ }
83
+
73
84
return nil , errors .New ("Unable to set OrgRepo context" )
74
85
}
75
86
@@ -85,8 +96,6 @@ func checkHookType(ctx *context.Context) string {
85
96
// WebhooksNew render creating webhook page
86
97
func WebhooksNew (ctx * context.Context ) {
87
98
ctx .Data ["Title" ] = ctx .Tr ("repo.settings.add_webhook" )
88
- ctx .Data ["PageIsSettingsHooks" ] = true
89
- ctx .Data ["PageIsSettingsHooksNew" ] = true
90
99
ctx .Data ["Webhook" ] = models.Webhook {HookEvent : & models.HookEvent {}}
91
100
92
101
orCtx , err := getOrgRepoCtx (ctx )
@@ -95,6 +104,14 @@ func WebhooksNew(ctx *context.Context) {
95
104
return
96
105
}
97
106
107
+ if orCtx .IsAdmin {
108
+ ctx .Data ["PageIsAdminHooks" ] = true
109
+ ctx .Data ["PageIsAdminHooksNew" ] = true
110
+ } else {
111
+ ctx .Data ["PageIsSettingsHooks" ] = true
112
+ ctx .Data ["PageIsSettingsHooksNew" ] = true
113
+ }
114
+
98
115
hookType := checkHookType (ctx )
99
116
ctx .Data ["HookType" ] = hookType
100
117
if ctx .Written () {
@@ -175,7 +192,7 @@ func WebHooksNewPost(ctx *context.Context, form auth.NewWebhookForm) {
175
192
}
176
193
177
194
ctx .Flash .Success (ctx .Tr ("repo.settings.add_hook_success" ))
178
- ctx .Redirect (orCtx .Link + "/settings/hooks" )
195
+ ctx .Redirect (orCtx .Link )
179
196
}
180
197
181
198
// GogsHooksNewPost response for creating webhook
@@ -222,7 +239,7 @@ func GogsHooksNewPost(ctx *context.Context, form auth.NewGogshookForm) {
222
239
}
223
240
224
241
ctx .Flash .Success (ctx .Tr ("repo.settings.add_hook_success" ))
225
- ctx .Redirect (orCtx .Link + "/settings/hooks" )
242
+ ctx .Redirect (orCtx .Link )
226
243
}
227
244
228
245
// DiscordHooksNewPost response for creating discord hook
@@ -271,7 +288,7 @@ func DiscordHooksNewPost(ctx *context.Context, form auth.NewDiscordHookForm) {
271
288
}
272
289
273
290
ctx .Flash .Success (ctx .Tr ("repo.settings.add_hook_success" ))
274
- ctx .Redirect (orCtx .Link + "/settings/hooks" )
291
+ ctx .Redirect (orCtx .Link )
275
292
}
276
293
277
294
// DingtalkHooksNewPost response for creating dingtalk hook
@@ -311,7 +328,7 @@ func DingtalkHooksNewPost(ctx *context.Context, form auth.NewDingtalkHookForm) {
311
328
}
312
329
313
330
ctx .Flash .Success (ctx .Tr ("repo.settings.add_hook_success" ))
314
- ctx .Redirect (orCtx .Link + "/settings/hooks" )
331
+ ctx .Redirect (orCtx .Link )
315
332
}
316
333
317
334
// SlackHooksNewPost response for creating slack hook
@@ -368,7 +385,7 @@ func SlackHooksNewPost(ctx *context.Context, form auth.NewSlackHookForm) {
368
385
}
369
386
370
387
ctx .Flash .Success (ctx .Tr ("repo.settings.add_hook_success" ))
371
- ctx .Redirect (orCtx .Link + "/settings/hooks" )
388
+ ctx .Redirect (orCtx .Link )
372
389
}
373
390
374
391
func checkWebhook (ctx * context.Context ) (* orgRepoCtx , * models.Webhook ) {
@@ -384,8 +401,10 @@ func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) {
384
401
var w * models.Webhook
385
402
if orCtx .RepoID > 0 {
386
403
w , err = models .GetWebhookByRepoID (ctx .Repo .Repository .ID , ctx .ParamsInt64 (":id" ))
387
- } else {
404
+ } else if orCtx . OrgID > 0 {
388
405
w , err = models .GetWebhookByOrgID (ctx .Org .Organization .ID , ctx .ParamsInt64 (":id" ))
406
+ } else {
407
+ w , err = models .GetDefaultWebhook (ctx .ParamsInt64 (":id" ))
389
408
}
390
409
if err != nil {
391
410
if models .IsErrWebhookNotExist (err ) {
@@ -462,7 +481,7 @@ func WebHooksEditPost(ctx *context.Context, form auth.NewWebhookForm) {
462
481
}
463
482
464
483
ctx .Flash .Success (ctx .Tr ("repo.settings.update_hook_success" ))
465
- ctx .Redirect (fmt .Sprintf ("%s/settings/hooks/ %d" , orCtx .Link , w .ID ))
484
+ ctx .Redirect (fmt .Sprintf ("%s/%d" , orCtx .Link , w .ID ))
466
485
}
467
486
468
487
// GogsHooksEditPost response for editing gogs hook
@@ -501,7 +520,7 @@ func GogsHooksEditPost(ctx *context.Context, form auth.NewGogshookForm) {
501
520
}
502
521
503
522
ctx .Flash .Success (ctx .Tr ("repo.settings.update_hook_success" ))
504
- ctx .Redirect (fmt .Sprintf ("%s/settings/hooks/ %d" , orCtx .Link , w .ID ))
523
+ ctx .Redirect (fmt .Sprintf ("%s/%d" , orCtx .Link , w .ID ))
505
524
}
506
525
507
526
// SlackHooksEditPost response for editing slack hook
@@ -551,7 +570,7 @@ func SlackHooksEditPost(ctx *context.Context, form auth.NewSlackHookForm) {
551
570
}
552
571
553
572
ctx .Flash .Success (ctx .Tr ("repo.settings.update_hook_success" ))
554
- ctx .Redirect (fmt .Sprintf ("%s/settings/hooks/ %d" , orCtx .Link , w .ID ))
573
+ ctx .Redirect (fmt .Sprintf ("%s/%d" , orCtx .Link , w .ID ))
555
574
}
556
575
557
576
// DiscordHooksEditPost response for editing discord hook
@@ -593,7 +612,7 @@ func DiscordHooksEditPost(ctx *context.Context, form auth.NewDiscordHookForm) {
593
612
}
594
613
595
614
ctx .Flash .Success (ctx .Tr ("repo.settings.update_hook_success" ))
596
- ctx .Redirect (fmt .Sprintf ("%s/settings/hooks/ %d" , orCtx .Link , w .ID ))
615
+ ctx .Redirect (fmt .Sprintf ("%s/%d" , orCtx .Link , w .ID ))
597
616
}
598
617
599
618
// DingtalkHooksEditPost response for editing discord hook
@@ -625,7 +644,7 @@ func DingtalkHooksEditPost(ctx *context.Context, form auth.NewDingtalkHookForm)
625
644
}
626
645
627
646
ctx .Flash .Success (ctx .Tr ("repo.settings.update_hook_success" ))
628
- ctx .Redirect (fmt .Sprintf ("%s/settings/hooks/ %d" , orCtx .Link , w .ID ))
647
+ ctx .Redirect (fmt .Sprintf ("%s/%d" , orCtx .Link , w .ID ))
629
648
}
630
649
631
650
// TestWebhook test if web hook is work fine
0 commit comments