2015-12-05 19:24:13 +01:00
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
2017-05-29 09:17:15 +02:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
2015-12-05 19:24:13 +01:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-04-05 17:30:52 +02:00
|
|
|
"net/http"
|
2021-11-16 19:18:25 +01:00
|
|
|
"net/url"
|
2019-03-19 03:33:20 +01:00
|
|
|
"path"
|
2015-12-05 19:24:13 +01:00
|
|
|
"strings"
|
|
|
|
|
2021-11-28 12:58:28 +01:00
|
|
|
"code.gitea.io/gitea/models/perm"
|
2021-11-24 10:49:20 +01:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-11-10 06:13:16 +01:00
|
|
|
"code.gitea.io/gitea/models/webhook"
|
2016-11-10 17:24:48 +01:00
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/context"
|
2020-10-17 06:23:08 +02:00
|
|
|
"code.gitea.io/gitea/modules/convert"
|
2019-03-27 10:33:00 +01:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2021-07-24 18:03:58 +02:00
|
|
|
"code.gitea.io/gitea/modules/json"
|
2016-11-10 17:24:48 +01:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2019-05-11 12:21:34 +02:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2020-12-25 10:59:32 +01:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2021-01-26 16:36:53 +01:00
|
|
|
"code.gitea.io/gitea/modules/web"
|
2021-04-06 21:44:05 +02:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
2021-11-10 06:13:16 +01:00
|
|
|
webhook_service "code.gitea.io/gitea/services/webhook"
|
2015-12-05 19:24:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-03-19 03:33:20 +01: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 19:24:13 +01:00
|
|
|
)
|
|
|
|
|
2016-11-24 08:04:31 +01:00
|
|
|
// Webhooks render web hooks list page
|
2016-03-11 17:56:52 +01:00
|
|
|
func Webhooks(ctx *context.Context) {
|
2015-12-05 19:24:13 +01:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings.hooks")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
2019-03-19 03:33:20 +01:00
|
|
|
ctx.Data["BaseLink"] = ctx.Repo.RepoLink + "/settings/hooks"
|
2021-01-15 00:24:03 +01:00
|
|
|
ctx.Data["BaseLinkNew"] = ctx.Repo.RepoLink + "/settings/hooks"
|
2018-01-03 16:50:09 +01:00
|
|
|
ctx.Data["Description"] = ctx.Tr("repo.settings.hooks_desc", "https://docs.gitea.io/en-us/webhooks/")
|
2015-12-05 19:24:13 +01:00
|
|
|
|
2022-05-20 16:08:52 +02:00
|
|
|
ws, err := webhook.ListWebhooksByOpts(ctx, &webhook.ListWebhookOptions{RepoID: ctx.Repo.Repository.ID})
|
2015-12-05 19:24:13 +01:00
|
|
|
if err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("GetWebhooksByRepoID", err)
|
2015-12-05 19:24:13 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.Data["Webhooks"] = ws
|
|
|
|
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, tplHooks)
|
2015-12-05 19:24:13 +01:00
|
|
|
}
|
|
|
|
|
2016-11-24 08:04:31 +01:00
|
|
|
type orgRepoCtx struct {
|
2020-03-08 23:08:05 +01:00
|
|
|
OrgID int64
|
|
|
|
RepoID int64
|
|
|
|
IsAdmin bool
|
|
|
|
IsSystemWebhook bool
|
|
|
|
Link string
|
2021-01-15 00:24:03 +01:00
|
|
|
LinkNew string
|
2020-03-08 23:08:05 +01:00
|
|
|
NewTemplate base.TplName
|
2015-12-05 19:24:13 +01:00
|
|
|
}
|
|
|
|
|
2020-03-08 23:08:05 +01:00
|
|
|
// getOrgRepoCtx determines whether this is a repo, organization, or admin (both default and system) context.
|
2016-11-24 08:04:31 +01:00
|
|
|
func getOrgRepoCtx(ctx *context.Context) (*orgRepoCtx, error) {
|
2015-12-05 19:24:13 +01:00
|
|
|
if len(ctx.Repo.RepoLink) > 0 {
|
2016-11-24 08:04:31 +01:00
|
|
|
return &orgRepoCtx{
|
2015-12-05 19:24:13 +01:00
|
|
|
RepoID: ctx.Repo.Repository.ID,
|
2019-03-19 03:33:20 +01:00
|
|
|
Link: path.Join(ctx.Repo.RepoLink, "settings/hooks"),
|
2021-01-15 00:24:03 +01:00
|
|
|
LinkNew: path.Join(ctx.Repo.RepoLink, "settings/hooks"),
|
2016-11-07 21:58:22 +01:00
|
|
|
NewTemplate: tplHookNew,
|
2015-12-05 19:24:13 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ctx.Org.OrgLink) > 0 {
|
2016-11-24 08:04:31 +01:00
|
|
|
return &orgRepoCtx{
|
2016-07-23 19:08:22 +02:00
|
|
|
OrgID: ctx.Org.Organization.ID,
|
2019-03-19 03:33:20 +01:00
|
|
|
Link: path.Join(ctx.Org.OrgLink, "settings/hooks"),
|
2021-01-15 00:24:03 +01:00
|
|
|
LinkNew: path.Join(ctx.Org.OrgLink, "settings/hooks"),
|
2016-11-07 21:58:22 +01:00
|
|
|
NewTemplate: tplOrgHookNew,
|
2015-12-05 19:24:13 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-03-22 08:03:22 +01:00
|
|
|
if ctx.Doer.IsAdmin {
|
2020-03-08 23:08:05 +01:00
|
|
|
// Are we looking at default webhooks?
|
2021-01-15 00:24:03 +01:00
|
|
|
if ctx.Params(":configType") == "default-hooks" {
|
2020-03-08 23:08:05 +01:00
|
|
|
return &orgRepoCtx{
|
|
|
|
IsAdmin: true,
|
|
|
|
Link: path.Join(setting.AppSubURL, "/admin/hooks"),
|
2021-01-15 00:24:03 +01:00
|
|
|
LinkNew: path.Join(setting.AppSubURL, "/admin/default-hooks"),
|
2020-03-08 23:08:05 +01:00
|
|
|
NewTemplate: tplAdminHookNew,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must be system webhooks instead
|
2019-03-19 03:33:20 +01:00
|
|
|
return &orgRepoCtx{
|
2020-03-08 23:08:05 +01:00
|
|
|
IsAdmin: true,
|
|
|
|
IsSystemWebhook: true,
|
2021-01-15 00:24:03 +01:00
|
|
|
Link: path.Join(setting.AppSubURL, "/admin/hooks"),
|
|
|
|
LinkNew: path.Join(setting.AppSubURL, "/admin/system-hooks"),
|
2020-03-08 23:08:05 +01:00
|
|
|
NewTemplate: tplAdminHookNew,
|
2019-03-19 03:33:20 +01:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-02-26 13:15:32 +01:00
|
|
|
return nil, errors.New("unable to set OrgRepo context")
|
2015-12-05 19:24:13 +01:00
|
|
|
}
|
|
|
|
|
2016-03-11 17:56:52 +01:00
|
|
|
func checkHookType(ctx *context.Context) string {
|
2015-12-05 19:24:13 +01:00
|
|
|
hookType := strings.ToLower(ctx.Params(":type"))
|
2020-12-25 10:59:32 +01:00
|
|
|
if !util.IsStringInSlice(hookType, setting.Webhook.Types, true) {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.NotFound("checkHookType", nil)
|
2015-12-05 19:24:13 +01:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return hookType
|
|
|
|
}
|
|
|
|
|
2016-11-24 08:04:31 +01:00
|
|
|
// WebhooksNew render creating webhook page
|
2016-03-11 17:56:52 +01:00
|
|
|
func WebhooksNew(ctx *context.Context) {
|
2015-12-05 19:24:13 +01:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
|
2021-11-10 06:13:16 +01:00
|
|
|
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
|
2015-12-05 19:24:13 +01:00
|
|
|
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
|
|
if err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
2015-12-05 19:24:13 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-08 23:08:05 +01:00
|
|
|
if orCtx.IsAdmin && orCtx.IsSystemWebhook {
|
|
|
|
ctx.Data["PageIsAdminSystemHooks"] = true
|
|
|
|
ctx.Data["PageIsAdminSystemHooksNew"] = true
|
|
|
|
} else if orCtx.IsAdmin {
|
2021-01-15 00:24:03 +01:00
|
|
|
ctx.Data["PageIsAdminDefaultHooks"] = true
|
|
|
|
ctx.Data["PageIsAdminDefaultHooksNew"] = true
|
2019-03-19 03:33:20 +01:00
|
|
|
} else {
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
|
|
|
}
|
|
|
|
|
2017-08-28 07:06:45 +02:00
|
|
|
hookType := checkHookType(ctx)
|
|
|
|
ctx.Data["HookType"] = hookType
|
2015-12-05 19:24:13 +01:00
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
2017-08-28 07:06:45 +02:00
|
|
|
if hookType == "discord" {
|
|
|
|
ctx.Data["DiscordHook"] = map[string]interface{}{
|
|
|
|
"Username": "Gitea",
|
|
|
|
}
|
|
|
|
}
|
2021-01-15 00:24:03 +01:00
|
|
|
ctx.Data["BaseLink"] = orCtx.LinkNew
|
2015-12-05 19:24:13 +01:00
|
|
|
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
2015-12-05 19:24:13 +01:00
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
// ParseHookEvent convert web form content to webhook.HookEvent
|
|
|
|
func ParseHookEvent(form forms.WebhookForm) *webhook.HookEvent {
|
|
|
|
return &webhook.HookEvent{
|
2015-12-05 19:24:13 +01:00
|
|
|
PushOnly: form.PushOnly(),
|
|
|
|
SendEverything: form.SendEverything(),
|
|
|
|
ChooseEvents: form.ChooseEvents(),
|
2021-11-10 06:13:16 +01:00
|
|
|
HookEvents: webhook.HookEvents{
|
2020-03-06 06:10:48 +01: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,
|
2022-03-30 10:42:47 +02:00
|
|
|
Package: form.Package,
|
2015-12-05 19:24:13 +01:00
|
|
|
},
|
2019-09-09 07:48:21 +02:00
|
|
|
BranchFilter: form.BranchFilter,
|
2015-12-05 19:24:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-08 23:08:05 +01:00
|
|
|
// GiteaHooksNewPost response for creating Gitea webhook
|
2021-01-26 16:36:53 +01:00
|
|
|
func GiteaHooksNewPost(ctx *context.Context) {
|
2021-04-06 21:44:05 +02:00
|
|
|
form := web.GetForm(ctx).(*forms.NewWebhookForm)
|
2017-05-29 09:17:15 +02:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
2021-11-10 06:13:16 +01:00
|
|
|
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
|
|
|
|
ctx.Data["HookType"] = webhook.GITEA
|
2017-05-29 09:17:15 +02:00
|
|
|
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
|
|
if err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
2017-05-29 09:17:15 +02:00
|
|
|
return
|
|
|
|
}
|
2021-01-15 00:24:03 +01:00
|
|
|
ctx.Data["BaseLink"] = orCtx.LinkNew
|
2017-05-29 09:17:15 +02:00
|
|
|
|
|
|
|
if ctx.HasError() {
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
2017-05-29 09:17:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
contentType := webhook.ContentTypeJSON
|
|
|
|
if webhook.HookContentType(form.ContentType) == webhook.ContentTypeForm {
|
|
|
|
contentType = webhook.ContentTypeForm
|
2017-05-29 09:17:15 +02:00
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
w := &webhook.Webhook{
|
2020-03-08 23:08:05 +01:00
|
|
|
RepoID: orCtx.RepoID,
|
|
|
|
URL: form.PayloadURL,
|
|
|
|
HTTPMethod: form.HTTPMethod,
|
|
|
|
ContentType: contentType,
|
|
|
|
Secret: form.Secret,
|
|
|
|
HookEvent: ParseHookEvent(form.WebhookForm),
|
|
|
|
IsActive: form.Active,
|
2021-11-10 06:13:16 +01:00
|
|
|
Type: webhook.GITEA,
|
2020-03-08 23:08:05 +01:00
|
|
|
OrgID: orCtx.OrgID,
|
|
|
|
IsSystemWebhook: orCtx.IsSystemWebhook,
|
2017-05-29 09:17:15 +02:00
|
|
|
}
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("UpdateEvent", err)
|
2017-05-29 09:17:15 +02:00
|
|
|
return
|
2022-03-22 16:22:54 +01:00
|
|
|
} else if err := webhook.CreateWebhook(ctx, w); err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("CreateWebhook", err)
|
2017-05-29 09:17:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
|
2019-03-19 03:33:20 +01:00
|
|
|
ctx.Redirect(orCtx.Link)
|
2017-05-29 09:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GogsHooksNewPost response for creating webhook
|
2021-01-26 16:36:53 +01:00
|
|
|
func GogsHooksNewPost(ctx *context.Context) {
|
2021-04-06 21:44:05 +02:00
|
|
|
form := web.GetForm(ctx).(*forms.NewGogshookForm)
|
2021-11-10 06:13:16 +01:00
|
|
|
newGogsWebhookPost(ctx, *form, webhook.GOGS)
|
2019-06-12 21:41:28 +02:00
|
|
|
}
|
|
|
|
|
2019-10-02 14:58:40 +02:00
|
|
|
// newGogsWebhookPost response for creating gogs hook
|
2021-11-10 06:13:16 +01:00
|
|
|
func newGogsWebhookPost(ctx *context.Context, form forms.NewGogshookForm, kind webhook.HookType) {
|
2015-12-05 19:24:13 +01:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
2021-11-10 06:13:16 +01:00
|
|
|
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
|
|
|
|
ctx.Data["HookType"] = webhook.GOGS
|
2015-12-05 19:24:13 +01:00
|
|
|
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
|
|
if err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
2015-12-05 19:24:13 +01:00
|
|
|
return
|
|
|
|
}
|
2021-01-15 00:24:03 +01:00
|
|
|
ctx.Data["BaseLink"] = orCtx.LinkNew
|
2015-12-05 19:24:13 +01:00
|
|
|
|
|
|
|
if ctx.HasError() {
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
2015-12-05 19:24:13 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
contentType := webhook.ContentTypeJSON
|
|
|
|
if webhook.HookContentType(form.ContentType) == webhook.ContentTypeForm {
|
|
|
|
contentType = webhook.ContentTypeForm
|
2015-12-05 19:24:13 +01:00
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
w := &webhook.Webhook{
|
2020-03-08 23:08:05 +01:00
|
|
|
RepoID: orCtx.RepoID,
|
|
|
|
URL: form.PayloadURL,
|
|
|
|
ContentType: contentType,
|
|
|
|
Secret: form.Secret,
|
|
|
|
HookEvent: ParseHookEvent(form.WebhookForm),
|
|
|
|
IsActive: form.Active,
|
2020-12-09 18:20:13 +01:00
|
|
|
Type: kind,
|
2020-03-08 23:08:05 +01:00
|
|
|
OrgID: orCtx.OrgID,
|
|
|
|
IsSystemWebhook: orCtx.IsSystemWebhook,
|
2015-12-05 19:24:13 +01:00
|
|
|
}
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("UpdateEvent", err)
|
2015-12-05 19:24:13 +01:00
|
|
|
return
|
2022-03-22 16:22:54 +01:00
|
|
|
} else if err := webhook.CreateWebhook(ctx, w); err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("CreateWebhook", err)
|
2015-12-05 19:24:13 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
|
2019-03-19 03:33:20 +01:00
|
|
|
ctx.Redirect(orCtx.Link)
|
2015-12-05 19:24:13 +01:00
|
|
|
}
|
|
|
|
|
2017-08-28 07:06:45 +02:00
|
|
|
// DiscordHooksNewPost response for creating discord hook
|
2021-01-26 16:36:53 +01:00
|
|
|
func DiscordHooksNewPost(ctx *context.Context) {
|
2021-04-06 21:44:05 +02:00
|
|
|
form := web.GetForm(ctx).(*forms.NewDiscordHookForm)
|
2017-08-28 07:06:45 +02:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
2021-11-10 06:13:16 +01:00
|
|
|
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
|
|
|
|
ctx.Data["HookType"] = webhook.DISCORD
|
2017-08-28 07:06:45 +02:00
|
|
|
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
|
|
if err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
2017-08-28 07:06:45 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
2017-08-28 07:06:45 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
meta, err := json.Marshal(&webhook_service.DiscordMeta{
|
2017-08-28 07:06:45 +02:00
|
|
|
Username: form.Username,
|
|
|
|
IconURL: form.IconURL,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("Marshal", err)
|
2017-08-28 07:06:45 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
w := &webhook.Webhook{
|
2020-03-08 23:08:05 +01:00
|
|
|
RepoID: orCtx.RepoID,
|
|
|
|
URL: form.PayloadURL,
|
2021-11-10 06:13:16 +01:00
|
|
|
ContentType: webhook.ContentTypeJSON,
|
2020-03-08 23:08:05 +01:00
|
|
|
HookEvent: ParseHookEvent(form.WebhookForm),
|
|
|
|
IsActive: form.Active,
|
2021-11-10 06:13:16 +01:00
|
|
|
Type: webhook.DISCORD,
|
2020-03-08 23:08:05 +01:00
|
|
|
Meta: string(meta),
|
|
|
|
OrgID: orCtx.OrgID,
|
|
|
|
IsSystemWebhook: orCtx.IsSystemWebhook,
|
2017-08-28 07:06:45 +02:00
|
|
|
}
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("UpdateEvent", err)
|
2017-08-28 07:06:45 +02:00
|
|
|
return
|
2022-03-22 16:22:54 +01:00
|
|
|
} else if err := webhook.CreateWebhook(ctx, w); err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("CreateWebhook", err)
|
2017-08-28 07:06:45 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
|
2019-03-19 03:33:20 +01:00
|
|
|
ctx.Redirect(orCtx.Link)
|
2017-08-28 07:06:45 +02:00
|
|
|
}
|
|
|
|
|
2017-11-21 05:26:43 +01:00
|
|
|
// DingtalkHooksNewPost response for creating dingtalk hook
|
2021-01-26 16:36:53 +01:00
|
|
|
func DingtalkHooksNewPost(ctx *context.Context) {
|
2021-04-06 21:44:05 +02:00
|
|
|
form := web.GetForm(ctx).(*forms.NewDingtalkHookForm)
|
2017-11-21 05:26:43 +01:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
2021-11-10 06:13:16 +01:00
|
|
|
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
|
|
|
|
ctx.Data["HookType"] = webhook.DINGTALK
|
2017-11-21 05:26:43 +01:00
|
|
|
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
|
|
if err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
2017-11-21 05:26:43 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
2017-11-21 05:26:43 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
w := &webhook.Webhook{
|
2020-03-08 23:08:05 +01:00
|
|
|
RepoID: orCtx.RepoID,
|
|
|
|
URL: form.PayloadURL,
|
2021-11-10 06:13:16 +01:00
|
|
|
ContentType: webhook.ContentTypeJSON,
|
2020-03-08 23:08:05 +01:00
|
|
|
HookEvent: ParseHookEvent(form.WebhookForm),
|
|
|
|
IsActive: form.Active,
|
2021-11-10 06:13:16 +01:00
|
|
|
Type: webhook.DINGTALK,
|
2020-03-08 23:08:05 +01:00
|
|
|
Meta: "",
|
|
|
|
OrgID: orCtx.OrgID,
|
|
|
|
IsSystemWebhook: orCtx.IsSystemWebhook,
|
2017-11-21 05:26:43 +01:00
|
|
|
}
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("UpdateEvent", err)
|
2017-11-21 05:26:43 +01:00
|
|
|
return
|
2022-03-22 16:22:54 +01:00
|
|
|
} else if err := webhook.CreateWebhook(ctx, w); err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("CreateWebhook", err)
|
2017-11-21 05:26:43 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
|
2019-03-19 03:33:20 +01:00
|
|
|
ctx.Redirect(orCtx.Link)
|
2017-11-21 05:26:43 +01:00
|
|
|
}
|
|
|
|
|
2019-04-19 04:45:02 +02:00
|
|
|
// TelegramHooksNewPost response for creating telegram hook
|
2021-01-26 16:36:53 +01:00
|
|
|
func TelegramHooksNewPost(ctx *context.Context) {
|
2021-04-06 21:44:05 +02:00
|
|
|
form := web.GetForm(ctx).(*forms.NewTelegramHookForm)
|
2019-04-19 04:45:02 +02:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
2021-11-10 06:13:16 +01:00
|
|
|
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
|
|
|
|
ctx.Data["HookType"] = webhook.TELEGRAM
|
2019-04-19 04:45:02 +02:00
|
|
|
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
2019-04-19 04:45:02 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
meta, err := json.Marshal(&webhook_service.TelegramMeta{
|
2019-04-19 04:45:02 +02:00
|
|
|
BotToken: form.BotToken,
|
|
|
|
ChatID: form.ChatID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("Marshal", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
w := &webhook.Webhook{
|
2020-03-08 23:08:05 +01:00
|
|
|
RepoID: orCtx.RepoID,
|
2021-11-16 19:18:25 +01:00
|
|
|
URL: fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s", url.PathEscape(form.BotToken), url.QueryEscape(form.ChatID)),
|
2021-11-10 06:13:16 +01:00
|
|
|
ContentType: webhook.ContentTypeJSON,
|
2020-03-08 23:08:05 +01:00
|
|
|
HookEvent: ParseHookEvent(form.WebhookForm),
|
|
|
|
IsActive: form.Active,
|
2021-11-10 06:13:16 +01:00
|
|
|
Type: webhook.TELEGRAM,
|
2020-03-08 23:08:05 +01:00
|
|
|
Meta: string(meta),
|
|
|
|
OrgID: orCtx.OrgID,
|
|
|
|
IsSystemWebhook: orCtx.IsSystemWebhook,
|
2019-04-19 04:45:02 +02:00
|
|
|
}
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
|
|
|
ctx.ServerError("UpdateEvent", err)
|
|
|
|
return
|
2022-03-22 16:22:54 +01:00
|
|
|
} else if err := webhook.CreateWebhook(ctx, w); err != nil {
|
2019-04-19 04:45:02 +02:00
|
|
|
ctx.ServerError("CreateWebhook", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
|
|
|
|
ctx.Redirect(orCtx.Link)
|
|
|
|
}
|
|
|
|
|
2020-03-28 14:09:55 +01:00
|
|
|
// MatrixHooksNewPost response for creating a Matrix hook
|
2021-01-26 16:36:53 +01:00
|
|
|
func MatrixHooksNewPost(ctx *context.Context) {
|
2021-04-06 21:44:05 +02:00
|
|
|
form := web.GetForm(ctx).(*forms.NewMatrixHookForm)
|
2020-03-28 14:09:55 +01:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
2021-11-10 06:13:16 +01:00
|
|
|
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
|
|
|
|
ctx.Data["HookType"] = webhook.MATRIX
|
2020-03-28 14:09:55 +01:00
|
|
|
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
2020-03-28 14:09:55 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
meta, err := json.Marshal(&webhook_service.MatrixMeta{
|
2020-03-28 14:09:55 +01:00
|
|
|
HomeserverURL: form.HomeserverURL,
|
|
|
|
Room: form.RoomID,
|
|
|
|
AccessToken: form.AccessToken,
|
|
|
|
MessageType: form.MessageType,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("Marshal", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
w := &webhook.Webhook{
|
2020-03-28 14:09:55 +01:00
|
|
|
RepoID: orCtx.RepoID,
|
2021-11-16 19:18:25 +01:00
|
|
|
URL: fmt.Sprintf("%s/_matrix/client/r0/rooms/%s/send/m.room.message", form.HomeserverURL, url.PathEscape(form.RoomID)),
|
2021-11-10 06:13:16 +01:00
|
|
|
ContentType: webhook.ContentTypeJSON,
|
2020-07-31 00:04:19 +02:00
|
|
|
HTTPMethod: "PUT",
|
2020-03-28 14:09:55 +01:00
|
|
|
HookEvent: ParseHookEvent(form.WebhookForm),
|
|
|
|
IsActive: form.Active,
|
2021-11-10 06:13:16 +01:00
|
|
|
Type: webhook.MATRIX,
|
2020-03-28 14:09:55 +01:00
|
|
|
Meta: string(meta),
|
|
|
|
OrgID: orCtx.OrgID,
|
|
|
|
IsSystemWebhook: orCtx.IsSystemWebhook,
|
|
|
|
}
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
|
|
|
ctx.ServerError("UpdateEvent", err)
|
|
|
|
return
|
2022-03-22 16:22:54 +01:00
|
|
|
} else if err := webhook.CreateWebhook(ctx, w); err != nil {
|
2020-03-28 14:09:55 +01:00
|
|
|
ctx.ServerError("CreateWebhook", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
|
|
|
|
ctx.Redirect(orCtx.Link)
|
|
|
|
}
|
|
|
|
|
2019-04-19 16:18:06 +02:00
|
|
|
// MSTeamsHooksNewPost response for creating MS Teams hook
|
2021-01-26 16:36:53 +01:00
|
|
|
func MSTeamsHooksNewPost(ctx *context.Context) {
|
2021-04-06 21:44:05 +02:00
|
|
|
form := web.GetForm(ctx).(*forms.NewMSTeamsHookForm)
|
2019-04-19 16:18:06 +02:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
2021-11-10 06:13:16 +01:00
|
|
|
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
|
|
|
|
ctx.Data["HookType"] = webhook.MSTEAMS
|
2019-04-19 16:18:06 +02:00
|
|
|
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
2019-04-19 16:18:06 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
w := &webhook.Webhook{
|
2020-03-08 23:08:05 +01:00
|
|
|
RepoID: orCtx.RepoID,
|
|
|
|
URL: form.PayloadURL,
|
2021-11-10 06:13:16 +01:00
|
|
|
ContentType: webhook.ContentTypeJSON,
|
2020-03-08 23:08:05 +01:00
|
|
|
HookEvent: ParseHookEvent(form.WebhookForm),
|
|
|
|
IsActive: form.Active,
|
2021-11-10 06:13:16 +01:00
|
|
|
Type: webhook.MSTEAMS,
|
2020-03-08 23:08:05 +01:00
|
|
|
Meta: "",
|
|
|
|
OrgID: orCtx.OrgID,
|
|
|
|
IsSystemWebhook: orCtx.IsSystemWebhook,
|
2019-04-19 16:18:06 +02:00
|
|
|
}
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
|
|
|
ctx.ServerError("UpdateEvent", err)
|
|
|
|
return
|
2022-03-22 16:22:54 +01:00
|
|
|
} else if err := webhook.CreateWebhook(ctx, w); err != nil {
|
2019-04-19 16:18:06 +02:00
|
|
|
ctx.ServerError("CreateWebhook", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
|
|
|
|
ctx.Redirect(orCtx.Link)
|
|
|
|
}
|
|
|
|
|
2016-11-24 08:04:31 +01:00
|
|
|
// SlackHooksNewPost response for creating slack hook
|
2021-01-26 16:36:53 +01:00
|
|
|
func SlackHooksNewPost(ctx *context.Context) {
|
2021-04-06 21:44:05 +02:00
|
|
|
form := web.GetForm(ctx).(*forms.NewSlackHookForm)
|
2015-12-05 19:24:13 +01:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
2021-11-10 06:13:16 +01:00
|
|
|
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
|
|
|
|
ctx.Data["HookType"] = webhook.SLACK
|
2015-12-05 19:24:13 +01:00
|
|
|
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
|
|
if err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
2015-12-05 19:24:13 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
2015-12-05 19:24:13 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-10 16:31:08 +02:00
|
|
|
if form.HasInvalidChannel() {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.settings.add_webhook.invalid_channel_name"))
|
2021-01-15 00:24:03 +01:00
|
|
|
ctx.Redirect(orCtx.LinkNew + "/slack/new")
|
2018-09-10 16:31:08 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
meta, err := json.Marshal(&webhook_service.SlackMeta{
|
2018-09-10 16:31:08 +02:00
|
|
|
Channel: strings.TrimSpace(form.Channel),
|
2015-12-05 19:24:13 +01:00
|
|
|
Username: form.Username,
|
|
|
|
IconURL: form.IconURL,
|
|
|
|
Color: form.Color,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("Marshal", err)
|
2015-12-05 19:24:13 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
w := &webhook.Webhook{
|
2020-03-08 23:08:05 +01:00
|
|
|
RepoID: orCtx.RepoID,
|
|
|
|
URL: form.PayloadURL,
|
2021-11-10 06:13:16 +01:00
|
|
|
ContentType: webhook.ContentTypeJSON,
|
2020-03-08 23:08:05 +01:00
|
|
|
HookEvent: ParseHookEvent(form.WebhookForm),
|
|
|
|
IsActive: form.Active,
|
2021-11-10 06:13:16 +01:00
|
|
|
Type: webhook.SLACK,
|
2020-03-08 23:08:05 +01:00
|
|
|
Meta: string(meta),
|
|
|
|
OrgID: orCtx.OrgID,
|
|
|
|
IsSystemWebhook: orCtx.IsSystemWebhook,
|
2015-12-05 19:24:13 +01:00
|
|
|
}
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("UpdateEvent", err)
|
2015-12-05 19:24:13 +01:00
|
|
|
return
|
2022-03-22 16:22:54 +01:00
|
|
|
} else if err := webhook.CreateWebhook(ctx, w); err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("CreateWebhook", err)
|
2015-12-05 19:24:13 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
|
2019-03-19 03:33:20 +01:00
|
|
|
ctx.Redirect(orCtx.Link)
|
2015-12-05 19:24:13 +01:00
|
|
|
}
|
|
|
|
|
2020-02-12 09:48:28 +01:00
|
|
|
// FeishuHooksNewPost response for creating feishu hook
|
2021-01-26 16:36:53 +01:00
|
|
|
func FeishuHooksNewPost(ctx *context.Context) {
|
2021-04-06 21:44:05 +02:00
|
|
|
form := web.GetForm(ctx).(*forms.NewFeishuHookForm)
|
2020-02-12 09:48:28 +01:00
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
2021-11-10 06:13:16 +01:00
|
|
|
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
|
|
|
|
ctx.Data["HookType"] = webhook.FEISHU
|
2020-02-12 09:48:28 +01:00
|
|
|
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
2020-02-12 09:48:28 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
w := &webhook.Webhook{
|
2020-03-08 23:08:05 +01:00
|
|
|
RepoID: orCtx.RepoID,
|
|
|
|
URL: form.PayloadURL,
|
2021-11-10 06:13:16 +01:00
|
|
|
ContentType: webhook.ContentTypeJSON,
|
2020-03-08 23:08:05 +01:00
|
|
|
HookEvent: ParseHookEvent(form.WebhookForm),
|
|
|
|
IsActive: form.Active,
|
2021-11-10 06:13:16 +01:00
|
|
|
Type: webhook.FEISHU,
|
2020-03-08 23:08:05 +01:00
|
|
|
Meta: "",
|
|
|
|
OrgID: orCtx.OrgID,
|
|
|
|
IsSystemWebhook: orCtx.IsSystemWebhook,
|
2020-02-12 09:48:28 +01:00
|
|
|
}
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
|
|
|
ctx.ServerError("UpdateEvent", err)
|
|
|
|
return
|
2022-03-22 16:22:54 +01:00
|
|
|
} else if err := webhook.CreateWebhook(ctx, w); err != nil {
|
2020-02-12 09:48:28 +01:00
|
|
|
ctx.ServerError("CreateWebhook", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
|
|
|
|
ctx.Redirect(orCtx.Link)
|
|
|
|
}
|
|
|
|
|
2021-07-23 06:41:27 +02:00
|
|
|
// WechatworkHooksNewPost response for creating wechatwork hook
|
|
|
|
func WechatworkHooksNewPost(ctx *context.Context) {
|
|
|
|
form := web.GetForm(ctx).(*forms.NewWechatWorkHookForm)
|
|
|
|
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
2021-11-10 06:13:16 +01:00
|
|
|
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
|
|
|
|
ctx.Data["HookType"] = webhook.WECHATWORK
|
2021-07-23 06:41:27 +02:00
|
|
|
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
w := &webhook.Webhook{
|
2021-07-23 06:41:27 +02:00
|
|
|
RepoID: orCtx.RepoID,
|
|
|
|
URL: form.PayloadURL,
|
2021-11-10 06:13:16 +01:00
|
|
|
ContentType: webhook.ContentTypeJSON,
|
2021-07-23 06:41:27 +02:00
|
|
|
HookEvent: ParseHookEvent(form.WebhookForm),
|
|
|
|
IsActive: form.Active,
|
2021-11-10 06:13:16 +01:00
|
|
|
Type: webhook.WECHATWORK,
|
2021-07-23 06:41:27 +02:00
|
|
|
Meta: "",
|
|
|
|
OrgID: orCtx.OrgID,
|
|
|
|
IsSystemWebhook: orCtx.IsSystemWebhook,
|
|
|
|
}
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
|
|
|
ctx.ServerError("UpdateEvent", err)
|
|
|
|
return
|
2022-03-22 16:22:54 +01:00
|
|
|
} else if err := webhook.CreateWebhook(ctx, w); err != nil {
|
2021-07-23 06:41:27 +02:00
|
|
|
ctx.ServerError("CreateWebhook", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
|
|
|
|
ctx.Redirect(orCtx.Link)
|
|
|
|
}
|
|
|
|
|
2022-01-23 14:46:30 +01:00
|
|
|
// PackagistHooksNewPost response for creating packagist hook
|
|
|
|
func PackagistHooksNewPost(ctx *context.Context) {
|
|
|
|
form := web.GetForm(ctx).(*forms.NewPackagistHookForm)
|
|
|
|
ctx.Data["Title"] = ctx.Tr("repo.settings")
|
|
|
|
ctx.Data["PageIsSettingsHooks"] = true
|
|
|
|
ctx.Data["PageIsSettingsHooksNew"] = true
|
|
|
|
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook.HookEvent{}}
|
|
|
|
ctx.Data["HookType"] = webhook.PACKAGIST
|
|
|
|
|
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if ctx.HasError() {
|
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
meta, err := json.Marshal(&webhook_service.PackagistMeta{
|
|
|
|
Username: form.Username,
|
|
|
|
APIToken: form.APIToken,
|
|
|
|
PackageURL: form.PackageURL,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("Marshal", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w := &webhook.Webhook{
|
|
|
|
RepoID: orCtx.RepoID,
|
|
|
|
URL: fmt.Sprintf("https://packagist.org/api/update-package?username=%s&apiToken=%s", url.QueryEscape(form.Username), url.QueryEscape(form.APIToken)),
|
|
|
|
ContentType: webhook.ContentTypeJSON,
|
|
|
|
HookEvent: ParseHookEvent(form.WebhookForm),
|
|
|
|
IsActive: form.Active,
|
|
|
|
Type: webhook.PACKAGIST,
|
|
|
|
Meta: string(meta),
|
|
|
|
OrgID: orCtx.OrgID,
|
|
|
|
IsSystemWebhook: orCtx.IsSystemWebhook,
|
|
|
|
}
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
|
|
|
ctx.ServerError("UpdateEvent", err)
|
|
|
|
return
|
2022-03-22 16:22:54 +01:00
|
|
|
} else if err := webhook.CreateWebhook(ctx, w); err != nil {
|
2022-01-23 14:46:30 +01:00
|
|
|
ctx.ServerError("CreateWebhook", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.add_hook_success"))
|
|
|
|
ctx.Redirect(orCtx.Link)
|
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
func checkWebhook(ctx *context.Context) (*orgRepoCtx, *webhook.Webhook) {
|
2015-12-05 19:24:13 +01:00
|
|
|
orCtx, err := getOrgRepoCtx(ctx)
|
|
|
|
if err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("getOrgRepoCtx", err)
|
2015-12-05 19:24:13 +01:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
ctx.Data["BaseLink"] = orCtx.Link
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
var w *webhook.Webhook
|
2016-07-15 19:02:55 +02:00
|
|
|
if orCtx.RepoID > 0 {
|
2021-11-10 06:13:16 +01:00
|
|
|
w, err = webhook.GetWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
|
2019-03-19 03:33:20 +01:00
|
|
|
} else if orCtx.OrgID > 0 {
|
2021-11-10 06:13:16 +01:00
|
|
|
w, err = webhook.GetWebhookByOrgID(ctx.Org.Organization.ID, ctx.ParamsInt64(":id"))
|
2021-01-15 00:24:03 +01:00
|
|
|
} else if orCtx.IsAdmin {
|
2021-11-10 06:13:16 +01:00
|
|
|
w, err = webhook.GetSystemOrDefaultWebhook(ctx.ParamsInt64(":id"))
|
2016-07-15 19:02:55 +02:00
|
|
|
}
|
2021-01-15 00:24:03 +01:00
|
|
|
if err != nil || w == nil {
|
2021-11-10 06:13:16 +01:00
|
|
|
if webhook.IsErrWebhookNotExist(err) {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.NotFound("GetWebhookByID", nil)
|
2015-12-05 19:24:13 +01:00
|
|
|
} else {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("GetWebhookByID", err)
|
2015-12-05 19:24:13 +01:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-12-09 18:20:13 +01:00
|
|
|
ctx.Data["HookType"] = w.Type
|
|
|
|
switch w.Type {
|
2021-11-10 06:13:16 +01:00
|
|
|
case webhook.SLACK:
|
|
|
|
ctx.Data["SlackHook"] = webhook_service.GetSlackHook(w)
|
|
|
|
case webhook.DISCORD:
|
|
|
|
ctx.Data["DiscordHook"] = webhook_service.GetDiscordHook(w)
|
|
|
|
case webhook.TELEGRAM:
|
|
|
|
ctx.Data["TelegramHook"] = webhook_service.GetTelegramHook(w)
|
|
|
|
case webhook.MATRIX:
|
|
|
|
ctx.Data["MatrixHook"] = webhook_service.GetMatrixHook(w)
|
2022-01-23 14:46:30 +01:00
|
|
|
case webhook.PACKAGIST:
|
|
|
|
ctx.Data["PackagistHook"] = webhook_service.GetPackagistHook(w)
|
2015-12-05 19:24:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Data["History"], err = w.History(1)
|
|
|
|
if err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("History", err)
|
2015-12-05 19:24:13 +01:00
|
|
|
}
|
|
|
|
return orCtx, w
|
|
|
|
}
|
|
|
|
|
2016-11-24 08:04:31 +01:00
|
|
|
// WebHooksEdit render editing web hook page
|
2016-03-11 17:56:52 +01:00
|
|
|
func WebHooksEdit(ctx *context.Context) {
|
2015-12-05 19:24:13 +01: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
|
|
|
|
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
2015-12-05 19:24:13 +01:00
|
|
|
}
|
|
|
|
|
2016-11-24 08:04:31 +01:00
|
|
|
// WebHooksEditPost response for editing web hook
|
2021-01-26 16:36:53 +01:00
|
|
|
func WebHooksEditPost(ctx *context.Context) {
|
2021-04-06 21:44:05 +02:00
|
|
|
form := web.GetForm(ctx).(*forms.NewWebhookForm)
|
2015-12-05 19:24:13 +01: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() {
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
2015-12-05 19:24:13 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
contentType := webhook.ContentTypeJSON
|
|
|
|
if webhook.HookContentType(form.ContentType) == webhook.ContentTypeForm {
|
|
|
|
contentType = webhook.ContentTypeForm
|
2015-12-05 19:24:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
w.URL = form.PayloadURL
|
|
|
|
w.ContentType = contentType
|
|
|
|
w.Secret = form.Secret
|
|
|
|
w.HookEvent = ParseHookEvent(form.WebhookForm)
|
|
|
|
w.IsActive = form.Active
|
2019-05-15 14:01:53 +02:00
|
|
|
w.HTTPMethod = form.HTTPMethod
|
2015-12-05 19:24:13 +01:00
|
|
|
if err := w.UpdateEvent(); err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("UpdateEvent", err)
|
2015-12-05 19:24:13 +01:00
|
|
|
return
|
2021-11-10 06:13:16 +01:00
|
|
|
} else if err := webhook.UpdateWebhook(w); err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("WebHooksEditPost", err)
|
2015-12-05 19:24:13 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
|
2019-03-19 03:33:20 +01:00
|
|
|
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
2017-05-29 09:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GogsHooksEditPost response for editing gogs hook
|
2021-01-26 16:36:53 +01:00
|
|
|
func GogsHooksEditPost(ctx *context.Context) {
|
2021-04-06 21:44:05 +02:00
|
|
|
form := web.GetForm(ctx).(*forms.NewGogshookForm)
|
2017-05-29 09:17:15 +02: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() {
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
2017-05-29 09:17:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
contentType := webhook.ContentTypeJSON
|
|
|
|
if webhook.HookContentType(form.ContentType) == webhook.ContentTypeForm {
|
|
|
|
contentType = webhook.ContentTypeForm
|
2017-05-29 09:17:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 22:34:17 +01:00
|
|
|
ctx.ServerError("UpdateEvent", err)
|
2017-05-29 09:17:15 +02:00
|
|
|
return
|
2021-11-10 06:13:16 +01:00
|
|
|
} else if err := webhook.UpdateWebhook(w); err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("GogsHooksEditPost", err)
|
2017-05-29 09:17:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
|
2019-03-19 03:33:20 +01:00
|
|
|
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
2015-12-05 19:24:13 +01:00
|
|
|
}
|
|
|
|
|
2016-11-27 12:59:12 +01:00
|
|
|
// SlackHooksEditPost response for editing slack hook
|
2021-01-26 16:36:53 +01:00
|
|
|
func SlackHooksEditPost(ctx *context.Context) {
|
2021-04-06 21:44:05 +02:00
|
|
|
form := web.GetForm(ctx).(*forms.NewSlackHookForm)
|
2015-12-05 19:24:13 +01: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() {
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
2015-12-05 19:24:13 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-09-10 16:31:08 +02:00
|
|
|
if form.HasInvalidChannel() {
|
|
|
|
ctx.Flash.Error(ctx.Tr("repo.settings.add_webhook.invalid_channel_name"))
|
2020-03-01 17:51:55 +01:00
|
|
|
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
2018-09-10 16:31:08 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
meta, err := json.Marshal(&webhook_service.SlackMeta{
|
2018-09-10 16:31:08 +02:00
|
|
|
Channel: strings.TrimSpace(form.Channel),
|
2015-12-05 19:24:13 +01:00
|
|
|
Username: form.Username,
|
|
|
|
IconURL: form.IconURL,
|
|
|
|
Color: form.Color,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("Marshal", err)
|
2015-12-05 19:24:13 +01: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 22:34:17 +01:00
|
|
|
ctx.ServerError("UpdateEvent", err)
|
2015-12-05 19:24:13 +01:00
|
|
|
return
|
2021-11-10 06:13:16 +01:00
|
|
|
} else if err := webhook.UpdateWebhook(w); err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("UpdateWebhook", err)
|
2017-08-28 07:06:45 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
|
2019-03-19 03:33:20 +01:00
|
|
|
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
2017-08-28 07:06:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DiscordHooksEditPost response for editing discord hook
|
2021-01-26 16:36:53 +01:00
|
|
|
func DiscordHooksEditPost(ctx *context.Context) {
|
2021-04-06 21:44:05 +02:00
|
|
|
form := web.GetForm(ctx).(*forms.NewDiscordHookForm)
|
2017-08-28 07:06:45 +02: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() {
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
2017-08-28 07:06:45 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
meta, err := json.Marshal(&webhook_service.DiscordMeta{
|
2017-08-28 07:06:45 +02:00
|
|
|
Username: form.Username,
|
|
|
|
IconURL: form.IconURL,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("Marshal", err)
|
2017-08-28 07:06:45 +02: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 22:34:17 +01:00
|
|
|
ctx.ServerError("UpdateEvent", err)
|
2017-08-28 07:06:45 +02:00
|
|
|
return
|
2021-11-10 06:13:16 +01:00
|
|
|
} else if err := webhook.UpdateWebhook(w); err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("UpdateWebhook", err)
|
2015-12-05 19:24:13 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
|
2019-03-19 03:33:20 +01:00
|
|
|
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
2015-12-05 19:24:13 +01:00
|
|
|
}
|
|
|
|
|
2017-11-21 05:26:43 +01:00
|
|
|
// DingtalkHooksEditPost response for editing discord hook
|
2021-01-26 16:36:53 +01:00
|
|
|
func DingtalkHooksEditPost(ctx *context.Context) {
|
2021-04-06 21:44:05 +02:00
|
|
|
form := web.GetForm(ctx).(*forms.NewDingtalkHookForm)
|
2017-11-21 05:26:43 +01: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() {
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
2017-11-21 05:26:43 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.URL = form.PayloadURL
|
|
|
|
w.HookEvent = ParseHookEvent(form.WebhookForm)
|
|
|
|
w.IsActive = form.Active
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("UpdateEvent", err)
|
2017-11-21 05:26:43 +01:00
|
|
|
return
|
2021-11-10 06:13:16 +01:00
|
|
|
} else if err := webhook.UpdateWebhook(w); err != nil {
|
2018-01-10 22:34:17 +01:00
|
|
|
ctx.ServerError("UpdateWebhook", err)
|
2017-11-21 05:26:43 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
|
2019-03-19 03:33:20 +01:00
|
|
|
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
2017-11-21 05:26:43 +01:00
|
|
|
}
|
|
|
|
|
2019-04-19 04:45:02 +02:00
|
|
|
// TelegramHooksEditPost response for editing discord hook
|
2021-01-26 16:36:53 +01:00
|
|
|
func TelegramHooksEditPost(ctx *context.Context) {
|
2021-04-06 21:44:05 +02:00
|
|
|
form := web.GetForm(ctx).(*forms.NewTelegramHookForm)
|
2019-04-19 04:45:02 +02: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() {
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
2019-04-19 04:45:02 +02:00
|
|
|
return
|
|
|
|
}
|
2021-07-24 18:03:58 +02:00
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
meta, err := json.Marshal(&webhook_service.TelegramMeta{
|
2019-04-19 04:45:02 +02:00
|
|
|
BotToken: form.BotToken,
|
|
|
|
ChatID: form.ChatID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("Marshal", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Meta = string(meta)
|
2021-11-16 19:18:25 +01:00
|
|
|
w.URL = fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage?chat_id=%s", url.PathEscape(form.BotToken), url.QueryEscape(form.ChatID))
|
2019-04-19 04:45:02 +02:00
|
|
|
w.HookEvent = ParseHookEvent(form.WebhookForm)
|
|
|
|
w.IsActive = form.Active
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
|
|
|
ctx.ServerError("UpdateEvent", err)
|
|
|
|
return
|
2021-11-10 06:13:16 +01:00
|
|
|
} else if err := webhook.UpdateWebhook(w); err != nil {
|
2019-04-19 04:45:02 +02:00
|
|
|
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 14:09:55 +01:00
|
|
|
// MatrixHooksEditPost response for editing a Matrix hook
|
2021-01-26 16:36:53 +01:00
|
|
|
func MatrixHooksEditPost(ctx *context.Context) {
|
2021-04-06 21:44:05 +02:00
|
|
|
form := web.GetForm(ctx).(*forms.NewMatrixHookForm)
|
2020-03-28 14:09:55 +01: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() {
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
2020-03-28 14:09:55 +01:00
|
|
|
return
|
|
|
|
}
|
2021-07-24 18:03:58 +02:00
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
meta, err := json.Marshal(&webhook_service.MatrixMeta{
|
2020-03-28 14:09:55 +01:00
|
|
|
HomeserverURL: form.HomeserverURL,
|
|
|
|
Room: form.RoomID,
|
|
|
|
AccessToken: form.AccessToken,
|
|
|
|
MessageType: form.MessageType,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("Marshal", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Meta = string(meta)
|
2021-11-16 19:18:25 +01:00
|
|
|
w.URL = fmt.Sprintf("%s/_matrix/client/r0/rooms/%s/send/m.room.message", form.HomeserverURL, url.PathEscape(form.RoomID))
|
2020-03-28 14:09:55 +01:00
|
|
|
|
|
|
|
w.HookEvent = ParseHookEvent(form.WebhookForm)
|
|
|
|
w.IsActive = form.Active
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
|
|
|
ctx.ServerError("UpdateEvent", err)
|
|
|
|
return
|
2021-11-10 06:13:16 +01:00
|
|
|
} else if err := webhook.UpdateWebhook(w); err != nil {
|
2020-03-28 14:09:55 +01:00
|
|
|
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 16:18:06 +02:00
|
|
|
// MSTeamsHooksEditPost response for editing MS Teams hook
|
2021-01-26 16:36:53 +01:00
|
|
|
func MSTeamsHooksEditPost(ctx *context.Context) {
|
2021-04-06 21:44:05 +02:00
|
|
|
form := web.GetForm(ctx).(*forms.NewMSTeamsHookForm)
|
2019-04-19 16:18:06 +02: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() {
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
2019-04-19 16:18:06 +02:00
|
|
|
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
|
2021-11-10 06:13:16 +01:00
|
|
|
} else if err := webhook.UpdateWebhook(w); err != nil {
|
2019-04-19 16:18:06 +02:00
|
|
|
ctx.ServerError("UpdateWebhook", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-12 09:48:28 +01: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
|
2021-01-26 16:36:53 +01:00
|
|
|
func FeishuHooksEditPost(ctx *context.Context) {
|
2021-04-06 21:44:05 +02:00
|
|
|
form := web.GetForm(ctx).(*forms.NewFeishuHookForm)
|
2020-02-12 09:48:28 +01: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() {
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.HTML(http.StatusOK, orCtx.NewTemplate)
|
2020-02-12 09:48:28 +01:00
|
|
|
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
|
2021-11-10 06:13:16 +01:00
|
|
|
} else if err := webhook.UpdateWebhook(w); err != nil {
|
2020-02-12 09:48:28 +01:00
|
|
|
ctx.ServerError("UpdateWebhook", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-19 16:18:06 +02:00
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.update_hook_success"))
|
|
|
|
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
|
|
|
}
|
|
|
|
|
2021-07-23 06:41:27 +02:00
|
|
|
// WechatworkHooksEditPost response for editing wechatwork hook
|
|
|
|
func WechatworkHooksEditPost(ctx *context.Context) {
|
|
|
|
form := web.GetForm(ctx).(*forms.NewWechatWorkHookForm)
|
|
|
|
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(http.StatusOK, orCtx.NewTemplate)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.URL = form.PayloadURL
|
|
|
|
w.HookEvent = ParseHookEvent(form.WebhookForm)
|
2022-01-23 14:46:30 +01:00
|
|
|
w.IsActive = form.Active
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
|
|
|
ctx.ServerError("UpdateEvent", err)
|
|
|
|
return
|
|
|
|
} else if err := webhook.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))
|
|
|
|
}
|
|
|
|
|
|
|
|
// PackagistHooksEditPost response for editing packagist hook
|
|
|
|
func PackagistHooksEditPost(ctx *context.Context) {
|
|
|
|
form := web.GetForm(ctx).(*forms.NewPackagistHookForm)
|
|
|
|
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(http.StatusOK, orCtx.NewTemplate)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
meta, err := json.Marshal(&webhook_service.PackagistMeta{
|
|
|
|
Username: form.Username,
|
|
|
|
APIToken: form.APIToken,
|
|
|
|
PackageURL: form.PackageURL,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ctx.ServerError("Marshal", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Meta = string(meta)
|
|
|
|
w.URL = fmt.Sprintf("https://packagist.org/api/update-package?username=%s&apiToken=%s", url.QueryEscape(form.Username), url.QueryEscape(form.APIToken))
|
|
|
|
w.HookEvent = ParseHookEvent(form.WebhookForm)
|
2021-07-23 06:41:27 +02:00
|
|
|
w.IsActive = form.Active
|
|
|
|
if err := w.UpdateEvent(); err != nil {
|
|
|
|
ctx.ServerError("UpdateEvent", err)
|
|
|
|
return
|
2021-11-10 06:13:16 +01:00
|
|
|
} else if err := webhook.UpdateWebhook(w); err != nil {
|
2021-07-23 06:41:27 +02:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2016-11-24 08:04:31 +01:00
|
|
|
// TestWebhook test if web hook is work fine
|
2016-03-11 17:56:52 +01:00
|
|
|
func TestWebhook(ctx *context.Context) {
|
2017-08-29 16:55:24 +02:00
|
|
|
hookID := ctx.ParamsInt64(":id")
|
2021-11-10 06:13:16 +01:00
|
|
|
w, err := webhook.GetWebhookByRepoID(ctx.Repo.Repository.ID, hookID)
|
2017-08-29 16:55:24 +02:00
|
|
|
if err != nil {
|
|
|
|
ctx.Flash.Error("GetWebhookByID: " + err.Error())
|
2022-03-23 05:54:07 +01:00
|
|
|
ctx.Status(http.StatusInternalServerError)
|
2017-08-29 16:55:24 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-08-15 14:53:47 +02:00
|
|
|
// Grab latest commit or fake one if it's empty repository.
|
|
|
|
commit := ctx.Repo.Commit
|
|
|
|
if commit == nil {
|
2021-11-24 10:49:20 +01:00
|
|
|
ghost := user_model.NewGhostUser()
|
2016-08-15 14:53:47 +02:00
|
|
|
commit = &git.Commit{
|
2016-12-22 10:30:52 +01:00
|
|
|
ID: git.MustIDFromString(git.EmptySHA),
|
2016-08-15 14:53:47 +02:00
|
|
|
Author: ghost.NewGitSig(),
|
|
|
|
Committer: ghost.NewGitSig(),
|
|
|
|
CommitMessage: "This is a fake commit",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 08:03:22 +01:00
|
|
|
apiUser := convert.ToUserWithAccessMode(ctx.Doer, perm.AccessModeNone)
|
2021-06-29 15:34:03 +02:00
|
|
|
|
|
|
|
apiCommit := &api.PayloadCommit{
|
|
|
|
ID: commit.ID.String(),
|
|
|
|
Message: commit.Message(),
|
2021-11-16 19:18:25 +01:00
|
|
|
URL: ctx.Repo.Repository.HTMLURL() + "/commit/" + url.PathEscape(commit.ID.String()),
|
2021-06-29 15:34:03 +02:00
|
|
|
Author: &api.PayloadUser{
|
|
|
|
Name: commit.Author.Name,
|
|
|
|
Email: commit.Author.Email,
|
|
|
|
},
|
|
|
|
Committer: &api.PayloadUser{
|
|
|
|
Name: commit.Committer.Name,
|
|
|
|
Email: commit.Committer.Email,
|
2015-12-05 19:24:13 +01:00
|
|
|
},
|
2021-06-29 15:34:03 +02:00
|
|
|
}
|
|
|
|
|
2022-09-04 17:12:01 +02:00
|
|
|
commitID := commit.ID.String()
|
2021-06-29 15:34:03 +02:00
|
|
|
p := &api.PushPayload{
|
|
|
|
Ref: git.BranchPrefix + ctx.Repo.Repository.DefaultBranch,
|
2022-09-04 17:12:01 +02:00
|
|
|
Before: commitID,
|
|
|
|
After: commitID,
|
|
|
|
CompareURL: setting.AppURL + ctx.Repo.Repository.ComposeCompareURL(commitID, commitID),
|
2021-06-29 15:34:03 +02:00
|
|
|
Commits: []*api.PayloadCommit{apiCommit},
|
|
|
|
HeadCommit: apiCommit,
|
2021-11-28 12:58:28 +01:00
|
|
|
Repo: convert.ToRepo(ctx.Repo.Repository, perm.AccessModeNone),
|
2021-06-29 15:34:03 +02:00
|
|
|
Pusher: apiUser,
|
|
|
|
Sender: apiUser,
|
2015-12-05 19:24:13 +01:00
|
|
|
}
|
2021-11-10 06:13:16 +01:00
|
|
|
if err := webhook_service.PrepareWebhook(w, ctx.Repo.Repository, webhook.HookEventPush, p); err != nil {
|
2017-08-29 16:55:24 +02:00
|
|
|
ctx.Flash.Error("PrepareWebhook: " + err.Error())
|
2022-03-23 05:54:07 +01:00
|
|
|
ctx.Status(http.StatusInternalServerError)
|
2015-12-05 19:24:13 +01:00
|
|
|
} else {
|
2022-01-05 22:00:20 +01:00
|
|
|
ctx.Flash.Info(ctx.Tr("repo.settings.webhook.delivery.success"))
|
2022-03-23 05:54:07 +01:00
|
|
|
ctx.Status(http.StatusOK)
|
2015-12-05 19:24:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-05 22:00:20 +01:00
|
|
|
// ReplayWebhook replays a webhook
|
|
|
|
func ReplayWebhook(ctx *context.Context) {
|
|
|
|
hookTaskUUID := ctx.Params(":uuid")
|
|
|
|
|
|
|
|
orCtx, w := checkWebhook(ctx)
|
|
|
|
if ctx.Written() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := webhook_service.ReplayHookTask(w, hookTaskUUID); err != nil {
|
|
|
|
if webhook.IsErrHookTaskNotExist(err) {
|
|
|
|
ctx.NotFound("ReplayHookTask", nil)
|
|
|
|
} else {
|
|
|
|
ctx.ServerError("ReplayHookTask", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.webhook.delivery.success"))
|
|
|
|
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
|
|
|
}
|
|
|
|
|
2016-11-24 08:04:31 +01:00
|
|
|
// DeleteWebhook delete a webhook
|
2016-03-11 17:56:52 +01:00
|
|
|
func DeleteWebhook(ctx *context.Context) {
|
2021-11-10 06:13:16 +01:00
|
|
|
if err := webhook.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.FormInt64("id")); err != nil {
|
2016-07-17 02:33:59 +02:00
|
|
|
ctx.Flash.Error("DeleteWebhookByRepoID: " + err.Error())
|
2015-12-05 19:24:13 +01:00
|
|
|
} else {
|
|
|
|
ctx.Flash.Success(ctx.Tr("repo.settings.webhook_deletion_success"))
|
|
|
|
}
|
|
|
|
|
2021-04-05 17:30:52 +02:00
|
|
|
ctx.JSON(http.StatusOK, map[string]interface{}{
|
2015-12-05 19:24:13 +01:00
|
|
|
"redirect": ctx.Repo.RepoLink + "/settings/hooks",
|
|
|
|
})
|
|
|
|
}
|