[REFACTOR] webhook dingtalk endpoint

This commit is contained in:
oliverpool 2024-03-21 13:57:14 +01:00
parent c3f8e6ed60
commit 9dff719523
4 changed files with 15 additions and 36 deletions

View file

@ -388,27 +388,6 @@ func gogsHookParams(ctx *context.Context) webhookParams {
} }
} }
// DingtalkHooksNewPost response for creating Dingtalk webhook
func DingtalkHooksNewPost(ctx *context.Context) {
createWebhook(ctx, dingtalkHookParams(ctx))
}
// DingtalkHooksEditPost response for editing Dingtalk webhook
func DingtalkHooksEditPost(ctx *context.Context) {
editWebhook(ctx, dingtalkHookParams(ctx))
}
func dingtalkHookParams(ctx *context.Context) webhookParams {
form := web.GetForm(ctx).(*forms.NewDingtalkHookForm)
return webhookParams{
Type: webhook_module.DINGTALK,
URL: form.PayloadURL,
ContentType: webhook.ContentTypeJSON,
WebhookForm: form.WebhookForm,
}
}
// TelegramHooksNewPost response for creating Telegram webhook // TelegramHooksNewPost response for creating Telegram webhook
func TelegramHooksNewPost(ctx *context.Context) { func TelegramHooksNewPost(ctx *context.Context) {
createWebhook(ctx, telegramHookParams(ctx)) createWebhook(ctx, telegramHookParams(ctx))

View file

@ -403,7 +403,6 @@ func registerRoutes(m *web.Route) {
addWebhookAddRoutes := func() { addWebhookAddRoutes := func() {
m.Get("/{type}/new", repo_setting.WebhooksNew) m.Get("/{type}/new", repo_setting.WebhooksNew)
m.Post("/gogs/new", web.Bind(forms.NewGogshookForm{}), repo_setting.GogsHooksNewPost) m.Post("/gogs/new", web.Bind(forms.NewGogshookForm{}), repo_setting.GogsHooksNewPost)
m.Post("/dingtalk/new", web.Bind(forms.NewDingtalkHookForm{}), repo_setting.DingtalkHooksNewPost)
m.Post("/telegram/new", web.Bind(forms.NewTelegramHookForm{}), repo_setting.TelegramHooksNewPost) m.Post("/telegram/new", web.Bind(forms.NewTelegramHookForm{}), repo_setting.TelegramHooksNewPost)
m.Post("/msteams/new", web.Bind(forms.NewMSTeamsHookForm{}), repo_setting.MSTeamsHooksNewPost) m.Post("/msteams/new", web.Bind(forms.NewMSTeamsHookForm{}), repo_setting.MSTeamsHooksNewPost)
m.Post("/feishu/new", web.Bind(forms.NewFeishuHookForm{}), repo_setting.FeishuHooksNewPost) m.Post("/feishu/new", web.Bind(forms.NewFeishuHookForm{}), repo_setting.FeishuHooksNewPost)
@ -414,7 +413,6 @@ func registerRoutes(m *web.Route) {
addWebhookEditRoutes := func() { addWebhookEditRoutes := func() {
m.Post("/gogs/{id}", web.Bind(forms.NewGogshookForm{}), repo_setting.GogsHooksEditPost) m.Post("/gogs/{id}", web.Bind(forms.NewGogshookForm{}), repo_setting.GogsHooksEditPost)
m.Post("/dingtalk/{id}", web.Bind(forms.NewDingtalkHookForm{}), repo_setting.DingtalkHooksEditPost)
m.Post("/telegram/{id}", web.Bind(forms.NewTelegramHookForm{}), repo_setting.TelegramHooksEditPost) m.Post("/telegram/{id}", web.Bind(forms.NewTelegramHookForm{}), repo_setting.TelegramHooksEditPost)
m.Post("/msteams/{id}", web.Bind(forms.NewMSTeamsHookForm{}), repo_setting.MSTeamsHooksEditPost) m.Post("/msteams/{id}", web.Bind(forms.NewMSTeamsHookForm{}), repo_setting.MSTeamsHooksEditPost)
m.Post("/feishu/{id}", web.Bind(forms.NewFeishuHookForm{}), repo_setting.FeishuHooksEditPost) m.Post("/feishu/{id}", web.Bind(forms.NewFeishuHookForm{}), repo_setting.FeishuHooksEditPost)

View file

@ -292,18 +292,6 @@ func (f *NewGogshookForm) Validate(req *http.Request, errs binding.Errors) bindi
return middleware.Validate(errs, ctx.Data, f, ctx.Locale) return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
} }
// NewDingtalkHookForm form for creating dingtalk hook
type NewDingtalkHookForm struct {
PayloadURL string `binding:"Required;ValidUrl"`
WebhookForm
}
// Validate validates the fields
func (f *NewDingtalkHookForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
ctx := context.GetValidateContext(req)
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
}
// NewTelegramHookForm form for creating telegram hook // NewTelegramHookForm form for creating telegram hook
type NewTelegramHookForm struct { type NewTelegramHookForm struct {
BotToken string `binding:"Required"` BotToken string `binding:"Required"`

View file

@ -15,6 +15,7 @@ import (
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/util"
webhook_module "code.gitea.io/gitea/modules/webhook" webhook_module "code.gitea.io/gitea/modules/webhook"
"code.gitea.io/gitea/services/forms"
dingtalk "gitea.com/lunny/dingtalk_webhook" dingtalk "gitea.com/lunny/dingtalk_webhook"
) )
@ -24,7 +25,20 @@ type dingtalkHandler struct{}
func (dingtalkHandler) Type() webhook_module.HookType { return webhook_module.DINGTALK } func (dingtalkHandler) Type() webhook_module.HookType { return webhook_module.DINGTALK }
func (dingtalkHandler) Metadata(*webhook_model.Webhook) any { return nil } func (dingtalkHandler) Metadata(*webhook_model.Webhook) any { return nil }
func (dingtalkHandler) FormFields(bind func(any)) FormFields { func (dingtalkHandler) FormFields(bind func(any)) FormFields {
panic("TODO") var form struct {
forms.WebhookForm
PayloadURL string `binding:"Required;ValidUrl"`
}
bind(&form)
return FormFields{
WebhookForm: form.WebhookForm,
URL: form.PayloadURL,
ContentType: webhook_model.ContentTypeJSON,
Secret: "",
HTTPMethod: http.MethodPost,
Metadata: nil,
}
} }
type ( type (