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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // responses:
  20. // "200":
  21. // "$ref": "#/responses/HookList"
  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:operation GET /orgs/{org}/hooks/{id} organization orgGetHook
  37. // ---
  38. // summary: Get a hook
  39. // produces:
  40. // - application/json
  41. // responses:
  42. // "200":
  43. // "$ref": "#/responses/Hook"
  44. org := ctx.Org.Organization
  45. hookID := ctx.ParamsInt64(":id")
  46. hook, err := utils.GetOrgHook(ctx, org.ID, hookID)
  47. if err != nil {
  48. return
  49. }
  50. ctx.JSON(200, convert.ToHook(org.HomeLink(), hook))
  51. }
  52. // CreateHook create a hook for an organization
  53. func CreateHook(ctx *context.APIContext, form api.CreateHookOption) {
  54. // swagger:operation POST /orgs/{org}/hooks/ organization orgCreateHook
  55. // ---
  56. // summary: Create a hook
  57. // consumes:
  58. // - application/json
  59. // produces:
  60. // - application/json
  61. // responses:
  62. // "201":
  63. // "$ref": "#/responses/Hook"
  64. if !utils.CheckCreateHookOption(ctx, &form) {
  65. return
  66. }
  67. utils.AddOrgHook(ctx, &form)
  68. }
  69. // EditHook modify a hook of a repository
  70. func EditHook(ctx *context.APIContext, form api.EditHookOption) {
  71. // swagger:operation PATCH /orgs/{org}/hooks/{id} organization orgEditHook
  72. // ---
  73. // summary: Update a hook
  74. // consumes:
  75. // - application/json
  76. // produces:
  77. // - application/json
  78. // responses:
  79. // "200":
  80. // "$ref": "#/responses/Hook"
  81. hookID := ctx.ParamsInt64(":id")
  82. utils.EditOrgHook(ctx, &form, hookID)
  83. }
  84. // DeleteHook delete a hook of an organization
  85. func DeleteHook(ctx *context.APIContext) {
  86. // swagger:operation DELETE /orgs/{org}/hooks/{id} organization orgDeleteHook
  87. // ---
  88. // summary: Delete a hook
  89. // produces:
  90. // - application/json
  91. // responses:
  92. // "204":
  93. // "$ref": "#/responses/empty"
  94. org := ctx.Org.Organization
  95. hookID := ctx.ParamsInt64(":id")
  96. if err := models.DeleteWebhookByOrgID(org.ID, hookID); err != nil {
  97. if models.IsErrWebhookNotExist(err) {
  98. ctx.Status(404)
  99. } else {
  100. ctx.Error(500, "DeleteWebhookByOrgID", err)
  101. }
  102. return
  103. }
  104. ctx.Status(204)
  105. }