Skip to content

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

Closed
wants to merge 8 commits into from

Conversation

kousu
Copy link
Contributor

@kousu kousu commented Feb 25, 2023

Fixes #23139, hopefully.

Also, allow editing (but not viewing) the secret associated with each hook.

@kousu
Copy link
Contributor Author

kousu commented Feb 25, 2023

This code is not elegant. It fixes the problems I ran into, but I'm sure it does it suboptimally. For one thing, AddSystemHook should be renamed. And maybe there should be separate /admin/hooks/system, /admin/hooks/default routes. And maybe you shouldn't be able to edit whether a hook is a system or default hook. And maybe there should be a POST /admin/hooks/{id}/secret which generates and returns a secret, instead of what I wrote here where the user submits a secret (and hence could easily craft it weakly).

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Feb 25, 2023
@yardenshoham yardenshoham added type/enhancement An improvement of existing functionality modifies/api This PR adds API routes or modifies them labels Feb 25, 2023
@yardenshoham yardenshoham added this to the 1.20.0 milestone Feb 25, 2023
@kousu
Copy link
Contributor Author

kousu commented Feb 25, 2023

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.

@yardenshoham yardenshoham added type/bug outdated/backport/v1.19 This PR should be backported to Gitea 1.19 and removed type/enhancement An improvement of existing functionality labels Feb 25, 2023
@yardenshoham
Copy link
Member

Labels make sense now?

@kousu
Copy link
Contributor Author

kousu commented Feb 25, 2023

Yes! Thank you :) :)

@kousu
Copy link
Contributor Author

kousu commented Feb 27, 2023

I want to be able to edit secret because I want to automate deployments with terraform/Docker/etc -- that's the main reason I'm using the API and not just giving install instructions for an admin to follow.

If I can edit a secret, I can do:

  • GET /admin/hooks
  • if a hook pointing at the expected URL exists: PATCH /admin/hooks/{id}
  • else: POST /admin/hooks

If I can't edit a secret, I can instead do:

  • GET /admin/hooks
  • if a hook pointing at the expected URL exists: DELETE /admin/hooks/{id}
  • POST /admin/hooks

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.

@kousu kousu force-pushed the system-webhooks-api branch 2 times, most recently from f031c51 to 3fcf7dd Compare March 25, 2023 01:21
Comment on lines 49 to 61
// 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)
}

Copy link
Contributor Author

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.
@kousu kousu force-pushed the system-webhooks-api branch from 3fcf7dd to 66c356c Compare March 28, 2023 18:55
@codecov-commenter
Copy link

Codecov Report

Merging #23142 (6fa610c) into main (f521e88) will decrease coverage by 47.15%.
The diff coverage is n/a.

❗ Current head 6fa610c differs from pull request most recent head f2f1bbf. Consider uploading reports for the commit f2f1bbf to get more accurate results

@@             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.

@kousu
Copy link
Contributor Author

kousu commented Apr 24, 2023

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.

@kousu kousu force-pushed the system-webhooks-api branch from d3a7a6d to a85c3ba Compare May 4, 2023 17:39
@@ -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"`
Copy link
Member

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?

Copy link
Contributor Author

@kousu kousu Jul 2, 2023

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:

gitea/routers/api/v1/api.go

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)
})

// swagger:operation GET /admin/{hookType} admin adminListHooks

// swagger:operation GET /admin/{hookType}/{id} admin adminGetHook

// swagger:operation POST /admin/{hookType} admin adminCreateHook

// swagger:operation PATCH /admin/{hookType}/{id} admin adminEditHook

// swagger:operation DELETE /admin/{hookType}/{id} admin adminDeleteHook

Doing this ballooned the PR, but it makes the API more friendly:

Screenshot 2023-07-01 at 23-41-57 Gitea API

and it also matches how the admin web UI works, where the two links on /admin/system-hooks/

Screenshot 2023-07-01 at 23-43-17 Gitea Git with a cup of tea

go to /admin/system-hooks/{kind}/new and http://localhost:3000/admin/default-hooks/{kind}/new respectively.

What do you think?

@@ -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)
Copy link
Member

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?

@delvh delvh removed this from the 1.20.0 milestone Jun 7, 2023
@kousu
Copy link
Contributor Author

kousu commented Jun 8, 2023

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!

@pull-request-size pull-request-size bot added size/XL and removed size/M labels Jul 2, 2023
…/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.
@kousu kousu force-pushed the system-webhooks-api branch from 02db086 to 5384c1d Compare July 2, 2023 03:35
@kousu
Copy link
Contributor Author

kousu commented Aug 9, 2023

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.

@kousu kousu closed this Aug 9, 2023
kousu added a commit to neuropoly/gitea that referenced this pull request Aug 11, 2023
"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.
@go-gitea go-gitea locked as resolved and limited conversation to collaborators Nov 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. modifies/api This PR adds API routes or modifies them outdated/backport/v1.19 This PR should be backported to Gitea 1.19 type/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

System Hooks API confused with Default Hooks API
6 participants