Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

hook.go 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. // format: int64
  68. // required: true
  69. // responses:
  70. // "200":
  71. // "$ref": "#/responses/Hook"
  72. repo := ctx.Repo
  73. hookID := ctx.ParamsInt64(":id")
  74. hook, err := utils.GetRepoHook(ctx, repo.Repository.ID, hookID)
  75. if err != nil {
  76. return
  77. }
  78. ctx.JSON(200, convert.ToHook(repo.RepoLink, hook))
  79. }
  80. // TestHook tests a hook
  81. func TestHook(ctx *context.APIContext) {
  82. // swagger:operation POST /repos/{owner}/{repo}/hooks/{id}/tests repository repoTestHook
  83. // ---
  84. // summary: Test a push webhook
  85. // produces:
  86. // - application/json
  87. // parameters:
  88. // - name: owner
  89. // in: path
  90. // description: owner of the repo
  91. // type: string
  92. // required: true
  93. // - name: repo
  94. // in: path
  95. // description: name of the repo
  96. // type: string
  97. // required: true
  98. // - name: id
  99. // in: path
  100. // description: id of the hook to test
  101. // type: integer
  102. // format: int64
  103. // required: true
  104. // responses:
  105. // "204":
  106. // "$ref": "#/responses/empty"
  107. if ctx.Repo.Commit == nil {
  108. // if repo does not have any commits, then don't send a webhook
  109. ctx.Status(204)
  110. return
  111. }
  112. hookID := ctx.ParamsInt64(":id")
  113. hook, err := utils.GetRepoHook(ctx, ctx.Repo.Repository.ID, hookID)
  114. if err != nil {
  115. return
  116. }
  117. if err := models.PrepareWebhook(hook, ctx.Repo.Repository, models.HookEventPush, &api.PushPayload{
  118. Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
  119. Before: ctx.Repo.Commit.ID.String(),
  120. After: ctx.Repo.Commit.ID.String(),
  121. Commits: []*api.PayloadCommit{
  122. convert.ToCommit(ctx.Repo.Repository, ctx.Repo.Commit),
  123. },
  124. Repo: ctx.Repo.Repository.APIFormat(models.AccessModeNone),
  125. Pusher: ctx.User.APIFormat(),
  126. Sender: ctx.User.APIFormat(),
  127. }); err != nil {
  128. ctx.Error(500, "PrepareWebhook: ", err)
  129. return
  130. }
  131. go models.HookQueue.Add(ctx.Repo.Repository.ID)
  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. }