You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

git_hook.go 4.3KB

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