Skip to content

Commit 3c79315

Browse files
Fix: system webhooks API bug (#28531) (#28666)
Backport #28531 by @pulltheflower - Fix the bug about admin/hooks API that `GET /admin/hooks` can only fetch system_hooks, `POST /admin/hooks` can only create default_hooks. Co-authored-by: vincent <[email protected]>
1 parent 3e1bd61 commit 3c79315

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

routers/api/v1/utils/hook.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package utils
66
import (
77
"fmt"
88
"net/http"
9+
"strconv"
910
"strings"
1011

1112
user_model "code.gitea.io/gitea/models/user"
@@ -162,20 +163,30 @@ func pullHook(events []string, event string) bool {
162163
// addHook add the hook specified by `form`, `ownerID` and `repoID`. If there is
163164
// an error, write to `ctx` accordingly. Return (webhook, ok)
164165
func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoID int64) (*webhook.Webhook, bool) {
166+
var isSystemWebhook bool
165167
if !checkCreateHookOption(ctx, form) {
166168
return nil, false
167169
}
168170

169171
if len(form.Events) == 0 {
170172
form.Events = []string{"push"}
171173
}
174+
if form.Config["is_system_webhook"] != "" {
175+
sw, err := strconv.ParseBool(form.Config["is_system_webhook"])
176+
if err != nil {
177+
ctx.Error(http.StatusUnprocessableEntity, "", "Invalid is_system_webhook value")
178+
return nil, false
179+
}
180+
isSystemWebhook = sw
181+
}
172182
w := &webhook.Webhook{
173-
OwnerID: ownerID,
174-
RepoID: repoID,
175-
URL: form.Config["url"],
176-
ContentType: webhook.ToHookContentType(form.Config["content_type"]),
177-
Secret: form.Config["secret"],
178-
HTTPMethod: "POST",
183+
OwnerID: ownerID,
184+
RepoID: repoID,
185+
URL: form.Config["url"],
186+
ContentType: webhook.ToHookContentType(form.Config["content_type"]),
187+
Secret: form.Config["secret"],
188+
HTTPMethod: "POST",
189+
IsSystemWebhook: isSystemWebhook,
179190
HookEvent: &webhook_module.HookEvent{
180191
ChooseEvents: true,
181192
HookEvents: webhook_module.HookEvents{

0 commit comments

Comments
 (0)