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 <gusted@noreply.codeberg.org>
Co-authored-by: Cyborus <cyborus@cyborus.xyz>
Co-committed-by: Cyborus <cyborus@cyborus.xyz>
(cherry picked from commit fb9839f16d)
This commit is contained in:
Cyborus 2025-11-07 07:04:21 +01:00 committed by forgejo-backport-action
parent dfdcbaf194
commit cf09611f6a
2 changed files with 45 additions and 0 deletions

View file

@ -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,
},

View file

@ -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)
}
}