func SlackHooksEditPost(ctx *middleware.Context, form auth.NewSlackHookForm) { ctx.Data["Title"] = ctx.Tr("repo.settings") ctx.Data["PageIsSettingsHooks"] = true ctx.Data["PageIsSettingsHooksEdit"] = true hookId := com.StrTo(ctx.Params(":id")).MustInt64() if hookId == 0 { ctx.Handle(404, "SlackHooksEditPost(hookId)", nil) return } orCtx, err := getOrgRepoCtx(ctx) if err != nil { ctx.Handle(500, "SlackHooksEditPost(getOrgRepoCtx)", err) return } w, err := models.GetWebhookById(hookId) if err != nil { if err == models.ErrWebhookNotExist { ctx.Handle(404, "GetWebhookById", nil) } else { ctx.Handle(500, "GetWebhookById", err) } return } w.GetEvent() ctx.Data["Webhook"] = w if ctx.HasError() { ctx.HTML(200, orCtx.NewTemplate) return } meta, err := json.Marshal(&models.Slack{ Channel: form.Channel, }) if err != nil { ctx.Handle(500, "SlackHooksNewPost: JSON marshal failed: ", err) return } w.Url = form.PayloadUrl w.Meta = string(meta) w.HookEvent = &models.HookEvent{ PushOnly: form.PushOnly, } w.IsActive = form.Active if err := w.UpdateEvent(); err != nil { ctx.Handle(500, "UpdateEvent", err) return } else if err := models.UpdateWebhook(w); err != nil { ctx.Handle(500, "SlackHooksEditPost", err) return } ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success")) ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, hookId)) }
func WebHooksEditPost(ctx *middleware.Context, form auth.NewWebhookForm) { ctx.Data["Title"] = ctx.Tr("repo.settings") ctx.Data["PageIsSettingsHooks"] = true ctx.Data["PageIsSettingsHooksEdit"] = true hookId := com.StrTo(ctx.Params(":id")).MustInt64() if hookId == 0 { ctx.Handle(404, "setting.WebHooksEditPost", nil) return } w, err := models.GetWebhookById(hookId) if err != nil { if err == models.ErrWebhookNotExist { ctx.Handle(404, "GetWebhookById", nil) } else { ctx.Handle(500, "GetWebhookById", err) } return } 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, orCtx.NewTemplate) return } ct := models.JSON if form.ContentType == "2" { ct = models.FORM } w.Url = form.PayloadUrl w.ContentType = ct w.Secret = form.Secret w.HookEvent = &models.HookEvent{ PushOnly: form.PushOnly, } w.IsActive = form.Active if err := w.UpdateEvent(); err != nil { ctx.Handle(500, "UpdateEvent", err) return } else if err := models.UpdateWebhook(w); err != nil { ctx.Handle(500, "WebHooksEditPost", err) return } ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success")) ctx.Redirect(fmt.Sprintf("%s/settings/hooks/%d", orCtx.Link, hookId)) }
// PATCH /repos/:username/:reponame/hooks/:id // https://developer.github.com/v3/repos/hooks/#edit-a-hook func EditRepoHook(ctx *middleware.Context, form api.EditHookOption) { w, err := models.GetWebhookById(ctx.ParamsInt64(":id")) if err != nil { ctx.JSON(500, &base.ApiJsonErr{"GetWebhookById: " + err.Error(), base.DOC_URL}) return } if form.Config != nil { if url, ok := form.Config["url"]; ok { w.Url = url } if ct, ok := form.Config["content_type"]; ok { if !models.IsValidHookContentType(ct) { ctx.JSON(422, &base.ApiJsonErr{"invalid content type", base.DOC_URL}) return } w.ContentType = models.ToHookContentType(ct) } if w.HookTaskType == models.SLACK { if channel, ok := form.Config["channel"]; ok { meta, err := json.Marshal(&models.Slack{ Channel: channel, }) if err != nil { ctx.JSON(500, &base.ApiJsonErr{"slack: JSON marshal failed: " + err.Error(), base.DOC_URL}) return } w.Meta = string(meta) } } } if form.Active != nil { w.IsActive = *form.Active } // FIXME: edit events if err := models.UpdateWebhook(w); err != nil { ctx.JSON(500, &base.ApiJsonErr{"UpdateWebhook: " + err.Error(), base.DOC_URL}) return } ctx.JSON(200, map[string]interface{}{ "ok": true, }) }
func WebHooksEdit(ctx *middleware.Context) { ctx.Data["Title"] = ctx.Tr("repo.settings") ctx.Data["PageIsSettingsHooks"] = true ctx.Data["PageIsSettingsHooksEdit"] = true hookId := com.StrTo(ctx.Params(":id")).MustInt64() if hookId == 0 { ctx.Handle(404, "setting.WebHooksEdit", nil) return } w, err := models.GetWebhookById(hookId) if err != nil { if err == models.ErrWebhookNotExist { ctx.Handle(404, "GetWebhookById", nil) } else { ctx.Handle(500, "GetWebhookById", err) } return } // set data per HookTaskType switch w.HookTaskType { case models.SLACK: { ctx.Data["SlackHook"] = w.GetSlackHook() ctx.Data["HookType"] = "Slack" } default: { ctx.Data["HookType"] = "Gogs" } } w.GetEvent() ctx.Data["Webhook"] = w orCtx, err := getOrgRepoCtx(ctx) if err != nil { ctx.Handle(500, "WebHooksEdit(getOrgRepoCtx)", err) return } ctx.HTML(200, orCtx.NewTemplate) }