[REFACTOR] simplify checkHookType

This commit is contained in:
oliverpool 2024-03-21 14:39:02 +01:00
parent c0dd92e9c5
commit 9c3611ec50
2 changed files with 8 additions and 25 deletions

View file

@ -15,7 +15,6 @@ var Webhook = struct {
DeliverTimeout int DeliverTimeout int
SkipTLSVerify bool SkipTLSVerify bool
AllowedHostList string AllowedHostList string
Types []string
PagingNum int PagingNum int
ProxyURL string ProxyURL string
ProxyURLFixed *url.URL ProxyURLFixed *url.URL
@ -35,7 +34,6 @@ func loadWebhookFrom(rootCfg ConfigProvider) {
Webhook.DeliverTimeout = sec.Key("DELIVER_TIMEOUT").MustInt(5) Webhook.DeliverTimeout = sec.Key("DELIVER_TIMEOUT").MustInt(5)
Webhook.SkipTLSVerify = sec.Key("SKIP_TLS_VERIFY").MustBool() Webhook.SkipTLSVerify = sec.Key("SKIP_TLS_VERIFY").MustBool()
Webhook.AllowedHostList = sec.Key("ALLOWED_HOST_LIST").MustString("") Webhook.AllowedHostList = sec.Key("ALLOWED_HOST_LIST").MustString("")
Webhook.Types = []string{"forgejo", "gitea", "gogs", "slack", "discord", "dingtalk", "telegram", "msteams", "feishu", "matrix", "wechatwork", "packagist"}
Webhook.PagingNum = sec.Key("PAGING_NUM").MustInt(10) Webhook.PagingNum = sec.Key("PAGING_NUM").MustInt(10)
Webhook.ProxyURL = sec.Key("PROXY_URL").MustString("") Webhook.ProxyURL = sec.Key("PROXY_URL").MustString("")
if Webhook.ProxyURL != "" { if Webhook.ProxyURL != "" {

View file

@ -10,7 +10,6 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"path" "path"
"strings"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/models/perm" "code.gitea.io/gitea/models/perm"
@ -22,7 +21,6 @@ import (
"code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/json"
"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/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"
@ -111,15 +109,6 @@ func getOwnerRepoCtx(ctx *context.Context) (*ownerRepoCtx, error) {
return nil, errors.New("unable to set OwnerRepo context") return nil, errors.New("unable to set OwnerRepo context")
} }
func checkHookType(ctx *context.Context) string {
hookType := strings.ToLower(ctx.Params(":type"))
if !util.SliceContainsString(setting.Webhook.Types, hookType, true) {
ctx.NotFound("checkHookType", nil)
return ""
}
return hookType
}
// WebhooksNew render creating webhook page // WebhooksNew render creating webhook page
func WebhooksNew(ctx *context.Context) { func WebhooksNew(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook") ctx.Data["Title"] = ctx.Tr("repo.settings.add_webhook")
@ -142,16 +131,12 @@ func WebhooksNew(ctx *context.Context) {
ctx.Data["PageIsSettingsHooksNew"] = true ctx.Data["PageIsSettingsHooksNew"] = true
} }
hookType := checkHookType(ctx) hookType := ctx.Params(":type")
ctx.Data["HookType"] = hookType if webhook_service.GetWebhookHandler(hookType) == nil {
if ctx.Written() { ctx.NotFound("GetWebhookHandler", nil)
return return
} }
if hookType == "discord" { ctx.Data["HookType"] = hookType
ctx.Data["DiscordHook"] = map[string]any{
"Username": "Gitea",
}
}
ctx.Data["BaseLink"] = orCtx.LinkNew ctx.Data["BaseLink"] = orCtx.LinkNew
ctx.Data["BaseLinkNew"] = orCtx.LinkNew ctx.Data["BaseLinkNew"] = orCtx.LinkNew
@ -192,8 +177,8 @@ func ParseHookEvent(form forms.WebhookForm) *webhook_module.HookEvent {
} }
func WebhookCreate(ctx *context.Context) { func WebhookCreate(ctx *context.Context) {
typ := ctx.Params(":type") hookType := ctx.Params(":type")
handler := webhook_service.GetWebhookHandler(typ) handler := webhook_service.GetWebhookHandler(hookType)
if handler == nil { if handler == nil {
ctx.NotFound("GetWebhookHandler", nil) ctx.NotFound("GetWebhookHandler", nil)
return return
@ -208,7 +193,7 @@ func WebhookCreate(ctx *context.Context) {
ctx.Data["PageIsSettingsHooks"] = true ctx.Data["PageIsSettingsHooks"] = true
ctx.Data["PageIsSettingsHooksNew"] = true ctx.Data["PageIsSettingsHooksNew"] = true
ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook_module.HookEvent{}} ctx.Data["Webhook"] = webhook.Webhook{HookEvent: &webhook_module.HookEvent{}}
ctx.Data["HookType"] = typ ctx.Data["HookType"] = hookType
orCtx, err := getOwnerRepoCtx(ctx) orCtx, err := getOwnerRepoCtx(ctx)
if err != nil { if err != nil {
@ -256,7 +241,7 @@ func WebhookCreate(ctx *context.Context) {
Secret: fields.Secret, Secret: fields.Secret,
HookEvent: ParseHookEvent(fields.WebhookForm), HookEvent: ParseHookEvent(fields.WebhookForm),
IsActive: fields.WebhookForm.Active, IsActive: fields.WebhookForm.Active,
Type: typ, Type: hookType,
Meta: string(meta), Meta: string(meta),
OwnerID: orCtx.OwnerID, OwnerID: orCtx.OwnerID,
IsSystemWebhook: orCtx.IsSystemWebhook, IsSystemWebhook: orCtx.IsSystemWebhook,