gitea源码

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