gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package user
  4. import (
  5. "net/http"
  6. api "code.gitea.io/gitea/modules/structs"
  7. "code.gitea.io/gitea/modules/web"
  8. "code.gitea.io/gitea/routers/api/v1/utils"
  9. "code.gitea.io/gitea/services/context"
  10. webhook_service "code.gitea.io/gitea/services/webhook"
  11. )
  12. // ListHooks list the authenticated user's webhooks
  13. func ListHooks(ctx *context.APIContext) {
  14. // swagger:operation GET /user/hooks user userListHooks
  15. // ---
  16. // summary: List the authenticated user's webhooks
  17. // produces:
  18. // - application/json
  19. // parameters:
  20. // - name: page
  21. // in: query
  22. // description: page number of results to return (1-based)
  23. // type: integer
  24. // - name: limit
  25. // in: query
  26. // description: page size of results
  27. // type: integer
  28. // responses:
  29. // "200":
  30. // "$ref": "#/responses/HookList"
  31. utils.ListOwnerHooks(
  32. ctx,
  33. ctx.Doer,
  34. )
  35. }
  36. // GetHook get the authenticated user's hook by id
  37. func GetHook(ctx *context.APIContext) {
  38. // swagger:operation GET /user/hooks/{id} user userGetHook
  39. // ---
  40. // summary: Get a hook
  41. // produces:
  42. // - application/json
  43. // parameters:
  44. // - name: id
  45. // in: path
  46. // description: id of the hook to get
  47. // type: integer
  48. // format: int64
  49. // required: true
  50. // responses:
  51. // "200":
  52. // "$ref": "#/responses/Hook"
  53. hook, err := utils.GetOwnerHook(ctx, ctx.Doer.ID, ctx.PathParamInt64("id"))
  54. if err != nil {
  55. return
  56. }
  57. if !ctx.Doer.IsAdmin && hook.OwnerID != ctx.Doer.ID {
  58. ctx.APIErrorNotFound()
  59. return
  60. }
  61. apiHook, err := webhook_service.ToHook(ctx.Doer.HomeLink(), hook)
  62. if err != nil {
  63. ctx.APIErrorInternal(err)
  64. return
  65. }
  66. ctx.JSON(http.StatusOK, apiHook)
  67. }
  68. // CreateHook create a hook for the authenticated user
  69. func CreateHook(ctx *context.APIContext) {
  70. // swagger:operation POST /user/hooks user userCreateHook
  71. // ---
  72. // summary: Create a hook
  73. // consumes:
  74. // - application/json
  75. // produces:
  76. // - application/json
  77. // parameters:
  78. // - name: body
  79. // in: body
  80. // required: true
  81. // schema:
  82. // "$ref": "#/definitions/CreateHookOption"
  83. // responses:
  84. // "201":
  85. // "$ref": "#/responses/Hook"
  86. utils.AddOwnerHook(
  87. ctx,
  88. ctx.Doer,
  89. web.GetForm(ctx).(*api.CreateHookOption),
  90. )
  91. }
  92. // EditHook modify a hook of the authenticated user
  93. func EditHook(ctx *context.APIContext) {
  94. // swagger:operation PATCH /user/hooks/{id} user userEditHook
  95. // ---
  96. // summary: Update a hook
  97. // consumes:
  98. // - application/json
  99. // produces:
  100. // - application/json
  101. // parameters:
  102. // - name: id
  103. // in: path
  104. // description: id of the hook to update
  105. // type: integer
  106. // format: int64
  107. // required: true
  108. // - name: body
  109. // in: body
  110. // schema:
  111. // "$ref": "#/definitions/EditHookOption"
  112. // responses:
  113. // "200":
  114. // "$ref": "#/responses/Hook"
  115. utils.EditOwnerHook(
  116. ctx,
  117. ctx.Doer,
  118. web.GetForm(ctx).(*api.EditHookOption),
  119. ctx.PathParamInt64("id"),
  120. )
  121. }
  122. // DeleteHook delete a hook of the authenticated user
  123. func DeleteHook(ctx *context.APIContext) {
  124. // swagger:operation DELETE /user/hooks/{id} user userDeleteHook
  125. // ---
  126. // summary: Delete a hook
  127. // produces:
  128. // - application/json
  129. // parameters:
  130. // - name: id
  131. // in: path
  132. // description: id of the hook to delete
  133. // type: integer
  134. // format: int64
  135. // required: true
  136. // responses:
  137. // "204":
  138. // "$ref": "#/responses/empty"
  139. utils.DeleteOwnerHook(
  140. ctx,
  141. ctx.Doer,
  142. ctx.PathParamInt64("id"),
  143. )
  144. }