-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Allow creating and editing system webhooks. #23142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This code is not elegant. It fixes the problems I ran into, but I'm sure it does it suboptimally. For one thing, |
Thank you for taking the time to curate this, @yardenshoham; I don't think this is an enhancement though, it is a bug fix to a feature that went in to 1.19 and isn't usable at the moment. I was hoping to get this fixed in 1.19. |
Labels make sense now? |
Yes! Thank you :) :) |
I want to be able to edit If I can edit a secret, I can do:
If I can't edit a secret, I can instead do:
This will wipe out logs of previous attempts on the hook, which is a little unfortunate, and requires 3 APIs calls then where the other needs 2. It's not the end of the world if you don't want to allow editing the secret; I can think of arguments in favour of that too, like making hooks symmetrical with tokens which also don't allow editing after their creation. |
f031c51
to
3fcf7dd
Compare
models/webhook/webhook_system.go
Outdated
// GetSystemOrDefaultWebhooks returns all admin webhooks. | ||
func GetSystemOrDefaultWebhooks(ctx context.Context, isActive util.OptionalBool) ([]*Webhook, error) { | ||
webhooks := make([]*Webhook, 0, 5) | ||
if isActive.IsNone() { | ||
return webhooks, db.GetEngine(ctx). | ||
Where("repo_id=? AND org_id=?", 0, 0). | ||
Find(&webhooks) | ||
} | ||
return webhooks, db.GetEngine(ctx). | ||
Where("repo_id=? AND org_id=? AND is_active = ?", 0, 0, isActive.IsTrue()). | ||
Find(&webhooks) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's improvements to be made here. For example, if this exists, do we really still need GetSystemWebhooks()
? Would you ever use both?
Fixes go-gitea#23139, hopefully. Also, allow creating/editing (but not viewing) the _secret_ associated with each hook.
3fcf7dd
to
66c356c
Compare
Codecov Report
@@ Coverage Diff @@
## main #23142 +/- ##
===========================================
- Coverage 47.14% 0 -47.15%
===========================================
Files 1149 0 -1149
Lines 151446 0 -151446
===========================================
- Hits 71397 0 -71397
+ Misses 71611 0 -71611
+ Partials 8438 0 -8438 see 1149 files with indirect coverage changes Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
Hi! I hope you're all doing well with working on Actions. Is there any attention available for looking this patch over? It's a pretty obvious bug and what I think is a straightforward fix. |
modules/structs/hook.go
Outdated
@@ -24,6 +24,7 @@ type Hook struct { | |||
Events []string `json:"events"` | |||
AuthorizationHeader string `json:"authorization_header"` | |||
Active bool `json:"active"` | |||
IsSystemWebhook bool `json:"is_system_webhook"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although the database stored as the name, but maybe we can have a better name like IsSystemOrDefault
? Or even ScopeType
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've gotten around to this again finally.
I decided to replace IsSystemWebhook
by a path parameter hookType
:
Lines 1384 to 1390 in 5384c1d
m.Group("/{hookType:system-hooks|default-hooks}", func() { | |
m.Combo("").Get(admin.ListHooks). | |
Post(bind(api.CreateHookOption{}), admin.CreateHook) | |
m.Combo("/{id}").Get(admin.GetHook). | |
Patch(bind(api.EditHookOption{}), admin.EditHook). | |
Delete(admin.DeleteHook) | |
}) |
gitea/routers/api/v1/admin/hooks.go
Line 24 in 5384c1d
// swagger:operation GET /admin/{hookType} admin adminListHooks |
gitea/routers/api/v1/admin/hooks.go
Line 62 in 5384c1d
// swagger:operation GET /admin/{hookType}/{id} admin adminGetHook |
gitea/routers/api/v1/admin/hooks.go
Line 107 in 5384c1d
// swagger:operation POST /admin/{hookType} admin adminCreateHook |
gitea/routers/api/v1/admin/hooks.go
Line 139 in 5384c1d
// swagger:operation PATCH /admin/{hookType}/{id} admin adminEditHook |
gitea/routers/api/v1/admin/hooks.go
Line 178 in 5384c1d
// swagger:operation DELETE /admin/{hookType}/{id} admin adminDeleteHook |
Doing this ballooned the PR, but it makes the API more friendly:
and it also matches how the admin web UI works, where the two links on /admin/system-hooks/
go to /admin/system-hooks/{kind}/new
and http://localhost:3000/admin/default-hooks/{kind}/new
respectively.
What do you think?
routers/api/v1/admin/hooks.go
Outdated
@@ -36,7 +36,7 @@ func ListHooks(ctx *context.APIContext) { | |||
// "200": | |||
// "$ref": "#/responses/HookList" | |||
|
|||
sysHooks, err := webhook.GetSystemWebhooks(ctx, util.OptionalBoolNone) | |||
sysHooks, err := webhook.GetSystemOrDefaultWebhooks(ctx, util.OptionalBoolNone) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should change the behaviour? Maybe we should have parameter or we could have another endpoint for default webhooks?
I'm still interested in this. I started to rewrite some parts to reply to Lunny's feedback -- I want to add separate endpoints for system vs default webhooks rather then pass it as a data field because I think that makes the API easier to understand -- but work got in the way before I got my rewrite building. I do want it to come though! |
…/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.
02db086
to
5384c1d
Compare
I've redone this to adopt #23142 (comment) into it, and then cleaned up the API/UI endpoints to match each other. Since that was a pretty extensive change, I've resubmitted this as #26418. It's still not quite ready but it's coming along. |
"System" webhooks -- webhooks run on all repos on a Gitea instance -- were added in go-gitea#14537 (I believe?) but managing them by the API is buggy. - In routers/api/v1/utils/hook.go, correctly handle the distinction between system and default webhooks. This enables actually creating, editing and deleting both kinds. - In routers/api/, move `/api/v1/admin/hooks` to `/api/v1/admin/hooks/{system,default}`. This allows users to access the code in the previous point. - In routers/web/, move `/admin/{system,default}-hooks` and most of `/admin/hooks/` into `/admin/hooks/{system,default}` to match API. - In model/, normalize vocabulary. Since the new sub-type, the terminology has been a confusing mix of "SystemWebhook", "DefaultSystemWebhook", "SystemOrDefaultWebhook" and "DefaultWebhook". Standardize on "AdminWebhook" everywhere with `isSystemWebhook bool` to separate the two sub-types. - Using a bool made it easier to handle both cases without duplicating the router endpoints - Make PATCH /admin/hooks/{system,default}/:id appropriately return 404. Fixes go-gitea#23139. Supersedes go-gitea#23142.
Fixes #23139, hopefully.
Also, allow editing (but not viewing) the secret associated with each hook.