You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

hook.go 4.4 kB

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