summaryrefslogtreecommitdiffstats
path: root/models
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2023-01-29 02:12:10 +0800
committerGitHub <noreply@github.com>2023-01-28 19:12:10 +0100
commitc0015979a692b795bcf7416196bec01c375d7aa2 (patch)
tree482f23fde88df51f20530b394358b7af5a50e141 /models
parent78e6b21c1a4c9867dd3054d6c167cc80407b020d (diff)
downloadgitea-c0015979a692b795bcf7416196bec01c375d7aa2.tar.gz
gitea-c0015979a692b795bcf7416196bec01c375d7aa2.zip
Support system hook API (#14537)
This add system hook API
Diffstat (limited to 'models')
-rw-r--r--models/webhook/webhook.go76
-rw-r--r--models/webhook/webhook_system.go81
2 files changed, 81 insertions, 76 deletions
diff --git a/models/webhook/webhook.go b/models/webhook/webhook.go
index c24404c42c..64119f1494 100644
--- a/models/webhook/webhook.go
+++ b/models/webhook/webhook.go
@@ -463,41 +463,6 @@ func CountWebhooksByOpts(opts *ListWebhookOptions) (int64, error) {
return db.GetEngine(db.DefaultContext).Where(opts.toCond()).Count(&Webhook{})
}
-// GetDefaultWebhooks returns all admin-default webhooks.
-func GetDefaultWebhooks(ctx context.Context) ([]*Webhook, error) {
- webhooks := make([]*Webhook, 0, 5)
- return webhooks, db.GetEngine(ctx).
- Where("repo_id=? AND org_id=? AND is_system_webhook=?", 0, 0, false).
- Find(&webhooks)
-}
-
-// GetSystemOrDefaultWebhook returns admin system or default webhook by given ID.
-func GetSystemOrDefaultWebhook(id int64) (*Webhook, error) {
- webhook := &Webhook{ID: id}
- has, err := db.GetEngine(db.DefaultContext).
- Where("repo_id=? AND org_id=?", 0, 0).
- Get(webhook)
- if err != nil {
- return nil, err
- } else if !has {
- return nil, ErrWebhookNotExist{ID: id}
- }
- return webhook, nil
-}
-
-// GetSystemWebhooks returns all admin system webhooks.
-func GetSystemWebhooks(ctx context.Context, isActive util.OptionalBool) ([]*Webhook, error) {
- webhooks := make([]*Webhook, 0, 5)
- if isActive.IsNone() {
- return webhooks, db.GetEngine(ctx).
- Where("repo_id=? AND org_id=? AND is_system_webhook=?", 0, 0, true).
- Find(&webhooks)
- }
- return webhooks, db.GetEngine(ctx).
- Where("repo_id=? AND org_id=? AND is_system_webhook=? AND is_active = ?", 0, 0, true, isActive.IsTrue()).
- Find(&webhooks)
-}
-
// UpdateWebhook updates information of webhook.
func UpdateWebhook(w *Webhook) error {
_, err := db.GetEngine(db.DefaultContext).ID(w.ID).AllCols().Update(w)
@@ -545,44 +510,3 @@ func DeleteWebhookByOrgID(orgID, id int64) error {
OrgID: orgID,
})
}
-
-// DeleteDefaultSystemWebhook deletes an admin-configured default or system webhook (where Org and Repo ID both 0)
-func DeleteDefaultSystemWebhook(id int64) error {
- ctx, committer, err := db.TxContext(db.DefaultContext)
- if err != nil {
- return err
- }
- defer committer.Close()
-
- count, err := db.GetEngine(ctx).
- Where("repo_id=? AND org_id=?", 0, 0).
- Delete(&Webhook{ID: id})
- if err != nil {
- return err
- } else if count == 0 {
- return ErrWebhookNotExist{ID: id}
- }
-
- if _, err := db.DeleteByBean(ctx, &HookTask{HookID: id}); err != nil {
- return err
- }
-
- return committer.Commit()
-}
-
-// CopyDefaultWebhooksToRepo creates copies of the default webhooks in a new repo
-func CopyDefaultWebhooksToRepo(ctx context.Context, repoID int64) error {
- ws, err := GetDefaultWebhooks(ctx)
- if err != nil {
- return fmt.Errorf("GetDefaultWebhooks: %w", err)
- }
-
- for _, w := range ws {
- w.ID = 0
- w.RepoID = repoID
- if err := CreateWebhook(ctx, w); err != nil {
- return fmt.Errorf("CreateWebhook: %w", err)
- }
- }
- return nil
-}
diff --git a/models/webhook/webhook_system.go b/models/webhook/webhook_system.go
new file mode 100644
index 0000000000..21dc0406a0
--- /dev/null
+++ b/models/webhook/webhook_system.go
@@ -0,0 +1,81 @@
+// Copyright 2021 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package webhook
+
+import (
+ "context"
+ "fmt"
+
+ "code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/modules/util"
+)
+
+// GetDefaultWebhooks returns all admin-default webhooks.
+func GetDefaultWebhooks(ctx context.Context) ([]*Webhook, error) {
+ webhooks := make([]*Webhook, 0, 5)
+ return webhooks, db.GetEngine(ctx).
+ Where("repo_id=? AND org_id=? AND is_system_webhook=?", 0, 0, false).
+ Find(&webhooks)
+}
+
+// GetSystemOrDefaultWebhook returns admin system or default webhook by given ID.
+func GetSystemOrDefaultWebhook(ctx context.Context, id int64) (*Webhook, error) {
+ webhook := &Webhook{ID: id}
+ has, err := db.GetEngine(ctx).
+ Where("repo_id=? AND org_id=?", 0, 0).
+ Get(webhook)
+ if err != nil {
+ return nil, err
+ } else if !has {
+ return nil, ErrWebhookNotExist{ID: id}
+ }
+ return webhook, nil
+}
+
+// GetSystemWebhooks returns all admin system webhooks.
+func GetSystemWebhooks(ctx context.Context, isActive util.OptionalBool) ([]*Webhook, error) {
+ webhooks := make([]*Webhook, 0, 5)
+ if isActive.IsNone() {
+ return webhooks, db.GetEngine(ctx).
+ Where("repo_id=? AND org_id=? AND is_system_webhook=?", 0, 0, true).
+ Find(&webhooks)
+ }
+ return webhooks, db.GetEngine(ctx).
+ Where("repo_id=? AND org_id=? AND is_system_webhook=? AND is_active = ?", 0, 0, true, isActive.IsTrue()).
+ Find(&webhooks)
+}
+
+// DeleteDefaultSystemWebhook deletes an admin-configured default or system webhook (where Org and Repo ID both 0)
+func DeleteDefaultSystemWebhook(ctx context.Context, id int64) error {
+ return db.WithTx(ctx, func(ctx context.Context) error {
+ count, err := db.GetEngine(ctx).
+ Where("repo_id=? AND org_id=?", 0, 0).
+ Delete(&Webhook{ID: id})
+ if err != nil {
+ return err
+ } else if count == 0 {
+ return ErrWebhookNotExist{ID: id}
+ }
+
+ _, err = db.DeleteByBean(ctx, &HookTask{HookID: id})
+ return err
+ })
+}
+
+// CopyDefaultWebhooksToRepo creates copies of the default webhooks in a new repo
+func CopyDefaultWebhooksToRepo(ctx context.Context, repoID int64) error {
+ ws, err := GetDefaultWebhooks(ctx)
+ if err != nil {
+ return fmt.Errorf("GetDefaultWebhooks: %v", err)
+ }
+
+ for _, w := range ws {
+ w.ID = 0
+ w.RepoID = repoID
+ if err := CreateWebhook(ctx, w); err != nil {
+ return fmt.Errorf("CreateWebhook: %v", err)
+ }
+ }
+ return nil
+}