From b34996a62937b23121d19912b37ed2b1023f1479 Mon Sep 17 00:00:00 2001 From: Russell Aunger Date: Mon, 18 Mar 2019 22:33:20 -0400 Subject: 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. --- models/webhook.go | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) (limited to 'models/webhook.go') 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 +} + // ___ ___ __ ___________ __ // / | \ ____ ____ | | _\__ ___/____ _____| | __ // / ~ \/ _ \ / _ \| |/ / | | \__ \ / ___/ |/ / -- cgit v1.2.3