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