2015-12-05 11:24:13 -07:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
2017-05-29 01:17:15 -06:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2015-12-05 11:24:13 -07:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-03-18 20:33:20 -06:00
|
|
|
"path"
|
2015-12-05 11:24:13 -07:00
|
|
|
"strings"
|
|
|
|
|
2016-11-10 09:24:48 -07:00
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/auth"
|
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2019-03-27 03:33:00 -06:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2016-11-10 09:24:48 -07:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-05-11 04:21:34 -06:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2019-11-01 16:51:22 -06:00
|
|
|
"code.gitea.io/gitea/modules/webhook"
|
2017-08-27 23:06:45 -06:00
|
|
|
|
2019-08-23 10:40:30 -06:00
|
|
|
"github.com/unknwon/com"
|
2015-12-05 11:24:13 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-03-18 20:33:20 -06:00
|
|
|
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"
|
2015-12-05 11:24:13 -07:00
|
|
|
)
|
|
|
|
|
2016-11-24 00:04:31 -07:00
|
|
|
// Webhooks render web hooks list page
|
2016-03-11 09:56:52 -07:00
|
|
|
func Webhooks(ctx *context.Context) {
|
2015-12-05 11:24:13 -07:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings.hooks")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
2019-03-18 20:33:20 -06:00
|
|
|
ctx.Data["BaseLink"] = ctx.Repo.RepoLink + "/settings/hooks"
|
2018-01-03 08:50:09 -07:00
|
|
|
ctx.Data["Description"] = ctx.Tr("repo.settings.hooks_desc", "https://docs.gitea.io/en-us/webhooks/")
|
2015-12-05 11:24:13 -07:00
|
|
|
|
2020-01-24 12:00:29 -07:00
|
|
|
ws, err := models.GetWebhooksByRepoID(ctx.Repo.Repository.ID, models.ListOptions{})
|
2015-12-05 11:24:13 -07:00
|
|
|
if err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("GetWebhooksByRepoID", err)
|
2015-12-05 11:24:13 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Webhooks"] = ws
|
|
|
|
|
2016-11-07 13:58:22 -07:00
|
|
|
ctx.HTML(200, tplHooks)
|
2015-12-05 11:24:13 -07:00
|
|
|
}
|
|
|
|
|
2016-11-24 00:04:31 -07:00
|
|
|
type orgRepoCtx struct {
|
2020-03-08 16:08:05 -06:00
|
|
|
OrgID int64
|
|
|
|
RepoID int64
|
|
|
|
IsAdmin bool
|
|
|
|
IsSystemWebhook bool
|
|
|
|
Link string
|
|
|
|
NewTemplate base.TplName
|
2015-12-05 11:24:13 -07:00
|
|
|
}
|
|
|
|
|
2020-03-08 16:08:05 -06:00
|
|
|
// getOrgRepoCtx determines whether this is a repo, organization, or admin (both default and system) context.
|
2016-11-24 00:04:31 -07:00
|
|
|
func getOrgRepoCtx(ctx *context.Context) (*orgRepoCtx, error) {
|
2015-12-05 11:24:13 -07:00
|
|
|
if len(ctx.Repo.RepoLink) > 0 {
|
2016-11-24 00:04:31 -07:00
|
|
|
return &orgRepoCtx{
|
2015-12-05 11:24:13 -07:00
|
|
|
RepoID: ctx.Repo.Repository.ID,
|
2019-03-18 20:33:20 -06:00
|
|
|
Link: path.Join(ctx.Repo.RepoLink, "settings/hooks"),
|
2016-11-07 13:58:22 -07:00
|
|
|
NewTemplate: tplHookNew,
|
2015-12-05 11:24:13 -07:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ctx.Org.OrgLink) > 0 {
|
2016-11-24 00:04:31 -07:00
|
|
|
return &orgRepoCtx{
|
2016-07-23 11:08:22 -06:00
|
|
|
OrgID: ctx.Org.Organization.ID,
|
2019-03-18 20:33:20 -06:00
|
|
|
Link: path.Join(ctx.Org.OrgLink, "settings/hooks"),
|
2016-11-07 13:58:22 -07:00
|
|
|
NewTemplate: tplOrgHookNew,
|
2015-12-05 11:24:13 -07:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-18 20:33:20 -06:00
|
|
|
if ctx.User.IsAdmin {
|
2020-03-08 16:08:05 -06:00
|
|
|
// Are we looking at default webhooks?
|
|
|
|
if ctx.Params(":configType") == "hooks" {
|
|
|
|
return &orgRepoCtx{
|
|
|
|
IsAdmin: true,
|
|
|
|
Link: path.Join(setting.AppSubURL, "/admin/hooks"),
|
|
|
|
NewTemplate: tplAdminHookNew,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must be system webhooks instead
|
2019-03-18 20:33:20 -06:00
|
|
|
return &orgRepoCtx{
|
2020-03-08 16:08:05 -06:00
|
|
|
IsAdmin: true,
|
|
|
|
IsSystemWebhook: true,
|
|
|
|
Link: path.Join(setting.AppSubURL, "/admin/system-hooks"),
|
|
|
|
NewTemplate: tplAdminHookNew,
|
2019-03-18 20:33:20 -06:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2015-12-05 11:24:13 -07:00
|
|
|
return nil, errors.New("Unable to set OrgRepo context")
|
|
|
|
}
|
|
|
|
|
2016-03-11 09:56:52 -07:00
|
|
|
func checkHookType(ctx *context.Context) string {
|
2015-12-05 11:24:13 -07:00
|
|
|
hookType := strings.ToLower(ctx.Params(":type"))
|
|
|
|
if !com.IsSliceContainsStr(setting.Webhook.Types, hookType) {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.NotFound("checkHookType", nil)
|
2015-12-05 11:24:13 -07:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return hookType
|
|
|
|
}
|
|
|
|
|
2016-11-24 00:04:31 -07:00
|
|
|
// WebhooksNew render creating webhook page
|
2016-03-11 09:56:52 -07:00
|
|
|
func WebhooksNew(ctx *context.Context) {
|
2015-12-05 11:24:13 -07:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
|
|
|
|
ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
|
|
|
|
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
|
|
if err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
2015-12-05 11:24:13 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-08 16:08:05 -06:00
|
|
|
if orCtx.IsAdmin && orCtx.IsSystemWebhook {
|
|
|
|
ctx.Data["PageIsAdminSystemHooks"] = true
|
|
|
|
ctx.Data["PageIsAdminSystemHooksNew"] = true
|
|
|
|
} else if orCtx.IsAdmin {
|
2019-03-18 20:33:20 -06:00
|
|
|
ctx.Data["PageIsAdminHooks"] = true
|
|
|
|
ctx.Data["PageIsAdminHooksNew"] = true
|
|
|
|
} else {
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
|
|
|
}
|
|
|
|
|
2017-08-27 23:06:45 -06:00
|
|
|
hookType := checkHookType(ctx)
|
|
|
|
ctx.Data["HookType"] = hookType
|
2015-12-05 11:24:13 -07:00
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
2017-08-27 23:06:45 -06:00
|
|
|
if hookType == "discord" {
|
|
|
|
ctx.Data["DiscordHook"] = map[string]interface{}{
|
|
|
|
"Username": "Gitea",
|
|
|
|
"IconURL": setting.AppURL + "img/favicon.png",
|
|
|
|
}
|
|
|
|
}
|
2015-12-05 11:24:13 -07:00
|
|
|
ctx.Data["BaseLink"] = orCtx.Link
|
|
|
|
|
|
|
|
ctx.HTML(200, orCtx.NewTemplate)
|
|
|
|
}
|
|
|
|
|
2016-11-24 00:04:31 -07:00
|
|
|
// ParseHookEvent convert web form content to models.HookEvent
|
2015-12-05 11:24:13 -07:00
|
|
|
func ParseHookEvent(form auth.WebhookForm) *models.HookEvent {
|
|
|
|
return &models.HookEvent{
|
|
|
|
PushOnly: form.PushOnly(),
|
|
|
|
SendEverything: form.SendEverything(),
|
|
|
|
ChooseEvents: form.ChooseEvents(),
|
|
|
|
HookEvents: models.HookEvents{
|
2020-03-05 22:10:48 -07:00
|
|
|
Create: form.Create,
|
|
|
|
Delete: form.Delete,
|
|
|
|
Fork: form.Fork,
|
|
|
|
Issues: form.Issues,
|
|
|
|
IssueAssign: form.IssueAssign,
|
|
|
|
IssueLabel: form.IssueLabel,
|
|
|
|
IssueMilestone: form.IssueMilestone,
|
|
|
|
IssueComment: form.IssueComment,
|
|
|
|
Release: form.Release,
|
|
|
|
Push: form.Push,
|
|
|
|
PullRequest: form.PullRequest,
|
|
|
|
PullRequestAssign: form.PullRequestAssign,
|
|
|
|
PullRequestLabel: form.PullRequestLabel,
|
|
|
|
PullRequestMilestone: form.PullRequestMilestone,
|
|
|
|
PullRequestComment: form.PullRequestComment,
|
|
|
|
PullRequestReview: form.PullRequestReview,
|
|
|
|
PullRequestSync: form.PullRequestSync,
|
|
|
|
Repository: form.Repository,
|
2015-12-05 11:24:13 -07:00
|
|
|
},
|
2019-09-08 23:48:21 -06:00
|
|
|
BranchFilter: form.BranchFilter,
|
2015-12-05 11:24:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 16:08:05 -06:00
|
|
|
// GiteaHooksNewPost response for creating Gitea webhook
|
|
|
|
func GiteaHooksNewPost(ctx *context.Context, form auth.NewWebhookForm) {
|
2017-05-29 01:17:15 -06:00
|
|
|
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{}}
|
2020-05-15 13:02:46 -06:00
|
|
|
ctx.Data["HookType"] = models.GITEA.Name()
|
2017-05-29 01:17:15 -06:00
|
|
|
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
|
|
if err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
2017-05-29 01:17:15 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["BaseLink"] = orCtx.Link
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(200, orCtx.NewTemplate)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
contentType := models.ContentTypeJSON
|
|
|
|
if models.HookContentType(form.ContentType) == models.ContentTypeForm {
|
|
|
|
contentType = models.ContentTypeForm
|
|
|
|
}
|
|
|
|
|
|
|
|
w := &models.Webhook{
|
2020-03-08 16:08:05 -06:00
|
|
|
RepoID: orCtx.RepoID,
|
|
|
|
URL: form.PayloadURL,
|
|
|
|
HTTPMethod: form.HTTPMethod,
|
|
|
|
ContentType: contentType,
|
|
|
|
Secret: form.Secret,
|
|
|
|
HookEvent: ParseHookEvent(form.WebhookForm),
|
|
|
|
IsActive: form.Active,
|
|
|
|
HookTaskType: models.GITEA,
|
|
|
|
OrgID: orCtx.OrgID,
|
|
|
|
IsSystemWebhook: orCtx.IsSystemWebhook,
|
2017-05-29 01:17:15 -06:00
|
|
|
}
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("UpdateEvent", err)
|
2017-05-29 01:17:15 -06:00
|
|
|
return
|
|
|
|
} else if err := models.CreateWebhook(w); err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("CreateWebhook", err)
|
2017-05-29 01:17:15 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
|
2019-03-18 20:33:20 -06:00
|
|
|
ctx.Redirect(orCtx.Link)
|
2017-05-29 01:17:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// GogsHooksNewPost response for creating webhook
|
2019-10-02 06:58:40 -06:00
|
|
|
func GogsHooksNewPost(ctx *context.Context, form auth.NewGogshookForm) {
|
|
|
|
newGogsWebhookPost(ctx, form, models.GOGS)
|
2019-06-12 13:41:28 -06:00
|
|
|
}
|
|
|
|
|
2019-10-02 06:58:40 -06:00
|
|
|
// newGogsWebhookPost response for creating gogs hook
|
|
|
|
func newGogsWebhookPost(ctx *context.Context, form auth.NewGogshookForm, kind models.HookTaskType) {
|
2015-12-05 11:24:13 -07:00
|
|
|
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{}}
|
2020-05-15 13:02:46 -06:00
|
|
|
ctx.Data["HookType"] = models.GOGS.Name()
|
2015-12-05 11:24:13 -07:00
|
|
|
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
|
|
if err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
2015-12-05 11:24:13 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["BaseLink"] = orCtx.Link
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(200, orCtx.NewTemplate)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-07 13:58:22 -07:00
|
|
|
contentType := models.ContentTypeJSON
|
2016-11-07 09:53:22 -07:00
|
|
|
if models.HookContentType(form.ContentType) == models.ContentTypeForm {
|
|
|
|
contentType = models.ContentTypeForm
|
2015-12-05 11:24:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
w := &models.Webhook{
|
2020-03-08 16:08:05 -06:00
|
|
|
RepoID: orCtx.RepoID,
|
|
|
|
URL: form.PayloadURL,
|
|
|
|
ContentType: contentType,
|
|
|
|
Secret: form.Secret,
|
|
|
|
HookEvent: ParseHookEvent(form.WebhookForm),
|
|
|
|
IsActive: form.Active,
|
|
|
|
HookTaskType: kind,
|
|
|
|
OrgID: orCtx.OrgID,
|
|
|
|
IsSystemWebhook: orCtx.IsSystemWebhook,
|
2015-12-05 11:24:13 -07:00
|
|
|
}
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("UpdateEvent", err)
|
2015-12-05 11:24:13 -07:00
|
|
|
return
|
|
|
|
} else if err := models.CreateWebhook(w); err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("CreateWebhook", err)
|
2015-12-05 11:24:13 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
|
2019-03-18 20:33:20 -06:00
|
|
|
ctx.Redirect(orCtx.Link)
|
2015-12-05 11:24:13 -07:00
|
|
|
}
|
|
|
|
|
2017-08-27 23:06:45 -06:00
|
|
|
// DiscordHooksNewPost response for creating discord hook
|
|
|
|
func DiscordHooksNewPost(ctx *context.Context, form auth.NewDiscordHookForm) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
|
|
|
ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
|
2020-05-15 13:02:46 -06:00
|
|
|
ctx.Data["HookType"] = models.DISCORD.Name()
|
2017-08-27 23:06:45 -06:00
|
|
|
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
|
|
if err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
2017-08-27 23:06:45 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(200, orCtx.NewTemplate)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-03 15:13:25 -07:00
|
|
|
meta, err := json.Marshal(&webhook.DiscordMeta{
|
2017-08-27 23:06:45 -06:00
|
|
|
Username: form.Username,
|
|
|
|
IconURL: form.IconURL,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("Marshal", err)
|
2017-08-27 23:06:45 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w := &models.Webhook{
|
2020-03-08 16:08:05 -06:00
|
|
|
RepoID: orCtx.RepoID,
|
|
|
|
URL: form.PayloadURL,
|
|
|
|
ContentType: models.ContentTypeJSON,
|
|
|
|
HookEvent: ParseHookEvent(form.WebhookForm),
|
|
|
|
IsActive: form.Active,
|
|
|
|
HookTaskType: models.DISCORD,
|
|
|
|
Meta: string(meta),
|
|
|
|
OrgID: orCtx.OrgID,
|
|
|
|
IsSystemWebhook: orCtx.IsSystemWebhook,
|
2017-08-27 23:06:45 -06:00
|
|
|
}
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("UpdateEvent", err)
|
2017-08-27 23:06:45 -06:00
|
|
|
return
|
|
|
|
} else if err := models.CreateWebhook(w); err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("CreateWebhook", err)
|
2017-08-27 23:06:45 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
|
2019-03-18 20:33:20 -06:00
|
|
|
ctx.Redirect(orCtx.Link)
|
2017-08-27 23:06:45 -06:00
|
|
|
}
|
|
|
|
|
2017-11-20 21:26:43 -07:00
|
|
|
// DingtalkHooksNewPost response for creating dingtalk hook
|
|
|
|
func DingtalkHooksNewPost(ctx *context.Context, form auth.NewDingtalkHookForm) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
|
|
|
ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
|
2020-05-15 13:02:46 -06:00
|
|
|
ctx.Data["HookType"] = models.DINGTALK.Name()
|
2017-11-20 21:26:43 -07:00
|
|
|
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
|
|
if err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
2017-11-20 21:26:43 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(200, orCtx.NewTemplate)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w := &models.Webhook{
|
2020-03-08 16:08:05 -06:00
|
|
|
RepoID: orCtx.RepoID,
|
|
|
|
URL: form.PayloadURL,
|
|
|
|
ContentType: models.ContentTypeJSON,
|
|
|
|
HookEvent: ParseHookEvent(form.WebhookForm),
|
|
|
|
IsActive: form.Active,
|
|
|
|
HookTaskType: models.DINGTALK,
|
|
|
|
Meta: "",
|
|
|
|
OrgID: orCtx.OrgID,
|
|
|
|
IsSystemWebhook: orCtx.IsSystemWebhook,
|
2017-11-20 21:26:43 -07:00
|
|
|
}
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("UpdateEvent", err)
|
2017-11-20 21:26:43 -07:00
|
|
|
return
|
|
|
|
} else if err := models.CreateWebhook(w); err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("CreateWebhook", err)
|
2017-11-20 21:26:43 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
|
2019-03-18 20:33:20 -06:00
|
|
|
ctx.Redirect(orCtx.Link)
|
2017-11-20 21:26:43 -07:00
|
|
|
}
|
|
|
|
|
2019-04-18 20:45:02 -06:00
|
|
|
// TelegramHooksNewPost response for creating telegram hook
|
|
|
|
func TelegramHooksNewPost(ctx *context.Context, form auth.NewTelegramHookForm) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
|
|
|
ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
|
2020-05-15 13:02:46 -06:00
|
|
|
ctx.Data["HookType"] = models.TELEGRAM.Name()
|
2019-04-18 20:45:02 -06:00
|
|
|
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(200, orCtx.NewTemplate)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-03 15:13:25 -07:00
|
|
|
meta, err := json.Marshal(&webhook.TelegramMeta{
|
2019-04-18 20:45:02 -06:00
|
|
|
BotToken: form.BotToken,
|
|
|
|
ChatID: form.ChatID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("Marshal", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w := &models.Webhook{
|
2020-03-08 16:08:05 -06:00
|
|
|
RepoID: orCtx.RepoID,
|
|
|
|
URL: fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s", form.BotToken, form.ChatID),
|
|
|
|
ContentType: models.ContentTypeJSON,
|
|
|
|
HookEvent: ParseHookEvent(form.WebhookForm),
|
|
|
|
IsActive: form.Active,
|
|
|
|
HookTaskType: models.TELEGRAM,
|
|
|
|
Meta: string(meta),
|
|
|
|
OrgID: orCtx.OrgID,
|
|
|
|
IsSystemWebhook: orCtx.IsSystemWebhook,
|
2019-04-18 20:45:02 -06:00
|
|
|
}
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
|
|
|
ctx.ServerError("UpdateEvent", err)
|
|
|
|
return
|
|
|
|
} else if err := models.CreateWebhook(w); err != nil {
|
|
|
|
ctx.ServerError("CreateWebhook", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
|
|
|
|
ctx.Redirect(orCtx.Link)
|
|
|
|
}
|
|
|
|
|
2020-03-28 07:09:55 -06:00
|
|
|
// MatrixHooksNewPost response for creating a Matrix hook
|
|
|
|
func MatrixHooksNewPost(ctx *context.Context, form auth.NewMatrixHookForm) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
|
|
|
ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
|
2020-05-15 13:02:46 -06:00
|
|
|
ctx.Data["HookType"] = models.MATRIX.Name()
|
2020-03-28 07:09:55 -06:00
|
|
|
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(200, orCtx.NewTemplate)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
meta, err := json.Marshal(&webhook.MatrixMeta{
|
|
|
|
HomeserverURL: form.HomeserverURL,
|
|
|
|
Room: form.RoomID,
|
|
|
|
AccessToken: form.AccessToken,
|
|
|
|
MessageType: form.MessageType,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("Marshal", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w := &models.Webhook{
|
|
|
|
RepoID: orCtx.RepoID,
|
|
|
|
URL: fmt.Sprintf("%s/_matrix/client/r0/rooms/%s/send/m.room.message", form.HomeserverURL, form.RoomID),
|
|
|
|
ContentType: models.ContentTypeJSON,
|
|
|
|
HookEvent: ParseHookEvent(form.WebhookForm),
|
|
|
|
IsActive: form.Active,
|
|
|
|
HookTaskType: models.MATRIX,
|
|
|
|
Meta: string(meta),
|
|
|
|
OrgID: orCtx.OrgID,
|
|
|
|
IsSystemWebhook: orCtx.IsSystemWebhook,
|
|
|
|
}
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
|
|
|
ctx.ServerError("UpdateEvent", err)
|
|
|
|
return
|
|
|
|
} else if err := models.CreateWebhook(w); err != nil {
|
|
|
|
ctx.ServerError("CreateWebhook", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
|
|
|
|
ctx.Redirect(orCtx.Link)
|
|
|
|
}
|
|
|
|
|
2019-04-19 08:18:06 -06:00
|
|
|
// MSTeamsHooksNewPost response for creating MS Teams hook
|
|
|
|
func MSTeamsHooksNewPost(ctx *context.Context, form auth.NewMSTeamsHookForm) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
|
|
|
ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
|
2020-05-15 13:02:46 -06:00
|
|
|
ctx.Data["HookType"] = models.MSTEAMS.Name()
|
2019-04-19 08:18:06 -06:00
|
|
|
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(200, orCtx.NewTemplate)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w := &models.Webhook{
|
2020-03-08 16:08:05 -06:00
|
|
|
RepoID: orCtx.RepoID,
|
|
|
|
URL: form.PayloadURL,
|
|
|
|
ContentType: models.ContentTypeJSON,
|
|
|
|
HookEvent: ParseHookEvent(form.WebhookForm),
|
|
|
|
IsActive: form.Active,
|
|
|
|
HookTaskType: models.MSTEAMS,
|
|
|
|
Meta: "",
|
|
|
|
OrgID: orCtx.OrgID,
|
|
|
|
IsSystemWebhook: orCtx.IsSystemWebhook,
|
2019-04-19 08:18:06 -06:00
|
|
|
}
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
|
|
|
ctx.ServerError("UpdateEvent", err)
|
|
|
|
return
|
|
|
|
} else if err := models.CreateWebhook(w); err != nil {
|
|
|
|
ctx.ServerError("CreateWebhook", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
|
|
|
|
ctx.Redirect(orCtx.Link)
|
|
|
|
}
|
|
|
|
|
2016-11-24 00:04:31 -07:00
|
|
|
// SlackHooksNewPost response for creating slack hook
|
2016-03-11 09:56:52 -07:00
|
|
|
func SlackHooksNewPost(ctx *context.Context, form auth.NewSlackHookForm) {
|
2015-12-05 11:24:13 -07:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
|
|
|
ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
|
2020-05-15 13:02:46 -06:00
|
|
|
ctx.Data["HookType"] = models.SLACK.Name()
|
2015-12-05 11:24:13 -07:00
|
|
|
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
|
|
if err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
2015-12-05 11:24:13 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(200, orCtx.NewTemplate)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-10 08:31:08 -06:00
|
|
|
if form.HasInvalidChannel() {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.settings.add_webhook.invalid_channel_name"))
|
2020-03-01 09:51:55 -07:00
|
|
|
ctx.Redirect(orCtx.Link + "/slack/new")
|
2018-09-10 08:31:08 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-03 15:13:25 -07:00
|
|
|
meta, err := json.Marshal(&webhook.SlackMeta{
|
2018-09-10 08:31:08 -06:00
|
|
|
Channel: strings.TrimSpace(form.Channel),
|
2015-12-05 11:24:13 -07:00
|
|
|
Username: form.Username,
|
|
|
|
IconURL: form.IconURL,
|
|
|
|
Color: form.Color,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("Marshal", err)
|
2015-12-05 11:24:13 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w := &models.Webhook{
|
2020-03-08 16:08:05 -06:00
|
|
|
RepoID: orCtx.RepoID,
|
|
|
|
URL: form.PayloadURL,
|
|
|
|
ContentType: models.ContentTypeJSON,
|
|
|
|
HookEvent: ParseHookEvent(form.WebhookForm),
|
|
|
|
IsActive: form.Active,
|
|
|
|
HookTaskType: models.SLACK,
|
|
|
|
Meta: string(meta),
|
|
|
|
OrgID: orCtx.OrgID,
|
|
|
|
IsSystemWebhook: orCtx.IsSystemWebhook,
|
2015-12-05 11:24:13 -07:00
|
|
|
}
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("UpdateEvent", err)
|
2015-12-05 11:24:13 -07:00
|
|
|
return
|
|
|
|
} else if err := models.CreateWebhook(w); err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("CreateWebhook", err)
|
2015-12-05 11:24:13 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
|
2019-03-18 20:33:20 -06:00
|
|
|
ctx.Redirect(orCtx.Link)
|
2015-12-05 11:24:13 -07:00
|
|
|
}
|
|
|
|
|
2020-02-12 01:48:28 -07:00
|
|
|
// FeishuHooksNewPost response for creating feishu hook
|
|
|
|
func FeishuHooksNewPost(ctx *context.Context, form auth.NewFeishuHookForm) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
|
|
|
ctx.Data["Webhook"] = models.Webhook{HookEvent: &models.HookEvent{}}
|
2020-05-15 13:02:46 -06:00
|
|
|
ctx.Data["HookType"] = models.FEISHU.Name()
|
2020-02-12 01:48:28 -07:00
|
|
|
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(200, orCtx.NewTemplate)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w := &models.Webhook{
|
2020-03-08 16:08:05 -06:00
|
|
|
RepoID: orCtx.RepoID,
|
|
|
|
URL: form.PayloadURL,
|
|
|
|
ContentType: models.ContentTypeJSON,
|
|
|
|
HookEvent: ParseHookEvent(form.WebhookForm),
|
|
|
|
IsActive: form.Active,
|
|
|
|
HookTaskType: models.FEISHU,
|
|
|
|
Meta: "",
|
|
|
|
OrgID: orCtx.OrgID,
|
|
|
|
IsSystemWebhook: orCtx.IsSystemWebhook,
|
2020-02-12 01:48:28 -07:00
|
|
|
}
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
|
|
|
ctx.ServerError("UpdateEvent", err)
|
|
|
|
return
|
|
|
|
} else if err := models.CreateWebhook(w); err != nil {
|
|
|
|
ctx.ServerError("CreateWebhook", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
|
|
|
|
ctx.Redirect(orCtx.Link)
|
|
|
|
}
|
|
|
|
|
2016-11-24 00:04:31 -07:00
|
|
|
func checkWebhook(ctx *context.Context) (*orgRepoCtx, *models.Webhook) {
|
2015-12-05 11:24:13 -07:00
|
|
|
ctx.Data["RequireHighlightJS"] = true
|
|
|
|
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
|
|
if err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
2015-12-05 11:24:13 -07:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
ctx.Data["BaseLink"] = orCtx.Link
|
|
|
|
|
2016-07-15 11:02:55 -06:00
|
|
|
var w *models.Webhook
|
|
|
|
if orCtx.RepoID > 0 {
|
|
|
|
w, err = models.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
|
2019-03-18 20:33:20 -06:00
|
|
|
} else if orCtx.OrgID > 0 {
|
2016-07-23 11:08:22 -06:00
|
|
|
w, err = models.GetWebhookByOrgID(ctx.Org.Organization.ID, ctx.ParamsInt64(":id"))
|
2020-03-08 16:08:05 -06:00
|
|
|
} else if orCtx.IsSystemWebhook {
|
|
|
|
w, err = models.GetSystemWebhook(ctx.ParamsInt64(":id"))
|
2019-03-18 20:33:20 -06:00
|
|
|
} else {
|
|
|
|
w, err = models.GetDefaultWebhook(ctx.ParamsInt64(":id"))
|
2016-07-15 11:02:55 -06:00
|
|
|
}
|
2015-12-05 11:24:13 -07:00
|
|
|
if err != nil {
|
|
|
|
if models.IsErrWebhookNotExist(err) {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.NotFound("GetWebhookByID", nil)
|
2015-12-05 11:24:13 -07:00
|
|
|
} else {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("GetWebhookByID", err)
|
2015-12-05 11:24:13 -07:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2017-11-20 21:26:43 -07:00
|
|
|
ctx.Data["HookType"] = w.HookTaskType.Name()
|
2015-12-05 11:24:13 -07:00
|
|
|
switch w.HookTaskType {
|
|
|
|
case models.SLACK:
|
2019-11-03 15:13:25 -07:00
|
|
|
ctx.Data["SlackHook"] = webhook.GetSlackHook(w)
|
2017-08-27 23:06:45 -06:00
|
|
|
case models.DISCORD:
|
2019-11-03 15:13:25 -07:00
|
|
|
ctx.Data["DiscordHook"] = webhook.GetDiscordHook(w)
|
2019-04-18 20:45:02 -06:00
|
|
|
case models.TELEGRAM:
|
2019-11-03 15:13:25 -07:00
|
|
|
ctx.Data["TelegramHook"] = webhook.GetTelegramHook(w)
|
2020-03-28 07:09:55 -06:00
|
|
|
case models.MATRIX:
|
|
|
|
ctx.Data["MatrixHook"] = webhook.GetMatrixHook(w)
|
2015-12-05 11:24:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Data["History"], err = w.History(1)
|
|
|
|
if err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("History", err)
|
2015-12-05 11:24:13 -07:00
|
|
|
}
|
|
|
|
return orCtx, w
|
|
|
|
}
|
|
|
|
|
2016-11-24 00:04:31 -07:00
|
|
|
// WebHooksEdit render editing web hook page
|
2016-03-11 09:56:52 -07:00
|
|
|
func WebHooksEdit(ctx *context.Context) {
|
2015-12-05 11:24:13 -07:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksEdit"] = true
|
|
|
|
|
|
|
|
orCtx, w := checkWebhook(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Webhook"] = w
|
|
|
|
|
|
|
|
ctx.HTML(200, orCtx.NewTemplate)
|
|
|
|
}
|
|
|
|
|
2016-11-24 00:04:31 -07:00
|
|
|
// WebHooksEditPost response for editing web hook
|
2016-03-11 09:56:52 -07:00
|
|
|
func WebHooksEditPost(ctx *context.Context, form auth.NewWebhookForm) {
|
2015-12-05 11:24:13 -07:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksEdit"] = true
|
|
|
|
|
|
|
|
orCtx, w := checkWebhook(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Webhook"] = w
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(200, orCtx.NewTemplate)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-07 13:58:22 -07:00
|
|
|
contentType := models.ContentTypeJSON
|
2016-11-07 09:53:22 -07:00
|
|
|
if models.HookContentType(form.ContentType) == models.ContentTypeForm {
|
|
|
|
contentType = models.ContentTypeForm
|
2015-12-05 11:24:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
w.URL = form.PayloadURL
|
|
|
|
w.ContentType = contentType
|
|
|
|
w.Secret = form.Secret
|
|
|
|
w.HookEvent = ParseHookEvent(form.WebhookForm)
|
|
|
|
w.IsActive = form.Active
|
2019-05-15 06:01:53 -06:00
|
|
|
w.HTTPMethod = form.HTTPMethod
|
2015-12-05 11:24:13 -07:00
|
|
|
if err := w.UpdateEvent(); err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("UpdateEvent", err)
|
2015-12-05 11:24:13 -07:00
|
|
|
return
|
|
|
|
} else if err := models.UpdateWebhook(w); err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("WebHooksEditPost", err)
|
2015-12-05 11:24:13 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
|
2019-03-18 20:33:20 -06:00
|
|
|
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
2017-05-29 01:17:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// GogsHooksEditPost response for editing gogs hook
|
2017-10-08 05:37:31 -06:00
|
|
|
func GogsHooksEditPost(ctx *context.Context, form auth.NewGogshookForm) {
|
2017-05-29 01:17:15 -06:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksEdit"] = true
|
|
|
|
|
|
|
|
orCtx, w := checkWebhook(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Webhook"] = w
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(200, orCtx.NewTemplate)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
contentType := models.ContentTypeJSON
|
|
|
|
if models.HookContentType(form.ContentType) == models.ContentTypeForm {
|
|
|
|
contentType = models.ContentTypeForm
|
|
|
|
}
|
|
|
|
|
|
|
|
w.URL = form.PayloadURL
|
|
|
|
w.ContentType = contentType
|
|
|
|
w.Secret = form.Secret
|
|
|
|
w.HookEvent = ParseHookEvent(form.WebhookForm)
|
|
|
|
w.IsActive = form.Active
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("UpdateEvent", err)
|
2017-05-29 01:17:15 -06:00
|
|
|
return
|
|
|
|
} else if err := models.UpdateWebhook(w); err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("GogsHooksEditPost", err)
|
2017-05-29 01:17:15 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
|
2019-03-18 20:33:20 -06:00
|
|
|
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
2015-12-05 11:24:13 -07:00
|
|
|
}
|
|
|
|
|
2016-11-27 04:59:12 -07:00
|
|
|
// SlackHooksEditPost response for editing slack hook
|
2016-03-11 09:56:52 -07:00
|
|
|
func SlackHooksEditPost(ctx *context.Context, form auth.NewSlackHookForm) {
|
2015-12-05 11:24:13 -07:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksEdit"] = true
|
|
|
|
|
|
|
|
orCtx, w := checkWebhook(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Webhook"] = w
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(200, orCtx.NewTemplate)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-10 08:31:08 -06:00
|
|
|
if form.HasInvalidChannel() {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.settings.add_webhook.invalid_channel_name"))
|
2020-03-01 09:51:55 -07:00
|
|
|
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
2018-09-10 08:31:08 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-03 15:13:25 -07:00
|
|
|
meta, err := json.Marshal(&webhook.SlackMeta{
|
2018-09-10 08:31:08 -06:00
|
|
|
Channel: strings.TrimSpace(form.Channel),
|
2015-12-05 11:24:13 -07:00
|
|
|
Username: form.Username,
|
|
|
|
IconURL: form.IconURL,
|
|
|
|
Color: form.Color,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("Marshal", err)
|
2015-12-05 11:24:13 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.URL = form.PayloadURL
|
|
|
|
w.Meta = string(meta)
|
|
|
|
w.HookEvent = ParseHookEvent(form.WebhookForm)
|
|
|
|
w.IsActive = form.Active
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("UpdateEvent", err)
|
2015-12-05 11:24:13 -07:00
|
|
|
return
|
|
|
|
} else if err := models.UpdateWebhook(w); err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("UpdateWebhook", err)
|
2017-08-27 23:06:45 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
|
2019-03-18 20:33:20 -06:00
|
|
|
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
2017-08-27 23:06:45 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// DiscordHooksEditPost response for editing discord hook
|
|
|
|
func DiscordHooksEditPost(ctx *context.Context, form auth.NewDiscordHookForm) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksEdit"] = true
|
|
|
|
|
|
|
|
orCtx, w := checkWebhook(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Webhook"] = w
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(200, orCtx.NewTemplate)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-03 15:13:25 -07:00
|
|
|
meta, err := json.Marshal(&webhook.DiscordMeta{
|
2017-08-27 23:06:45 -06:00
|
|
|
Username: form.Username,
|
|
|
|
IconURL: form.IconURL,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("Marshal", err)
|
2017-08-27 23:06:45 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.URL = form.PayloadURL
|
|
|
|
w.Meta = string(meta)
|
|
|
|
w.HookEvent = ParseHookEvent(form.WebhookForm)
|
|
|
|
w.IsActive = form.Active
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("UpdateEvent", err)
|
2017-08-27 23:06:45 -06:00
|
|
|
return
|
|
|
|
} else if err := models.UpdateWebhook(w); err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("UpdateWebhook", err)
|
2015-12-05 11:24:13 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
|
2019-03-18 20:33:20 -06:00
|
|
|
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
2015-12-05 11:24:13 -07:00
|
|
|
}
|
|
|
|
|
2017-11-20 21:26:43 -07:00
|
|
|
// DingtalkHooksEditPost response for editing discord hook
|
|
|
|
func DingtalkHooksEditPost(ctx *context.Context, form auth.NewDingtalkHookForm) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksEdit"] = true
|
|
|
|
|
|
|
|
orCtx, w := checkWebhook(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Webhook"] = w
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(200, orCtx.NewTemplate)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.URL = form.PayloadURL
|
|
|
|
w.HookEvent = ParseHookEvent(form.WebhookForm)
|
|
|
|
w.IsActive = form.Active
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("UpdateEvent", err)
|
2017-11-20 21:26:43 -07:00
|
|
|
return
|
|
|
|
} else if err := models.UpdateWebhook(w); err != nil {
|
2018-01-10 14:34:17 -07:00
|
|
|
ctx.ServerError("UpdateWebhook", err)
|
2017-11-20 21:26:43 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
|
2019-03-18 20:33:20 -06:00
|
|
|
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
2017-11-20 21:26:43 -07:00
|
|
|
}
|
|
|
|
|
2019-04-18 20:45:02 -06:00
|
|
|
// TelegramHooksEditPost response for editing discord hook
|
|
|
|
func TelegramHooksEditPost(ctx *context.Context, form auth.NewTelegramHookForm) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksEdit"] = true
|
|
|
|
|
|
|
|
orCtx, w := checkWebhook(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Webhook"] = w
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(200, orCtx.NewTemplate)
|
|
|
|
return
|
|
|
|
}
|
2019-11-03 15:13:25 -07:00
|
|
|
meta, err := json.Marshal(&webhook.TelegramMeta{
|
2019-04-18 20:45:02 -06:00
|
|
|
BotToken: form.BotToken,
|
|
|
|
ChatID: form.ChatID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("Marshal", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Meta = string(meta)
|
|
|
|
w.URL = fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s", form.BotToken, form.ChatID)
|
|
|
|
w.HookEvent = ParseHookEvent(form.WebhookForm)
|
|
|
|
w.IsActive = form.Active
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
|
|
|
ctx.ServerError("UpdateEvent", err)
|
|
|
|
return
|
|
|
|
} else if err := models.UpdateWebhook(w); err != nil {
|
|
|
|
ctx.ServerError("UpdateWebhook", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
|
|
|
|
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
|
|
|
}
|
|
|
|
|
2020-03-28 07:09:55 -06:00
|
|
|
// MatrixHooksEditPost response for editing a Matrix hook
|
|
|
|
func MatrixHooksEditPost(ctx *context.Context, form auth.NewMatrixHookForm) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksEdit"] = true
|
|
|
|
|
|
|
|
orCtx, w := checkWebhook(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Webhook"] = w
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(200, orCtx.NewTemplate)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
meta, err := json.Marshal(&webhook.MatrixMeta{
|
|
|
|
HomeserverURL: form.HomeserverURL,
|
|
|
|
Room: form.RoomID,
|
|
|
|
AccessToken: form.AccessToken,
|
|
|
|
MessageType: form.MessageType,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("Marshal", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Meta = string(meta)
|
|
|
|
w.URL = fmt.Sprintf("%s/_matrix/client/r0/rooms/%s/send/m.room.message", form.HomeserverURL, form.RoomID)
|
|
|
|
|
|
|
|
w.HookEvent = ParseHookEvent(form.WebhookForm)
|
|
|
|
w.IsActive = form.Active
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
|
|
|
ctx.ServerError("UpdateEvent", err)
|
|
|
|
return
|
|
|
|
} else if err := models.UpdateWebhook(w); err != nil {
|
|
|
|
ctx.ServerError("UpdateWebhook", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
|
|
|
|
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
|
|
|
}
|
|
|
|
|
2019-04-19 08:18:06 -06:00
|
|
|
// MSTeamsHooksEditPost response for editing MS Teams hook
|
|
|
|
func MSTeamsHooksEditPost(ctx *context.Context, form auth.NewMSTeamsHookForm) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksEdit"] = true
|
|
|
|
|
|
|
|
orCtx, w := checkWebhook(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Webhook"] = w
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(200, orCtx.NewTemplate)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.URL = form.PayloadURL
|
|
|
|
w.HookEvent = ParseHookEvent(form.WebhookForm)
|
|
|
|
w.IsActive = form.Active
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
|
|
|
ctx.ServerError("UpdateEvent", err)
|
|
|
|
return
|
|
|
|
} else if err := models.UpdateWebhook(w); err != nil {
|
|
|
|
ctx.ServerError("UpdateWebhook", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:48:28 -07:00
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
|
|
|
|
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
|
|
|
}
|
|
|
|
|
|
|
|
// FeishuHooksEditPost response for editing feishu hook
|
|
|
|
func FeishuHooksEditPost(ctx *context.Context, form auth.NewFeishuHookForm) {
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksEdit"] = true
|
|
|
|
|
|
|
|
orCtx, w := checkWebhook(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Webhook"] = w
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(200, orCtx.NewTemplate)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.URL = form.PayloadURL
|
|
|
|
w.HookEvent = ParseHookEvent(form.WebhookForm)
|
|
|
|
w.IsActive = form.Active
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
|
|
|
ctx.ServerError("UpdateEvent", err)
|
|
|
|
return
|
|
|
|
} else if err := models.UpdateWebhook(w); err != nil {
|
|
|
|
ctx.ServerError("UpdateWebhook", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-19 08:18:06 -06:00
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
|
|
|
|
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
|
|
|
}
|
|
|
|
|
2016-11-24 00:04:31 -07:00
|
|
|
// TestWebhook test if web hook is work fine
|
2016-03-11 09:56:52 -07:00
|
|
|
func TestWebhook(ctx *context.Context) {
|
2017-08-29 08:55:24 -06:00
|
|
|
hookID := ctx.ParamsInt64(":id")
|
|
|
|
w, err := models.GetWebhookByRepoID(ctx.Repo.Repository.ID, hookID)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Flash.Error("GetWebhookByID: " + err.Error())
|
|
|
|
ctx.Status(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-15 06:53:47 -06:00
|
|
|
// Grab latest commit or fake one if it's empty repository.
|
|
|
|
commit := ctx.Repo.Commit
|
|
|
|
if commit == nil {
|
|
|
|
ghost := models.NewGhostUser()
|
|
|
|
commit = &git.Commit{
|
2016-12-22 02:30:52 -07:00
|
|
|
ID: git.MustIDFromString(git.EmptySHA),
|
2016-08-15 06:53:47 -06:00
|
|
|
Author: ghost.NewGitSig(),
|
|
|
|
Committer: ghost.NewGitSig(),
|
|
|
|
CommitMessage: "This is a fake commit",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-14 05:17:26 -06:00
|
|
|
apiUser := ctx.User.APIFormat()
|
2015-12-05 11:24:13 -07:00
|
|
|
p := &api.PushPayload{
|
2016-12-22 02:30:52 -07:00
|
|
|
Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
|
2016-08-15 06:53:47 -06:00
|
|
|
Before: commit.ID.String(),
|
|
|
|
After: commit.ID.String(),
|
2015-12-05 11:24:13 -07:00
|
|
|
Commits: []*api.PayloadCommit{
|
|
|
|
{
|
2016-08-15 06:53:47 -06:00
|
|
|
ID: commit.ID.String(),
|
|
|
|
Message: commit.Message(),
|
2016-08-16 11:19:09 -06:00
|
|
|
URL: ctx.Repo.Repository.HTMLURL() + "/commit/" + commit.ID.String(),
|
2016-08-14 05:17:26 -06:00
|
|
|
Author: &api.PayloadUser{
|
2016-08-15 06:53:47 -06:00
|
|
|
Name: commit.Author.Name,
|
|
|
|
Email: commit.Author.Email,
|
2016-08-09 23:01:57 -06:00
|
|
|
},
|
2016-08-14 05:17:26 -06:00
|
|
|
Committer: &api.PayloadUser{
|
2016-08-15 06:53:47 -06:00
|
|
|
Name: commit.Committer.Name,
|
|
|
|
Email: commit.Committer.Email,
|
2015-12-05 11:24:13 -07:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2016-12-05 16:48:51 -07:00
|
|
|
Repo: ctx.Repo.Repository.APIFormat(models.AccessModeNone),
|
2016-08-14 05:17:26 -06:00
|
|
|
Pusher: apiUser,
|
|
|
|
Sender: apiUser,
|
2015-12-05 11:24:13 -07:00
|
|
|
}
|
2019-11-01 16:51:22 -06:00
|
|
|
if err := webhook.PrepareWebhook(w, ctx.Repo.Repository, models.HookEventPush, p); err != nil {
|
2017-08-29 08:55:24 -06:00
|
|
|
ctx.Flash.Error("PrepareWebhook: " + err.Error())
|
2015-12-05 11:24:13 -07:00
|
|
|
ctx.Status(500)
|
|
|
|
} else {
|
|
|
|
ctx.Flash.Info(ctx.Tr("repo.settings.webhook.test_delivery_success"))
|
|
|
|
ctx.Status(200)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 00:04:31 -07:00
|
|
|
// DeleteWebhook delete a webhook
|
2016-03-11 09:56:52 -07:00
|
|
|
func DeleteWebhook(ctx *context.Context) {
|
2016-07-16 18:33:59 -06:00
|
|
|
if err := models.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.QueryInt64("id")); err != nil {
|
|
|
|
ctx.Flash.Error("DeleteWebhookByRepoID: " + err.Error())
|
2015-12-05 11:24:13 -07:00
|
|
|
} else {
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.JSON(200, map[string]interface{}{
|
|
|
|
"redirect": ctx.Repo.RepoLink + "/settings/hooks",
|
|
|
|
})
|
|
|
|
}
|