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.

266 lines
6.7 KiB

API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
4 years ago
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2020 The Gitea Authors.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package repo
  6. import (
  7. "net/http"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/convert"
  11. "code.gitea.io/gitea/modules/git"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "code.gitea.io/gitea/modules/webhook"
  14. "code.gitea.io/gitea/routers/api/v1/utils"
  15. )
  16. // ListHooks list all hooks of a repository
  17. func ListHooks(ctx *context.APIContext) {
  18. // swagger:operation GET /repos/{owner}/{repo}/hooks repository repoListHooks
  19. // ---
  20. // summary: List the hooks in a repository
  21. // produces:
  22. // - application/json
  23. // parameters:
  24. // - name: owner
  25. // in: path
  26. // description: owner of the repo
  27. // type: string
  28. // required: true
  29. // - name: repo
  30. // in: path
  31. // description: name of the repo
  32. // type: string
  33. // required: true
  34. // - name: page
  35. // in: query
  36. // description: page number of results to return (1-based)
  37. // type: integer
  38. // - name: limit
  39. // in: query
  40. // description: page size of results, maximum page size is 50
  41. // type: integer
  42. // responses:
  43. // "200":
  44. // "$ref": "#/responses/HookList"
  45. hooks, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID, utils.GetListOptions(ctx))
  46. if err != nil {
  47. ctx.Error(http.StatusInternalServerError, "GetWebhooksByRepoID", err)
  48. return
  49. }
  50. apiHooks := make([]*api.Hook, len(hooks))
  51. for i := range hooks {
  52. apiHooks[i] = convert.ToHook(ctx.Repo.RepoLink, hooks[i])
  53. }
  54. ctx.JSON(http.StatusOK, &apiHooks)
  55. }
  56. // GetHook get a repo's hook by id
  57. func GetHook(ctx *context.APIContext) {
  58. // swagger:operation GET /repos/{owner}/{repo}/hooks/{id} repository repoGetHook
  59. // ---
  60. // summary: Get a hook
  61. // produces:
  62. // - application/json
  63. // parameters:
  64. // - name: owner
  65. // in: path
  66. // description: owner of the repo
  67. // type: string
  68. // required: true
  69. // - name: repo
  70. // in: path
  71. // description: name of the repo
  72. // type: string
  73. // required: true
  74. // - name: id
  75. // in: path
  76. // description: id of the hook to get
  77. // type: integer
  78. // format: int64
  79. // required: true
  80. // responses:
  81. // "200":
  82. // "$ref": "#/responses/Hook"
  83. // "404":
  84. // "$ref": "#/responses/notFound"
  85. repo := ctx.Repo
  86. hookID := ctx.ParamsInt64(":id")
  87. hook, err := utils.GetRepoHook(ctx, repo.Repository.ID, hookID)
  88. if err != nil {
  89. return
  90. }
  91. ctx.JSON(http.StatusOK, convert.ToHook(repo.RepoLink, hook))
  92. }
  93. // TestHook tests a hook
  94. func TestHook(ctx *context.APIContext) {
  95. // swagger:operation POST /repos/{owner}/{repo}/hooks/{id}/tests repository repoTestHook
  96. // ---
  97. // summary: Test a push webhook
  98. // produces:
  99. // - application/json
  100. // parameters:
  101. // - name: owner
  102. // in: path
  103. // description: owner of the repo
  104. // type: string
  105. // required: true
  106. // - name: repo
  107. // in: path
  108. // description: name of the repo
  109. // type: string
  110. // required: true
  111. // - name: id
  112. // in: path
  113. // description: id of the hook to test
  114. // type: integer
  115. // format: int64
  116. // required: true
  117. // responses:
  118. // "204":
  119. // "$ref": "#/responses/empty"
  120. if ctx.Repo.Commit == nil {
  121. // if repo does not have any commits, then don't send a webhook
  122. ctx.Status(http.StatusNoContent)
  123. return
  124. }
  125. hookID := ctx.ParamsInt64(":id")
  126. hook, err := utils.GetRepoHook(ctx, ctx.Repo.Repository.ID, hookID)
  127. if err != nil {
  128. return
  129. }
  130. if err := webhook.PrepareWebhook(hook, ctx.Repo.Repository, models.HookEventPush, &api.PushPayload{
  131. Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
  132. Before: ctx.Repo.Commit.ID.String(),
  133. After: ctx.Repo.Commit.ID.String(),
  134. Commits: []*api.PayloadCommit{
  135. convert.ToCommit(ctx.Repo.Repository, ctx.Repo.Commit),
  136. },
  137. Repo: ctx.Repo.Repository.APIFormat(models.AccessModeNone),
  138. Pusher: convert.ToUser(ctx.User, ctx.IsSigned, false),
  139. Sender: convert.ToUser(ctx.User, ctx.IsSigned, false),
  140. }); err != nil {
  141. ctx.Error(http.StatusInternalServerError, "PrepareWebhook: ", err)
  142. return
  143. }
  144. ctx.Status(http.StatusNoContent)
  145. }
  146. // CreateHook create a hook for a repository
  147. func CreateHook(ctx *context.APIContext, form api.CreateHookOption) {
  148. // swagger:operation POST /repos/{owner}/{repo}/hooks repository repoCreateHook
  149. // ---
  150. // summary: Create a hook
  151. // consumes:
  152. // - application/json
  153. // produces:
  154. // - application/json
  155. // parameters:
  156. // - name: owner
  157. // in: path
  158. // description: owner of the repo
  159. // type: string
  160. // required: true
  161. // - name: repo
  162. // in: path
  163. // description: name of the repo
  164. // type: string
  165. // required: true
  166. // - name: body
  167. // in: body
  168. // schema:
  169. // "$ref": "#/definitions/CreateHookOption"
  170. // responses:
  171. // "201":
  172. // "$ref": "#/responses/Hook"
  173. if !utils.CheckCreateHookOption(ctx, &form) {
  174. return
  175. }
  176. utils.AddRepoHook(ctx, &form)
  177. }
  178. // EditHook modify a hook of a repository
  179. func EditHook(ctx *context.APIContext, form api.EditHookOption) {
  180. // swagger:operation PATCH /repos/{owner}/{repo}/hooks/{id} repository repoEditHook
  181. // ---
  182. // summary: Edit a hook in a repository
  183. // produces:
  184. // - application/json
  185. // parameters:
  186. // - name: owner
  187. // in: path
  188. // description: owner of the repo
  189. // type: string
  190. // required: true
  191. // - name: repo
  192. // in: path
  193. // description: name of the repo
  194. // type: string
  195. // required: true
  196. // - name: id
  197. // in: path
  198. // description: index of the hook
  199. // type: integer
  200. // format: int64
  201. // required: true
  202. // - name: body
  203. // in: body
  204. // schema:
  205. // "$ref": "#/definitions/EditHookOption"
  206. // responses:
  207. // "200":
  208. // "$ref": "#/responses/Hook"
  209. hookID := ctx.ParamsInt64(":id")
  210. utils.EditRepoHook(ctx, &form, hookID)
  211. }
  212. // DeleteHook delete a hook of a repository
  213. func DeleteHook(ctx *context.APIContext) {
  214. // swagger:operation DELETE /repos/{owner}/{repo}/hooks/{id} repository repoDeleteHook
  215. // ---
  216. // summary: Delete 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: id of the hook to delete
  233. // type: integer
  234. // format: int64
  235. // required: true
  236. // responses:
  237. // "204":
  238. // "$ref": "#/responses/empty"
  239. // "404":
  240. // "$ref": "#/responses/notFound"
  241. if err := models.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
  242. if models.IsErrWebhookNotExist(err) {
  243. ctx.NotFound()
  244. } else {
  245. ctx.Error(http.StatusInternalServerError, "DeleteWebhookByRepoID", err)
  246. }
  247. return
  248. }
  249. ctx.Status(http.StatusNoContent)
  250. }