mirror of
https://github.com/go-gitea/gitea
synced 2025-07-22 18:28:37 +00:00
Add user webhooks (#21563)
Currently we can add webhooks for organizations but not for users. This PR adds the latter. You can access it from the current users settings. 
This commit is contained in:
@@ -105,10 +105,7 @@ func CreateHook(ctx *context.APIContext) {
|
||||
// "$ref": "#/responses/Hook"
|
||||
|
||||
form := web.GetForm(ctx).(*api.CreateHookOption)
|
||||
// TODO in body params
|
||||
if !utils.CheckCreateHookOption(ctx, form) {
|
||||
return
|
||||
}
|
||||
|
||||
utils.AddSystemHook(ctx, form)
|
||||
}
|
||||
|
||||
|
@@ -835,6 +835,13 @@ func Routes(ctx gocontext.Context) *web.Route {
|
||||
m.Get("/stopwatches", reqToken(auth_model.AccessTokenScopeRepo), repo.GetStopwatches)
|
||||
m.Get("/subscriptions", reqToken(auth_model.AccessTokenScopeRepo), user.GetMyWatchedRepos)
|
||||
m.Get("/teams", reqToken(auth_model.AccessTokenScopeRepo), org.ListUserTeams)
|
||||
m.Group("/hooks", func() {
|
||||
m.Combo("").Get(user.ListHooks).
|
||||
Post(bind(api.CreateHookOption{}), user.CreateHook)
|
||||
m.Combo("/{id}").Get(user.GetHook).
|
||||
Patch(bind(api.EditHookOption{}), user.EditHook).
|
||||
Delete(user.DeleteHook)
|
||||
}, reqToken(auth_model.AccessTokenScopeAdminUserHook), reqWebhooksEnabled())
|
||||
}, reqToken(""))
|
||||
|
||||
// Repositories
|
||||
|
@@ -6,7 +6,6 @@ package org
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
webhook_model "code.gitea.io/gitea/models/webhook"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/web"
|
||||
@@ -39,34 +38,10 @@ func ListHooks(ctx *context.APIContext) {
|
||||
// "200":
|
||||
// "$ref": "#/responses/HookList"
|
||||
|
||||
opts := &webhook_model.ListWebhookOptions{
|
||||
ListOptions: utils.GetListOptions(ctx),
|
||||
OrgID: ctx.Org.Organization.ID,
|
||||
}
|
||||
|
||||
count, err := webhook_model.CountWebhooksByOpts(opts)
|
||||
if err != nil {
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
}
|
||||
|
||||
orgHooks, err := webhook_model.ListWebhooksByOpts(ctx, opts)
|
||||
if err != nil {
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
}
|
||||
|
||||
hooks := make([]*api.Hook, len(orgHooks))
|
||||
for i, hook := range orgHooks {
|
||||
hooks[i], err = webhook_service.ToHook(ctx.Org.Organization.AsUser().HomeLink(), hook)
|
||||
if err != nil {
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
ctx.SetTotalCountHeader(count)
|
||||
ctx.JSON(http.StatusOK, hooks)
|
||||
utils.ListOwnerHooks(
|
||||
ctx,
|
||||
ctx.ContextUser,
|
||||
)
|
||||
}
|
||||
|
||||
// GetHook get an organization's hook by id
|
||||
@@ -92,14 +67,12 @@ func GetHook(ctx *context.APIContext) {
|
||||
// "200":
|
||||
// "$ref": "#/responses/Hook"
|
||||
|
||||
org := ctx.Org.Organization
|
||||
hookID := ctx.ParamsInt64(":id")
|
||||
hook, err := utils.GetOrgHook(ctx, org.ID, hookID)
|
||||
hook, err := utils.GetOwnerHook(ctx, ctx.ContextUser.ID, ctx.ParamsInt64("id"))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
apiHook, err := webhook_service.ToHook(org.AsUser().HomeLink(), hook)
|
||||
apiHook, err := webhook_service.ToHook(ctx.ContextUser.HomeLink(), hook)
|
||||
if err != nil {
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
@@ -131,15 +104,14 @@ func CreateHook(ctx *context.APIContext) {
|
||||
// "201":
|
||||
// "$ref": "#/responses/Hook"
|
||||
|
||||
form := web.GetForm(ctx).(*api.CreateHookOption)
|
||||
// TODO in body params
|
||||
if !utils.CheckCreateHookOption(ctx, form) {
|
||||
return
|
||||
}
|
||||
utils.AddOrgHook(ctx, form)
|
||||
utils.AddOwnerHook(
|
||||
ctx,
|
||||
ctx.ContextUser,
|
||||
web.GetForm(ctx).(*api.CreateHookOption),
|
||||
)
|
||||
}
|
||||
|
||||
// EditHook modify a hook of a repository
|
||||
// EditHook modify a hook of an organization
|
||||
func EditHook(ctx *context.APIContext) {
|
||||
// swagger:operation PATCH /orgs/{org}/hooks/{id} organization orgEditHook
|
||||
// ---
|
||||
@@ -168,11 +140,12 @@ func EditHook(ctx *context.APIContext) {
|
||||
// "200":
|
||||
// "$ref": "#/responses/Hook"
|
||||
|
||||
form := web.GetForm(ctx).(*api.EditHookOption)
|
||||
|
||||
// TODO in body params
|
||||
hookID := ctx.ParamsInt64(":id")
|
||||
utils.EditOrgHook(ctx, form, hookID)
|
||||
utils.EditOwnerHook(
|
||||
ctx,
|
||||
ctx.ContextUser,
|
||||
web.GetForm(ctx).(*api.EditHookOption),
|
||||
ctx.ParamsInt64("id"),
|
||||
)
|
||||
}
|
||||
|
||||
// DeleteHook delete a hook of an organization
|
||||
@@ -198,15 +171,9 @@ func DeleteHook(ctx *context.APIContext) {
|
||||
// "204":
|
||||
// "$ref": "#/responses/empty"
|
||||
|
||||
org := ctx.Org.Organization
|
||||
hookID := ctx.ParamsInt64(":id")
|
||||
if err := webhook_model.DeleteWebhookByOrgID(org.ID, hookID); err != nil {
|
||||
if webhook_model.IsErrWebhookNotExist(err) {
|
||||
ctx.NotFound()
|
||||
} else {
|
||||
ctx.Error(http.StatusInternalServerError, "DeleteWebhookByOrgID", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
ctx.Status(http.StatusNoContent)
|
||||
utils.DeleteOwnerHook(
|
||||
ctx,
|
||||
ctx.ContextUser,
|
||||
ctx.ParamsInt64("id"),
|
||||
)
|
||||
}
|
||||
|
@@ -223,12 +223,8 @@ func CreateHook(ctx *context.APIContext) {
|
||||
// responses:
|
||||
// "201":
|
||||
// "$ref": "#/responses/Hook"
|
||||
form := web.GetForm(ctx).(*api.CreateHookOption)
|
||||
|
||||
if !utils.CheckCreateHookOption(ctx, form) {
|
||||
return
|
||||
}
|
||||
utils.AddRepoHook(ctx, form)
|
||||
utils.AddRepoHook(ctx, web.GetForm(ctx).(*api.CreateHookOption))
|
||||
}
|
||||
|
||||
// EditHook modify a hook of a repository
|
||||
|
154
routers/api/v1/user/hook.go
Normal file
154
routers/api/v1/user/hook.go
Normal file
@@ -0,0 +1,154 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package user
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
api "code.gitea.io/gitea/modules/structs"
|
||||
"code.gitea.io/gitea/modules/web"
|
||||
"code.gitea.io/gitea/routers/api/v1/utils"
|
||||
webhook_service "code.gitea.io/gitea/services/webhook"
|
||||
)
|
||||
|
||||
// ListHooks list the authenticated user's webhooks
|
||||
func ListHooks(ctx *context.APIContext) {
|
||||
// swagger:operation GET /user/hooks user userListHooks
|
||||
// ---
|
||||
// summary: List the authenticated user'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
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/HookList"
|
||||
|
||||
utils.ListOwnerHooks(
|
||||
ctx,
|
||||
ctx.Doer,
|
||||
)
|
||||
}
|
||||
|
||||
// GetHook get the authenticated user's hook by id
|
||||
func GetHook(ctx *context.APIContext) {
|
||||
// swagger:operation GET /user/hooks/{id} user userGetHook
|
||||
// ---
|
||||
// summary: Get a hook
|
||||
// produces:
|
||||
// - application/json
|
||||
// parameters:
|
||||
// - name: id
|
||||
// in: path
|
||||
// description: id of the hook to get
|
||||
// type: integer
|
||||
// format: int64
|
||||
// required: true
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/Hook"
|
||||
|
||||
hook, err := utils.GetOwnerHook(ctx, ctx.Doer.ID, ctx.ParamsInt64("id"))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
apiHook, err := webhook_service.ToHook(ctx.Doer.HomeLink(), hook)
|
||||
if err != nil {
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
}
|
||||
ctx.JSON(http.StatusOK, apiHook)
|
||||
}
|
||||
|
||||
// CreateHook create a hook for the authenticated user
|
||||
func CreateHook(ctx *context.APIContext) {
|
||||
// swagger:operation POST /user/hooks user userCreateHook
|
||||
// ---
|
||||
// summary: Create a hook
|
||||
// consumes:
|
||||
// - application/json
|
||||
// produces:
|
||||
// - application/json
|
||||
// parameters:
|
||||
// - name: body
|
||||
// in: body
|
||||
// required: true
|
||||
// schema:
|
||||
// "$ref": "#/definitions/CreateHookOption"
|
||||
// responses:
|
||||
// "201":
|
||||
// "$ref": "#/responses/Hook"
|
||||
|
||||
utils.AddOwnerHook(
|
||||
ctx,
|
||||
ctx.Doer,
|
||||
web.GetForm(ctx).(*api.CreateHookOption),
|
||||
)
|
||||
}
|
||||
|
||||
// EditHook modify a hook of the authenticated user
|
||||
func EditHook(ctx *context.APIContext) {
|
||||
// swagger:operation PATCH /user/hooks/{id} user userEditHook
|
||||
// ---
|
||||
// summary: Update a hook
|
||||
// consumes:
|
||||
// - application/json
|
||||
// produces:
|
||||
// - application/json
|
||||
// parameters:
|
||||
// - name: id
|
||||
// in: path
|
||||
// description: id of the hook to update
|
||||
// type: integer
|
||||
// format: int64
|
||||
// required: true
|
||||
// - name: body
|
||||
// in: body
|
||||
// schema:
|
||||
// "$ref": "#/definitions/EditHookOption"
|
||||
// responses:
|
||||
// "200":
|
||||
// "$ref": "#/responses/Hook"
|
||||
|
||||
utils.EditOwnerHook(
|
||||
ctx,
|
||||
ctx.Doer,
|
||||
web.GetForm(ctx).(*api.EditHookOption),
|
||||
ctx.ParamsInt64("id"),
|
||||
)
|
||||
}
|
||||
|
||||
// DeleteHook delete a hook of the authenticated user
|
||||
func DeleteHook(ctx *context.APIContext) {
|
||||
// swagger:operation DELETE /user/hooks/{id} user userDeleteHook
|
||||
// ---
|
||||
// summary: Delete a hook
|
||||
// produces:
|
||||
// - application/json
|
||||
// parameters:
|
||||
// - name: id
|
||||
// in: path
|
||||
// description: id of the hook to delete
|
||||
// type: integer
|
||||
// format: int64
|
||||
// required: true
|
||||
// responses:
|
||||
// "204":
|
||||
// "$ref": "#/responses/empty"
|
||||
|
||||
utils.DeleteOwnerHook(
|
||||
ctx,
|
||||
ctx.Doer,
|
||||
ctx.ParamsInt64("id"),
|
||||
)
|
||||
}
|
@@ -8,6 +8,7 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/models/webhook"
|
||||
"code.gitea.io/gitea/modules/context"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
@@ -18,15 +19,46 @@ import (
|
||||
webhook_service "code.gitea.io/gitea/services/webhook"
|
||||
)
|
||||
|
||||
// GetOrgHook get an organization's webhook. If there is an error, write to
|
||||
// `ctx` accordingly and return the error
|
||||
func GetOrgHook(ctx *context.APIContext, orgID, hookID int64) (*webhook.Webhook, error) {
|
||||
w, err := webhook.GetWebhookByOrgID(orgID, hookID)
|
||||
// ListOwnerHooks lists the webhooks of the provided owner
|
||||
func ListOwnerHooks(ctx *context.APIContext, owner *user_model.User) {
|
||||
opts := &webhook.ListWebhookOptions{
|
||||
ListOptions: GetListOptions(ctx),
|
||||
OwnerID: owner.ID,
|
||||
}
|
||||
|
||||
count, err := webhook.CountWebhooksByOpts(opts)
|
||||
if err != nil {
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
}
|
||||
|
||||
hooks, err := webhook.ListWebhooksByOpts(ctx, opts)
|
||||
if err != nil {
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
}
|
||||
|
||||
apiHooks := make([]*api.Hook, len(hooks))
|
||||
for i, hook := range hooks {
|
||||
apiHooks[i], err = webhook_service.ToHook(owner.HomeLink(), hook)
|
||||
if err != nil {
|
||||
ctx.InternalServerError(err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
ctx.SetTotalCountHeader(count)
|
||||
ctx.JSON(http.StatusOK, apiHooks)
|
||||
}
|
||||
|
||||
// GetOwnerHook gets an user or organization webhook. Errors are written to ctx.
|
||||
func GetOwnerHook(ctx *context.APIContext, ownerID, hookID int64) (*webhook.Webhook, error) {
|
||||
w, err := webhook.GetWebhookByOwnerID(ownerID, hookID)
|
||||
if err != nil {
|
||||
if webhook.IsErrWebhookNotExist(err) {
|
||||
ctx.NotFound()
|
||||
} else {
|
||||
ctx.Error(http.StatusInternalServerError, "GetWebhookByOrgID", err)
|
||||
ctx.Error(http.StatusInternalServerError, "GetWebhookByOwnerID", err)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
@@ -48,9 +80,9 @@ func GetRepoHook(ctx *context.APIContext, repoID, hookID int64) (*webhook.Webhoo
|
||||
return w, nil
|
||||
}
|
||||
|
||||
// CheckCreateHookOption check if a CreateHookOption form is valid. If invalid,
|
||||
// checkCreateHookOption check if a CreateHookOption form is valid. If invalid,
|
||||
// write the appropriate error to `ctx`. Return whether the form is valid
|
||||
func CheckCreateHookOption(ctx *context.APIContext, form *api.CreateHookOption) bool {
|
||||
func checkCreateHookOption(ctx *context.APIContext, form *api.CreateHookOption) bool {
|
||||
if !webhook_service.IsValidHookTaskType(form.Type) {
|
||||
ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("Invalid hook type: %s", form.Type))
|
||||
return false
|
||||
@@ -81,14 +113,13 @@ func AddSystemHook(ctx *context.APIContext, form *api.CreateHookOption) {
|
||||
}
|
||||
}
|
||||
|
||||
// AddOrgHook add a hook to an organization. Writes to `ctx` accordingly
|
||||
func AddOrgHook(ctx *context.APIContext, form *api.CreateHookOption) {
|
||||
org := ctx.Org.Organization
|
||||
hook, ok := addHook(ctx, form, org.ID, 0)
|
||||
// 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)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
apiHook, ok := toAPIHook(ctx, org.AsUser().HomeLink(), hook)
|
||||
apiHook, ok := toAPIHook(ctx, owner.HomeLink(), hook)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
@@ -128,14 +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`, `orgID` and `repoID`. If there is
|
||||
// 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, orgID, repoID int64) (*webhook.Webhook, bool) {
|
||||
func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoID int64) (*webhook.Webhook, bool) {
|
||||
if !checkCreateHookOption(ctx, form) {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
if len(form.Events) == 0 {
|
||||
form.Events = []string{"push"}
|
||||
}
|
||||
w := &webhook.Webhook{
|
||||
OrgID: orgID,
|
||||
OwnerID: ownerID,
|
||||
RepoID: repoID,
|
||||
URL: form.Config["url"],
|
||||
ContentType: webhook.ToHookContentType(form.Config["content_type"]),
|
||||
@@ -234,21 +269,20 @@ func EditSystemHook(ctx *context.APIContext, form *api.EditHookOption, hookID in
|
||||
ctx.JSON(http.StatusOK, h)
|
||||
}
|
||||
|
||||
// EditOrgHook edit webhook `w` according to `form`. Writes to `ctx` accordingly
|
||||
func EditOrgHook(ctx *context.APIContext, form *api.EditHookOption, hookID int64) {
|
||||
org := ctx.Org.Organization
|
||||
hook, err := GetOrgHook(ctx, org.ID, hookID)
|
||||
// EditOwnerHook updates a webhook of an user or organization
|
||||
func EditOwnerHook(ctx *context.APIContext, owner *user_model.User, form *api.EditHookOption, hookID int64) {
|
||||
hook, err := GetOwnerHook(ctx, owner.ID, hookID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if !editHook(ctx, form, hook) {
|
||||
return
|
||||
}
|
||||
updated, err := GetOrgHook(ctx, org.ID, hookID)
|
||||
updated, err := GetOwnerHook(ctx, owner.ID, hookID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
apiHook, ok := toAPIHook(ctx, org.AsUser().HomeLink(), updated)
|
||||
apiHook, ok := toAPIHook(ctx, owner.HomeLink(), updated)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
@@ -362,3 +396,16 @@ func editHook(ctx *context.APIContext, form *api.EditHookOption, w *webhook.Webh
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// DeleteOwnerHook deletes the hook owned by the owner.
|
||||
func DeleteOwnerHook(ctx *context.APIContext, owner *user_model.User, hookID int64) {
|
||||
if err := webhook.DeleteWebhookByOwnerID(owner.ID, hookID); err != nil {
|
||||
if webhook.IsErrWebhookNotExist(err) {
|
||||
ctx.NotFound()
|
||||
} else {
|
||||
ctx.Error(http.StatusInternalServerError, "DeleteWebhookByOwnerID", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
ctx.Status(http.StatusNoContent)
|
||||
}
|
||||
|
Reference in New Issue
Block a user