gitea源码

webhook_system.go 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package webhook
  4. import (
  5. "context"
  6. "fmt"
  7. "code.gitea.io/gitea/models/db"
  8. "code.gitea.io/gitea/modules/optional"
  9. )
  10. // GetSystemOrDefaultWebhooks returns webhooks by given argument or all if argument is missing.
  11. func GetSystemOrDefaultWebhooks(ctx context.Context, isSystemWebhook optional.Option[bool]) ([]*Webhook, error) {
  12. webhooks := make([]*Webhook, 0, 5)
  13. if !isSystemWebhook.Has() {
  14. return webhooks, db.GetEngine(ctx).Where("repo_id=? AND owner_id=?", 0, 0).
  15. Find(&webhooks)
  16. }
  17. return webhooks, db.GetEngine(ctx).
  18. Where("repo_id=? AND owner_id=? AND is_system_webhook=?", 0, 0, isSystemWebhook.Value()).
  19. Find(&webhooks)
  20. }
  21. // GetDefaultWebhooks returns all admin-default webhooks.
  22. func GetDefaultWebhooks(ctx context.Context) ([]*Webhook, error) {
  23. webhooks := make([]*Webhook, 0, 5)
  24. return webhooks, db.GetEngine(ctx).
  25. Where("repo_id=? AND owner_id=? AND is_system_webhook=?", 0, 0, false).
  26. Find(&webhooks)
  27. }
  28. // GetSystemOrDefaultWebhook returns admin system or default webhook by given ID.
  29. func GetSystemOrDefaultWebhook(ctx context.Context, id int64) (*Webhook, error) {
  30. webhook := &Webhook{ID: id}
  31. has, err := db.GetEngine(ctx).
  32. Where("repo_id=? AND owner_id=?", 0, 0).
  33. Get(webhook)
  34. if err != nil {
  35. return nil, err
  36. } else if !has {
  37. return nil, ErrWebhookNotExist{ID: id}
  38. }
  39. return webhook, nil
  40. }
  41. // GetSystemWebhooks returns all admin system webhooks.
  42. func GetSystemWebhooks(ctx context.Context, isActive optional.Option[bool]) ([]*Webhook, error) {
  43. webhooks := make([]*Webhook, 0, 5)
  44. if !isActive.Has() {
  45. return webhooks, db.GetEngine(ctx).
  46. Where("repo_id=? AND owner_id=? AND is_system_webhook=?", 0, 0, true).
  47. Find(&webhooks)
  48. }
  49. return webhooks, db.GetEngine(ctx).
  50. Where("repo_id=? AND owner_id=? AND is_system_webhook=? AND is_active = ?", 0, 0, true, isActive.Value()).
  51. Find(&webhooks)
  52. }
  53. // DeleteDefaultSystemWebhook deletes an admin-configured default or system webhook (where Org and Repo ID both 0)
  54. func DeleteDefaultSystemWebhook(ctx context.Context, id int64) error {
  55. return db.WithTx(ctx, func(ctx context.Context) error {
  56. count, err := db.GetEngine(ctx).
  57. Where("repo_id=? AND owner_id=?", 0, 0).
  58. Delete(&Webhook{ID: id})
  59. if err != nil {
  60. return err
  61. } else if count == 0 {
  62. return ErrWebhookNotExist{ID: id}
  63. }
  64. _, err = db.DeleteByBean(ctx, &HookTask{HookID: id})
  65. return err
  66. })
  67. }
  68. // CopyDefaultWebhooksToRepo creates copies of the default webhooks in a new repo
  69. func CopyDefaultWebhooksToRepo(ctx context.Context, repoID int64) error {
  70. ws, err := GetDefaultWebhooks(ctx)
  71. if err != nil {
  72. return fmt.Errorf("GetDefaultWebhooks: %v", err)
  73. }
  74. for _, w := range ws {
  75. w.ID = 0
  76. w.RepoID = repoID
  77. if err := CreateWebhook(ctx, w); err != nil {
  78. return fmt.Errorf("CreateWebhook: %v", err)
  79. }
  80. }
  81. return nil
  82. }