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. ctx.Status(204)
  133. }
  134. // CreateHook create a hook for a repository
  135. func CreateHook(ctx *context.APIContext, form api.CreateHookOption) {
  136. // swagger:operation POST /repos/{owner}/{repo}/hooks repository repoCreateHook
  137. // ---
  138. // summary: Create a hook
  139. // consumes:
  140. // - application/json
  141. // produces:
  142. // - application/json
  143. // parameters:
  144. // - name: owner
  145. // in: path
  146. // description: owner of the repo
  147. // type: string
  148. // required: true
  149. // - name: repo
  150. // in: path
  151. // description: name of the repo
  152. // type: string
  153. // required: true
  154. // - name: body
  155. // in: body
  156. // schema:
  157. // "$ref": "#/definitions/CreateHookOption"
  158. // responses:
  159. // "201":
  160. // "$ref": "#/responses/Hook"
  161. if !utils.CheckCreateHookOption(ctx, &form) {
  162. return
  163. }
  164. utils.AddRepoHook(ctx, &form)
  165. }
  166. // EditHook modify a hook of a repository
  167. func EditHook(ctx *context.APIContext, form api.EditHookOption) {
  168. // swagger:operation PATCH /repos/{owner}/{repo}/hooks/{id} repository repoEditHook
  169. // ---
  170. // summary: Edit a hook in a repository
  171. // produces:
  172. // - application/json
  173. // parameters:
  174. // - name: owner
  175. // in: path
  176. // description: owner of the repo
  177. // type: string
  178. // required: true
  179. // - name: repo
  180. // in: path
  181. // description: name of the repo
  182. // type: string
  183. // required: true
  184. // - name: id
  185. // in: path
  186. // description: index of the hook
  187. // type: integer
  188. // format: int64
  189. // required: true
  190. // - name: body
  191. // in: body
  192. // schema:
  193. // "$ref": "#/definitions/EditHookOption"
  194. // responses:
  195. // "200":
  196. // "$ref": "#/responses/Hook"
  197. hookID := ctx.ParamsInt64(":id")
  198. utils.EditRepoHook(ctx, &form, hookID)
  199. }
  200. // DeleteHook delete a hook of a repository
  201. func DeleteHook(ctx *context.APIContext) {
  202. // swagger:operation DELETE /repos/{owner}/{repo}/hooks/{id} repository repoDeleteHook
  203. // ---
  204. // summary: Delete a hook in a repository
  205. // produces:
  206. // - application/json
  207. // parameters:
  208. // - name: owner
  209. // in: path
  210. // description: owner of the repo
  211. // type: string
  212. // required: true
  213. // - name: repo
  214. // in: path
  215. // description: name of the repo
  216. // type: string
  217. // required: true
  218. // - name: id
  219. // in: path
  220. // description: id of the hook to delete
  221. // type: integer
  222. // format: int64
  223. // required: true
  224. // responses:
  225. // "204":
  226. // "$ref": "#/responses/empty"
  227. // "404":
  228. // "$ref": "#/responses/notFound"
  229. if err := models.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
  230. if models.IsErrWebhookNotExist(err) {
  231. ctx.NotFound()
  232. } else {
  233. ctx.Error(500, "DeleteWebhookByRepoID", err)
  234. }
  235. return
  236. }
  237. ctx.Status(204)
  238. }