code.gitea.io/gitea@v1.21.7/routers/api/v1/user/hook.go (about) 1 // Copyright 2022 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package user 5 6 import ( 7 "net/http" 8 9 "code.gitea.io/gitea/modules/context" 10 api "code.gitea.io/gitea/modules/structs" 11 "code.gitea.io/gitea/modules/web" 12 "code.gitea.io/gitea/routers/api/v1/utils" 13 webhook_service "code.gitea.io/gitea/services/webhook" 14 ) 15 16 // ListHooks list the authenticated user's webhooks 17 func ListHooks(ctx *context.APIContext) { 18 // swagger:operation GET /user/hooks user userListHooks 19 // --- 20 // summary: List the authenticated user's webhooks 21 // produces: 22 // - application/json 23 // parameters: 24 // - name: page 25 // in: query 26 // description: page number of results to return (1-based) 27 // type: integer 28 // - name: limit 29 // in: query 30 // description: page size of results 31 // type: integer 32 // responses: 33 // "200": 34 // "$ref": "#/responses/HookList" 35 36 utils.ListOwnerHooks( 37 ctx, 38 ctx.Doer, 39 ) 40 } 41 42 // GetHook get the authenticated user's hook by id 43 func GetHook(ctx *context.APIContext) { 44 // swagger:operation GET /user/hooks/{id} user userGetHook 45 // --- 46 // summary: Get a hook 47 // produces: 48 // - application/json 49 // parameters: 50 // - name: id 51 // in: path 52 // description: id of the hook to get 53 // type: integer 54 // format: int64 55 // required: true 56 // responses: 57 // "200": 58 // "$ref": "#/responses/Hook" 59 60 hook, err := utils.GetOwnerHook(ctx, ctx.Doer.ID, ctx.ParamsInt64("id")) 61 if err != nil { 62 return 63 } 64 65 if !ctx.Doer.IsAdmin && hook.OwnerID != ctx.Doer.ID { 66 ctx.NotFound() 67 return 68 } 69 70 apiHook, err := webhook_service.ToHook(ctx.Doer.HomeLink(), hook) 71 if err != nil { 72 ctx.InternalServerError(err) 73 return 74 } 75 ctx.JSON(http.StatusOK, apiHook) 76 } 77 78 // CreateHook create a hook for the authenticated user 79 func CreateHook(ctx *context.APIContext) { 80 // swagger:operation POST /user/hooks user userCreateHook 81 // --- 82 // summary: Create a hook 83 // consumes: 84 // - application/json 85 // produces: 86 // - application/json 87 // parameters: 88 // - name: body 89 // in: body 90 // required: true 91 // schema: 92 // "$ref": "#/definitions/CreateHookOption" 93 // responses: 94 // "201": 95 // "$ref": "#/responses/Hook" 96 97 utils.AddOwnerHook( 98 ctx, 99 ctx.Doer, 100 web.GetForm(ctx).(*api.CreateHookOption), 101 ) 102 } 103 104 // EditHook modify a hook of the authenticated user 105 func EditHook(ctx *context.APIContext) { 106 // swagger:operation PATCH /user/hooks/{id} user userEditHook 107 // --- 108 // summary: Update a hook 109 // consumes: 110 // - application/json 111 // produces: 112 // - application/json 113 // parameters: 114 // - name: id 115 // in: path 116 // description: id of the hook to update 117 // type: integer 118 // format: int64 119 // required: true 120 // - name: body 121 // in: body 122 // schema: 123 // "$ref": "#/definitions/EditHookOption" 124 // responses: 125 // "200": 126 // "$ref": "#/responses/Hook" 127 128 utils.EditOwnerHook( 129 ctx, 130 ctx.Doer, 131 web.GetForm(ctx).(*api.EditHookOption), 132 ctx.ParamsInt64("id"), 133 ) 134 } 135 136 // DeleteHook delete a hook of the authenticated user 137 func DeleteHook(ctx *context.APIContext) { 138 // swagger:operation DELETE /user/hooks/{id} user userDeleteHook 139 // --- 140 // summary: Delete a hook 141 // produces: 142 // - application/json 143 // parameters: 144 // - name: id 145 // in: path 146 // description: id of the hook to delete 147 // type: integer 148 // format: int64 149 // required: true 150 // responses: 151 // "204": 152 // "$ref": "#/responses/empty" 153 154 utils.DeleteOwnerHook( 155 ctx, 156 ctx.Doer, 157 ctx.ParamsInt64("id"), 158 ) 159 }