mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-14 06:39:40 +01:00
[REFACTOR] webhook gogs endpoint
This commit is contained in:
parent
dce754cde1
commit
4ab341e971
4 changed files with 27 additions and 44 deletions
|
@ -23,7 +23,6 @@ import (
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
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"
|
||||||
"code.gitea.io/gitea/modules/web"
|
|
||||||
"code.gitea.io/gitea/modules/web/middleware"
|
"code.gitea.io/gitea/modules/web/middleware"
|
||||||
webhook_module "code.gitea.io/gitea/modules/webhook"
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
||||||
"code.gitea.io/gitea/services/context"
|
"code.gitea.io/gitea/services/context"
|
||||||
|
@ -361,33 +360,6 @@ func editWebhook(ctx *context.Context, params webhookParams) {
|
||||||
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
ctx.Redirect(fmt.Sprintf("%s/%d", orCtx.Link, w.ID))
|
||||||
}
|
}
|
||||||
|
|
||||||
// GogsHooksNewPost response for creating Gogs webhook
|
|
||||||
func GogsHooksNewPost(ctx *context.Context) {
|
|
||||||
createWebhook(ctx, gogsHookParams(ctx))
|
|
||||||
}
|
|
||||||
|
|
||||||
// GogsHooksEditPost response for editing Gogs webhook
|
|
||||||
func GogsHooksEditPost(ctx *context.Context) {
|
|
||||||
editWebhook(ctx, gogsHookParams(ctx))
|
|
||||||
}
|
|
||||||
|
|
||||||
func gogsHookParams(ctx *context.Context) webhookParams {
|
|
||||||
form := web.GetForm(ctx).(*forms.NewGogshookForm)
|
|
||||||
|
|
||||||
contentType := webhook.ContentTypeJSON
|
|
||||||
if webhook.HookContentType(form.ContentType) == webhook.ContentTypeForm {
|
|
||||||
contentType = webhook.ContentTypeForm
|
|
||||||
}
|
|
||||||
|
|
||||||
return webhookParams{
|
|
||||||
Type: webhook_module.GOGS,
|
|
||||||
URL: form.PayloadURL,
|
|
||||||
ContentType: contentType,
|
|
||||||
Secret: form.Secret,
|
|
||||||
WebhookForm: form.WebhookForm,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkWebhook(ctx *context.Context) (*ownerRepoCtx, *webhook.Webhook) {
|
func checkWebhook(ctx *context.Context) (*ownerRepoCtx, *webhook.Webhook) {
|
||||||
orCtx, err := getOwnerRepoCtx(ctx)
|
orCtx, err := getOwnerRepoCtx(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -402,12 +402,10 @@ 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("/{type}/new", repo_setting.WebhookCreate)
|
m.Post("/{type}/new", repo_setting.WebhookCreate)
|
||||||
}
|
}
|
||||||
|
|
||||||
addWebhookEditRoutes := func() {
|
addWebhookEditRoutes := func() {
|
||||||
m.Post("/gogs/{id}", web.Bind(forms.NewGogshookForm{}), repo_setting.GogsHooksEditPost)
|
|
||||||
m.Post("/{type}/{id:[0-9]+}", repo_setting.WebhookUpdate)
|
m.Post("/{type}/{id:[0-9]+}", repo_setting.WebhookUpdate)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -278,20 +278,6 @@ func (f WebhookForm) ChooseEvents() bool {
|
||||||
return f.Events == "choose_events"
|
return f.Events == "choose_events"
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewGogshookForm form for creating gogs hook
|
|
||||||
type NewGogshookForm struct {
|
|
||||||
PayloadURL string `binding:"Required;ValidUrl"`
|
|
||||||
ContentType int `binding:"Required"`
|
|
||||||
Secret string
|
|
||||||
WebhookForm
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate validates the fields
|
|
||||||
func (f *NewGogshookForm) Validate(req *http.Request, errs binding.Errors) binding.Errors {
|
|
||||||
ctx := context.GetValidateContext(req)
|
|
||||||
return middleware.Validate(errs, ctx.Data, f, ctx.Locale)
|
|
||||||
}
|
|
||||||
|
|
||||||
// .___
|
// .___
|
||||||
// | | ______ ________ __ ____
|
// | | ______ ________ __ ____
|
||||||
// | |/ ___// ___/ | \_/ __ \
|
// | |/ ___// ___/ | \_/ __ \
|
||||||
|
|
|
@ -4,9 +4,36 @@
|
||||||
package webhook
|
package webhook
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
webhook_model "code.gitea.io/gitea/models/webhook"
|
||||||
webhook_module "code.gitea.io/gitea/modules/webhook"
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
||||||
|
"code.gitea.io/gitea/services/forms"
|
||||||
)
|
)
|
||||||
|
|
||||||
type gogsHandler struct{ defaultHandler }
|
type gogsHandler struct{ defaultHandler }
|
||||||
|
|
||||||
func (gogsHandler) Type() webhook_module.HookType { return webhook_module.GOGS }
|
func (gogsHandler) Type() webhook_module.HookType { return webhook_module.GOGS }
|
||||||
|
|
||||||
|
func (gogsHandler) FormFields(bind func(any)) FormFields {
|
||||||
|
var form struct {
|
||||||
|
forms.WebhookForm
|
||||||
|
PayloadURL string `binding:"Required;ValidUrl"`
|
||||||
|
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: http.MethodPost,
|
||||||
|
Metadata: nil,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue