diff --git a/models/repo.go b/models/repo.go index cafde29363..9a85c77f0c 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 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 +} + // ___ ___ __ ___________ __ // / | \ ____ ____ | | _\__ ___/____ _____| | __ // / ~ \/ _ \ / _ \| |/ / | | \__ \ / ___/ |/ / diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 06e4e2c552..ffccad1d50 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 0000000000..b80ed3cc3c --- /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 1c4e75288a..0526a96e6f 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 6c69354c70..cea5145353 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 5f55d6cf9d..fafef0830e 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 0000000000..c9ee14a982 --- /dev/null +++ b/templates/admin/hook_new.tmpl @@ -0,0 +1,37 @@ +{{template "base/head" .}} +
+ {{template "admin/navbar" .}} +
+ {{template "base/alert" .}} +

+ {{if .PageIsAdminHooksNew}} + {{.i18n.Tr "admin.hooks.add_webhook"}} + {{else}} + {{.i18n.Tr "admin.hooks.update_webhook"}} + {{end}} +
+ {{if eq .HookType "gitea"}} + + {{else if eq .HookType "gogs"}} + + {{else if eq .HookType "slack"}} + + {{else if eq .HookType "discord"}} + + {{else if eq .HookType "dingtalk"}} + + {{end}} +
+

+
+ {{template "repo/settings/webhook/gitea" .}} + {{template "repo/settings/webhook/gogs" .}} + {{template "repo/settings/webhook/slack" .}} + {{template "repo/settings/webhook/discord" .}} + {{template "repo/settings/webhook/dingtalk" .}} +
+ + {{template "repo/settings/webhook/history" .}} +
+
+{{template "base/footer" .}} diff --git a/templates/admin/hooks.tmpl b/templates/admin/hooks.tmpl new file mode 100644 index 0000000000..3265a1d69a --- /dev/null +++ b/templates/admin/hooks.tmpl @@ -0,0 +1,8 @@ +{{template "base/head" .}} +
+ {{template "admin/navbar" .}} +
+ {{template "repo/settings/webhook/list" .}} +
+
+{{template "base/footer" .}} diff --git a/templates/admin/nav.tmpl b/templates/admin/nav.tmpl index 7129ee11ed..d95a0d7ecf 100644 --- a/templates/admin/nav.tmpl +++ b/templates/admin/nav.tmpl @@ -6,6 +6,7 @@
  • {{.i18n.Tr "admin.users"}}
  • {{.i18n.Tr "admin.organizations"}}
  • {{.i18n.Tr "admin.repositories"}}
  • +
  • {{.i18n.Tr "admin.hooks"}}
  • {{.i18n.Tr "admin.authentication"}}
  • {{.i18n.Tr "admin.config"}}
  • {{.i18n.Tr "admin.notices"}}
  • diff --git a/templates/admin/navbar.tmpl b/templates/admin/navbar.tmpl index c5ad124157..caa8c1f323 100644 --- a/templates/admin/navbar.tmpl +++ b/templates/admin/navbar.tmpl @@ -11,6 +11,9 @@ {{.i18n.Tr "admin.repositories"}} + + {{.i18n.Tr "admin.hooks"}} + {{.i18n.Tr "admin.authentication"}} @@ -23,4 +26,4 @@ {{.i18n.Tr "admin.monitor"}} - \ No newline at end of file + diff --git a/templates/repo/settings/webhook/dingtalk.tmpl b/templates/repo/settings/webhook/dingtalk.tmpl index 3e6504f651..7a82c03eb4 100644 --- a/templates/repo/settings/webhook/dingtalk.tmpl +++ b/templates/repo/settings/webhook/dingtalk.tmpl @@ -1,6 +1,6 @@ {{if eq .HookType "dingtalk"}}

    {{.i18n.Tr "repo.settings.add_dingtalk_hook_desc" "https://dingtalk.com" | Str2html}}

    -
    + {{.CsrfTokenHtml}}
    diff --git a/templates/repo/settings/webhook/discord.tmpl b/templates/repo/settings/webhook/discord.tmpl index 75c31efb51..01f6f08f69 100644 --- a/templates/repo/settings/webhook/discord.tmpl +++ b/templates/repo/settings/webhook/discord.tmpl @@ -1,6 +1,6 @@ {{if eq .HookType "discord"}}

    {{.i18n.Tr "repo.settings.add_discord_hook_desc" "https://discordapp.com" | Str2html}}

    - + {{.CsrfTokenHtml}}
    diff --git a/templates/repo/settings/webhook/gitea.tmpl b/templates/repo/settings/webhook/gitea.tmpl index 87a8813d0e..605022256d 100644 --- a/templates/repo/settings/webhook/gitea.tmpl +++ b/templates/repo/settings/webhook/gitea.tmpl @@ -1,6 +1,6 @@ {{if eq .HookType "gitea"}}

    {{.i18n.Tr "repo.settings.add_webhook_desc" "https://docs.gitea.io/en-us/webhooks/" | Str2html}}

    - + {{.CsrfTokenHtml}}
    diff --git a/templates/repo/settings/webhook/gogs.tmpl b/templates/repo/settings/webhook/gogs.tmpl index 649fb54aea..203a0a7954 100644 --- a/templates/repo/settings/webhook/gogs.tmpl +++ b/templates/repo/settings/webhook/gogs.tmpl @@ -1,6 +1,6 @@ {{if eq .HookType "gogs"}}

    {{.i18n.Tr "repo.settings.add_webhook_desc" "https://docs.gitea.io/en-us/webhooks/" | Str2html}}

    - + {{.CsrfTokenHtml}}
    diff --git a/templates/repo/settings/webhook/list.tmpl b/templates/repo/settings/webhook/list.tmpl index d98976cf5b..d2985c3676 100644 --- a/templates/repo/settings/webhook/list.tmpl +++ b/templates/repo/settings/webhook/list.tmpl @@ -5,19 +5,19 @@ diff --git a/templates/repo/settings/webhook/settings.tmpl b/templates/repo/settings/webhook/settings.tmpl index f04c25a0a3..3f9f739775 100644 --- a/templates/repo/settings/webhook/settings.tmpl +++ b/templates/repo/settings/webhook/settings.tmpl @@ -1,9 +1,10 @@ +{{$isNew:=or .PageIsSettingsHooksNew .PageIsAdminHooksNew}}

    {{.i18n.Tr "repo.settings.event_desc"}}

    - +
    @@ -119,17 +120,17 @@
    - + {{.i18n.Tr "repo.settings.active_helper"}}
    - {{if .PageIsSettingsHooksNew}} + {{if $isNew}} {{else}} - {{.i18n.Tr "repo.settings.delete_webhook"}} + {{.i18n.Tr "repo.settings.delete_webhook"}} {{end}}
    diff --git a/templates/repo/settings/webhook/slack.tmpl b/templates/repo/settings/webhook/slack.tmpl index c35a679da7..d7b6eebf3b 100644 --- a/templates/repo/settings/webhook/slack.tmpl +++ b/templates/repo/settings/webhook/slack.tmpl @@ -1,6 +1,6 @@ {{if eq .HookType "slack"}}

    {{.i18n.Tr "repo.settings.add_slack_hook_desc" "http://slack.com" | Str2html}}

    - + {{.CsrfTokenHtml}}