From 8d2fe064c5d3858d76ebd67d5597a0bfccce4ecc Mon Sep 17 00:00:00 2001 From: Christopher Brickley Date: Wed, 3 Sep 2014 22:13:09 -0400 Subject: [PATCH 1/4] modify grammar undo -> undone --- conf/locale/locale_en-US.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conf/locale/locale_en-US.ini b/conf/locale/locale_en-US.ini index 946d56046..4f1acdcda 100644 --- a/conf/locale/locale_en-US.ini +++ b/conf/locale/locale_en-US.ini @@ -159,7 +159,7 @@ unbind = Unbind unbind_success = Social account has been unbound. delete_account = Delete Your Account -delete_prompt = The operation will delete your account permanently, and CANNOT be undo! +delete_prompt = The operation will delete your account permanently, and CANNOT be undone! confirm_delete_account = Confirm Deletion [repo] @@ -268,7 +268,7 @@ settings.update_settings = Update Settings settings.update_setting_success = Organization setting has been successfully updated. settings.delete = Delete Organization settings.delete_account = Delete This Organization -settings.delete_prompt = The operation will delete this organization permanently, and CANNOT be undo! +settings.delete_prompt = The operation will delete this organization permanently, and CANNOT be undone! settings.confirm_delete_account = Confirm Deletion members.public = Public From 7269b06fd5992776c07ae0303f85e0d05b1e62e9 Mon Sep 17 00:00:00 2001 From: Christopher Brickley Date: Wed, 3 Sep 2014 22:14:55 -0400 Subject: [PATCH 2/4] HookType is case-sensitive MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update webhook wasn’t showing up because of the wrong case --- routers/repo/setting.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/repo/setting.go b/routers/repo/setting.go index fba9eed6a..745678122 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -354,11 +354,11 @@ func WebHooksEdit(ctx *middleware.Context) { case models.SLACK: { ctx.Data["SlackHook"] = w.GetSlackHook() - ctx.Data["HookType"] = "slack" + ctx.Data["HookType"] = "Slack" } default: { - ctx.Data["HookType"] = "gogs" + ctx.Data["HookType"] = "Gogs" } } w.GetEvent() From 85c35a6b8bb7430568d375d1e792e1417bbd7f4b Mon Sep 17 00:00:00 2001 From: Christopher Brickley Date: Thu, 4 Sep 2014 07:17:00 -0400 Subject: [PATCH 3/4] add organization-level webhooks --- cmd/web.go | 7 ++++ conf/locale/locale_en-US.ini | 1 + models/action.go | 16 ++++++- models/webhook.go | 13 ++++++ public/ng/js/gogs.js | 29 ++++++++----- routers/org/setting.go | 28 +++++++++++++ routers/repo/setting.go | 56 +++++++++++++++++++++---- templates/org/settings/hook_new.tmpl | 37 ++++++++++++++++ templates/org/settings/hooks.tmpl | 38 +++++++++++++++++ templates/org/settings/nav.tmpl | 3 +- templates/repo/settings/hook_gogs.tmpl | 2 +- templates/repo/settings/hook_slack.tmpl | 2 +- 12 files changed, 208 insertions(+), 24 deletions(-) create mode 100644 templates/org/settings/hook_new.tmpl create mode 100644 templates/org/settings/hooks.tmpl diff --git a/cmd/web.go b/cmd/web.go index 57164683a..f7b8d9212 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -260,6 +260,13 @@ func runWeb(*cli.Context) { m.Group("/settings", func(r *macaron.Router) { r.Get("", org.Settings) r.Post("", bindIgnErr(auth.UpdateOrgSettingForm{}), org.SettingsPost) + r.Get("/hooks", org.SettingsHooks) + r.Get("/hooks/new", repo.WebHooksNew) + r.Post("/hooks/gogs/new", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksNewPost) + r.Post("/hooks/slack/new", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksNewPost) + r.Get("/hooks/:id", repo.WebHooksEdit) + r.Post("/hooks/gogs/:id", bindIgnErr(auth.NewWebhookForm{}), repo.WebHooksEditPost) + r.Post("/hooks/slack/:id", bindIgnErr(auth.NewSlackHookForm{}), repo.SlackHooksEditPost) r.Route("/delete", "GET,POST", org.SettingsDelete) }) diff --git a/conf/locale/locale_en-US.ini b/conf/locale/locale_en-US.ini index 4f1acdcda..3969074ee 100644 --- a/conf/locale/locale_en-US.ini +++ b/conf/locale/locale_en-US.ini @@ -270,6 +270,7 @@ settings.delete = Delete Organization settings.delete_account = Delete This Organization settings.delete_prompt = The operation will delete this organization permanently, and CANNOT be undone! settings.confirm_delete_account = Confirm Deletion +settings.hooks_desc = Add webhooks that will be triggered for all repositories under this organization. members.public = Public members.public_helper = make private diff --git a/models/action.go b/models/action.go index f739fc353..c0992dba6 100644 --- a/models/action.go +++ b/models/action.go @@ -220,8 +220,20 @@ func CommitRepoAction(userId, repoUserId int64, userName, actEmail string, ws, err := GetActiveWebhooksByRepoId(repoId) if err != nil { - return errors.New("action.CommitRepoAction(GetWebhooksByRepoId): " + err.Error()) - } else if len(ws) == 0 { + return errors.New("action.CommitRepoAction(GetActiveWebhooksByRepoId): " + err.Error()) + } + + // check if repo belongs to org and append additional webhooks + if repo.Owner.IsOrganization() { + // get hooks for org + orgws, err := GetActiveWebhooksByOrgId(repo.OwnerId) + if err != nil { + return errors.New("action.CommitRepoAction(GetActiveWebhooksByOrgId): " + err.Error()) + } + ws = append(ws, orgws...) + } + + if len(ws) == 0 { return nil } diff --git a/models/webhook.go b/models/webhook.go index 0b7b3a994..5acc83f59 100644 --- a/models/webhook.go +++ b/models/webhook.go @@ -45,6 +45,7 @@ type Webhook struct { IsActive bool HookTaskType HookTaskType Meta string `xorm:"TEXT"` // store hook-specific attributes + OrgId int64 } // GetEvent handles conversion from Events to HookEvent. @@ -120,6 +121,18 @@ func DeleteWebhook(hookId int64) error { return err } +// GetWebhooksByOrgId returns all webhooks for an organization. +func GetWebhooksByOrgId(orgId int64) (ws []*Webhook, err error) { + err = x.Find(&ws, &Webhook{OrgId: orgId}) + return ws, err +} + +// GetActiveWebhooksByOrgId returns all active webhooks for an organization. +func GetActiveWebhooksByOrgId(orgId int64) (ws []*Webhook, err error) { + err = x.Find(&ws, &Webhook{OrgId: orgId, IsActive: true}) + return ws, err +} + // ___ ___ __ ___________ __ // / | \ ____ ____ | | _\__ ___/____ _____| | __ // / ~ \/ _ \ / _ \| |/ / | | \__ \ / ___/ |/ / diff --git a/public/ng/js/gogs.js b/public/ng/js/gogs.js index c08a887a4..c60a5cf64 100644 --- a/public/ng/js/gogs.js +++ b/public/ng/js/gogs.js @@ -349,17 +349,8 @@ function initRepo() { }) } -function initRepoSetting() { - // Options. - // Confirmation of changing repository name. - $('#repo-setting-form').submit(function (e) { - var $reponame = $('#repo_name'); - if (($reponame.data('repo-name') != $reponame.val()) && !confirm('Repository name has been changed, do you want to continue?')) { - e.preventDefault(); - return true; - } - }); - +// when user changes hook type, hide/show proper divs +function initHookTypeChange() { // web hook type change $('select#hook-type').on("change", function () { hookTypes = ['Gogs','Slack']; @@ -374,6 +365,20 @@ function initRepoSetting() { } }); }); +} + +function initRepoSetting() { + // Options. + // Confirmation of changing repository name. + $('#repo-setting-form').submit(function (e) { + var $reponame = $('#repo_name'); + if (($reponame.data('repo-name') != $reponame.val()) && !confirm('Repository name has been changed, do you want to continue?')) { + e.preventDefault(); + return true; + } + }); + + initHookTypeChange(); $('#transfer-button').click(function () { $('#transfer-form').show(); @@ -421,6 +426,8 @@ function initOrgSetting() { return true; } }); + + initHookTypeChange(); } function initInvite() { diff --git a/routers/org/setting.go b/routers/org/setting.go index 0ddf0065c..f853ef0e8 100644 --- a/routers/org/setting.go +++ b/routers/org/setting.go @@ -5,6 +5,7 @@ package org import ( + "github.com/Unknwon/com" "github.com/gogits/gogs/models" "github.com/gogits/gogs/modules/auth" "github.com/gogits/gogs/modules/base" @@ -15,6 +16,7 @@ import ( const ( SETTINGS_OPTIONS base.TplName = "org/settings/options" SETTINGS_DELETE base.TplName = "org/settings/delete" + SETTINGS_HOOKS base.TplName = "org/settings/hooks" ) func Settings(ctx *middleware.Context) { @@ -97,3 +99,29 @@ func SettingsDelete(ctx *middleware.Context) { ctx.HTML(200, SETTINGS_DELETE) } + +func SettingsHooks(ctx *middleware.Context) { + ctx.Data["Title"] = ctx.Tr("org.settings") + ctx.Data["PageIsSettingsHooks"] = true + + // Delete web hook. + remove := com.StrTo(ctx.Query("remove")).MustInt64() + if remove > 0 { + if err := models.DeleteWebhook(remove); err != nil { + ctx.Handle(500, "DeleteWebhook", err) + return + } + ctx.Flash.Success(ctx.Tr("repo.settings.remove_hook_success")) + ctx.Redirect(ctx.Org.OrgLink + "/settings/hooks") + return + } + + ws, err := models.GetWebhooksByOrgId(ctx.Org.Organization.Id) + if err != nil { + ctx.Handle(500, "GetWebhooksByOrgId", err) + return + } + + ctx.Data["Webhooks"] = ws + ctx.HTML(200, SETTINGS_HOOKS) +} diff --git a/routers/repo/setting.go b/routers/repo/setting.go index 745678122..81747d43e 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -6,6 +6,7 @@ package repo import ( "encoding/json" + "errors" "fmt" "strings" "time" @@ -26,6 +27,7 @@ const ( COLLABORATION base.TplName = "repo/settings/collaboration" HOOKS base.TplName = "repo/settings/hooks" HOOK_NEW base.TplName = "repo/settings/hook_new" + ORG_HOOK_NEW base.TplName = "org/settings/hook_new" ) func Settings(ctx *middleware.Context) { @@ -284,7 +286,14 @@ func WebHooksNew(ctx *middleware.Context) { ctx.Data["PageIsSettingsHooksNew"] = true ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}} renderHookTypes(ctx) - ctx.HTML(200, HOOK_NEW) + orgId, repoId, _ := getOrgRepoCtx(ctx) + if repoId > 0 { + ctx.HTML(200, HOOK_NEW) + } else if orgId > 0 { + ctx.HTML(200, ORG_HOOK_NEW) + } else { + ctx.Handle(500, "WebHooksEdit(DetermineContext)", errors.New("Can't determine hook context")) + } } func WebHooksNewPost(ctx *middleware.Context, form auth.NewWebhookForm) { @@ -293,6 +302,8 @@ func WebHooksNewPost(ctx *middleware.Context, form auth.NewWebhookForm) { ctx.Data["PageIsSettingsHooksNew"] = true ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}} + orgId, repoId, link := getOrgRepoCtx(ctx) + if ctx.HasError() { ctx.HTML(200, HOOK_NEW) return @@ -304,7 +315,7 @@ func WebHooksNewPost(ctx *middleware.Context, form auth.NewWebhookForm) { } w := &models.Webhook{ - RepoId: ctx.Repo.Repository.Id, + RepoId: repoId, Url: form.PayloadUrl, ContentType: ct, Secret: form.Secret, @@ -314,6 +325,7 @@ func WebHooksNewPost(ctx *middleware.Context, form auth.NewWebhookForm) { IsActive: form.Active, HookTaskType: models.GOGS, Meta: "", + OrgId: orgId, } if err := w.UpdateEvent(); err != nil { @@ -325,7 +337,7 @@ func WebHooksNewPost(ctx *middleware.Context, form auth.NewWebhookForm) { } ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success")) - ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks") + ctx.Redirect(link + "/settings/hooks") } func WebHooksEdit(ctx *middleware.Context) { @@ -363,7 +375,14 @@ func WebHooksEdit(ctx *middleware.Context) { } w.GetEvent() ctx.Data["Webhook"] = w - ctx.HTML(200, HOOK_NEW) + orgId, repoId, _ := getOrgRepoCtx(ctx) + if repoId > 0 { + ctx.HTML(200, HOOK_NEW) + } else if orgId > 0 { + ctx.HTML(200, ORG_HOOK_NEW) + } else { + ctx.Handle(500, "WebHooksEdit(DetermineContext)", errors.New("Can't determine hook context")) + } } func WebHooksEditPost(ctx *middleware.Context, form auth.NewWebhookForm) { @@ -413,9 +432,10 @@ func WebHooksEditPost(ctx *middleware.Context, form auth.NewWebhookForm) { ctx.Handle(500, "WebHooksEditPost", err) return } + _, _, link := getOrgRepoCtx(ctx) ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success")) - ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", ctx.Repo.RepoLink, hookId)) + ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", link, hookId)) } func SlackHooksNewPost(ctx *middleware.Context, form auth.NewSlackHookForm) { @@ -428,6 +448,7 @@ func SlackHooksNewPost(ctx *middleware.Context, form auth.NewSlackHookForm) { ctx.HTML(200, HOOK_NEW) return } + orgId, repoId, link := getOrgRepoCtx(ctx) meta, err := json.Marshal(&models.Slack{ Domain: form.Domain, @@ -440,7 +461,7 @@ func SlackHooksNewPost(ctx *middleware.Context, form auth.NewSlackHookForm) { } w := &models.Webhook{ - RepoId: ctx.Repo.Repository.Id, + RepoId: repoId, Url: models.GetSlackURL(form.Domain, form.Token), ContentType: models.JSON, Secret: "", @@ -450,6 +471,7 @@ func SlackHooksNewPost(ctx *middleware.Context, form auth.NewSlackHookForm) { IsActive: form.Active, HookTaskType: models.SLACK, Meta: string(meta), + OrgId: orgId, } if err := w.UpdateEvent(); err != nil { ctx.Handle(500, "UpdateEvent", err) @@ -460,7 +482,7 @@ func SlackHooksNewPost(ctx *middleware.Context, form auth.NewSlackHookForm) { } ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success")) - ctx.Redirect(ctx.Repo.RepoLink + "/settings/hooks") + ctx.Redirect(link + "/settings/hooks") } func SlackHooksEditPost(ctx *middleware.Context, form auth.NewSlackHookForm) { @@ -514,7 +536,25 @@ func SlackHooksEditPost(ctx *middleware.Context, form auth.NewSlackHookForm) { ctx.Handle(500, "SlackHooksEditPost", err) return } + _, _, link := getOrgRepoCtx(ctx) ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success")) - ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", ctx.Repo.RepoLink, hookId)) + ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", link, hookId)) +} + +func getOrgRepoCtx(ctx *middleware.Context) (int64, int64, string) { + orgId := int64(0) + repoId := int64(0) + link := "" + if _, ok := ctx.Data["RepoLink"]; ok { + repoId = ctx.Repo.Repository.Id + link = ctx.Repo.RepoLink + } + + if _, ok := ctx.Data["OrgLink"]; ok { + orgId = ctx.Org.Organization.Id + link = ctx.Org.OrgLink + } + + return orgId, repoId, link } diff --git a/templates/org/settings/hook_new.tmpl b/templates/org/settings/hook_new.tmpl new file mode 100644 index 000000000..6e7ee5360 --- /dev/null +++ b/templates/org/settings/hook_new.tmpl @@ -0,0 +1,37 @@ +{{template "ng/base/head" .}} +{{template "ng/base/header" .}} +{{template "org/base/header" .}} +
+
+ {{template "org/settings/nav" .}} +
+
+ {{template "ng/base/alert" .}} +
+
+
+ {{if .PageIsSettingsHooksNew}}{{.i18n.Tr "repo.settings.add_webhook"}}{{else}}{{.i18n.Tr "repo.settings.update_webhook"}}{{end}} +
+ {{template "repo/settings/hook_types" .}} + {{template "repo/settings/hook_gogs" .}} + {{template "repo/settings/hook_slack" .}} +
+
+ {{if .PageIsSettingsHooksEdit}} +
+
+
+
+ {{.i18n.Tr "repo.settings.recent_deliveries"}} +
+
    +
  • Coming soon!
  • +
+
+
+ {{end}} +
+
+
+
+{{template "ng/base/footer" .}} diff --git a/templates/org/settings/hooks.tmpl b/templates/org/settings/hooks.tmpl new file mode 100644 index 000000000..713cfeb45 --- /dev/null +++ b/templates/org/settings/hooks.tmpl @@ -0,0 +1,38 @@ +{{template "ng/base/head" .}} +{{template "ng/base/header" .}} +{{template "org/base/header" .}} +
+
+ {{template "org/settings/nav" .}} +
+
+ {{template "ng/base/alert" .}} +
+
+
+ {{.i18n.Tr "repo.settings.add_webhook"}} + {{.i18n.Tr "repo.settings.hooks"}} +
+
    +
  • {{.i18n.Tr "org.settings.hooks_desc" | Str2html}}
  • + {{range .Webhooks}} +
  • + {{if .IsActive}} + + {{else}} + + {{end}} + {{.Url}} + + +
  • + {{end}} +
+
+
+
+
+
+
+ +{{template "ng/base/footer" .}} diff --git a/templates/org/settings/nav.tmpl b/templates/org/settings/nav.tmpl index 950569d68..954893c6c 100644 --- a/templates/org/settings/nav.tmpl +++ b/templates/org/settings/nav.tmpl @@ -5,7 +5,8 @@ - \ No newline at end of file + diff --git a/templates/repo/settings/hook_gogs.tmpl b/templates/repo/settings/hook_gogs.tmpl index 35b589953..31a04ce09 100644 --- a/templates/repo/settings/hook_gogs.tmpl +++ b/templates/repo/settings/hook_gogs.tmpl @@ -1,5 +1,5 @@
-
+ {{.CsrfTokenHtml}}
{{.i18n.Tr "repo.settings.add_webhook_desc" | Str2html}}
diff --git a/templates/repo/settings/hook_slack.tmpl b/templates/repo/settings/hook_slack.tmpl index 50d28e2f5..ed7a42e6f 100644 --- a/templates/repo/settings/hook_slack.tmpl +++ b/templates/repo/settings/hook_slack.tmpl @@ -1,5 +1,5 @@
- + {{.CsrfTokenHtml}}
{{.i18n.Tr "repo.settings.add_slack_hook_desc" | Str2html}}
From f7be61c81935a30c21797351db5da32183e3188d Mon Sep 17 00:00:00 2001 From: Christopher Brickley Date: Thu, 4 Sep 2014 19:05:21 -0400 Subject: [PATCH 4/4] getOrgRepoCtx returns values directly & err --- routers/repo/setting.go | 115 ++++++++++++++++++++++++---------------- 1 file changed, 70 insertions(+), 45 deletions(-) diff --git a/routers/repo/setting.go b/routers/repo/setting.go index 81747d43e..80a064492 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -286,14 +286,13 @@ func WebHooksNew(ctx *middleware.Context) { ctx.Data["PageIsSettingsHooksNew"] = true ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}} renderHookTypes(ctx) - orgId, repoId, _ := getOrgRepoCtx(ctx) - if repoId > 0 { - ctx.HTML(200, HOOK_NEW) - } else if orgId > 0 { - ctx.HTML(200, ORG_HOOK_NEW) - } else { - ctx.Handle(500, "WebHooksEdit(DetermineContext)", errors.New("Can't determine hook context")) + orCtx, err := getOrgRepoCtx(ctx) + if err != nil { + ctx.Handle(500, "WebHooksNew(getOrgRepoCtx)", err) + return } + + ctx.HTML(200, orCtx.NewTemplate) } func WebHooksNewPost(ctx *middleware.Context, form auth.NewWebhookForm) { @@ -302,10 +301,14 @@ func WebHooksNewPost(ctx *middleware.Context, form auth.NewWebhookForm) { ctx.Data["PageIsSettingsHooksNew"] = true ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}} - orgId, repoId, link := getOrgRepoCtx(ctx) + orCtx, err := getOrgRepoCtx(ctx) + if err != nil { + ctx.Handle(500, "WebHooksNewPost(getOrgRepoCtx)", err) + return + } if ctx.HasError() { - ctx.HTML(200, HOOK_NEW) + ctx.HTML(200, orCtx.NewTemplate) return } @@ -315,7 +318,7 @@ func WebHooksNewPost(ctx *middleware.Context, form auth.NewWebhookForm) { } w := &models.Webhook{ - RepoId: repoId, + RepoId: orCtx.RepoId, Url: form.PayloadUrl, ContentType: ct, Secret: form.Secret, @@ -325,7 +328,7 @@ func WebHooksNewPost(ctx *middleware.Context, form auth.NewWebhookForm) { IsActive: form.Active, HookTaskType: models.GOGS, Meta: "", - OrgId: orgId, + OrgId: orCtx.OrgId, } if err := w.UpdateEvent(); err != nil { @@ -337,7 +340,7 @@ func WebHooksNewPost(ctx *middleware.Context, form auth.NewWebhookForm) { } ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success")) - ctx.Redirect(link + "/settings/hooks") + ctx.Redirect(orCtx.Link + "/settings/hooks") } func WebHooksEdit(ctx *middleware.Context) { @@ -375,14 +378,12 @@ func WebHooksEdit(ctx *middleware.Context) { } w.GetEvent() ctx.Data["Webhook"] = w - orgId, repoId, _ := getOrgRepoCtx(ctx) - if repoId > 0 { - ctx.HTML(200, HOOK_NEW) - } else if orgId > 0 { - ctx.HTML(200, ORG_HOOK_NEW) - } else { - ctx.Handle(500, "WebHooksEdit(DetermineContext)", errors.New("Can't determine hook context")) + orCtx, err := getOrgRepoCtx(ctx) + if err != nil { + ctx.Handle(500, "WebHooksEdit(getOrgRepoCtx)", err) + return } + ctx.HTML(200, orCtx.NewTemplate) } func WebHooksEditPost(ctx *middleware.Context, form auth.NewWebhookForm) { @@ -408,8 +409,13 @@ func WebHooksEditPost(ctx *middleware.Context, form auth.NewWebhookForm) { w.GetEvent() ctx.Data["Webhook"] = w + orCtx, err := getOrgRepoCtx(ctx) + if err != nil { + ctx.Handle(500, "WebHooksEditPost(getOrgRepoCtx)", err) + return + } if ctx.HasError() { - ctx.HTML(200, HOOK_NEW) + ctx.HTML(200, orCtx.NewTemplate) return } @@ -432,10 +438,9 @@ func WebHooksEditPost(ctx *middleware.Context, form auth.NewWebhookForm) { ctx.Handle(500, "WebHooksEditPost", err) return } - _, _, link := getOrgRepoCtx(ctx) ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success")) - ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", link, hookId)) + ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, hookId)) } func SlackHooksNewPost(ctx *middleware.Context, form auth.NewSlackHookForm) { @@ -444,11 +449,16 @@ func SlackHooksNewPost(ctx *middleware.Context, form auth.NewSlackHookForm) { ctx.Data["PageIsSettingsHooksNew"] = true ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}} + orCtx, err := getOrgRepoCtx(ctx) + if err != nil { + ctx.Handle(500, "SlackHooksNewPost(getOrgRepoCtx)", err) + return + } + if ctx.HasError() { - ctx.HTML(200, HOOK_NEW) + ctx.HTML(200, orCtx.NewTemplate) return } - orgId, repoId, link := getOrgRepoCtx(ctx) meta, err := json.Marshal(&models.Slack{ Domain: form.Domain, @@ -461,7 +471,7 @@ func SlackHooksNewPost(ctx *middleware.Context, form auth.NewSlackHookForm) { } w := &models.Webhook{ - RepoId: repoId, + RepoId: orCtx.RepoId, Url: models.GetSlackURL(form.Domain, form.Token), ContentType: models.JSON, Secret: "", @@ -471,7 +481,7 @@ func SlackHooksNewPost(ctx *middleware.Context, form auth.NewSlackHookForm) { IsActive: form.Active, HookTaskType: models.SLACK, Meta: string(meta), - OrgId: orgId, + OrgId: orCtx.OrgId, } if err := w.UpdateEvent(); err != nil { ctx.Handle(500, "UpdateEvent", err) @@ -482,7 +492,7 @@ func SlackHooksNewPost(ctx *middleware.Context, form auth.NewSlackHookForm) { } ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success")) - ctx.Redirect(link + "/settings/hooks") + ctx.Redirect(orCtx.Link + "/settings/hooks") } func SlackHooksEditPost(ctx *middleware.Context, form auth.NewSlackHookForm) { @@ -491,9 +501,14 @@ func SlackHooksEditPost(ctx *middleware.Context, form auth.NewSlackHookForm) { ctx.Data["PageIsSettingsHooksEdit"] = true hookId := com.StrTo(ctx.Params(":id")).MustInt64() - fmt.Println("hookId slack=%d", hookId) if hookId == 0 { - ctx.Handle(404, "setting.WebHooksEditPost", nil) + ctx.Handle(404, "SlackHooksEditPost(hookId)", nil) + return + } + + orCtx, err := getOrgRepoCtx(ctx) + if err != nil { + ctx.Handle(500, "SlackHooksEditPost(getOrgRepoCtx)", err) return } @@ -510,7 +525,7 @@ func SlackHooksEditPost(ctx *middleware.Context, form auth.NewSlackHookForm) { ctx.Data["Webhook"] = w if ctx.HasError() { - ctx.HTML(200, HOOK_NEW) + ctx.HTML(200, orCtx.NewTemplate) return } meta, err := json.Marshal(&models.Slack{ @@ -536,25 +551,35 @@ func SlackHooksEditPost(ctx *middleware.Context, form auth.NewSlackHookForm) { ctx.Handle(500, "SlackHooksEditPost", err) return } - _, _, link := getOrgRepoCtx(ctx) ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success")) - ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", link, hookId)) + ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, hookId)) } -func getOrgRepoCtx(ctx *middleware.Context) (int64, int64, string) { - orgId := int64(0) - repoId := int64(0) - link := "" - if _, ok := ctx.Data["RepoLink"]; ok { - repoId = ctx.Repo.Repository.Id - link = ctx.Repo.RepoLink - } +type OrgRepoCtx struct { + OrgId int64 + RepoId int64 + Link string + NewTemplate base.TplName +} - if _, ok := ctx.Data["OrgLink"]; ok { - orgId = ctx.Org.Organization.Id - link = ctx.Org.OrgLink +// determines whether this is a repo context or organization context +func getOrgRepoCtx(ctx *middleware.Context) (*OrgRepoCtx, error) { + if _, ok := ctx.Data["RepoLink"]; ok { + return &OrgRepoCtx{ + OrgId: int64(0), + RepoId: ctx.Repo.Repository.Id, + Link: ctx.Repo.RepoLink, + NewTemplate: HOOK_NEW, + }, nil + } else if _, ok := ctx.Data["OrgLink"]; ok { + return &OrgRepoCtx{ + OrgId: ctx.Org.Organization.Id, + RepoId: int64(0), + Link: ctx.Org.OrgLink, + NewTemplate: ORG_HOOK_NEW, + }, nil + } else { + return &OrgRepoCtx{}, errors.New("Unable to set OrgRepo context") } - - return orgId, repoId, link }