Skip to content

Commit bc46bd7

Browse files
committed
Split /api/v1/admin/hooks into /api/v1/admin/system-hooks and /api/v1/admin/default-hooks
This should better address the ambiguity that led to go-gitea#23139. Rename parts of the supporting module to match this naming convention.
1 parent e45d9c9 commit bc46bd7

File tree

11 files changed

+312
-254
lines changed

11 files changed

+312
-254
lines changed

models/webhook/webhook_system.go

+15-30
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,11 @@ import (
1111
"code.gitea.io/gitea/modules/util"
1212
)
1313

14-
// GetDefaultWebhooks returns all admin-default webhooks.
15-
func GetDefaultWebhooks(ctx context.Context) ([]*Webhook, error) {
16-
webhooks := make([]*Webhook, 0, 5)
17-
return webhooks, db.GetEngine(ctx).
18-
Where("repo_id=? AND owner_id=? AND is_system_webhook=?", 0, 0, false).
19-
Find(&webhooks)
20-
}
21-
22-
// GetSystemOrDefaultWebhook returns admin system or default webhook by given ID.
23-
func GetSystemOrDefaultWebhook(ctx context.Context, id int64) (*Webhook, error) {
14+
// GetSystemWebhook returns admin default webhook by given ID.
15+
func GetAdminWebhook(ctx context.Context, id int64, isSystemWebhook bool) (*Webhook, error) {
2416
webhook := &Webhook{ID: id}
2517
has, err := db.GetEngine(ctx).
26-
Where("repo_id=? AND owner_id=?", 0, 0).
18+
Where("repo_id=? AND owner_id=? AND is_system_webhook=?", 0, 0, isSystemWebhook).
2719
Get(webhook)
2820
if err != nil {
2921
return nil, err
@@ -33,34 +25,27 @@ func GetSystemOrDefaultWebhook(ctx context.Context, id int64) (*Webhook, error)
3325
return webhook, nil
3426
}
3527

36-
// GetSystemWebhooks returns all admin system webhooks.
37-
func GetSystemWebhooks(ctx context.Context, isActive util.OptionalBool) ([]*Webhook, error) {
38-
webhooks := make([]*Webhook, 0, 5)
39-
if isActive.IsNone() {
40-
return webhooks, db.GetEngine(ctx).
41-
Where("repo_id=? AND owner_id=? AND is_system_webhook=?", 0, 0, true).
42-
Find(&webhooks)
28+
// returns all admin system or default webhooks.
29+
// isSystemWebhook == true gives system webhooks, otherwise gives default webhooks.
30+
// isActive filters system webhooks to those currently enabled or disabled; pass util.OptionalBoolNone to get both.
31+
// isActive is ignored when requesting default webhooks.
32+
func GetAdminWebhooks(ctx context.Context, isSystemWebhook bool, isActive util.OptionalBool) ([]*Webhook, error) {
33+
if !isSystemWebhook {
34+
isActive = util.OptionalBoolNone
4335
}
44-
return webhooks, db.GetEngine(ctx).
45-
Where("repo_id=? AND owner_id=? AND is_system_webhook=? AND is_active = ?", 0, 0, true, isActive.IsTrue()).
46-
Find(&webhooks)
47-
}
48-
49-
// GetSystemOrDefaultWebhooks returns all admin webhooks.
50-
func GetSystemOrDefaultWebhooks(ctx context.Context, isActive util.OptionalBool) ([]*Webhook, error) {
5136
webhooks := make([]*Webhook, 0, 5)
5237
if isActive.IsNone() {
5338
return webhooks, db.GetEngine(ctx).
54-
Where("repo_id=? AND org_id=?", 0, 0).
39+
Where("repo_id=? AND owner_id=? AND is_system_webhook=?", 0, 0, isSystemWebhook).
5540
Find(&webhooks)
5641
}
5742
return webhooks, db.GetEngine(ctx).
58-
Where("repo_id=? AND org_id=? AND is_active = ?", 0, 0, isActive.IsTrue()).
43+
Where("repo_id=? AND owner_id=? AND is_system_webhook=? AND is_active = ?", 0, 0, isSystemWebhook, isActive.IsTrue()).
5944
Find(&webhooks)
6045
}
6146

62-
// DeleteDefaultSystemWebhook deletes an admin-configured default or system webhook (where Org and Repo ID both 0)
63-
func DeleteDefaultSystemWebhook(ctx context.Context, id int64) error {
47+
// DeleteWebhook deletes an admin-configured default or system webhook (where Org and Repo ID both 0)
48+
func DeleteAdminWebhook(ctx context.Context, id int64) error {
6449
return db.WithTx(ctx, func(ctx context.Context) error {
6550
count, err := db.GetEngine(ctx).
6651
Where("repo_id=? AND owner_id=?", 0, 0).
@@ -78,7 +63,7 @@ func DeleteDefaultSystemWebhook(ctx context.Context, id int64) error {
7863

7964
// CopyDefaultWebhooksToRepo creates copies of the default webhooks in a new repo
8065
func CopyDefaultWebhooksToRepo(ctx context.Context, repoID int64) error {
81-
ws, err := GetDefaultWebhooks(ctx)
66+
ws, err := GetAdminWebhooks(ctx, false, util.OptionalBoolNone)
8267
if err != nil {
8368
return fmt.Errorf("GetDefaultWebhooks: %v", err)
8469
}

modules/structs/hook.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ type Hook struct {
2424
Events []string `json:"events"`
2525
AuthorizationHeader string `json:"authorization_header"`
2626
Active bool `json:"active"`
27-
IsSystemWebhook bool `json:"is_system_webhook"`
2827
// swagger:strfmt date-time
2928
Updated time.Time `json:"updated_at"`
3029
// swagger:strfmt date-time
@@ -49,8 +48,7 @@ type CreateHookOption struct {
4948
BranchFilter string `json:"branch_filter" binding:"GlobPattern"`
5049
AuthorizationHeader string `json:"authorization_header"`
5150
// default: false
52-
Active bool `json:"active"`
53-
IsSystemWebhook bool `json:"is_system_webhook"`
51+
Active bool `json:"active"`
5452
}
5553

5654
// EditHookOption options when modify one hook
@@ -59,7 +57,6 @@ type EditHookOption struct {
5957
Events []string `json:"events"`
6058
BranchFilter string `json:"branch_filter" binding:"GlobPattern"`
6159
AuthorizationHeader string `json:"authorization_header"`
62-
IsSystemWebhook *bool `json:"is_system_webhook"`
6360
Active *bool `json:"active"`
6461
}
6562

routers/api/v1/admin/hooks.go

+62-28
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,36 @@ import (
1717
webhook_service "code.gitea.io/gitea/services/webhook"
1818
)
1919

20-
// ListHooks list system's webhooks
20+
// swaggeroperation GET /admin/{hookType:default-hooks|system-hooks} admin adminListHooks
21+
22+
// list system or default webhooks
2123
func ListHooks(ctx *context.APIContext) {
22-
// swagger:operation GET /admin/hooks admin adminListHooks
24+
// swagger:operation GET /admin/{hookType} admin adminListHooks
2325
// ---
2426
// summary: List system's webhooks
2527
// produces:
2628
// - application/json
2729
// parameters:
28-
// - name: page
29-
// in: query
30-
// description: page number of results to return (1-based)
31-
// type: integer
32-
// - name: limit
33-
// in: query
34-
// description: page size of results
35-
// type: integer
30+
// - name: hookType
31+
// in: path
32+
// description: whether the hook is system-wide or copied-to-each-new-repo
33+
// type: string
34+
// enum: [system-hooks, default-hooks]
35+
// required: true
3636
// responses:
3737
// "200":
3838
// "$ref": "#/responses/HookList"
3939

40-
sysHooks, err := webhook.GetSystemOrDefaultWebhooks(ctx, util.OptionalBoolNone)
40+
isSystemWebhook := ctx.Params(":hookType") == "system-hooks"
41+
42+
adminHooks, err := webhook.GetAdminWebhooks(ctx, isSystemWebhook, util.OptionalBoolNone)
4143
if err != nil {
42-
ctx.Error(http.StatusInternalServerError, "GetSystemWebhooks", err)
44+
ctx.Error(http.StatusInternalServerError, "GetAdminWebhooks", err)
4345
return
4446
}
45-
hooks := make([]*api.Hook, len(sysHooks))
46-
for i, hook := range sysHooks {
47+
48+
hooks := make([]*api.Hook, len(adminHooks))
49+
for i, hook := range adminHooks {
4750
h, err := webhook_service.ToHook(setting.AppURL+"/admin", hook)
4851
if err != nil {
4952
ctx.Error(http.StatusInternalServerError, "convert.ToHook", err)
@@ -54,14 +57,20 @@ func ListHooks(ctx *context.APIContext) {
5457
ctx.JSON(http.StatusOK, hooks)
5558
}
5659

57-
// GetHook get an organization's hook by id
60+
// get a system/default hook by id
5861
func GetHook(ctx *context.APIContext) {
59-
// swagger:operation GET /admin/hooks/{id} admin adminGetHook
62+
// swagger:operation GET /admin/{hookType}/{id} admin adminGetHook
6063
// ---
6164
// summary: Get a hook
6265
// produces:
6366
// - application/json
6467
// parameters:
68+
// - name: hookType
69+
// in: path
70+
// description: whether the hook is system-wide or copied-to-each-new-repo
71+
// type: string
72+
// enum: [system-hooks, default-hooks]
73+
// required: true
6574
// - name: id
6675
// in: path
6776
// description: id of the hook to get
@@ -72,16 +81,19 @@ func GetHook(ctx *context.APIContext) {
7281
// "200":
7382
// "$ref": "#/responses/Hook"
7483

84+
isSystemWebhook := ctx.Params(":hookType") == "system-hooks"
85+
7586
hookID := ctx.ParamsInt64(":id")
76-
hook, err := webhook.GetSystemOrDefaultWebhook(ctx, hookID)
87+
hook, err := webhook.GetAdminWebhook(ctx, hookID, isSystemWebhook)
7788
if err != nil {
7889
if errors.Is(err, util.ErrNotExist) {
7990
ctx.NotFound()
8091
} else {
81-
ctx.Error(http.StatusInternalServerError, "GetSystemOrDefaultWebhook", err)
92+
ctx.Error(http.StatusInternalServerError, "GetAdminWebhook", err)
8293
}
8394
return
8495
}
96+
8597
h, err := webhook_service.ToHook("/admin/", hook)
8698
if err != nil {
8799
ctx.Error(http.StatusInternalServerError, "convert.ToHook", err)
@@ -90,16 +102,22 @@ func GetHook(ctx *context.APIContext) {
90102
ctx.JSON(http.StatusOK, h)
91103
}
92104

93-
// CreateHook create a hook for an organization
105+
// create a system or default hook
94106
func CreateHook(ctx *context.APIContext) {
95-
// swagger:operation POST /admin/hooks admin adminCreateHook
107+
// swagger:operation POST /admin/{hookType} admin adminCreateHook
96108
// ---
97109
// summary: Create a hook
98110
// consumes:
99111
// - application/json
100112
// produces:
101113
// - application/json
102114
// parameters:
115+
// - name: hookType
116+
// in: path
117+
// description: whether the hook is system-wide or copied-to-each-new-repo
118+
// type: string
119+
// enum: [system-hooks, default-hooks]
120+
// required: true
103121
// - name: body
104122
// in: body
105123
// required: true
@@ -109,21 +127,29 @@ func CreateHook(ctx *context.APIContext) {
109127
// "201":
110128
// "$ref": "#/responses/Hook"
111129

130+
isSystemWebhook := ctx.Params(":hookType") == "system-hooks"
131+
112132
form := web.GetForm(ctx).(*api.CreateHookOption)
113133

114-
utils.AddSystemHook(ctx, form)
134+
utils.AddAdminHook(ctx, form, isSystemWebhook)
115135
}
116136

117-
// EditHook modify a hook of a repository
137+
// modify a system or default hook
118138
func EditHook(ctx *context.APIContext) {
119-
// swagger:operation PATCH /admin/hooks/{id} admin adminEditHook
139+
// swagger:operation PATCH /admin/{hookType}/{id} admin adminEditHook
120140
// ---
121141
// summary: Update a hook
122142
// consumes:
123143
// - application/json
124144
// produces:
125145
// - application/json
126146
// parameters:
147+
// - name: hookType
148+
// in: path
149+
// description: whether the hook is system-wide or copied-to-each-new-repo
150+
// type: string
151+
// enum: [system-hooks, default-hooks]
152+
// required: true
127153
// - name: id
128154
// in: path
129155
// description: id of the hook to update
@@ -138,21 +164,29 @@ func EditHook(ctx *context.APIContext) {
138164
// "200":
139165
// "$ref": "#/responses/Hook"
140166

167+
isSystemWebhook := ctx.Params(":hookType") == "system-hooks"
168+
141169
form := web.GetForm(ctx).(*api.EditHookOption)
142170

143171
// TODO in body params
144172
hookID := ctx.ParamsInt64(":id")
145-
utils.EditSystemHook(ctx, form, hookID)
173+
utils.EditHook(ctx, form, hookID, isSystemWebhook)
146174
}
147175

148-
// DeleteHook delete a system hook
176+
// delete a system or default hook
149177
func DeleteHook(ctx *context.APIContext) {
150-
// swagger:operation DELETE /admin/hooks/{id} admin adminDeleteHook
178+
// swagger:operation DELETE /admin/{hookType}/{id} admin adminDeleteHook
151179
// ---
152180
// summary: Delete a hook
153181
// produces:
154182
// - application/json
155183
// parameters:
184+
// - name: hookType
185+
// in: path
186+
// description: whether the hook is system-wide or copied-to-each-new-repo
187+
// type: string
188+
// enum: [system-hooks, default-hooks]
189+
// required: true
156190
// - name: id
157191
// in: path
158192
// description: id of the hook to delete
@@ -164,11 +198,11 @@ func DeleteHook(ctx *context.APIContext) {
164198
// "$ref": "#/responses/empty"
165199

166200
hookID := ctx.ParamsInt64(":id")
167-
if err := webhook.DeleteDefaultSystemWebhook(ctx, hookID); err != nil {
201+
if err := webhook.DeleteAdminWebhook(ctx, hookID); err != nil {
168202
if errors.Is(err, util.ErrNotExist) {
169203
ctx.NotFound()
170204
} else {
171-
ctx.Error(http.StatusInternalServerError, "DeleteDefaultSystemWebhook", err)
205+
ctx.Error(http.StatusInternalServerError, "DeleteAdminWebhook", err)
172206
}
173207
return
174208
}

routers/api/v1/api.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1376,12 +1376,12 @@ func Routes() *web.Route {
13761376
m.Get("", admin.GetAllEmails)
13771377
m.Get("/search", admin.SearchEmail)
13781378
})
1379-
m.Group("/unadopted", func() {
1379+
m.Group("/uinadopted", func() {
13801380
m.Get("", admin.ListUnadoptedRepositories)
13811381
m.Post("/{username}/{reponame}", admin.AdoptRepository)
13821382
m.Delete("/{username}/{reponame}", admin.DeleteUnadoptedRepository)
13831383
})
1384-
m.Group("/hooks", func() {
1384+
m.Group("/{hookType:system-hooks|default-hooks}", func() {
13851385
m.Combo("").Get(admin.ListHooks).
13861386
Post(bind(api.CreateHookOption{}), admin.CreateHook)
13871387
m.Combo("/{id}").Get(admin.GetHook).

0 commit comments

Comments
 (0)