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.4 kB

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