aboutsummaryrefslogtreecommitdiffstats
path: root/models/webhook.go
diff options
context:
space:
mode:
authorRussell Aunger <rba@live.com>2019-03-18 22:33:20 -0400
committertechknowlogick <matti@mdranta.net>2019-03-18 22:33:20 -0400
commitb34996a62937b23121d19912b37ed2b1023f1479 (patch)
tree9a080ef145d738c71956279980bd7f56b70b6825 /models/webhook.go
parentcac9e6e7605184f5679b1ebfbe5b5805191d9a53 (diff)
downloadgitea-b34996a62937b23121d19912b37ed2b1023f1479.tar.gz
gitea-b34996a62937b23121d19912b37ed2b1023f1479.zip
Implement Default Webhooks (#4299)
Partially implement #770. Add "Default Webhooks" page in site admin UI. Persist to the existing webhooks table, but store with RepoID=0 and OrgID=0. Upon repo creation, copy the set of default webhooks into the new repo.
Diffstat (limited to 'models/webhook.go')
-rw-r--r--models/webhook.go73
1 files changed, 72 insertions, 1 deletions
diff --git a/models/webhook.go b/models/webhook.go
index a764455f5f..1c6fc45ae3 100644
--- a/models/webhook.go
+++ b/models/webhook.go
@@ -241,7 +241,11 @@ func (w *Webhook) EventsArray() []string {
// CreateWebhook creates a new web hook.
func CreateWebhook(w *Webhook) error {
- _, err := x.Insert(w)
+ return createWebhook(x, w)
+}
+
+func createWebhook(e Engine, w *Webhook) error {
+ _, err := e.Insert(w)
return err
}
@@ -316,6 +320,32 @@ func GetWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
return ws, err
}
+// GetDefaultWebhook returns admin-default webhook by given ID.
+func GetDefaultWebhook(id int64) (*Webhook, error) {
+ webhook := &Webhook{ID: id}
+ has, err := x.
+ Where("repo_id=? AND org_id=?", 0, 0).
+ Get(webhook)
+ if err != nil {
+ return nil, err
+ } else if !has {
+ return nil, ErrWebhookNotExist{id}
+ }
+ return webhook, nil
+}
+
+// GetDefaultWebhooks returns all admin-default webhooks.
+func GetDefaultWebhooks() ([]*Webhook, error) {
+ return getDefaultWebhooks(x)
+}
+
+func getDefaultWebhooks(e Engine) ([]*Webhook, error) {
+ webhooks := make([]*Webhook, 0, 5)
+ return webhooks, e.
+ Where("repo_id=? AND org_id=?", 0, 0).
+ Find(&webhooks)
+}
+
// UpdateWebhook updates information of webhook.
func UpdateWebhook(w *Webhook) error {
_, err := x.ID(w.ID).AllCols().Update(w)
@@ -364,6 +394,47 @@ func DeleteWebhookByOrgID(orgID, id int64) error {
})
}
+// DeleteDefaultWebhook deletes an admin-default webhook by given ID.
+func DeleteDefaultWebhook(id int64) error {
+ sess := x.NewSession()
+ defer sess.Close()
+ if err := sess.Begin(); err != nil {
+ return err
+ }
+
+ count, err := sess.
+ 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 := sess.Delete(&HookTask{HookID: id}); err != nil {
+ return err
+ }
+
+ return sess.Commit()
+}
+
+// copyDefaultWebhooksToRepo creates copies of the default webhooks in a new repo
+func copyDefaultWebhooksToRepo(e Engine, repoID int64) error {
+ ws, err := getDefaultWebhooks(e)
+ if err != nil {
+ return fmt.Errorf("GetDefaultWebhooks: %v", err)
+ }
+
+ for _, w := range ws {
+ w.ID = 0
+ w.RepoID = repoID
+ if err := createWebhook(e, w); err != nil {
+ return fmt.Errorf("CreateWebhook: %v", err)
+ }
+ }
+ return nil
+}
+
// ___ ___ __ ___________ __
// / | \ ____ ____ | | _\__ ___/____ _____| | __
// / ~ \/ _ \ / _ \| |/ / | | \__ \ / ___/ |/ /