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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. // 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. }