From fb9839f16dddf4af48c3d5541ca0158046a52d30 Mon Sep 17 00:00:00 2001 From: Cyborus Date: Fri, 7 Nov 2025 07:04:21 +0100 Subject: [PATCH] fix(api): set all hook event types (#9997) The `addHook` function (and subsequently all endpoints that add a webhook) did not set the `Package`, `ActionRunFailure`, `ActionRunRecover`, or `ActionRunSuccess` event types on the newly created webhook. Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9997 Reviewed-by: Gusted Co-authored-by: Cyborus Co-committed-by: Cyborus --- routers/api/v1/utils/hook.go | 4 +++ routers/api/v1/utils/hook_test.go | 41 +++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/routers/api/v1/utils/hook.go b/routers/api/v1/utils/hook.go index fc4b3293ac..9a935f0cce 100644 --- a/routers/api/v1/utils/hook.go +++ b/routers/api/v1/utils/hook.go @@ -210,6 +210,10 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, ownerID, repoI Wiki: util.SliceContainsString(form.Events, string(webhook_module.HookEventWiki), true), Repository: util.SliceContainsString(form.Events, string(webhook_module.HookEventRepository), true), Release: util.SliceContainsString(form.Events, string(webhook_module.HookEventRelease), true), + Package: util.SliceContainsString(form.Events, string(webhook_module.HookEventPackage), true), + ActionRunFailure: util.SliceContainsString(form.Events, string(webhook_module.HookEventActionRunFailure), true), + ActionRunRecover: util.SliceContainsString(form.Events, string(webhook_module.HookEventActionRunRecover), true), + ActionRunSuccess: util.SliceContainsString(form.Events, string(webhook_module.HookEventActionRunSuccess), true), }, BranchFilter: form.BranchFilter, }, diff --git a/routers/api/v1/utils/hook_test.go b/routers/api/v1/utils/hook_test.go index 3d0e6db079..d7aff76d34 100644 --- a/routers/api/v1/utils/hook_test.go +++ b/routers/api/v1/utils/hook_test.go @@ -5,13 +5,16 @@ package utils import ( "net/http" + "reflect" "testing" "forgejo.org/models/unittest" "forgejo.org/modules/structs" + webhook_module "forgejo.org/modules/webhook" "forgejo.org/services/contexttest" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestTestHookValidation(t *testing.T) { @@ -84,3 +87,41 @@ func TestTestHookValidation(t *testing.T) { assert.Equal(t, http.StatusUnprocessableEntity, ctx.Resp.WrittenStatus()) }) } + +func TestHookEventInclusion(t *testing.T) { + ctx, _ := contexttest.MockAPIContext(t, "user2/repo1/hooks") + contexttest.LoadRepo(t, ctx, 1) + contexttest.LoadGitRepo(t, ctx) + contexttest.LoadRepoCommit(t, ctx) + contexttest.LoadUser(t, ctx, 2) + + opts := structs.CreateHookOption{ + Type: "forgejo", + Config: structs.CreateHookOptionConfig{ + "content_type": "json", + "url": "http://example.com/webhook", + }, + Events: []string{ + string(webhook_module.HookEventCreate), + string(webhook_module.HookEventDelete), + string(webhook_module.HookEventFork), + string(webhook_module.HookEventIssues), + string(webhook_module.HookEventPush), + string(webhook_module.HookEventPullRequest), + string(webhook_module.HookEventWiki), + string(webhook_module.HookEventRepository), + string(webhook_module.HookEventRelease), + string(webhook_module.HookEventPackage), + string(webhook_module.HookEventActionRunFailure), + string(webhook_module.HookEventActionRunRecover), + string(webhook_module.HookEventActionRunSuccess), + }, + } + hook, ok := addHook(ctx, &opts, 2, 1) + require.True(t, ok) + val := reflect.ValueOf(hook.HookEvents) + ty := val.Type() + for i := range val.NumField() { + assert.Truef(t, val.Field(i).Interface().(bool), "missing '%s' event", ty.Field(i).Name) + } +}