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.

174 lines
4.2 KiB

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