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