code.gitea.io/gitea@v1.21.7/routers/api/v1/admin/hooks.go (about) 1 // Copyright 2021 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package admin 5 6 import ( 7 "errors" 8 "net/http" 9 10 "code.gitea.io/gitea/models/webhook" 11 "code.gitea.io/gitea/modules/context" 12 "code.gitea.io/gitea/modules/setting" 13 api "code.gitea.io/gitea/modules/structs" 14 "code.gitea.io/gitea/modules/util" 15 "code.gitea.io/gitea/modules/web" 16 "code.gitea.io/gitea/routers/api/v1/utils" 17 webhook_service "code.gitea.io/gitea/services/webhook" 18 ) 19 20 // ListHooks list system's webhooks 21 func ListHooks(ctx *context.APIContext) { 22 // swagger:operation GET /admin/hooks admin adminListHooks 23 // --- 24 // summary: List system's webhooks 25 // produces: 26 // - application/json 27 // parameters: 28 // - name: page 29 // in: query 30 // description: page number of results to return (1-based) 31 // type: integer 32 // - name: limit 33 // in: query 34 // description: page size of results 35 // type: integer 36 // responses: 37 // "200": 38 // "$ref": "#/responses/HookList" 39 40 sysHooks, err := webhook.GetSystemWebhooks(ctx, util.OptionalBoolNone) 41 if err != nil { 42 ctx.Error(http.StatusInternalServerError, "GetSystemWebhooks", err) 43 return 44 } 45 hooks := make([]*api.Hook, len(sysHooks)) 46 for i, hook := range sysHooks { 47 h, err := webhook_service.ToHook(setting.AppURL+"/admin", hook) 48 if err != nil { 49 ctx.Error(http.StatusInternalServerError, "convert.ToHook", err) 50 return 51 } 52 hooks[i] = h 53 } 54 ctx.JSON(http.StatusOK, hooks) 55 } 56 57 // GetHook get an organization's hook by id 58 func GetHook(ctx *context.APIContext) { 59 // swagger:operation GET /admin/hooks/{id} admin adminGetHook 60 // --- 61 // summary: Get a hook 62 // produces: 63 // - application/json 64 // parameters: 65 // - name: id 66 // in: path 67 // description: id of the hook to get 68 // type: integer 69 // format: int64 70 // required: true 71 // responses: 72 // "200": 73 // "$ref": "#/responses/Hook" 74 75 hookID := ctx.ParamsInt64(":id") 76 hook, err := webhook.GetSystemOrDefaultWebhook(ctx, hookID) 77 if err != nil { 78 if errors.Is(err, util.ErrNotExist) { 79 ctx.NotFound() 80 } else { 81 ctx.Error(http.StatusInternalServerError, "GetSystemOrDefaultWebhook", err) 82 } 83 return 84 } 85 h, err := webhook_service.ToHook("/admin/", hook) 86 if err != nil { 87 ctx.Error(http.StatusInternalServerError, "convert.ToHook", err) 88 return 89 } 90 ctx.JSON(http.StatusOK, h) 91 } 92 93 // CreateHook create a hook for an organization 94 func CreateHook(ctx *context.APIContext) { 95 // swagger:operation POST /admin/hooks admin adminCreateHook 96 // --- 97 // summary: Create a hook 98 // consumes: 99 // - application/json 100 // produces: 101 // - application/json 102 // parameters: 103 // - name: body 104 // in: body 105 // required: true 106 // schema: 107 // "$ref": "#/definitions/CreateHookOption" 108 // responses: 109 // "201": 110 // "$ref": "#/responses/Hook" 111 112 form := web.GetForm(ctx).(*api.CreateHookOption) 113 114 utils.AddSystemHook(ctx, form) 115 } 116 117 // EditHook modify a hook of a repository 118 func EditHook(ctx *context.APIContext) { 119 // swagger:operation PATCH /admin/hooks/{id} admin adminEditHook 120 // --- 121 // summary: Update a hook 122 // consumes: 123 // - application/json 124 // produces: 125 // - application/json 126 // parameters: 127 // - name: id 128 // in: path 129 // description: id of the hook to update 130 // type: integer 131 // format: int64 132 // required: true 133 // - name: body 134 // in: body 135 // schema: 136 // "$ref": "#/definitions/EditHookOption" 137 // responses: 138 // "200": 139 // "$ref": "#/responses/Hook" 140 141 form := web.GetForm(ctx).(*api.EditHookOption) 142 143 // TODO in body params 144 hookID := ctx.ParamsInt64(":id") 145 utils.EditSystemHook(ctx, form, hookID) 146 } 147 148 // DeleteHook delete a system hook 149 func DeleteHook(ctx *context.APIContext) { 150 // swagger:operation DELETE /admin/hooks/{id} admin adminDeleteHook 151 // --- 152 // summary: Delete a hook 153 // produces: 154 // - application/json 155 // parameters: 156 // - name: id 157 // in: path 158 // description: id of the hook to delete 159 // type: integer 160 // format: int64 161 // required: true 162 // responses: 163 // "204": 164 // "$ref": "#/responses/empty" 165 166 hookID := ctx.ParamsInt64(":id") 167 if err := webhook.DeleteDefaultSystemWebhook(ctx, hookID); err != nil { 168 if errors.Is(err, util.ErrNotExist) { 169 ctx.NotFound() 170 } else { 171 ctx.Error(http.StatusInternalServerError, "DeleteDefaultSystemWebhook", err) 172 } 173 return 174 } 175 ctx.Status(http.StatusNoContent) 176 }