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.

162 lines
3.9 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/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 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. // required: true
  58. // responses:
  59. // "200":
  60. // "$ref": "#/responses/Hook"
  61. org := ctx.Org.Organization
  62. hookID := ctx.ParamsInt64(":id")
  63. hook, err := utils.GetOrgHook(ctx, org.ID, hookID)
  64. if err != nil {
  65. return
  66. }
  67. ctx.JSON(200, convert.ToHook(org.HomeLink(), hook))
  68. }
  69. // CreateHook create a hook for an organization
  70. func CreateHook(ctx *context.APIContext, form api.CreateHookOption) {
  71. // swagger:operation POST /orgs/{org}/hooks/ organization orgCreateHook
  72. // ---
  73. // summary: Create a hook
  74. // consumes:
  75. // - application/json
  76. // produces:
  77. // - application/json
  78. // parameters:
  79. // - name: org
  80. // in: path
  81. // description: name of the organization
  82. // type: string
  83. // required: true
  84. // responses:
  85. // "201":
  86. // "$ref": "#/responses/Hook"
  87. //TODO in body params
  88. if !utils.CheckCreateHookOption(ctx, &form) {
  89. return
  90. }
  91. utils.AddOrgHook(ctx, &form)
  92. }
  93. // EditHook modify a hook of a repository
  94. func EditHook(ctx *context.APIContext, form api.EditHookOption) {
  95. // swagger:operation PATCH /orgs/{org}/hooks/{id} organization orgEditHook
  96. // ---
  97. // summary: Update a hook
  98. // consumes:
  99. // - application/json
  100. // produces:
  101. // - application/json
  102. // parameters:
  103. // - name: org
  104. // in: path
  105. // description: name of the organization
  106. // type: string
  107. // required: true
  108. // - name: id
  109. // in: path
  110. // description: id of the hook to update
  111. // type: integer
  112. // required: true
  113. // responses:
  114. // "200":
  115. // "$ref": "#/responses/Hook"
  116. //TODO in body params
  117. hookID := ctx.ParamsInt64(":id")
  118. utils.EditOrgHook(ctx, &form, hookID)
  119. }
  120. // DeleteHook delete a hook of an organization
  121. func DeleteHook(ctx *context.APIContext) {
  122. // swagger:operation DELETE /orgs/{org}/hooks/{id} organization orgDeleteHook
  123. // ---
  124. // summary: Delete a hook
  125. // produces:
  126. // - application/json
  127. // parameters:
  128. // - name: org
  129. // in: path
  130. // description: name of the organization
  131. // type: string
  132. // required: true
  133. // - name: id
  134. // in: path
  135. // description: id of the hook to delete
  136. // type: integer
  137. // required: true
  138. // responses:
  139. // "204":
  140. // "$ref": "#/responses/empty"
  141. org := ctx.Org.Organization
  142. hookID := ctx.ParamsInt64(":id")
  143. if err := models.DeleteWebhookByOrgID(org.ID, hookID); err != nil {
  144. if models.IsErrWebhookNotExist(err) {
  145. ctx.Status(404)
  146. } else {
  147. ctx.Error(500, "DeleteWebhookByOrgID", err)
  148. }
  149. return
  150. }
  151. ctx.Status(204)
  152. }