gitea源码

git_hook.go 4.5KB

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