code.gitea.io/gitea@v1.21.7/routers/api/v1/repo/git_hook.go (about)

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package repo
     5  
     6  import (
     7  	"net/http"
     8  
     9  	"code.gitea.io/gitea/modules/context"
    10  	"code.gitea.io/gitea/modules/git"
    11  	api "code.gitea.io/gitea/modules/structs"
    12  	"code.gitea.io/gitea/modules/web"
    13  	"code.gitea.io/gitea/services/convert"
    14  )
    15  
    16  // ListGitHooks list all Git hooks of a repository
    17  func ListGitHooks(ctx *context.APIContext) {
    18  	// swagger:operation GET /repos/{owner}/{repo}/hooks/git repository repoListGitHooks
    19  	// ---
    20  	// summary: List the Git hooks in a repository
    21  	// produces:
    22  	// - application/json
    23  	// parameters:
    24  	// - name: owner
    25  	//   in: path
    26  	//   description: owner of the repo
    27  	//   type: string
    28  	//   required: true
    29  	// - name: repo
    30  	//   in: path
    31  	//   description: name of the repo
    32  	//   type: string
    33  	//   required: true
    34  	// responses:
    35  	//   "200":
    36  	//     "$ref": "#/responses/GitHookList"
    37  	//   "404":
    38  	//     "$ref": "#/responses/notFound"
    39  
    40  	hooks, err := ctx.Repo.GitRepo.Hooks()
    41  	if err != nil {
    42  		ctx.Error(http.StatusInternalServerError, "Hooks", err)
    43  		return
    44  	}
    45  
    46  	apiHooks := make([]*api.GitHook, len(hooks))
    47  	for i := range hooks {
    48  		apiHooks[i] = convert.ToGitHook(hooks[i])
    49  	}
    50  	ctx.JSON(http.StatusOK, &apiHooks)
    51  }
    52  
    53  // GetGitHook get a repo's Git hook by id
    54  func GetGitHook(ctx *context.APIContext) {
    55  	// swagger:operation GET /repos/{owner}/{repo}/hooks/git/{id} repository repoGetGitHook
    56  	// ---
    57  	// summary: Get a Git hook
    58  	// produces:
    59  	// - application/json
    60  	// parameters:
    61  	// - name: owner
    62  	//   in: path
    63  	//   description: owner of the repo
    64  	//   type: string
    65  	//   required: true
    66  	// - name: repo
    67  	//   in: path
    68  	//   description: name of the repo
    69  	//   type: string
    70  	//   required: true
    71  	// - name: id
    72  	//   in: path
    73  	//   description: id of the hook to get
    74  	//   type: string
    75  	//   required: true
    76  	// responses:
    77  	//   "200":
    78  	//     "$ref": "#/responses/GitHook"
    79  	//   "404":
    80  	//     "$ref": "#/responses/notFound"
    81  
    82  	hookID := ctx.Params(":id")
    83  	hook, err := ctx.Repo.GitRepo.GetHook(hookID)
    84  	if err != nil {
    85  		if err == git.ErrNotValidHook {
    86  			ctx.NotFound()
    87  		} else {
    88  			ctx.Error(http.StatusInternalServerError, "GetHook", err)
    89  		}
    90  		return
    91  	}
    92  	ctx.JSON(http.StatusOK, convert.ToGitHook(hook))
    93  }
    94  
    95  // EditGitHook modify a Git hook of a repository
    96  func EditGitHook(ctx *context.APIContext) {
    97  	// swagger:operation PATCH /repos/{owner}/{repo}/hooks/git/{id} repository repoEditGitHook
    98  	// ---
    99  	// summary: Edit a Git hook in a repository
   100  	// produces:
   101  	// - application/json
   102  	// parameters:
   103  	// - name: owner
   104  	//   in: path
   105  	//   description: owner of the repo
   106  	//   type: string
   107  	//   required: true
   108  	// - name: repo
   109  	//   in: path
   110  	//   description: name of the repo
   111  	//   type: string
   112  	//   required: true
   113  	// - name: id
   114  	//   in: path
   115  	//   description: id of the hook to get
   116  	//   type: string
   117  	//   required: true
   118  	// - name: body
   119  	//   in: body
   120  	//   schema:
   121  	//     "$ref": "#/definitions/EditGitHookOption"
   122  	// responses:
   123  	//   "200":
   124  	//     "$ref": "#/responses/GitHook"
   125  	//   "404":
   126  	//     "$ref": "#/responses/notFound"
   127  
   128  	form := web.GetForm(ctx).(*api.EditGitHookOption)
   129  	hookID := ctx.Params(":id")
   130  	hook, err := ctx.Repo.GitRepo.GetHook(hookID)
   131  	if err != nil {
   132  		if err == git.ErrNotValidHook {
   133  			ctx.NotFound()
   134  		} else {
   135  			ctx.Error(http.StatusInternalServerError, "GetHook", err)
   136  		}
   137  		return
   138  	}
   139  
   140  	hook.Content = form.Content
   141  	if err = hook.Update(); err != nil {
   142  		ctx.Error(http.StatusInternalServerError, "hook.Update", err)
   143  		return
   144  	}
   145  
   146  	ctx.JSON(http.StatusOK, convert.ToGitHook(hook))
   147  }
   148  
   149  // DeleteGitHook delete a Git hook of a repository
   150  func DeleteGitHook(ctx *context.APIContext) {
   151  	// swagger:operation DELETE /repos/{owner}/{repo}/hooks/git/{id} repository repoDeleteGitHook
   152  	// ---
   153  	// summary: Delete a Git hook in a repository
   154  	// produces:
   155  	// - application/json
   156  	// parameters:
   157  	// - name: owner
   158  	//   in: path
   159  	//   description: owner of the repo
   160  	//   type: string
   161  	//   required: true
   162  	// - name: repo
   163  	//   in: path
   164  	//   description: name of the repo
   165  	//   type: string
   166  	//   required: true
   167  	// - name: id
   168  	//   in: path
   169  	//   description: id of the hook to get
   170  	//   type: string
   171  	//   required: true
   172  	// responses:
   173  	//   "204":
   174  	//     "$ref": "#/responses/empty"
   175  	//   "404":
   176  	//     "$ref": "#/responses/notFound"
   177  
   178  	hookID := ctx.Params(":id")
   179  	hook, err := ctx.Repo.GitRepo.GetHook(hookID)
   180  	if err != nil {
   181  		if err == git.ErrNotValidHook {
   182  			ctx.NotFound()
   183  		} else {
   184  			ctx.Error(http.StatusInternalServerError, "GetHook", err)
   185  		}
   186  		return
   187  	}
   188  
   189  	hook.Content = ""
   190  	if err = hook.Update(); err != nil {
   191  		ctx.Error(http.StatusInternalServerError, "hook.Update", err)
   192  		return
   193  	}
   194  
   195  	ctx.Status(http.StatusNoContent)
   196  }