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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // Copyright 2016 The Gitea 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 org
  5. import (
  6. "net/http"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/convert"
  10. api "code.gitea.io/gitea/modules/structs"
  11. "code.gitea.io/gitea/modules/web"
  12. "code.gitea.io/gitea/routers/api/v1/utils"
  13. )
  14. // ListHooks list an organziation's webhooks
  15. func ListHooks(ctx *context.APIContext) {
  16. // swagger:operation GET /orgs/{org}/hooks organization orgListHooks
  17. // ---
  18. // summary: List an organization's webhooks
  19. // produces:
  20. // - application/json
  21. // parameters:
  22. // - name: org
  23. // in: path
  24. // description: name of the organization
  25. // type: string
  26. // required: true
  27. // - name: page
  28. // in: query
  29. // description: page number of results to return (1-based)
  30. // type: integer
  31. // - name: limit
  32. // in: query
  33. // description: page size of results
  34. // type: integer
  35. // responses:
  36. // "200":
  37. // "$ref": "#/responses/HookList"
  38. opts := &models.ListWebhookOptions{
  39. ListOptions: utils.GetListOptions(ctx),
  40. OrgID: ctx.Org.Organization.ID,
  41. }
  42. count, err := models.CountWebhooksByOpts(opts)
  43. if err != nil {
  44. ctx.InternalServerError(err)
  45. return
  46. }
  47. orgHooks, err := models.ListWebhooksByOpts(opts)
  48. if err != nil {
  49. ctx.InternalServerError(err)
  50. return
  51. }
  52. hooks := make([]*api.Hook, len(orgHooks))
  53. for i, hook := range orgHooks {
  54. hooks[i] = convert.ToHook(ctx.Org.Organization.HomeLink(), hook)
  55. }
  56. ctx.SetTotalCountHeader(count)
  57. ctx.JSON(http.StatusOK, hooks)
  58. }
  59. // GetHook get an organization's hook by id
  60. func GetHook(ctx *context.APIContext) {
  61. // swagger:operation GET /orgs/{org}/hooks/{id} organization orgGetHook
  62. // ---
  63. // summary: Get a hook
  64. // produces:
  65. // - application/json
  66. // parameters:
  67. // - name: org
  68. // in: path
  69. // description: name of the organization
  70. // type: string
  71. // required: true
  72. // - name: id
  73. // in: path
  74. // description: id of the hook to get
  75. // type: integer
  76. // format: int64
  77. // required: true
  78. // responses:
  79. // "200":
  80. // "$ref": "#/responses/Hook"
  81. org := ctx.Org.Organization
  82. hookID := ctx.ParamsInt64(":id")
  83. hook, err := utils.GetOrgHook(ctx, org.ID, hookID)
  84. if err != nil {
  85. return
  86. }
  87. ctx.JSON(http.StatusOK, convert.ToHook(org.HomeLink(), hook))
  88. }
  89. // CreateHook create a hook for an organization
  90. func CreateHook(ctx *context.APIContext) {
  91. // swagger:operation POST /orgs/{org}/hooks/ organization orgCreateHook
  92. // ---
  93. // summary: Create a hook
  94. // consumes:
  95. // - application/json
  96. // produces:
  97. // - application/json
  98. // parameters:
  99. // - name: org
  100. // in: path
  101. // description: name of the organization
  102. // type: string
  103. // required: true
  104. // - name: body
  105. // in: body
  106. // required: true
  107. // schema:
  108. // "$ref": "#/definitions/CreateHookOption"
  109. // responses:
  110. // "201":
  111. // "$ref": "#/responses/Hook"
  112. form := web.GetForm(ctx).(*api.CreateHookOption)
  113. //TODO in body params
  114. if !utils.CheckCreateHookOption(ctx, form) {
  115. return
  116. }
  117. utils.AddOrgHook(ctx, form)
  118. }
  119. // EditHook modify a hook of a repository
  120. func EditHook(ctx *context.APIContext) {
  121. // swagger:operation PATCH /orgs/{org}/hooks/{id} organization orgEditHook
  122. // ---
  123. // summary: Update a hook
  124. // consumes:
  125. // - application/json
  126. // produces:
  127. // - application/json
  128. // parameters:
  129. // - name: org
  130. // in: path
  131. // description: name of the organization
  132. // type: string
  133. // required: true
  134. // - name: id
  135. // in: path
  136. // description: id of the hook to update
  137. // type: integer
  138. // format: int64
  139. // required: true
  140. // - name: body
  141. // in: body
  142. // schema:
  143. // "$ref": "#/definitions/EditHookOption"
  144. // responses:
  145. // "200":
  146. // "$ref": "#/responses/Hook"
  147. form := web.GetForm(ctx).(*api.EditHookOption)
  148. //TODO in body params
  149. hookID := ctx.ParamsInt64(":id")
  150. utils.EditOrgHook(ctx, form, hookID)
  151. }
  152. // DeleteHook delete a hook of an organization
  153. func DeleteHook(ctx *context.APIContext) {
  154. // swagger:operation DELETE /orgs/{org}/hooks/{id} organization orgDeleteHook
  155. // ---
  156. // summary: Delete a hook
  157. // produces:
  158. // - application/json
  159. // parameters:
  160. // - name: org
  161. // in: path
  162. // description: name of the organization
  163. // type: string
  164. // required: true
  165. // - name: id
  166. // in: path
  167. // description: id of the hook to delete
  168. // type: integer
  169. // format: int64
  170. // required: true
  171. // responses:
  172. // "204":
  173. // "$ref": "#/responses/empty"
  174. org := ctx.Org.Organization
  175. hookID := ctx.ParamsInt64(":id")
  176. if err := models.DeleteWebhookByOrgID(org.ID, hookID); err != nil {
  177. if models.IsErrWebhookNotExist(err) {
  178. ctx.NotFound()
  179. } else {
  180. ctx.Error(http.StatusInternalServerError, "DeleteWebhookByOrgID", err)
  181. }
  182. return
  183. }
  184. ctx.Status(http.StatusNoContent)
  185. }