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.

hook.go 6.1KB

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