Skip to content

Commit 66c356c

Browse files
committed
Allow creating and editing system webhooks.
Fixes #23139, hopefully. Also, allow creating/editing (but not viewing) the _secret_ associated with each hook.
1 parent f384b13 commit 66c356c

File tree

6 files changed

+45
-8
lines changed

6 files changed

+45
-8
lines changed

models/webhook/webhook_system.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ func GetSystemWebhooks(ctx context.Context, isActive util.OptionalBool) ([]*Webh
4646
Find(&webhooks)
4747
}
4848

49+
// GetSystemOrDefaultWebhooks returns all admin webhooks.
50+
func GetSystemOrDefaultWebhooks(ctx context.Context, isActive util.OptionalBool) ([]*Webhook, error) {
51+
webhooks := make([]*Webhook, 0, 5)
52+
if isActive.IsNone() {
53+
return webhooks, db.GetEngine(ctx).
54+
Where("repo_id=? AND org_id=?", 0, 0).
55+
Find(&webhooks)
56+
}
57+
return webhooks, db.GetEngine(ctx).
58+
Where("repo_id=? AND org_id=? AND is_active = ?", 0, 0, isActive.IsTrue()).
59+
Find(&webhooks)
60+
}
61+
4962
// DeleteDefaultSystemWebhook deletes an admin-configured default or system webhook (where Org and Repo ID both 0)
5063
func DeleteDefaultSystemWebhook(ctx context.Context, id int64) error {
5164
return db.WithTx(ctx, func(ctx context.Context) error {

modules/structs/hook.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ 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"`
2728
// swagger:strfmt date-time
2829
Updated time.Time `json:"updated_at"`
2930
// swagger:strfmt date-time
@@ -48,7 +49,8 @@ type CreateHookOption struct {
4849
BranchFilter string `json:"branch_filter" binding:"GlobPattern"`
4950
AuthorizationHeader string `json:"authorization_header"`
5051
// default: false
51-
Active bool `json:"active"`
52+
Active bool `json:"active"`
53+
IsSystemWebhook bool `json:"is_system_webhook"`
5254
}
5355

5456
// EditHookOption options when modify one hook
@@ -57,6 +59,7 @@ type EditHookOption struct {
5759
Events []string `json:"events"`
5860
BranchFilter string `json:"branch_filter" binding:"GlobPattern"`
5961
AuthorizationHeader string `json:"authorization_header"`
62+
IsSystemWebhook *bool `json:"is_system_webhook"`
6063
Active *bool `json:"active"`
6164
}
6265

routers/api/v1/admin/hooks.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func ListHooks(ctx *context.APIContext) {
3636
// "200":
3737
// "$ref": "#/responses/HookList"
3838

39-
sysHooks, err := webhook.GetSystemWebhooks(ctx, util.OptionalBoolNone)
39+
sysHooks, err := webhook.GetSystemOrDefaultWebhooks(ctx, util.OptionalBoolNone)
4040
if err != nil {
4141
ctx.Error(http.StatusInternalServerError, "GetSystemWebhooks", err)
4242
return

routers/api/v1/utils/hook.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,13 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoI
170170
form.Events = []string{"push"}
171171
}
172172
w := &webhook.Webhook{
173-
OwnerID: ownerID,
174-
RepoID: repoID,
175-
URL: form.Config["url"],
176-
ContentType: webhook.ToHookContentType(form.Config["content_type"]),
177-
Secret: form.Config["secret"],
178-
HTTPMethod: "POST",
173+
OwnerID: ownerID,
174+
RepoID: repoID,
175+
URL: form.Config["url"],
176+
ContentType: webhook.ToHookContentType(form.Config["content_type"]),
177+
Secret: form.Config["secret"],
178+
IsSystemWebhook: form.IsSystemWebhook,
179+
HTTPMethod: "POST",
179180
HookEvent: &webhook_module.HookEvent{
180181
ChooseEvents: true,
181182
HookEvents: webhook_module.HookEvents{
@@ -324,6 +325,9 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh
324325
}
325326
w.ContentType = webhook.ToHookContentType(ct)
326327
}
328+
if secret, ok := form.Config["secret"]; ok {
329+
w.Secret = secret
330+
}
327331

328332
if w.Type == webhook_module.SLACK {
329333
if channel, ok := form.Config["channel"]; ok {
@@ -386,6 +390,10 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh
386390
return false
387391
}
388392

393+
if form.IsSystemWebhook != nil {
394+
w.IsSystemWebhook = *form.IsSystemWebhook
395+
}
396+
389397
if form.Active != nil {
390398
w.IsActive = *form.Active
391399
}

services/webhook/general.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ func ToHook(repoLink string, w *webhook_model.Webhook) (*api.Hook, error) {
251251
Type: w.Type,
252252
URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
253253
Active: w.IsActive,
254+
IsSystemWebhook: w.IsSystemWebhook,
254255
Config: config,
255256
Events: w.EventsArray(),
256257
AuthorizationHeader: authorizationHeader,

templates/swagger/v1_json.tmpl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15903,6 +15903,10 @@
1590315903
},
1590415904
"x-go-name": "Events"
1590515905
},
15906+
"is_system_webhook": {
15907+
"type": "boolean",
15908+
"x-go-name": "IsSystemWebhook"
15909+
},
1590615910
"type": {
1590715911
"type": "string",
1590815912
"enum": [
@@ -16914,6 +16918,10 @@
1691416918
"type": "string"
1691516919
},
1691616920
"x-go-name": "Events"
16921+
},
16922+
"is_system_webhook": {
16923+
"type": "boolean",
16924+
"x-go-name": "IsSystemWebhook"
1691716925
}
1691816926
},
1691916927
"x-go-package": "code.gitea.io/gitea/modules/structs"
@@ -18067,6 +18075,10 @@
1806718075
"format": "int64",
1806818076
"x-go-name": "ID"
1806918077
},
18078+
"is_system_webhook": {
18079+
"type": "boolean",
18080+
"x-go-name": "IsSystemWebhook"
18081+
},
1807018082
"type": {
1807118083
"type": "string",
1807218084
"x-go-name": "Type"

0 commit comments

Comments
 (0)