summaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
authoroliverpool <git@olivier.pfad.fr>2024-03-21 13:23:27 +0100
committeroliverpool <git@olivier.pfad.fr>2024-03-27 15:38:38 +0100
commit8dfbbfef07bb35f921e4ab4228c8006ce26dd183 (patch)
tree466c7f25ce1609e27ef2c09e75dbb40fe9e663f5 /routers
parentMerge pull request '[REFACTOR] webhook.Handler interface' (#2758) from oliver... (diff)
downloadforgejo-8dfbbfef07bb35f921e4ab4228c8006ce26dd183.tar.xz
forgejo-8dfbbfef07bb35f921e4ab4228c8006ce26dd183.zip
[REFACTOR] webhook matrix endpoints
Diffstat (limited to 'routers')
-rw-r--r--routers/web/repo/setting/webhook.go76
-rw-r--r--routers/web/web.go4
2 files changed, 51 insertions, 29 deletions
diff --git a/routers/web/repo/setting/webhook.go b/routers/web/repo/setting/webhook.go
index 1f78681dae..f41752e470 100644
--- a/routers/web/repo/setting/webhook.go
+++ b/routers/web/repo/setting/webhook.go
@@ -24,11 +24,14 @@ import (
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web"
+ "code.gitea.io/gitea/modules/web/middleware"
webhook_module "code.gitea.io/gitea/modules/webhook"
"code.gitea.io/gitea/services/context"
"code.gitea.io/gitea/services/convert"
"code.gitea.io/gitea/services/forms"
webhook_service "code.gitea.io/gitea/services/webhook"
+
+ "gitea.com/go-chi/binding"
)
const (
@@ -201,6 +204,29 @@ type webhookParams struct {
Meta any
}
+func WebhookCreate(ctx *context.Context) {
+ typ := ctx.Params(":type")
+ handler := webhook_service.GetWebhookHandler(typ)
+ if handler == nil {
+ ctx.NotFound("GetWebhookHandler", nil)
+ return
+ }
+
+ fields := handler.FormFields(func(form any) {
+ errs := binding.Bind(ctx.Req, form)
+ middleware.Validate(errs, ctx.Data, form, ctx.Locale) // error will be checked later in ctx.HasError
+ })
+ createWebhook(ctx, webhookParams{
+ Type: typ,
+ URL: fields.URL,
+ ContentType: fields.ContentType,
+ Secret: fields.Secret,
+ HTTPMethod: fields.HTTPMethod,
+ WebhookForm: fields.WebhookForm,
+ Meta: fields.Metadata,
+ })
+}
+
func createWebhook(ctx *context.Context, params webhookParams) {
ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
ctx.Data["PageIsSettingsHooks"] = true
@@ -260,6 +286,29 @@ func createWebhook(ctx *context.Context, params webhookParams) {
ctx.Redirect(orCtx.Link)
}
+func WebhookUpdate(ctx *context.Context) {
+ typ := ctx.Params(":type")
+ handler := webhook_service.GetWebhookHandler(typ)
+ if handler == nil {
+ ctx.NotFound("GetWebhookHandler", nil)
+ return
+ }
+
+ fields := handler.FormFields(func(form any) {
+ errs := binding.Bind(ctx.Req, form)
+ middleware.Validate(errs, ctx.Data, form, ctx.Locale) // error will be checked later in ctx.HasError
+ })
+ editWebhook(ctx, webhookParams{
+ Type: typ,
+ URL: fields.URL,
+ ContentType: fields.ContentType,
+ Secret: fields.Secret,
+ HTTPMethod: fields.HTTPMethod,
+ WebhookForm: fields.WebhookForm,
+ Meta: fields.Metadata,
+ })
+}
+
func editWebhook(ctx *context.Context, params webhookParams) {
ctx.Data["Title"] = ctx.Tr("repo.settings.update_webhook")
ctx.Data["PageIsSettingsHooks"] = true
@@ -467,33 +516,6 @@ func telegramHookParams(ctx *context.Context) webhookParams {
}
}
-// MatrixHooksNewPost response for creating Matrix webhook
-func MatrixHooksNewPost(ctx *context.Context) {
- createWebhook(ctx, matrixHookParams(ctx))
-}
-
-// MatrixHooksEditPost response for editing Matrix webhook
-func MatrixHooksEditPost(ctx *context.Context) {
- editWebhook(ctx, matrixHookParams(ctx))
-}
-
-func matrixHookParams(ctx *context.Context) webhookParams {
- form := web.GetForm(ctx).(*forms.NewMatrixHookForm)
-
- return webhookParams{
- Type: webhook_module.MATRIX,
- URL: fmt.Sprintf("%s/_matrix/client/r0/rooms/%s/send/m.room.message", form.HomeserverURL, url.PathEscape(form.RoomID)),
- ContentType: webhook.ContentTypeJSON,
- HTTPMethod: http.MethodPut,
- WebhookForm: form.WebhookForm,
- Meta: &webhook_service.MatrixMeta{
- HomeserverURL: form.HomeserverURL,
- Room: form.RoomID,
- MessageType: form.MessageType,
- },
- }
-}
-
// MSTeamsHooksNewPost response for creating MSTeams webhook
func MSTeamsHooksNewPost(ctx *context.Context) {
createWebhook(ctx, mSTeamsHookParams(ctx))
diff --git a/routers/web/web.go b/routers/web/web.go
index 50a73e9b85..b23068a29d 100644
--- a/routers/web/web.go
+++ b/routers/web/web.go
@@ -409,11 +409,11 @@ func registerRoutes(m *web.Route) {
m.Post("/discord/new", web.Bind(forms.NewDiscordHookForm{}), repo_setting.DiscordHooksNewPost)
m.Post("/dingtalk/new", web.Bind(forms.NewDingtalkHookForm{}), repo_setting.DingtalkHooksNewPost)
m.Post("/telegram/new", web.Bind(forms.NewTelegramHookForm{}), repo_setting.TelegramHooksNewPost)
- m.Post("/matrix/new", web.Bind(forms.NewMatrixHookForm{}), repo_setting.MatrixHooksNewPost)
m.Post("/msteams/new", web.Bind(forms.NewMSTeamsHookForm{}), repo_setting.MSTeamsHooksNewPost)
m.Post("/feishu/new", web.Bind(forms.NewFeishuHookForm{}), repo_setting.FeishuHooksNewPost)
m.Post("/wechatwork/new", web.Bind(forms.NewWechatWorkHookForm{}), repo_setting.WechatworkHooksNewPost)
m.Post("/packagist/new", web.Bind(forms.NewPackagistHookForm{}), repo_setting.PackagistHooksNewPost)
+ m.Post("/{type}/new", repo_setting.WebhookCreate)
}
addWebhookEditRoutes := func() {
@@ -424,11 +424,11 @@ func registerRoutes(m *web.Route) {
m.Post("/discord/{id}", web.Bind(forms.NewDiscordHookForm{}), repo_setting.DiscordHooksEditPost)
m.Post("/dingtalk/{id}", web.Bind(forms.NewDingtalkHookForm{}), repo_setting.DingtalkHooksEditPost)
m.Post("/telegram/{id}", web.Bind(forms.NewTelegramHookForm{}), repo_setting.TelegramHooksEditPost)
- m.Post("/matrix/{id}", web.Bind(forms.NewMatrixHookForm{}), repo_setting.MatrixHooksEditPost)
m.Post("/msteams/{id}", web.Bind(forms.NewMSTeamsHookForm{}), repo_setting.MSTeamsHooksEditPost)
m.Post("/feishu/{id}", web.Bind(forms.NewFeishuHookForm{}), repo_setting.FeishuHooksEditPost)
m.Post("/wechatwork/{id}", web.Bind(forms.NewWechatWorkHookForm{}), repo_setting.WechatworkHooksEditPost)
m.Post("/packagist/{id}", web.Bind(forms.NewPackagistHookForm{}), repo_setting.PackagistHooksEditPost)
+ m.Post("/{type}/{id:[0-9]+}", repo_setting.WebhookUpdate)
}
addSettingsVariablesRoutes := func() {