From bc46bd76addf40fce094835f40ee0933c06d7ece Mon Sep 17 00:00:00 2001 From: Nick Guenther Date: Tue, 9 May 2023 20:58:03 -0400 Subject: [PATCH] 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 https://github.com/go-gitea/gitea/issues/23139. Rename parts of the supporting module to match this naming convention. --- models/webhook/webhook_system.go | 45 ++-- modules/structs/hook.go | 5 +- routers/api/v1/admin/hooks.go | 90 +++++--- routers/api/v1/api.go | 4 +- routers/api/v1/utils/hook.go | 48 ++-- routers/web/admin/hooks.go | 18 +- routers/web/repo/setting/webhook.go | 2 +- routers/web/web.go | 4 +- services/webhook/general.go | 1 - services/webhook/webhook.go | 2 +- templates/swagger/v1_json.tmpl | 347 +++++++++++++++------------- 11 files changed, 312 insertions(+), 254 deletions(-) diff --git a/models/webhook/webhook_system.go b/models/webhook/webhook_system.go index b17e08429f5ca..3bbc10299c8c6 100644 --- a/models/webhook/webhook_system.go +++ b/models/webhook/webhook_system.go @@ -11,19 +11,11 @@ import ( "code.gitea.io/gitea/modules/util" ) -// GetDefaultWebhooks returns all admin-default webhooks. -func GetDefaultWebhooks(ctx context.Context) ([]*Webhook, error) { - webhooks := make([]*Webhook, 0, 5) - return webhooks, db.GetEngine(ctx). - Where("repo_id=? AND owner_id=? AND is_system_webhook=?", 0, 0, false). - Find(&webhooks) -} - -// GetSystemOrDefaultWebhook returns admin system or default webhook by given ID. -func GetSystemOrDefaultWebhook(ctx context.Context, id int64) (*Webhook, error) { +// GetSystemWebhook returns admin default webhook by given ID. +func GetAdminWebhook(ctx context.Context, id int64, isSystemWebhook bool) (*Webhook, error) { webhook := &Webhook{ID: id} has, err := db.GetEngine(ctx). - Where("repo_id=? AND owner_id=?", 0, 0). + Where("repo_id=? AND owner_id=? AND is_system_webhook=?", 0, 0, isSystemWebhook). Get(webhook) if err != nil { return nil, err @@ -33,34 +25,27 @@ func GetSystemOrDefaultWebhook(ctx context.Context, id int64) (*Webhook, error) return webhook, nil } -// GetSystemWebhooks returns all admin system webhooks. -func GetSystemWebhooks(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 owner_id=? AND is_system_webhook=?", 0, 0, true). - Find(&webhooks) +// returns all admin system or default webhooks. +// isSystemWebhook == true gives system webhooks, otherwise gives default webhooks. +// isActive filters system webhooks to those currently enabled or disabled; pass util.OptionalBoolNone to get both. +// isActive is ignored when requesting default webhooks. +func GetAdminWebhooks(ctx context.Context, isSystemWebhook bool, isActive util.OptionalBool) ([]*Webhook, error) { + if !isSystemWebhook { + isActive = util.OptionalBoolNone } - return webhooks, db.GetEngine(ctx). - Where("repo_id=? AND owner_id=? AND is_system_webhook=? AND is_active = ?", 0, 0, true, isActive.IsTrue()). - Find(&webhooks) -} - -// 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). + Where("repo_id=? AND owner_id=? AND is_system_webhook=?", 0, 0, isSystemWebhook). Find(&webhooks) } return webhooks, db.GetEngine(ctx). - Where("repo_id=? AND org_id=? AND is_active = ?", 0, 0, isActive.IsTrue()). + Where("repo_id=? AND owner_id=? AND is_system_webhook=? AND is_active = ?", 0, 0, isSystemWebhook, isActive.IsTrue()). Find(&webhooks) } -// DeleteDefaultSystemWebhook deletes an admin-configured default or system webhook (where Org and Repo ID both 0) -func DeleteDefaultSystemWebhook(ctx context.Context, id int64) error { +// DeleteWebhook deletes an admin-configured default or system webhook (where Org and Repo ID both 0) +func DeleteAdminWebhook(ctx context.Context, id int64) error { return db.WithTx(ctx, func(ctx context.Context) error { count, err := db.GetEngine(ctx). Where("repo_id=? AND owner_id=?", 0, 0). @@ -78,7 +63,7 @@ func DeleteDefaultSystemWebhook(ctx context.Context, id int64) error { // CopyDefaultWebhooksToRepo creates copies of the default webhooks in a new repo func CopyDefaultWebhooksToRepo(ctx context.Context, repoID int64) error { - ws, err := GetDefaultWebhooks(ctx) + ws, err := GetAdminWebhooks(ctx, false, util.OptionalBoolNone) if err != nil { return fmt.Errorf("GetDefaultWebhooks: %v", err) } diff --git a/modules/structs/hook.go b/modules/structs/hook.go index a318eec15f915..cd91d4bc462a4 100644 --- a/modules/structs/hook.go +++ b/modules/structs/hook.go @@ -24,7 +24,6 @@ type Hook struct { Events []string `json:"events"` AuthorizationHeader string `json:"authorization_header"` Active bool `json:"active"` - IsSystemWebhook bool `json:"is_system_webhook"` // swagger:strfmt date-time Updated time.Time `json:"updated_at"` // swagger:strfmt date-time @@ -49,8 +48,7 @@ type CreateHookOption struct { BranchFilter string `json:"branch_filter" binding:"GlobPattern"` AuthorizationHeader string `json:"authorization_header"` // default: false - Active bool `json:"active"` - IsSystemWebhook bool `json:"is_system_webhook"` + Active bool `json:"active"` } // EditHookOption options when modify one hook @@ -59,7 +57,6 @@ type EditHookOption struct { Events []string `json:"events"` BranchFilter string `json:"branch_filter" binding:"GlobPattern"` AuthorizationHeader string `json:"authorization_header"` - IsSystemWebhook *bool `json:"is_system_webhook"` Active *bool `json:"active"` } diff --git a/routers/api/v1/admin/hooks.go b/routers/api/v1/admin/hooks.go index 1a8f6dbeaaf3d..beb31184d4fda 100644 --- a/routers/api/v1/admin/hooks.go +++ b/routers/api/v1/admin/hooks.go @@ -17,33 +17,36 @@ import ( webhook_service "code.gitea.io/gitea/services/webhook" ) -// ListHooks list system's webhooks +// swaggeroperation GET /admin/{hookType:default-hooks|system-hooks} admin adminListHooks + +// list system or default webhooks func ListHooks(ctx *context.APIContext) { - // swagger:operation GET /admin/hooks admin adminListHooks + // swagger:operation GET /admin/{hookType} admin adminListHooks // --- // summary: List system's webhooks // produces: // - application/json // parameters: - // - name: page - // in: query - // description: page number of results to return (1-based) - // type: integer - // - name: limit - // in: query - // description: page size of results - // type: integer + // - name: hookType + // in: path + // description: whether the hook is system-wide or copied-to-each-new-repo + // type: string + // enum: [system-hooks, default-hooks] + // required: true // responses: // "200": // "$ref": "#/responses/HookList" - sysHooks, err := webhook.GetSystemOrDefaultWebhooks(ctx, util.OptionalBoolNone) + isSystemWebhook := ctx.Params(":hookType") == "system-hooks" + + adminHooks, err := webhook.GetAdminWebhooks(ctx, isSystemWebhook, util.OptionalBoolNone) if err != nil { - ctx.Error(http.StatusInternalServerError, "GetSystemWebhooks", err) + ctx.Error(http.StatusInternalServerError, "GetAdminWebhooks", err) return } - hooks := make([]*api.Hook, len(sysHooks)) - for i, hook := range sysHooks { + + hooks := make([]*api.Hook, len(adminHooks)) + for i, hook := range adminHooks { h, err := webhook_service.ToHook(setting.AppURL+"/admin", hook) if err != nil { ctx.Error(http.StatusInternalServerError, "convert.ToHook", err) @@ -54,14 +57,20 @@ func ListHooks(ctx *context.APIContext) { ctx.JSON(http.StatusOK, hooks) } -// GetHook get an organization's hook by id +// get a system/default hook by id func GetHook(ctx *context.APIContext) { - // swagger:operation GET /admin/hooks/{id} admin adminGetHook + // swagger:operation GET /admin/{hookType}/{id} admin adminGetHook // --- // summary: Get a hook // produces: // - application/json // parameters: + // - name: hookType + // in: path + // description: whether the hook is system-wide or copied-to-each-new-repo + // type: string + // enum: [system-hooks, default-hooks] + // required: true // - name: id // in: path // description: id of the hook to get @@ -72,16 +81,19 @@ func GetHook(ctx *context.APIContext) { // "200": // "$ref": "#/responses/Hook" + isSystemWebhook := ctx.Params(":hookType") == "system-hooks" + hookID := ctx.ParamsInt64(":id") - hook, err := webhook.GetSystemOrDefaultWebhook(ctx, hookID) + hook, err := webhook.GetAdminWebhook(ctx, hookID, isSystemWebhook) if err != nil { if errors.Is(err, util.ErrNotExist) { ctx.NotFound() } else { - ctx.Error(http.StatusInternalServerError, "GetSystemOrDefaultWebhook", err) + ctx.Error(http.StatusInternalServerError, "GetAdminWebhook", err) } return } + h, err := webhook_service.ToHook("/admin/", hook) if err != nil { ctx.Error(http.StatusInternalServerError, "convert.ToHook", err) @@ -90,9 +102,9 @@ func GetHook(ctx *context.APIContext) { ctx.JSON(http.StatusOK, h) } -// CreateHook create a hook for an organization +// create a system or default hook func CreateHook(ctx *context.APIContext) { - // swagger:operation POST /admin/hooks admin adminCreateHook + // swagger:operation POST /admin/{hookType} admin adminCreateHook // --- // summary: Create a hook // consumes: @@ -100,6 +112,12 @@ func CreateHook(ctx *context.APIContext) { // produces: // - application/json // parameters: + // - name: hookType + // in: path + // description: whether the hook is system-wide or copied-to-each-new-repo + // type: string + // enum: [system-hooks, default-hooks] + // required: true // - name: body // in: body // required: true @@ -109,14 +127,16 @@ func CreateHook(ctx *context.APIContext) { // "201": // "$ref": "#/responses/Hook" + isSystemWebhook := ctx.Params(":hookType") == "system-hooks" + form := web.GetForm(ctx).(*api.CreateHookOption) - utils.AddSystemHook(ctx, form) + utils.AddAdminHook(ctx, form, isSystemWebhook) } -// EditHook modify a hook of a repository +// modify a system or default hook func EditHook(ctx *context.APIContext) { - // swagger:operation PATCH /admin/hooks/{id} admin adminEditHook + // swagger:operation PATCH /admin/{hookType}/{id} admin adminEditHook // --- // summary: Update a hook // consumes: @@ -124,6 +144,12 @@ func EditHook(ctx *context.APIContext) { // produces: // - application/json // parameters: + // - name: hookType + // in: path + // description: whether the hook is system-wide or copied-to-each-new-repo + // type: string + // enum: [system-hooks, default-hooks] + // required: true // - name: id // in: path // description: id of the hook to update @@ -138,21 +164,29 @@ func EditHook(ctx *context.APIContext) { // "200": // "$ref": "#/responses/Hook" + isSystemWebhook := ctx.Params(":hookType") == "system-hooks" + form := web.GetForm(ctx).(*api.EditHookOption) // TODO in body params hookID := ctx.ParamsInt64(":id") - utils.EditSystemHook(ctx, form, hookID) + utils.EditHook(ctx, form, hookID, isSystemWebhook) } -// DeleteHook delete a system hook +// delete a system or default hook func DeleteHook(ctx *context.APIContext) { - // swagger:operation DELETE /admin/hooks/{id} admin adminDeleteHook + // swagger:operation DELETE /admin/{hookType}/{id} admin adminDeleteHook // --- // summary: Delete a hook // produces: // - application/json // parameters: + // - name: hookType + // in: path + // description: whether the hook is system-wide or copied-to-each-new-repo + // type: string + // enum: [system-hooks, default-hooks] + // required: true // - name: id // in: path // description: id of the hook to delete @@ -164,11 +198,11 @@ func DeleteHook(ctx *context.APIContext) { // "$ref": "#/responses/empty" hookID := ctx.ParamsInt64(":id") - if err := webhook.DeleteDefaultSystemWebhook(ctx, hookID); err != nil { + if err := webhook.DeleteAdminWebhook(ctx, hookID); err != nil { if errors.Is(err, util.ErrNotExist) { ctx.NotFound() } else { - ctx.Error(http.StatusInternalServerError, "DeleteDefaultSystemWebhook", err) + ctx.Error(http.StatusInternalServerError, "DeleteAdminWebhook", err) } return } diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 397eb105582b0..76d4d912e9b92 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -1376,12 +1376,12 @@ func Routes() *web.Route { m.Get("", admin.GetAllEmails) m.Get("/search", admin.SearchEmail) }) - m.Group("/unadopted", func() { + m.Group("/uinadopted", func() { m.Get("", admin.ListUnadoptedRepositories) m.Post("/{username}/{reponame}", admin.AdoptRepository) m.Delete("/{username}/{reponame}", admin.DeleteUnadoptedRepository) }) - m.Group("/hooks", func() { + 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). diff --git a/routers/api/v1/utils/hook.go b/routers/api/v1/utils/hook.go index 344021451595e..8f3443bbf8930 100644 --- a/routers/api/v1/utils/hook.go +++ b/routers/api/v1/utils/hook.go @@ -100,9 +100,9 @@ func checkCreateHookOption(ctx *context.APIContext, form *api.CreateHookOption) return true } -// AddSystemHook add a system hook -func AddSystemHook(ctx *context.APIContext, form *api.CreateHookOption) { - hook, ok := addHook(ctx, form, 0, 0) +// add a system or default hook +func AddAdminHook(ctx *context.APIContext, form *api.CreateHookOption, isSystemWebhook bool) { + hook, ok := addHook(ctx, form, 0, 0, isSystemWebhook) if ok { h, err := webhook_service.ToHook(setting.AppSubURL+"/admin", hook) if err != nil { @@ -115,7 +115,7 @@ func AddSystemHook(ctx *context.APIContext, form *api.CreateHookOption) { // AddOwnerHook adds a hook to an user or organization func AddOwnerHook(ctx *context.APIContext, owner *user_model.User, form *api.CreateHookOption) { - hook, ok := addHook(ctx, form, owner.ID, 0) + hook, ok := addHook(ctx, form, owner.ID, 0, false) if !ok { return } @@ -129,7 +129,7 @@ func AddOwnerHook(ctx *context.APIContext, owner *user_model.User, form *api.Cre // AddRepoHook add a hook to a repo. Writes to `ctx` accordingly func AddRepoHook(ctx *context.APIContext, form *api.CreateHookOption) { repo := ctx.Repo - hook, ok := addHook(ctx, form, 0, repo.Repository.ID) + hook, ok := addHook(ctx, form, 0, repo.Repository.ID, false) if !ok { return } @@ -159,9 +159,18 @@ func pullHook(events []string, event string) bool { return util.SliceContainsString(events, event, true) || util.SliceContainsString(events, string(webhook_module.HookEventPullRequest), true) } -// addHook add the hook specified by `form`, `ownerID` and `repoID`. If there is -// an error, write to `ctx` accordingly. Return (webhook, ok) -func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoID int64) (*webhook.Webhook, bool) { +// addHook add the hook specified by `form`, `ownerID`, `repoID`, and `isSystemWebhook`. +// `isSystemWebhook` == true means it's a hook attached automatically to all repos +// `isSystemWebhook` == false && ownerID == 0 && repoID == 0 means it's a default hook, automatically copied to all new repos +// `ownerID` != 0 means it runs on all their repos +// `repoID` != 0 means it is an active hook, attached to that repo +// If there is an error, write to `ctx` accordingly. Return (webhook, ok) +func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoID int64, isSystemWebhook bool) (*webhook.Webhook, bool) { + if isSystemWebhook && (ownerID != 0 || repoID != 0) { + ctx.Error(http.StatusInternalServerError, "addHook", fmt.Errorf("cannot create a hook with an owner or repo that is also a system hook")) + return nil, false + } + if !checkCreateHookOption(ctx, form) { return nil, false } @@ -175,7 +184,7 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoI URL: form.Config["url"], ContentType: webhook.ToHookContentType(form.Config["content_type"]), Secret: form.Config["secret"], - IsSystemWebhook: form.IsSystemWebhook, + IsSystemWebhook: isSystemWebhook, HTTPMethod: "POST", HookEvent: &webhook_module.HookEvent{ ChooseEvents: true, @@ -248,21 +257,28 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoI } // EditSystemHook edit system webhook `w` according to `form`. Writes to `ctx` accordingly -func EditSystemHook(ctx *context.APIContext, form *api.EditHookOption, hookID int64) { - hook, err := webhook.GetSystemOrDefaultWebhook(ctx, hookID) +func EditHook(ctx *context.APIContext, form *api.EditHookOption, hookID int64, isSystemWebhook bool) { + hook, err := webhook.GetAdminWebhook(ctx, hookID, isSystemWebhook) if err != nil { - ctx.Error(http.StatusInternalServerError, "GetSystemOrDefaultWebhook", err) + ctx.Error(http.StatusInternalServerError, "GetAdminWebhook", err) + return + } + + if err != nil { + ctx.Error(http.StatusInternalServerError, "Edithook", err) return } if !editHook(ctx, form, hook) { ctx.Error(http.StatusInternalServerError, "editHook", err) return } - updated, err := webhook.GetSystemOrDefaultWebhook(ctx, hookID) + + updated, err := webhook.GetAdminWebhook(ctx, hookID, isSystemWebhook) if err != nil { - ctx.Error(http.StatusInternalServerError, "GetSystemOrDefaultWebhook", err) + ctx.Error(http.StatusInternalServerError, "GetAdminWebhook", err) return } + h, err := webhook_service.ToHook(setting.AppURL+"/admin", updated) if err != nil { ctx.Error(http.StatusInternalServerError, "convert.ToHook", err) @@ -389,10 +405,6 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh return false } - if form.IsSystemWebhook != nil { - w.IsSystemWebhook = *form.IsSystemWebhook - } - if form.Active != nil { w.IsActive = *form.Active } diff --git a/routers/web/admin/hooks.go b/routers/web/admin/hooks.go index cd8cc29cdfbc0..ef1a54242011e 100644 --- a/routers/web/admin/hooks.go +++ b/routers/web/admin/hooks.go @@ -18,8 +18,8 @@ const ( tplAdminHooks base.TplName = "admin/hooks" ) -// DefaultOrSystemWebhooks renders both admin default and system webhook list pages -func DefaultOrSystemWebhooks(ctx *context.Context) { +// renders both admin default and system webhook list pages +func Webhooks(ctx *context.Context) { var err error ctx.Data["Title"] = ctx.Tr("admin.hooks") @@ -35,21 +35,21 @@ func DefaultOrSystemWebhooks(ctx *context.Context) { sys["Title"] = ctx.Tr("admin.systemhooks") sys["Description"] = ctx.Tr("admin.systemhooks.desc") - sys["Webhooks"], err = webhook.GetSystemWebhooks(ctx, util.OptionalBoolNone) + sys["Webhooks"], err = webhook.GetAdminWebhooks(ctx, true, util.OptionalBoolNone) sys["BaseLink"] = setting.AppSubURL + "/admin/hooks" sys["BaseLinkNew"] = setting.AppSubURL + "/admin/system-hooks" if err != nil { - ctx.ServerError("GetWebhooksAdmin", err) + ctx.ServerError("GetAdminWebhooks", err) return } def["Title"] = ctx.Tr("admin.defaulthooks") def["Description"] = ctx.Tr("admin.defaulthooks.desc") - def["Webhooks"], err = webhook.GetDefaultWebhooks(ctx) + def["Webhooks"], err = webhook.GetAdminWebhooks(ctx, false, util.OptionalBoolNone) def["BaseLink"] = setting.AppSubURL + "/admin/hooks" def["BaseLinkNew"] = setting.AppSubURL + "/admin/default-hooks" if err != nil { - ctx.ServerError("GetWebhooksAdmin", err) + ctx.ServerError("GetAdminWebhooks", err) return } @@ -59,9 +59,9 @@ func DefaultOrSystemWebhooks(ctx *context.Context) { ctx.HTML(http.StatusOK, tplAdminHooks) } -// DeleteDefaultOrSystemWebhook handler to delete an admin-defined system or default webhook -func DeleteDefaultOrSystemWebhook(ctx *context.Context) { - if err := webhook.DeleteDefaultSystemWebhook(ctx, ctx.FormInt64("id")); err != nil { +// handler to delete an admin-defined system or default webhook +func DeleteWebhook(ctx *context.Context) { + if err := webhook.DeleteAdminWebhook(ctx, ctx.FormInt64("id")); err != nil { ctx.Flash.Error("DeleteDefaultWebhook: " + err.Error()) } else { ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success")) diff --git a/routers/web/repo/setting/webhook.go b/routers/web/repo/setting/webhook.go index 5c4e1d47d09ad..840a9aea07eb4 100644 --- a/routers/web/repo/setting/webhook.go +++ b/routers/web/repo/setting/webhook.go @@ -592,7 +592,7 @@ func checkWebhook(ctx *context.Context) (*ownerRepoCtx, *webhook.Webhook) { } else if orCtx.OwnerID > 0 { w, err = webhook.GetWebhookByOwnerID(orCtx.OwnerID, ctx.ParamsInt64(":id")) } else if orCtx.IsAdmin { - w, err = webhook.GetSystemOrDefaultWebhook(ctx, ctx.ParamsInt64(":id")) + w, err = webhook.GetAdminWebhook(ctx, ctx.ParamsInt64(":id"), orCtx.IsSystemWebhook) } if err != nil || w == nil { if webhook.IsErrWebhookNotExist(err) { diff --git a/routers/web/web.go b/routers/web/web.go index 2c2309e827f82..4a5ca4a01d6dd 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -601,8 +601,8 @@ func registerRoutes(m *web.Route) { }, packagesEnabled) m.Group("/hooks", func() { - m.Get("", admin.DefaultOrSystemWebhooks) - m.Post("/delete", admin.DeleteDefaultOrSystemWebhook) + m.Get("", admin.Webhooks) + m.Post("/delete", admin.DeleteWebhook) m.Group("/{id}", func() { m.Get("", repo_setting.WebHooksEdit) m.Post("/replay/{uuid}", repo_setting.ReplayWebhook) diff --git a/services/webhook/general.go b/services/webhook/general.go index ecc99b552ac70..f53ea31ffad75 100644 --- a/services/webhook/general.go +++ b/services/webhook/general.go @@ -255,7 +255,6 @@ func ToHook(repoLink string, w *webhook_model.Webhook) (*api.Hook, error) { Type: w.Type, URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID), Active: w.IsActive, - IsSystemWebhook: w.IsSystemWebhook, Config: config, Events: w.EventsArray(), AuthorizationHeader: authorizationHeader, diff --git a/services/webhook/webhook.go b/services/webhook/webhook.go index 9d5dab85f7977..e8661dc889001 100644 --- a/services/webhook/webhook.go +++ b/services/webhook/webhook.go @@ -247,7 +247,7 @@ func PrepareWebhooks(ctx context.Context, source EventSource, event webhook_modu } // Add any admin-defined system webhooks - systemHooks, err := webhook_model.GetSystemWebhooks(ctx, util.OptionalBoolTrue) + systemHooks, err := webhook_model.GetAdminWebhooks(ctx, true, util.OptionalBoolTrue) if err != nil { return fmt.Errorf("GetSystemWebhooks: %w", err) } diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index dfe8719d98919..213b7049a61bf 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -212,152 +212,6 @@ } } }, - "/admin/hooks": { - "get": { - "produces": [ - "application/json" - ], - "tags": [ - "admin" - ], - "summary": "List system's webhooks", - "operationId": "adminListHooks", - "parameters": [ - { - "type": "integer", - "description": "page number of results to return (1-based)", - "name": "page", - "in": "query" - }, - { - "type": "integer", - "description": "page size of results", - "name": "limit", - "in": "query" - } - ], - "responses": { - "200": { - "$ref": "#/responses/HookList" - } - } - }, - "post": { - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "admin" - ], - "summary": "Create a hook", - "operationId": "adminCreateHook", - "parameters": [ - { - "name": "body", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/CreateHookOption" - } - } - ], - "responses": { - "201": { - "$ref": "#/responses/Hook" - } - } - } - }, - "/admin/hooks/{id}": { - "get": { - "produces": [ - "application/json" - ], - "tags": [ - "admin" - ], - "summary": "Get a hook", - "operationId": "adminGetHook", - "parameters": [ - { - "type": "integer", - "format": "int64", - "description": "id of the hook to get", - "name": "id", - "in": "path", - "required": true - } - ], - "responses": { - "200": { - "$ref": "#/responses/Hook" - } - } - }, - "delete": { - "produces": [ - "application/json" - ], - "tags": [ - "admin" - ], - "summary": "Delete a hook", - "operationId": "adminDeleteHook", - "parameters": [ - { - "type": "integer", - "format": "int64", - "description": "id of the hook to delete", - "name": "id", - "in": "path", - "required": true - } - ], - "responses": { - "204": { - "$ref": "#/responses/empty" - } - } - }, - "patch": { - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "admin" - ], - "summary": "Update a hook", - "operationId": "adminEditHook", - "parameters": [ - { - "type": "integer", - "format": "int64", - "description": "id of the hook to update", - "name": "id", - "in": "path", - "required": true - }, - { - "name": "body", - "in": "body", - "schema": { - "$ref": "#/definitions/EditHookOption" - } - } - ], - "responses": { - "200": { - "$ref": "#/responses/Hook" - } - } - } - }, "/admin/orgs": { "get": { "produces": [ @@ -886,6 +740,195 @@ } } }, + "/admin/{hookType}": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "admin" + ], + "summary": "List system's webhooks", + "operationId": "adminListHooks", + "parameters": [ + { + "enum": [ + "system-hooks", + "default-hooks" + ], + "type": "string", + "description": "whether the hook is system-wide or copied-to-each-new-repo", + "name": "hookType", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "$ref": "#/responses/HookList" + } + } + }, + "post": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "admin" + ], + "summary": "Create a hook", + "operationId": "adminCreateHook", + "parameters": [ + { + "enum": [ + "system-hooks", + "default-hooks" + ], + "type": "string", + "description": "whether the hook is system-wide or copied-to-each-new-repo", + "name": "hookType", + "in": "path", + "required": true + }, + { + "name": "body", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/CreateHookOption" + } + } + ], + "responses": { + "201": { + "$ref": "#/responses/Hook" + } + } + } + }, + "/admin/{hookType}/{id}": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "admin" + ], + "summary": "Get a hook", + "operationId": "adminGetHook", + "parameters": [ + { + "enum": [ + "system-hooks", + "default-hooks" + ], + "type": "string", + "description": "whether the hook is system-wide or copied-to-each-new-repo", + "name": "hookType", + "in": "path", + "required": true + }, + { + "type": "integer", + "format": "int64", + "description": "id of the hook to get", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "$ref": "#/responses/Hook" + } + } + }, + "delete": { + "produces": [ + "application/json" + ], + "tags": [ + "admin" + ], + "summary": "Delete a hook", + "operationId": "adminDeleteHook", + "parameters": [ + { + "enum": [ + "system-hooks", + "default-hooks" + ], + "type": "string", + "description": "whether the hook is system-wide or copied-to-each-new-repo", + "name": "hookType", + "in": "path", + "required": true + }, + { + "type": "integer", + "format": "int64", + "description": "id of the hook to delete", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "204": { + "$ref": "#/responses/empty" + } + } + }, + "patch": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "admin" + ], + "summary": "Update a hook", + "operationId": "adminEditHook", + "parameters": [ + { + "enum": [ + "system-hooks", + "default-hooks" + ], + "type": "string", + "description": "whether the hook is system-wide or copied-to-each-new-repo", + "name": "hookType", + "in": "path", + "required": true + }, + { + "type": "integer", + "format": "int64", + "description": "id of the hook to update", + "name": "id", + "in": "path", + "required": true + }, + { + "name": "body", + "in": "body", + "schema": { + "$ref": "#/definitions/EditHookOption" + } + } + ], + "responses": { + "200": { + "$ref": "#/responses/Hook" + } + } + } + }, "/gitignore/templates": { "get": { "produces": [ @@ -16908,10 +16951,6 @@ }, "x-go-name": "Events" }, - "is_system_webhook": { - "type": "boolean", - "x-go-name": "IsSystemWebhook" - }, "type": { "type": "string", "enum": [ @@ -17920,10 +17959,6 @@ "type": "string" }, "x-go-name": "Events" - }, - "is_system_webhook": { - "type": "boolean", - "x-go-name": "IsSystemWebhook" } }, "x-go-package": "code.gitea.io/gitea/modules/structs" @@ -19121,10 +19156,6 @@ "format": "int64", "x-go-name": "ID" }, - "is_system_webhook": { - "type": "boolean", - "x-go-name": "IsSystemWebhook" - }, "type": { "type": "string", "x-go-name": "Type"