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 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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:route GET /orgs/{orgname}/hooks organization orgListHooks
  15. //
  16. // Produces:
  17. // - application/json
  18. //
  19. // Responses:
  20. // 200: HookList
  21. // 500: error
  22. org := ctx.Org.Organization
  23. orgHooks, err := models.GetWebhooksByOrgID(org.ID)
  24. if err != nil {
  25. ctx.Error(500, "GetWebhooksByOrgID", err)
  26. return
  27. }
  28. hooks := make([]*api.Hook, len(orgHooks))
  29. for i, hook := range orgHooks {
  30. hooks[i] = convert.ToHook(org.HomeLink(), hook)
  31. }
  32. ctx.JSON(200, hooks)
  33. }
  34. // GetHook get an organization's hook by id
  35. func GetHook(ctx *context.APIContext) {
  36. // swagger:route GET /orgs/{orgname}/hooks/{id} organization orgGetHook
  37. //
  38. // Produces:
  39. // - application/json
  40. //
  41. // Responses:
  42. // 200: Hook
  43. // 404: notFound
  44. // 500: error
  45. org := ctx.Org.Organization
  46. hookID := ctx.ParamsInt64(":id")
  47. hook, err := utils.GetOrgHook(ctx, org.ID, hookID)
  48. if err != nil {
  49. return
  50. }
  51. ctx.JSON(200, convert.ToHook(org.HomeLink(), hook))
  52. }
  53. // CreateHook create a hook for an organization
  54. func CreateHook(ctx *context.APIContext, form api.CreateHookOption) {
  55. // swagger:route POST /orgs/{orgname}/hooks/ organization orgCreateHook
  56. //
  57. // Consumes:
  58. // - application/json
  59. //
  60. // Produces:
  61. // - application/json
  62. //
  63. // Responses:
  64. // 201: Hook
  65. // 422: validationError
  66. // 500: error
  67. if !utils.CheckCreateHookOption(ctx, &form) {
  68. return
  69. }
  70. utils.AddOrgHook(ctx, &form)
  71. }
  72. // EditHook modify a hook of a repository
  73. func EditHook(ctx *context.APIContext, form api.EditHookOption) {
  74. // swagger:route PATCH /orgs/{orgname}/hooks/{id} organization orgEditHook
  75. //
  76. // Consumes:
  77. // - application/json
  78. //
  79. // Produces:
  80. // - application/json
  81. //
  82. // Responses:
  83. // 200: Hook
  84. // 422: validationError
  85. // 404: notFound
  86. // 500: error
  87. hookID := ctx.ParamsInt64(":id")
  88. utils.EditOrgHook(ctx, &form, hookID)
  89. }
  90. // DeleteHook delete a hook of an organization
  91. func DeleteHook(ctx *context.APIContext) {
  92. // swagger:route DELETE /orgs/{orgname}/hooks/{id} organization orgDeleteHook
  93. //
  94. // Produces:
  95. // - application/json
  96. //
  97. // Responses:
  98. // 204: empty
  99. // 404: notFound
  100. // 500: error
  101. org := ctx.Org.Organization
  102. hookID := ctx.ParamsInt64(":id")
  103. if err := models.DeleteWebhookByOrgID(org.ID, hookID); err != nil {
  104. if models.IsErrWebhookNotExist(err) {
  105. ctx.Status(404)
  106. } else {
  107. ctx.Error(500, "DeleteWebhookByOrgID", err)
  108. }
  109. return
  110. }
  111. ctx.Status(204)
  112. }