diff options
Diffstat (limited to 'routers/repo/setting.go')
-rw-r--r-- | routers/repo/setting.go | 97 |
1 files changed, 81 insertions, 16 deletions
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") + } } |