Possible fix the webhook API creation (#13960)

* Possible fix the webhook API creation

* Fix api create webhook bug
This commit is contained in:
Lunny Xiao 2020-12-12 23:33:19 +08:00 коммит произвёл GitHub
Родитель 6074e13c8d
Коммит 9f100a45c6
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 7 добавлений и 3 удалений

Просмотреть файл

@ -6,6 +6,7 @@ package utils
import ( import (
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
"strings" "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 // write the appropriate error to `ctx`. Return whether the form is valid
func CheckCreateHookOption(ctx *context.APIContext, form *api.CreateHookOption) bool { func CheckCreateHookOption(ctx *context.APIContext, form *api.CreateHookOption) bool {
if !webhook.IsValidHookTaskType(form.Type) { 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 return false
} }
for _, name := range []string{"url", "content_type"} { for _, name := range []string{"url", "content_type"} {

Просмотреть файл

@ -60,12 +60,15 @@ var (
// RegisterWebhook registers a webhook // RegisterWebhook registers a webhook
func RegisterWebhook(name string, webhook *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 // IsValidHookTaskType returns true if a webhook registered
func IsValidHookTaskType(name string) bool { func IsValidHookTaskType(name string) bool {
_, ok := webhooks[models.HookTaskType(name)] if name == models.GITEA || name == models.GOGS {
return true
}
_, ok := webhooks[models.HookTaskType(strings.TrimSpace(name))]
return ok return ok
} }