diff options
author | 无闻 <joe2010xtmf@163.com> | 2014-09-07 19:30:51 -0400 |
---|---|---|
committer | 无闻 <joe2010xtmf@163.com> | 2014-09-07 19:30:51 -0400 |
commit | e79e4b158090bf7c5dd86ac21cd0dbf44fbc82bd (patch) | |
tree | aec4d8a53265080363b3ab7a4423bc0820133ea5 /routers | |
parent | 40bc130b19cf71a997d5073a2587ea2649128bf8 (diff) | |
parent | f7be61c81935a30c21797351db5da32183e3188d (diff) | |
download | gitea-e79e4b158090bf7c5dd86ac21cd0dbf44fbc82bd.tar.gz gitea-e79e4b158090bf7c5dd86ac21cd0dbf44fbc82bd.zip |
Merge pull request #442 from compressed/org_hook
Organization-level Webhooks
Diffstat (limited to 'routers')
-rw-r--r-- | routers/org/setting.go | 28 | ||||
-rw-r--r-- | routers/repo/setting.go | 97 |
2 files changed, 109 insertions, 16 deletions
diff --git a/routers/org/setting.go b/routers/org/setting.go index 0ddf0065cc..f853ef0e8e 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 fba9eed6a2..80a064492e 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,13 @@ func WebHooksNew(ctx *middleware.Context) { ctx.Data["PageIsSettingsHooksNew"] = true ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}} renderHookTypes(ctx) - ctx.HTML(200, HOOK_NEW) + 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) { @@ -293,8 +301,14 @@ func WebHooksNewPost(ctx *middleware.Context, form auth.NewWebhookForm) { ctx.Data["PageIsSettingsHooksNew"] = true ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}} + 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 } @@ -304,7 +318,7 @@ func WebHooksNewPost(ctx *middleware.Context, form auth.NewWebhookForm) { } w := &models.Webhook{ - RepoId: ctx.Repo.Repository.Id, + RepoId: orCtx.RepoId, Url: form.PayloadUrl, ContentType: ct, Secret: form.Secret, @@ -314,6 +328,7 @@ func WebHooksNewPost(ctx *middleware.Context, form auth.NewWebhookForm) { IsActive: form.Active, HookTaskType: models.GOGS, Meta: "", + OrgId: orCtx.OrgId, } if err := w.UpdateEvent(); err != nil { @@ -325,7 +340,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(orCtx.Link + "/settings/hooks") } func WebHooksEdit(ctx *middleware.Context) { @@ -354,16 +369,21 @@ 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() ctx.Data["Webhook"] = w - ctx.HTML(200, HOOK_NEW) + 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) { @@ -389,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 } @@ -415,7 +440,7 @@ func WebHooksEditPost(ctx *middleware.Context, form auth.NewWebhookForm) { } 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", orCtx.Link, hookId)) } func SlackHooksNewPost(ctx *middleware.Context, form auth.NewSlackHookForm) { @@ -424,8 +449,14 @@ 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 } @@ -440,7 +471,7 @@ func SlackHooksNewPost(ctx *middleware.Context, form auth.NewSlackHookForm) { } w := &models.Webhook{ - RepoId: ctx.Repo.Repository.Id, + RepoId: orCtx.RepoId, Url: models.GetSlackURL(form.Domain, form.Token), ContentType: models.JSON, Secret: "", @@ -450,6 +481,7 @@ func SlackHooksNewPost(ctx *middleware.Context, form auth.NewSlackHookForm) { IsActive: form.Active, HookTaskType: models.SLACK, Meta: string(meta), + OrgId: orCtx.OrgId, } if err := w.UpdateEvent(); err != nil { ctx.Handle(500, "UpdateEvent", err) @@ -460,7 +492,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(orCtx.Link + "/settings/hooks") } func SlackHooksEditPost(ctx *middleware.Context, form auth.NewSlackHookForm) { @@ -469,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 } @@ -488,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{ @@ -516,5 +553,33 @@ func SlackHooksEditPost(ctx *middleware.Context, form auth.NewSlackHookForm) { } 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", orCtx.Link, hookId)) +} + +type OrgRepoCtx struct { + OrgId int64 + RepoId int64 + Link string + NewTemplate base.TplName +} + +// 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") + } } |