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

hook.go 6.6 kB

9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // Copyright 2014 The Gogs 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/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/convert"
  10. "code.gitea.io/gitea/modules/git"
  11. api "code.gitea.io/gitea/modules/structs"
  12. "code.gitea.io/gitea/modules/webhook"
  13. "code.gitea.io/gitea/routers/api/v1/utils"
  14. )
  15. // ListHooks list all hooks of a repository
  16. func ListHooks(ctx *context.APIContext) {
  17. // swagger:operation GET /repos/{owner}/{repo}/hooks repository repoListHooks
  18. // ---
  19. // summary: List the hooks in a repository
  20. // produces:
  21. // - application/json
  22. // parameters:
  23. // - name: owner
  24. // in: path
  25. // description: owner of the repo
  26. // type: string
  27. // required: true
  28. // - name: repo
  29. // in: path
  30. // description: name of the repo
  31. // type: string
  32. // required: true
  33. // responses:
  34. // "200":
  35. // "$ref": "#/responses/HookList"
  36. hooks, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID)
  37. if err != nil {
  38. ctx.Error(http.StatusInternalServerError, "GetWebhooksByRepoID", err)
  39. return
  40. }
  41. apiHooks := make([]*api.Hook, len(hooks))
  42. for i := range hooks {
  43. apiHooks[i] = convert.ToHook(ctx.Repo.RepoLink, hooks[i])
  44. }
  45. ctx.JSON(http.StatusOK, &apiHooks)
  46. }
  47. // GetHook get a repo's hook by id
  48. func GetHook(ctx *context.APIContext) {
  49. // swagger:operation GET /repos/{owner}/{repo}/hooks/{id} repository repoGetHook
  50. // ---
  51. // summary: Get a 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: integer
  69. // format: int64
  70. // required: true
  71. // responses:
  72. // "200":
  73. // "$ref": "#/responses/Hook"
  74. // "404":
  75. // "$ref": "#/responses/notFound"
  76. repo := ctx.Repo
  77. hookID := ctx.ParamsInt64(":id")
  78. hook, err := utils.GetRepoHook(ctx, repo.Repository.ID, hookID)
  79. if err != nil {
  80. return
  81. }
  82. ctx.JSON(http.StatusOK, convert.ToHook(repo.RepoLink, hook))
  83. }
  84. // TestHook tests a hook
  85. func TestHook(ctx *context.APIContext) {
  86. // swagger:operation POST /repos/{owner}/{repo}/hooks/{id}/tests repository repoTestHook
  87. // ---
  88. // summary: Test a push webhook
  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 test
  105. // type: integer
  106. // format: int64
  107. // required: true
  108. // responses:
  109. // "204":
  110. // "$ref": "#/responses/empty"
  111. if ctx.Repo.Commit == nil {
  112. // if repo does not have any commits, then don't send a webhook
  113. ctx.Status(http.StatusNoContent)
  114. return
  115. }
  116. hookID := ctx.ParamsInt64(":id")
  117. hook, err := utils.GetRepoHook(ctx, ctx.Repo.Repository.ID, hookID)
  118. if err != nil {
  119. return
  120. }
  121. if err := webhook.PrepareWebhook(hook, ctx.Repo.Repository, models.HookEventPush, &api.PushPayload{
  122. Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
  123. Before: ctx.Repo.Commit.ID.String(),
  124. After: ctx.Repo.Commit.ID.String(),
  125. Commits: []*api.PayloadCommit{
  126. convert.ToCommit(ctx.Repo.Repository, ctx.Repo.Commit),
  127. },
  128. Repo: ctx.Repo.Repository.APIFormat(models.AccessModeNone),
  129. Pusher: convert.ToUser(ctx.User, ctx.IsSigned, false),
  130. Sender: convert.ToUser(ctx.User, ctx.IsSigned, false),
  131. }); err != nil {
  132. ctx.Error(http.StatusInternalServerError, "PrepareWebhook: ", err)
  133. return
  134. }
  135. ctx.Status(http.StatusNoContent)
  136. }
  137. // CreateHook create a hook for a repository
  138. func CreateHook(ctx *context.APIContext, form api.CreateHookOption) {
  139. // swagger:operation POST /repos/{owner}/{repo}/hooks repository repoCreateHook
  140. // ---
  141. // summary: Create a hook
  142. // consumes:
  143. // - application/json
  144. // produces:
  145. // - application/json
  146. // parameters:
  147. // - name: owner
  148. // in: path
  149. // description: owner of the repo
  150. // type: string
  151. // required: true
  152. // - name: repo
  153. // in: path
  154. // description: name of the repo
  155. // type: string
  156. // required: true
  157. // - name: body
  158. // in: body
  159. // schema:
  160. // "$ref": "#/definitions/CreateHookOption"
  161. // responses:
  162. // "201":
  163. // "$ref": "#/responses/Hook"
  164. if !utils.CheckCreateHookOption(ctx, &form) {
  165. return
  166. }
  167. utils.AddRepoHook(ctx, &form)
  168. }
  169. // EditHook modify a hook of a repository
  170. func EditHook(ctx *context.APIContext, form api.EditHookOption) {
  171. // swagger:operation PATCH /repos/{owner}/{repo}/hooks/{id} repository repoEditHook
  172. // ---
  173. // summary: Edit a hook in a repository
  174. // produces:
  175. // - application/json
  176. // parameters:
  177. // - name: owner
  178. // in: path
  179. // description: owner of the repo
  180. // type: string
  181. // required: true
  182. // - name: repo
  183. // in: path
  184. // description: name of the repo
  185. // type: string
  186. // required: true
  187. // - name: id
  188. // in: path
  189. // description: index of the hook
  190. // type: integer
  191. // format: int64
  192. // required: true
  193. // - name: body
  194. // in: body
  195. // schema:
  196. // "$ref": "#/definitions/EditHookOption"
  197. // responses:
  198. // "200":
  199. // "$ref": "#/responses/Hook"
  200. hookID := ctx.ParamsInt64(":id")
  201. utils.EditRepoHook(ctx, &form, hookID)
  202. }
  203. // DeleteHook delete a hook of a repository
  204. func DeleteHook(ctx *context.APIContext) {
  205. // swagger:operation DELETE /repos/{owner}/{repo}/hooks/{id} repository repoDeleteHook
  206. // ---
  207. // summary: Delete a hook in a repository
  208. // produces:
  209. // - application/json
  210. // parameters:
  211. // - name: owner
  212. // in: path
  213. // description: owner of the repo
  214. // type: string
  215. // required: true
  216. // - name: repo
  217. // in: path
  218. // description: name of the repo
  219. // type: string
  220. // required: true
  221. // - name: id
  222. // in: path
  223. // description: id of the hook to delete
  224. // type: integer
  225. // format: int64
  226. // required: true
  227. // responses:
  228. // "204":
  229. // "$ref": "#/responses/empty"
  230. // "404":
  231. // "$ref": "#/responses/notFound"
  232. if err := models.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
  233. if models.IsErrWebhookNotExist(err) {
  234. ctx.NotFound()
  235. } else {
  236. ctx.Error(http.StatusInternalServerError, "DeleteWebhookByRepoID", err)
  237. }
  238. return
  239. }
  240. ctx.Status(http.StatusNoContent)
  241. }