From c3d1ad7bbc8cf375630b7fb188f6a06b0c9c5819 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Sat, 14 Jan 2023 10:07:01 +0100 Subject: [PATCH] [BRANDING] define the forgejo webhook type templates/swagger/v1_json.tmpl updated with `make generate-swagger` (cherry picked from commit 88899c492efeedd138ba088a36b9c0bc733ead7b) (cherry picked from commit 7171bd9617c32c4911e3bdbc23c02a19e80d2465) (cherry picked from commit 1a742446c17aef9ca62fe75bfc0a388d40138154) (cherry picked from commit 132342ae8d2015726c2e21d0d34a3b0bf41ab6f7) (cherry picked from commit b47b815fbec083a67068654c13a0046e250e34af) (cherry picked from commit 2dbb52c8ec99f5febf91ed4084905997ba293ee9) (cherry picked from commit c846de432281e0800defc51450433852cf52ea3f) --- modules/setting/webhook.go | 2 +- modules/structs/hook.go | 2 +- modules/webhook/type.go | 1 + routers/web/repo/webhook.go | 28 +++++++++++++ routers/web/web.go | 6 +++ services/webhook/webhook.go | 4 +- templates/admin/hook_new.tmpl | 7 +++- templates/org/settings/hook_new.tmpl | 6 ++- .../repo/settings/webhook/base_list.tmpl | 5 ++- templates/repo/settings/webhook/forgejo.tmpl | 40 +++++++++++++++++++ templates/repo/settings/webhook/new.tmpl | 7 +++- templates/swagger/v1_json.tmpl | 1 + tests/integration/links_test.go | 38 ++++++++++++++++++ 13 files changed, 136 insertions(+), 11 deletions(-) create mode 100644 templates/repo/settings/webhook/forgejo.tmpl diff --git a/modules/setting/webhook.go b/modules/setting/webhook.go index c01261dbbd..b56c55c439 100644 --- a/modules/setting/webhook.go +++ b/modules/setting/webhook.go @@ -35,7 +35,7 @@ func loadWebhookFrom(rootCfg ConfigProvider) { Webhook.DeliverTimeout = sec.Key("DELIVER_TIMEOUT").MustInt(5) Webhook.SkipTLSVerify = sec.Key("SKIP_TLS_VERIFY").MustBool() Webhook.AllowedHostList = sec.Key("ALLOWED_HOST_LIST").MustString("") - Webhook.Types = []string{"gitea", "gogs", "slack", "discord", "dingtalk", "telegram", "msteams", "feishu", "matrix", "wechatwork", "packagist"} + Webhook.Types = []string{"forgejo", "gitea", "gogs", "slack", "discord", "dingtalk", "telegram", "msteams", "feishu", "matrix", "wechatwork", "packagist"} Webhook.PagingNum = sec.Key("PAGING_NUM").MustInt(10) Webhook.ProxyURL = sec.Key("PROXY_URL").MustString("") if Webhook.ProxyURL != "" { diff --git a/modules/structs/hook.go b/modules/structs/hook.go index df5da6790f..5055561a4b 100644 --- a/modules/structs/hook.go +++ b/modules/structs/hook.go @@ -40,7 +40,7 @@ type CreateHookOptionConfig map[string]string // CreateHookOption options when create a hook type CreateHookOption struct { // required: true - // enum: dingtalk,discord,gitea,gogs,msteams,slack,telegram,feishu,wechatwork,packagist + // enum: forgejo,dingtalk,discord,gitea,gogs,msteams,slack,telegram,feishu,wechatwork,packagist Type string `json:"type" binding:"Required"` // required: true Config CreateHookOptionConfig `json:"config" binding:"Required"` diff --git a/modules/webhook/type.go b/modules/webhook/type.go index db4ab17931..fac5590e21 100644 --- a/modules/webhook/type.go +++ b/modules/webhook/type.go @@ -71,6 +71,7 @@ type HookType = string // Types of webhooks const ( + FORGEJO HookType = "forgejo" GITEA HookType = "gitea" GOGS HookType = "gogs" SLACK HookType = "slack" diff --git a/routers/web/repo/webhook.go b/routers/web/repo/webhook.go index d27d0f1bf0..15a1691e5c 100644 --- a/routers/web/repo/webhook.go +++ b/routers/web/repo/webhook.go @@ -308,6 +308,34 @@ 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)) diff --git a/routers/web/web.go b/routers/web/web.go index c462de918a..c4d6786b33 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -577,6 +577,7 @@ func RegisterRoutes(m *web.Route) { m.Get("", repo.WebHooksEdit) m.Post("/replay/{uuid}", repo.ReplayWebhook) }) + m.Post("/forgejo/{id}", web.Bind(forms.NewWebhookForm{}), repo.ForgejoHooksEditPost) m.Post("/gitea/{id}", web.Bind(forms.NewWebhookForm{}), repo.GiteaHooksEditPost) m.Post("/gogs/{id}", web.Bind(forms.NewGogshookForm{}), repo.GogsHooksEditPost) m.Post("/slack/{id}", web.Bind(forms.NewSlackHookForm{}), repo.SlackHooksEditPost) @@ -592,6 +593,7 @@ func RegisterRoutes(m *web.Route) { m.Group("/{configType:default-hooks|system-hooks}", func() { m.Get("/{type}/new", repo.WebhooksNew) + m.Post("/forgejo/new", web.Bind(forms.NewWebhookForm{}), repo.ForgejoHooksNewPost) m.Post("/gitea/new", web.Bind(forms.NewWebhookForm{}), repo.GiteaHooksNewPost) m.Post("/gogs/new", web.Bind(forms.NewGogshookForm{}), repo.GogsHooksNewPost) m.Post("/slack/new", web.Bind(forms.NewSlackHookForm{}), repo.SlackHooksNewPost) @@ -808,6 +810,7 @@ func RegisterRoutes(m *web.Route) { m.Get("", org.Webhooks) m.Post("/delete", org.DeleteWebhook) m.Get("/{type}/new", repo.WebhooksNew) + m.Post("/forgejo/new", web.Bind(forms.NewWebhookForm{}), repo.ForgejoHooksNewPost) m.Post("/gitea/new", web.Bind(forms.NewWebhookForm{}), repo.GiteaHooksNewPost) m.Post("/gogs/new", web.Bind(forms.NewGogshookForm{}), repo.GogsHooksNewPost) m.Post("/slack/new", web.Bind(forms.NewSlackHookForm{}), repo.SlackHooksNewPost) @@ -822,6 +825,7 @@ func RegisterRoutes(m *web.Route) { m.Get("", repo.WebHooksEdit) m.Post("/replay/{uuid}", repo.ReplayWebhook) }) + m.Post("/forgejo/{id}", web.Bind(forms.NewWebhookForm{}), repo.ForgejoHooksEditPost) m.Post("/gitea/{id}", web.Bind(forms.NewWebhookForm{}), repo.GiteaHooksEditPost) m.Post("/gogs/{id}", web.Bind(forms.NewGogshookForm{}), repo.GogsHooksEditPost) m.Post("/slack/{id}", web.Bind(forms.NewSlackHookForm{}), repo.SlackHooksEditPost) @@ -1006,6 +1010,7 @@ func RegisterRoutes(m *web.Route) { m.Get("", repo.Webhooks) m.Post("/delete", repo.DeleteWebhook) m.Get("/{type}/new", repo.WebhooksNew) + m.Post("/forgejo/new", web.Bind(forms.NewWebhookForm{}), repo.ForgejoHooksNewPost) m.Post("/gitea/new", web.Bind(forms.NewWebhookForm{}), repo.GiteaHooksNewPost) m.Post("/gogs/new", web.Bind(forms.NewGogshookForm{}), repo.GogsHooksNewPost) m.Post("/slack/new", web.Bind(forms.NewSlackHookForm{}), repo.SlackHooksNewPost) @@ -1022,6 +1027,7 @@ func RegisterRoutes(m *web.Route) { m.Post("/test", repo.TestWebhook) m.Post("/replay/{uuid}", repo.ReplayWebhook) }) + m.Post("/forgejo/{id}", web.Bind(forms.NewWebhookForm{}), repo.ForgejoHooksEditPost) m.Post("/gitea/{id}", web.Bind(forms.NewWebhookForm{}), repo.GiteaHooksEditPost) m.Post("/gogs/{id}", web.Bind(forms.NewGogshookForm{}), repo.GogsHooksEditPost) m.Post("/slack/{id}", web.Bind(forms.NewSlackHookForm{}), repo.SlackHooksEditPost) diff --git a/services/webhook/webhook.go b/services/webhook/webhook.go index afd8e3c105..b2c622e3d2 100644 --- a/services/webhook/webhook.go +++ b/services/webhook/webhook.go @@ -69,7 +69,7 @@ var webhooks = map[webhook_module.HookType]*webhook{ // IsValidHookTaskType returns true if a webhook registered func IsValidHookTaskType(name string) bool { - if name == webhook_module.GITEA || name == webhook_module.GOGS { + if name == webhook_module.FORGEJO || name == webhook_module.GITEA || name == webhook_module.GOGS { return true } _, ok := webhooks[name] @@ -172,7 +172,7 @@ func PrepareWebhook(ctx context.Context, w *webhook_model.Webhook, event webhook // Avoid sending "0 new commits" to non-integration relevant webhooks (e.g. slack, discord, etc.). // Integration webhooks (e.g. drone) still receive the required data. if pushEvent, ok := p.(*api.PushPayload); ok && - w.Type != webhook_module.GITEA && w.Type != webhook_module.GOGS && + w.Type != webhook_module.FORGEJO && w.Type != webhook_module.GITEA && w.Type != webhook_module.GOGS && len(pushEvent.Commits) == 0 { return nil } diff --git a/templates/admin/hook_new.tmpl b/templates/admin/hook_new.tmpl index 0c018ff293..f15a1c20f4 100644 --- a/templates/admin/hook_new.tmpl +++ b/templates/admin/hook_new.tmpl @@ -14,8 +14,10 @@ {{.locale.Tr "admin.defaulthooks.update_webhook"}} {{end}}
- {{if eq .HookType "gitea"}} - + {{if eq .HookType "forgejo"}} + + {{else if eq .HookType "gitea"}} + {{else if eq .HookType "gogs"}} {{else if eq .HookType "slack"}} @@ -40,6 +42,7 @@
+ {{template "repo/settings/webhook/forgejo" .}} {{template "repo/settings/webhook/gitea" .}} {{template "repo/settings/webhook/gogs" .}} {{template "repo/settings/webhook/slack" .}} diff --git a/templates/org/settings/hook_new.tmpl b/templates/org/settings/hook_new.tmpl index 4685225f4c..35c26c4465 100644 --- a/templates/org/settings/hook_new.tmpl +++ b/templates/org/settings/hook_new.tmpl @@ -9,8 +9,10 @@

{{if .PageIsSettingsHooksNew}}{{.locale.Tr "repo.settings.add_webhook"}}{{else}}{{.locale.Tr "repo.settings.update_webhook"}}{{end}}
- {{if eq .HookType "gitea"}} - + {{if eq .HookType "forgejo"}} + + {{else if eq .HookType "gitea"}} + {{else if eq .HookType "gogs"}} {{else if eq .HookType "slack"}} diff --git a/templates/repo/settings/webhook/base_list.tmpl b/templates/repo/settings/webhook/base_list.tmpl index 61c5799513..6c69e3a2a0 100644 --- a/templates/repo/settings/webhook/base_list.tmpl +++ b/templates/repo/settings/webhook/base_list.tmpl @@ -4,8 +4,11 @@