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 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2020 The Gitea Authors.
  3. // SPDX-License-Identifier: MIT
  4. package repo
  5. import (
  6. "net/http"
  7. "code.gitea.io/gitea/models/perm"
  8. access_model "code.gitea.io/gitea/models/perm/access"
  9. "code.gitea.io/gitea/models/webhook"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/git"
  12. "code.gitea.io/gitea/modules/setting"
  13. api "code.gitea.io/gitea/modules/structs"
  14. "code.gitea.io/gitea/modules/web"
  15. webhook_module "code.gitea.io/gitea/modules/webhook"
  16. "code.gitea.io/gitea/routers/api/v1/utils"
  17. "code.gitea.io/gitea/services/convert"
  18. webhook_service "code.gitea.io/gitea/services/webhook"
  19. )
  20. // ListHooks list all hooks of a repository
  21. func ListHooks(ctx *context.APIContext) {
  22. // swagger:operation GET /repos/{owner}/{repo}/hooks repository repoListHooks
  23. // ---
  24. // summary: List the hooks in a repository
  25. // produces:
  26. // - application/json
  27. // parameters:
  28. // - name: owner
  29. // in: path
  30. // description: owner of the repo
  31. // type: string
  32. // required: true
  33. // - name: repo
  34. // in: path
  35. // description: name of the repo
  36. // type: string
  37. // required: true
  38. // - name: page
  39. // in: query
  40. // description: page number of results to return (1-based)
  41. // type: integer
  42. // - name: limit
  43. // in: query
  44. // description: page size of results
  45. // type: integer
  46. // responses:
  47. // "200":
  48. // "$ref": "#/responses/HookList"
  49. // "404":
  50. // "$ref": "#/responses/notFound"
  51. opts := &webhook.ListWebhookOptions{
  52. ListOptions: utils.GetListOptions(ctx),
  53. RepoID: ctx.Repo.Repository.ID,
  54. }
  55. count, err := webhook.CountWebhooksByOpts(opts)
  56. if err != nil {
  57. ctx.InternalServerError(err)
  58. return
  59. }
  60. hooks, err := webhook.ListWebhooksByOpts(ctx, opts)
  61. if err != nil {
  62. ctx.InternalServerError(err)
  63. return
  64. }
  65. apiHooks := make([]*api.Hook, len(hooks))
  66. for i := range hooks {
  67. apiHooks[i], err = webhook_service.ToHook(ctx.Repo.RepoLink, hooks[i])
  68. if err != nil {
  69. ctx.InternalServerError(err)
  70. return
  71. }
  72. }
  73. ctx.SetTotalCountHeader(count)
  74. ctx.JSON(http.StatusOK, &apiHooks)
  75. }
  76. // GetHook get a repo's hook by id
  77. func GetHook(ctx *context.APIContext) {
  78. // swagger:operation GET /repos/{owner}/{repo}/hooks/{id} repository repoGetHook
  79. // ---
  80. // summary: Get a hook
  81. // produces:
  82. // - application/json
  83. // parameters:
  84. // - name: owner
  85. // in: path
  86. // description: owner of the repo
  87. // type: string
  88. // required: true
  89. // - name: repo
  90. // in: path
  91. // description: name of the repo
  92. // type: string
  93. // required: true
  94. // - name: id
  95. // in: path
  96. // description: id of the hook to get
  97. // type: integer
  98. // format: int64
  99. // required: true
  100. // responses:
  101. // "200":
  102. // "$ref": "#/responses/Hook"
  103. // "404":
  104. // "$ref": "#/responses/notFound"
  105. repo := ctx.Repo
  106. hookID := ctx.ParamsInt64(":id")
  107. hook, err := utils.GetRepoHook(ctx, repo.Repository.ID, hookID)
  108. if err != nil {
  109. return
  110. }
  111. apiHook, err := webhook_service.ToHook(repo.RepoLink, hook)
  112. if err != nil {
  113. ctx.InternalServerError(err)
  114. return
  115. }
  116. ctx.JSON(http.StatusOK, apiHook)
  117. }
  118. // TestHook tests a hook
  119. func TestHook(ctx *context.APIContext) {
  120. // swagger:operation POST /repos/{owner}/{repo}/hooks/{id}/tests repository repoTestHook
  121. // ---
  122. // summary: Test a push webhook
  123. // produces:
  124. // - application/json
  125. // parameters:
  126. // - name: owner
  127. // in: path
  128. // description: owner of the repo
  129. // type: string
  130. // required: true
  131. // - name: repo
  132. // in: path
  133. // description: name of the repo
  134. // type: string
  135. // required: true
  136. // - name: id
  137. // in: path
  138. // description: id of the hook to test
  139. // type: integer
  140. // format: int64
  141. // required: true
  142. // - name: ref
  143. // in: query
  144. // description: "The name of the commit/branch/tag, indicates which commit will be loaded to the webhook payload."
  145. // type: string
  146. // required: false
  147. // responses:
  148. // "204":
  149. // "$ref": "#/responses/empty"
  150. // "404":
  151. // "$ref": "#/responses/notFound"
  152. if ctx.Repo.Commit == nil {
  153. // if repo does not have any commits, then don't send a webhook
  154. ctx.Status(http.StatusNoContent)
  155. return
  156. }
  157. ref := git.BranchPrefix + ctx.Repo.Repository.DefaultBranch
  158. if r := ctx.FormTrim("ref"); r != "" {
  159. ref = r
  160. }
  161. hookID := ctx.ParamsInt64(":id")
  162. hook, err := utils.GetRepoHook(ctx, ctx.Repo.Repository.ID, hookID)
  163. if err != nil {
  164. return
  165. }
  166. commit := convert.ToPayloadCommit(ctx, ctx.Repo.Repository, ctx.Repo.Commit)
  167. commitID := ctx.Repo.Commit.ID.String()
  168. if err := webhook_service.PrepareWebhook(ctx, hook, webhook_module.HookEventPush, &api.PushPayload{
  169. Ref: ref,
  170. Before: commitID,
  171. After: commitID,
  172. CompareURL: setting.AppURL + ctx.Repo.Repository.ComposeCompareURL(commitID, commitID),
  173. Commits: []*api.PayloadCommit{commit},
  174. TotalCommits: 1,
  175. HeadCommit: commit,
  176. Repo: convert.ToRepo(ctx, ctx.Repo.Repository, access_model.Permission{AccessMode: perm.AccessModeNone}),
  177. Pusher: convert.ToUserWithAccessMode(ctx, ctx.Doer, perm.AccessModeNone),
  178. Sender: convert.ToUserWithAccessMode(ctx, ctx.Doer, perm.AccessModeNone),
  179. }); err != nil {
  180. ctx.Error(http.StatusInternalServerError, "PrepareWebhook: ", err)
  181. return
  182. }
  183. ctx.Status(http.StatusNoContent)
  184. }
  185. // CreateHook create a hook for a repository
  186. func CreateHook(ctx *context.APIContext) {
  187. // swagger:operation POST /repos/{owner}/{repo}/hooks repository repoCreateHook
  188. // ---
  189. // summary: Create a hook
  190. // consumes:
  191. // - application/json
  192. // produces:
  193. // - application/json
  194. // parameters:
  195. // - name: owner
  196. // in: path
  197. // description: owner of the repo
  198. // type: string
  199. // required: true
  200. // - name: repo
  201. // in: path
  202. // description: name of the repo
  203. // type: string
  204. // required: true
  205. // - name: body
  206. // in: body
  207. // schema:
  208. // "$ref": "#/definitions/CreateHookOption"
  209. // responses:
  210. // "201":
  211. // "$ref": "#/responses/Hook"
  212. // "404":
  213. // "$ref": "#/responses/notFound"
  214. utils.AddRepoHook(ctx, web.GetForm(ctx).(*api.CreateHookOption))
  215. }
  216. // EditHook modify a hook of a repository
  217. func EditHook(ctx *context.APIContext) {
  218. // swagger:operation PATCH /repos/{owner}/{repo}/hooks/{id} repository repoEditHook
  219. // ---
  220. // summary: Edit a hook in a repository
  221. // produces:
  222. // - application/json
  223. // parameters:
  224. // - name: owner
  225. // in: path
  226. // description: owner of the repo
  227. // type: string
  228. // required: true
  229. // - name: repo
  230. // in: path
  231. // description: name of the repo
  232. // type: string
  233. // required: true
  234. // - name: id
  235. // in: path
  236. // description: index of the hook
  237. // type: integer
  238. // format: int64
  239. // required: true
  240. // - name: body
  241. // in: body
  242. // schema:
  243. // "$ref": "#/definitions/EditHookOption"
  244. // responses:
  245. // "200":
  246. // "$ref": "#/responses/Hook"
  247. // "404":
  248. // "$ref": "#/responses/notFound"
  249. form := web.GetForm(ctx).(*api.EditHookOption)
  250. hookID := ctx.ParamsInt64(":id")
  251. utils.EditRepoHook(ctx, form, hookID)
  252. }
  253. // DeleteHook delete a hook of a repository
  254. func DeleteHook(ctx *context.APIContext) {
  255. // swagger:operation DELETE /repos/{owner}/{repo}/hooks/{id} repository repoDeleteHook
  256. // ---
  257. // summary: Delete a hook in a repository
  258. // produces:
  259. // - application/json
  260. // parameters:
  261. // - name: owner
  262. // in: path
  263. // description: owner of the repo
  264. // type: string
  265. // required: true
  266. // - name: repo
  267. // in: path
  268. // description: name of the repo
  269. // type: string
  270. // required: true
  271. // - name: id
  272. // in: path
  273. // description: id of the hook to delete
  274. // type: integer
  275. // format: int64
  276. // required: true
  277. // responses:
  278. // "204":
  279. // "$ref": "#/responses/empty"
  280. // "404":
  281. // "$ref": "#/responses/notFound"
  282. if err := webhook.DeleteWebhookByRepoID(ctx, ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
  283. if webhook.IsErrWebhookNotExist(err) {
  284. ctx.NotFound()
  285. } else {
  286. ctx.Error(http.StatusInternalServerError, "DeleteWebhookByRepoID", err)
  287. }
  288. return
  289. }
  290. ctx.Status(http.StatusNoContent)
  291. }