diff --git a/models/repo.go b/models/repo.go index cafde2936..9a85c77f0 100644 --- a/models/repo.go +++ b/models/repo.go @@ -1366,6 +1366,10 @@ func createRepository(e *xorm.Session, doer, u *User, repo *Repository) (err err return fmt.Errorf("newRepoAction: %v", err) } + if err = copyDefaultWebhooksToRepo(e, repo.ID); err != nil { + return fmt.Errorf("copyDefaultWebhooksToRepo: %v", err) + } + return nil } diff --git a/models/webhook.go b/models/webhook.go index a764455f5..1c6fc45ae 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 +} + // ___ ___ __ ___________ __ // / | \ ____ ____ | | _\__ ___/____ _____| | __ // / ~ \/ _ \ / _ \| |/ / | | \__ \ / ___/ |/ / diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 06e4e2c55..ffccad1d5 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1433,6 +1433,7 @@ dashboard = Dashboard users = User Accounts organizations = Organizations repositories = Repositories +hooks = Default Webhooks authentication = Authentication Sources config = Configuration notices = System Notices @@ -1546,6 +1547,10 @@ repos.forks = Forks repos.issues = Issues repos.size = Size +hooks.desc = Webhooks automatically make HTTP POST requests to a server when certain Gitea events trigger. Webhooks defined here are defaults and will be copied into all new repositories. Read more in the webhooks guide. +hooks.add_webhook = Add Default Webhook +hooks.update_webhook = Update Default Webhook + auths.auth_manage_panel = Authentication Source Management auths.new = Add Authentication Source auths.name = Name diff --git a/routers/admin/hooks.go b/routers/admin/hooks.go new file mode 100644 index 000000000..b80ed3cc3 --- /dev/null +++ b/routers/admin/hooks.go @@ -0,0 +1,47 @@ +// Copyright 2018 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package admin + +import ( + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/base" + "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/setting" +) + +const ( + // tplAdminHooks template path for render hook settings + tplAdminHooks base.TplName = "admin/hooks" +) + +// DefaultWebhooks render admin-default webhook list page +func DefaultWebhooks(ctx *context.Context) { + ctx.Data["Title"] = ctx.Tr("admin.hooks") + ctx.Data["PageIsAdminHooks"] = true + ctx.Data["BaseLink"] = setting.AppSubURL + "/admin/hooks" + ctx.Data["Description"] = ctx.Tr("admin.hooks.desc") + + ws, err := models.GetDefaultWebhooks() + if err != nil { + ctx.ServerError("GetWebhooksDefaults", err) + return + } + + ctx.Data["Webhooks"] = ws + ctx.HTML(200, tplAdminHooks) +} + +// DeleteDefaultWebhook response for delete admin-default webhook +func DeleteDefaultWebhook(ctx *context.Context) { + if err := models.DeleteDefaultWebhook(ctx.QueryInt64("id")); err != nil { + ctx.Flash.Error("DeleteDefaultWebhook: " + err.Error()) + } else { + ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success")) + } + + ctx.JSON(200, map[string]interface{}{ + "redirect": setting.AppSubURL + "/admin/hooks", + }) +} diff --git a/routers/org/setting.go b/routers/org/setting.go index 1c4e75288..0526a96e6 100644 --- a/routers/org/setting.go +++ b/routers/org/setting.go @@ -150,7 +150,7 @@ func SettingsDelete(ctx *context.Context) { func Webhooks(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("org.settings") ctx.Data["PageIsSettingsHooks"] = true - ctx.Data["BaseLink"] = ctx.Org.OrgLink + ctx.Data["BaseLink"] = ctx.Org.OrgLink + "/settings/hooks" ctx.Data["Description"] = ctx.Tr("org.settings.hooks_desc") ws, err := models.GetWebhooksByOrgID(ctx.Org.Organization.ID) diff --git a/routers/repo/webhook.go b/routers/repo/webhook.go index 6c69354c7..cea514535 100644 --- a/routers/repo/webhook.go +++ b/routers/repo/webhook.go @@ -9,6 +9,7 @@ import ( "encoding/json" "errors" "fmt" + "path" "strings" "code.gitea.io/git" @@ -23,16 +24,17 @@ import ( ) const ( - tplHooks base.TplName = "repo/settings/webhook/base" - tplHookNew base.TplName = "repo/settings/webhook/new" - tplOrgHookNew base.TplName = "org/settings/hook_new" + tplHooks base.TplName = "repo/settings/webhook/base" + tplHookNew base.TplName = "repo/settings/webhook/new" + tplOrgHookNew base.TplName = "org/settings/hook_new" + tplAdminHookNew base.TplName = "admin/hook_new" ) // Webhooks render web hooks list page func Webhooks(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("repo.settings.hooks") ctx.Data["PageIsSettingsHooks"] = true - ctx.Data["BaseLink"] = ctx.Repo.RepoLink + ctx.Data["BaseLink"] = ctx.Repo.RepoLink + "/settings/hooks" ctx.Data["Description"] = ctx.Tr("repo.settings.hooks_desc", "https://docs.gitea.io/en-us/webhooks/") ws, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID) @@ -48,16 +50,17 @@ func Webhooks(ctx *context.Context) { type orgRepoCtx struct { OrgID int64 RepoID int64 + IsAdmin bool Link string NewTemplate base.TplName } -// getOrgRepoCtx determines whether this is a repo context or organization context. +// getOrgRepoCtx determines whether this is a repo, organization, or admin context. func getOrgRepoCtx(ctx *context.Context) (*orgRepoCtx, error) { if len(ctx.Repo.RepoLink) > 0 { return &orgRepoCtx{ RepoID: ctx.Repo.Repository.ID, - Link: ctx.Repo.RepoLink, + Link: path.Join(ctx.Repo.RepoLink, "settings/hooks"), NewTemplate: tplHookNew, }, nil } @@ -65,11 +68,19 @@ func getOrgRepoCtx(ctx *context.Context) (*orgRepoCtx, error) { if len(ctx.Org.OrgLink) > 0 { return &orgRepoCtx{ OrgID: ctx.Org.Organization.ID, - Link: ctx.Org.OrgLink, + Link: path.Join(ctx.Org.OrgLink, "settings/hooks"), NewTemplate: tplOrgHookNew, }, nil } + if ctx.User.IsAdmin { + return &orgRepoCtx{ + IsAdmin: true, + Link: path.Join(setting.AppSubURL, "/admin/hooks"), + NewTemplate: tplAdminHookNew, + }, nil + } + return nil, errors.New("Unable to set OrgRepo context") } @@ -85,8 +96,6 @@ func checkHookType(ctx *context.Context) string { // WebhooksNew render creating webhook page func WebhooksNew(ctx *context.Context) { ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook") - ctx.Data["PageIsSettingsHooks"] = true - ctx.Data["PageIsSettingsHooksNew"] = true ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}} orCtx, err := getOrgRepoCtx(ctx) @@ -95,6 +104,14 @@ func WebhooksNew(ctx *context.Context) { return } + if orCtx.IsAdmin { + ctx.Data["PageIsAdminHooks"] = true + ctx.Data["PageIsAdminHooksNew"] = true + } else { + ctx.Data["PageIsSettingsHooks"] = true + ctx.Data["PageIsSettingsHooksNew"] = true + } + hookType := checkHookType(ctx) ctx.Data["HookType"] = hookType if ctx.Written() { @@ -175,7 +192,7 @@ func WebHooksNewPost(ctx *context.Context, form auth.NewWebhookForm) { } ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success")) - ctx.Redirect(orCtx.Link + "/settings/hooks") + ctx.Redirect(orCtx.Link) } // GogsHooksNewPost response for creating webhook @@ -222,7 +239,7 @@ func GogsHooksNewPost(ctx *context.Context, form auth.NewGogshookForm) { } ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success")) - ctx.Redirect(orCtx.Link + "/settings/hooks") + ctx.Redirect(orCtx.Link) } // DiscordHooksNewPost response for creating discord hook @@ -271,7 +288,7 @@ func DiscordHooksNewPost(ctx *context.Context, form auth.NewDiscordHookForm) { } ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success")) - ctx.Redirect(orCtx.Link + "/settings/hooks") + ctx.Redirect(orCtx.Link) } // DingtalkHooksNewPost response for creating dingtalk hook @@ -311,7 +328,7 @@ func DingtalkHooksNewPost(ctx *context.Context, form auth.NewDingtalkHookForm) { } ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success")) - ctx.Redirect(orCtx.Link + "/settings/hooks") + ctx.Redirect(orCtx.Link) } // SlackHooksNewPost response for creating slack hook @@ -368,7 +385,7 @@ func SlackHooksNewPost(ctx *context.Context, form auth.NewSlackHookForm) { } ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success")) - ctx.Redirect(orCtx.Link + "/settings/hooks") + ctx.Redirect(orCtx.Link) } func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) { @@ -384,8 +401,10 @@ func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) { var w *models.Webhook if orCtx.RepoID > 0 { w, err = models.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")) - } else { + } else if orCtx.OrgID > 0 { w, err = models.GetWebhookByOrgID(ctx.Org.Organization.ID, ctx.ParamsInt64(":id")) + } else { + w, err = models.GetDefaultWebhook(ctx.ParamsInt64(":id")) } if err != nil { if models.IsErrWebhookNotExist(err) { @@ -462,7 +481,7 @@ func WebHooksEditPost(ctx *context.Context, form auth.NewWebhookForm) { } ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success")) - ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID)) + ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID)) } // GogsHooksEditPost response for editing gogs hook @@ -501,7 +520,7 @@ func GogsHooksEditPost(ctx *context.Context, form auth.NewGogshookForm) { } ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success")) - ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID)) + ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID)) } // SlackHooksEditPost response for editing slack hook @@ -551,7 +570,7 @@ func SlackHooksEditPost(ctx *context.Context, form auth.NewSlackHookForm) { } ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success")) - ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID)) + ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID)) } // DiscordHooksEditPost response for editing discord hook @@ -593,7 +612,7 @@ func DiscordHooksEditPost(ctx *context.Context, form auth.NewDiscordHookForm) { } ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success")) - ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID)) + ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID)) } // DingtalkHooksEditPost response for editing discord hook @@ -625,7 +644,7 @@ func DingtalkHooksEditPost(ctx *context.Context, form auth.NewDingtalkHookForm) } ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success")) - ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, w.ID)) + ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID)) } // TestWebhook test if web hook is work fine diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 5f55d6cf9..fafef0830 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -373,6 +373,23 @@ func RegisterRoutes(m *macaron.Macaron) { m.Post("/delete", admin.DeleteRepo) }) + m.Group("/hooks", func() { + m.Get("", admin.DefaultWebhooks) + m.Post("/delete", admin.DeleteDefaultWebhook) + m.Get("/:type/new", repo.WebhooksNew) + m.Post("/gitea/new", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksNewPost) + m.Post("/gogs/new", bindIgnErr(auth.NewGogshookForm{}), repo.GogsHooksNewPost) + m.Post("/slack/new", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksNewPost) + m.Post("/discord/new", bindIgnErr(auth.NewDiscordHookForm{}), repo.DiscordHooksNewPost) + m.Post("/dingtalk/new", bindIgnErr(auth.NewDingtalkHookForm{}), repo.DingtalkHooksNewPost) + m.Get("/:id", repo.WebHooksEdit) + m.Post("/gitea/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost) + m.Post("/gogs/:id", bindIgnErr(auth.NewGogshookForm{}), repo.GogsHooksEditPost) + m.Post("/slack/:id", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksEditPost) + m.Post("/discord/:id", bindIgnErr(auth.NewDiscordHookForm{}), repo.DiscordHooksEditPost) + m.Post("/dingtalk/:id", bindIgnErr(auth.NewDingtalkHookForm{}), repo.DingtalkHooksEditPost) + }) + m.Group("/auths", func() { m.Get("", admin.Authentications) m.Combo("/new").Get(admin.NewAuthSource).Post(bindIgnErr(auth.AuthenticationForm{}), admin.NewAuthSourcePost) diff --git a/templates/admin/hook_new.tmpl b/templates/admin/hook_new.tmpl new file mode 100644 index 000000000..c9ee14a98 --- /dev/null +++ b/templates/admin/hook_new.tmpl @@ -0,0 +1,37 @@ +{{template "base/head" .}} +
{{.i18n.Tr "repo.settings.add_dingtalk_hook_desc" "https://dingtalk.com" | Str2html}}
-