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 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. org := ctx.Org.Organization
  15. orgHooks, err := models.GetWebhooksByOrgID(org.ID)
  16. if err != nil {
  17. ctx.Error(500, "GetWebhooksByOrgID", err)
  18. return
  19. }
  20. hooks := make([]*api.Hook, len(orgHooks))
  21. for i, hook := range orgHooks {
  22. hooks[i] = convert.ToHook(org.HomeLink(), hook)
  23. }
  24. ctx.JSON(200, hooks)
  25. }
  26. // GetHook get an organization's hook by id
  27. func GetHook(ctx *context.APIContext) {
  28. org := ctx.Org.Organization
  29. hookID := ctx.ParamsInt64(":id")
  30. hook, err := utils.GetOrgHook(ctx, org.ID, hookID)
  31. if err != nil {
  32. return
  33. }
  34. ctx.JSON(200, convert.ToHook(org.HomeLink(), hook))
  35. }
  36. // CreateHook create a hook for an organization
  37. func CreateHook(ctx *context.APIContext, form api.CreateHookOption) {
  38. if !utils.CheckCreateHookOption(ctx, &form) {
  39. return
  40. }
  41. utils.AddOrgHook(ctx, &form)
  42. }
  43. // EditHook modify a hook of a repository
  44. func EditHook(ctx *context.APIContext, form api.EditHookOption) {
  45. hookID := ctx.ParamsInt64(":id")
  46. utils.EditOrgHook(ctx, &form, hookID)
  47. }
  48. // DeleteHook delete a hook of an organization
  49. func DeleteHook(ctx *context.APIContext) {
  50. org := ctx.Org.Organization
  51. hookID := ctx.ParamsInt64(":id")
  52. if err := models.DeleteWebhookByOrgID(org.ID, hookID); err != nil {
  53. ctx.Error(500, "DeleteWebhookByOrgID", err)
  54. return
  55. }
  56. ctx.Status(204)
  57. }