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.6KB

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