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.

184 lines
4.5 KiB

9 years ago
  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. api "code.gitea.io/sdk/gitea"
  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. )
  12. // ListHooks list all hooks of a repository
  13. func ListHooks(ctx *context.APIContext) {
  14. // swagger:operation GET /repos/{owner}/{repo}/hooks repository repoListHooks
  15. // ---
  16. // summary: List the hooks in a repository
  17. // produces:
  18. // - application/json
  19. // parameters:
  20. // - name: owner
  21. // in: path
  22. // description: owner of the repo
  23. // type: string
  24. // required: true
  25. // - name: repo
  26. // in: path
  27. // description: name of the repo
  28. // type: string
  29. // required: true
  30. // responses:
  31. // "200":
  32. // "$ref": "#/responses/HookList"
  33. hooks, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID)
  34. if err != nil {
  35. ctx.Error(500, "GetWebhooksByRepoID", err)
  36. return
  37. }
  38. apiHooks := make([]*api.Hook, len(hooks))
  39. for i := range hooks {
  40. apiHooks[i] = convert.ToHook(ctx.Repo.RepoLink, hooks[i])
  41. }
  42. ctx.JSON(200, &apiHooks)
  43. }
  44. // GetHook get a repo's hook by id
  45. func GetHook(ctx *context.APIContext) {
  46. // swagger:operation GET /repos/{owner}/{repo}/hooks/{id} repository repoGetHook
  47. // ---
  48. // summary: Get a hook
  49. // produces:
  50. // - application/json
  51. // parameters:
  52. // - name: owner
  53. // in: path
  54. // description: owner of the repo
  55. // type: string
  56. // required: true
  57. // - name: repo
  58. // in: path
  59. // description: name of the repo
  60. // type: string
  61. // required: true
  62. // - name: id
  63. // in: path
  64. // description: id of the hook to get
  65. // type: integer
  66. // required: true
  67. // responses:
  68. // "200":
  69. // "$ref": "#/responses/Hook"
  70. repo := ctx.Repo
  71. hookID := ctx.ParamsInt64(":id")
  72. hook, err := utils.GetRepoHook(ctx, repo.Repository.ID, hookID)
  73. if err != nil {
  74. return
  75. }
  76. ctx.JSON(200, convert.ToHook(repo.RepoLink, hook))
  77. }
  78. // CreateHook create a hook for a repository
  79. func CreateHook(ctx *context.APIContext, form api.CreateHookOption) {
  80. // swagger:operation POST /repos/{owner}/{repo}/hooks repository repoCreateHook
  81. // ---
  82. // summary: Create a hook
  83. // consumes:
  84. // - application/json
  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: body
  99. // in: body
  100. // schema:
  101. // "$ref": "#/definitions/CreateHookOption"
  102. // responses:
  103. // "201":
  104. // "$ref": "#/responses/Hook"
  105. if !utils.CheckCreateHookOption(ctx, &form) {
  106. return
  107. }
  108. utils.AddRepoHook(ctx, &form)
  109. }
  110. // EditHook modify a hook of a repository
  111. func EditHook(ctx *context.APIContext, form api.EditHookOption) {
  112. // swagger:operation PATCH /repos/{owner}/{repo}/hooks/{id} repository repoEditHook
  113. // ---
  114. // summary: Edit a hook in a repository
  115. // produces:
  116. // - application/json
  117. // parameters:
  118. // - name: owner
  119. // in: path
  120. // description: owner of the repo
  121. // type: string
  122. // required: true
  123. // - name: repo
  124. // in: path
  125. // description: name of the repo
  126. // type: string
  127. // required: true
  128. // - name: body
  129. // in: body
  130. // schema:
  131. // "$ref": "#/definitions/EditHookOption"
  132. // responses:
  133. // "200":
  134. // "$ref": "#/responses/Hook"
  135. hookID := ctx.ParamsInt64(":id")
  136. utils.EditRepoHook(ctx, &form, hookID)
  137. }
  138. // DeleteHook delete a hook of a repository
  139. func DeleteHook(ctx *context.APIContext) {
  140. // swagger:operation DELETE /repos/{user}/{repo}/hooks/{id} repository repoDeleteHook
  141. // ---
  142. // summary: Delete a hook in a repository
  143. // produces:
  144. // - application/json
  145. // parameters:
  146. // - name: owner
  147. // in: path
  148. // description: owner of the repo
  149. // type: string
  150. // required: true
  151. // - name: repo
  152. // in: path
  153. // description: name of the repo
  154. // type: string
  155. // required: true
  156. // - name: id
  157. // in: path
  158. // description: id of the hook to delete
  159. // type: integer
  160. // required: true
  161. // responses:
  162. // "204":
  163. // "$ref": "#/responses/empty"
  164. // "404":
  165. // "$ref": "#/responses/notFound"
  166. if err := models.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
  167. if models.IsErrWebhookNotExist(err) {
  168. ctx.Status(404)
  169. } else {
  170. ctx.Error(500, "DeleteWebhookByRepoID", err)
  171. }
  172. return
  173. }
  174. ctx.Status(204)
  175. }