From 22e267ee27e8f9f32af44c7fb6fc642dfc7e026f Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 12 Dec 2020 10:23:11 +0800 Subject: [PATCH 1/2] Possible fix the webhook API creation --- routers/api/v1/utils/hook.go | 3 ++- services/webhook/webhook.go | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/routers/api/v1/utils/hook.go b/routers/api/v1/utils/hook.go index c8471184f571a..8decc5cf4329d 100644 --- a/routers/api/v1/utils/hook.go +++ b/routers/api/v1/utils/hook.go @@ -6,6 +6,7 @@ package utils import ( "encoding/json" + "fmt" "net/http" "strings" @@ -53,7 +54,7 @@ func GetRepoHook(ctx *context.APIContext, repoID, hookID int64) (*models.Webhook // write the appropriate error to `ctx`. Return whether the form is valid func CheckCreateHookOption(ctx *context.APIContext, form *api.CreateHookOption) bool { if !webhook.IsValidHookTaskType(form.Type) { - ctx.Error(http.StatusUnprocessableEntity, "", "Invalid hook type") + ctx.Error(http.StatusUnprocessableEntity, "", fmt.Sprintf("Invalid hook type: %s", form.Type)) return false } for _, name := range []string{"url", "content_type"} { diff --git a/services/webhook/webhook.go b/services/webhook/webhook.go index a86d638ab5057..651bc18e0e07d 100644 --- a/services/webhook/webhook.go +++ b/services/webhook/webhook.go @@ -60,12 +60,12 @@ var ( // RegisterWebhook registers a webhook func RegisterWebhook(name string, webhook *webhook) { - webhooks[models.HookTaskType(name)] = webhook + webhooks[models.HookTaskType(strings.TrimSpace(name))] = webhook } // IsValidHookTaskType returns true if a webhook registered func IsValidHookTaskType(name string) bool { - _, ok := webhooks[models.HookTaskType(name)] + _, ok := webhooks[models.HookTaskType(strings.TrimSpace(name))] return ok } From 7dc392748609fbcfb4426162d4d2a159f7cb2595 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sat, 12 Dec 2020 19:12:56 +0800 Subject: [PATCH 2/2] Fix api create webhook bug --- services/webhook/webhook.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/services/webhook/webhook.go b/services/webhook/webhook.go index 651bc18e0e07d..b779b38466ade 100644 --- a/services/webhook/webhook.go +++ b/services/webhook/webhook.go @@ -65,6 +65,9 @@ func RegisterWebhook(name string, webhook *webhook) { // IsValidHookTaskType returns true if a webhook registered func IsValidHookTaskType(name string) bool { + if name == models.GITEA || name == models.GOGS { + return true + } _, ok := webhooks[models.HookTaskType(strings.TrimSpace(name))] return ok }