[REFACTOR] webhook forgejo/gitea endpoints
This commit is contained in:
parent
8dfbbfef07
commit
7e0965b02c
4 changed files with 22 additions and 76 deletions
|
@ -361,62 +361,6 @@ func editWebhook(ctx *context.Context, params webhookParams) {
|
|||
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
||||
}
|
||||
|
||||
// ForgejoHooksNewPost response for creating Forgejo webhook
|
||||
func ForgejoHooksNewPost(ctx *context.Context) {
|
||||
createWebhook(ctx, forgejoHookParams(ctx))
|
||||
}
|
||||
|
||||
// ForgejoHooksEditPost response for editing Forgejo webhook
|
||||
func ForgejoHooksEditPost(ctx *context.Context) {
|
||||
editWebhook(ctx, forgejoHookParams(ctx))
|
||||
}
|
||||
|
||||
func forgejoHookParams(ctx *context.Context) webhookParams {
|
||||
form := web.GetForm(ctx).(*forms.NewWebhookForm)
|
||||
|
||||
contentType := webhook.ContentTypeJSON
|
||||
if webhook.HookContentType(form.ContentType) == webhook.ContentTypeForm {
|
||||
contentType = webhook.ContentTypeForm
|
||||
}
|
||||
|
||||
return webhookParams{
|
||||
Type: webhook_module.FORGEJO,
|
||||
URL: form.PayloadURL,
|
||||
ContentType: contentType,
|
||||
Secret: form.Secret,
|
||||
HTTPMethod: form.HTTPMethod,
|
||||
WebhookForm: form.WebhookForm,
|
||||
}
|
||||
}
|
||||
|
||||
// GiteaHooksNewPost response for creating Gitea webhook
|
||||
func GiteaHooksNewPost(ctx *context.Context) {
|
||||
createWebhook(ctx, giteaHookParams(ctx))
|
||||
}
|
||||
|
||||
// GiteaHooksEditPost response for editing Gitea webhook
|
||||
func GiteaHooksEditPost(ctx *context.Context) {
|
||||
editWebhook(ctx, giteaHookParams(ctx))
|
||||
}
|
||||
|
||||
func giteaHookParams(ctx *context.Context) webhookParams {
|
||||
form := web.GetForm(ctx).(*forms.NewWebhookForm)
|
||||
|
||||
contentType := webhook.ContentTypeJSON
|
||||
if webhook.HookContentType(form.ContentType) == webhook.ContentTypeForm {
|
||||
contentType = webhook.ContentTypeForm
|
||||
}
|
||||
|
||||
return webhookParams{
|
||||
Type: webhook_module.GITEA,
|
||||
URL: form.PayloadURL,
|
||||
ContentType: contentType,
|
||||
Secret: form.Secret,
|
||||
HTTPMethod: form.HTTPMethod,
|
||||
WebhookForm: form.WebhookForm,
|
||||
}
|
||||
}
|
||||
|
||||
// GogsHooksNewPost response for creating Gogs webhook
|
||||
func GogsHooksNewPost(ctx *context.Context) {
|
||||
createWebhook(ctx, gogsHookParams(ctx))
|
||||
|
|
|
@ -402,8 +402,6 @@ func registerRoutes(m *web.Route) {
|
|||
|
||||
addWebhookAddRoutes := func() {
|
||||
m.Get("/{type}/new", repo_setting.WebhooksNew)
|
||||
m.Post("/forgejo/new", web.Bind(forms.NewWebhookForm{}), repo_setting.ForgejoHooksNewPost)
|
||||
m.Post("/gitea/new", web.Bind(forms.NewWebhookForm{}), repo_setting.GiteaHooksNewPost)
|
||||
m.Post("/gogs/new", web.Bind(forms.NewGogshookForm{}), repo_setting.GogsHooksNewPost)
|
||||
m.Post("/slack/new", web.Bind(forms.NewSlackHookForm{}), repo_setting.SlackHooksNewPost)
|
||||
m.Post("/discord/new", web.Bind(forms.NewDiscordHookForm{}), repo_setting.DiscordHooksNewPost)
|
||||
|
@ -417,8 +415,6 @@ func registerRoutes(m *web.Route) {
|
|||
}
|
||||
|
||||
addWebhookEditRoutes := func() {
|
||||
m.Post("/forgejo/{id}", web.Bind(forms.NewWebhookForm{}), repo_setting.ForgejoHooksEditPost)
|
||||
m.Post("/gitea/{id}", web.Bind(forms.NewWebhookForm{}), repo_setting.GiteaHooksEditPost)
|
||||
m.Post("/gogs/{id}", web.Bind(forms.NewGogshookForm{}), repo_setting.GogsHooksEditPost)
|
||||
m.Post("/slack/{id}", web.Bind(forms.NewSlackHookForm{}), repo_setting.SlackHooksEditPost)
|
||||
m.Post("/discord/{id}", web.Bind(forms.NewDiscordHookForm{}), repo_setting.DiscordHooksEditPost)
|
||||
|
|
|
@ -279,21 +279,6 @@ func (f WebhookForm) ChooseEvents() bool {
|
|||
return f.Events == "choose_events"
|
||||
}
|
||||
|
||||
// NewWebhookForm form for creating web hook
|
||||
type NewWebhookForm struct {
|
||||
PayloadURL string `binding:"Required;ValidUrl"`
|
||||
HTTPMethod string `binding:"Required;In(POST,GET)"`
|
||||
ContentType int `binding:"Required"`
|
||||
Secret string
|
||||
WebhookForm
|
||||
}
|
||||
|
||||
// Validate validates the fields
|
||||
func (f *NewWebhookForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
|
||||
ctx := context.GetValidateContext(req)
|
||||
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
|
||||
}
|
||||
|
||||
// NewGogshookForm form for creating gogs hook
|
||||
type NewGogshookForm struct {
|
||||
PayloadURL string `binding:"Required;ValidUrl"`
|
||||
|
|
|
@ -18,6 +18,7 @@ import (
|
|||
webhook_model "code.gitea.io/gitea/models/webhook"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
webhook_module "code.gitea.io/gitea/modules/webhook"
|
||||
"code.gitea.io/gitea/services/forms"
|
||||
)
|
||||
|
||||
var _ Handler = defaultHandler{}
|
||||
|
@ -36,7 +37,27 @@ func (dh defaultHandler) Type() webhook_module.HookType {
|
|||
func (defaultHandler) Metadata(*webhook_model.Webhook) any { return nil }
|
||||
|
||||
func (defaultHandler) FormFields(bind func(any)) FormFields {
|
||||
panic("TODO")
|
||||
var form struct {
|
||||
forms.WebhookForm
|
||||
PayloadURL string `binding:"Required;ValidUrl"`
|
||||
HTTPMethod string `binding:"Required;In(POST,GET)"`
|
||||
ContentType int `binding:"Required"`
|
||||
Secret string
|
||||
}
|
||||
bind(&form)
|
||||
|
||||
contentType := webhook_model.ContentTypeJSON
|
||||
if webhook_model.HookContentType(form.ContentType) == webhook_model.ContentTypeForm {
|
||||
contentType = webhook_model.ContentTypeForm
|
||||
}
|
||||
return FormFields{
|
||||
WebhookForm: form.WebhookForm,
|
||||
URL: form.PayloadURL,
|
||||
ContentType: contentType,
|
||||
Secret: form.Secret,
|
||||
HTTPMethod: form.HTTPMethod,
|
||||
Metadata: nil,
|
||||
}
|
||||
}
|
||||
|
||||
func (defaultHandler) NewRequest(ctx context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (req *http.Request, body []byte, err error) {
|
||||
|
|
Loading…
Reference in a new issue