mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-07 21:36:48 +02:00
c4a1ff7d16
Backport #37459 by cyphercodes This fixes the scheduled action panic when an event payload is JSON `null` by initializing the payload map before adding `schedule`. It also adds regression coverage for the null-payload case. Fixes #37447. Co-authored-by: Rayan Salhab <r.salhab@aiyexpertsolutions.com> Co-authored-by: cyphercodes <cyphercodes@users.noreply.github.com> Co-authored-by: Hermes Agent (GPT-5.5) <hermes-agent@users.noreply.github.com> Co-authored-by: Nicolas <bircni@icloud.com> Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: Claude (Opus 4.7) <noreply@anthropic.com>
53 lines
1.5 KiB
Go
53 lines
1.5 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package actions
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"code.gitea.io/gitea/modules/json"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestWithScheduleInEventPayload(t *testing.T) {
|
|
t.Run("adds schedule to existing payload", func(t *testing.T) {
|
|
payload := `{"ref":"refs/heads/main"}`
|
|
updated := withScheduleInEventPayload(payload, "*/5 * * * *")
|
|
|
|
event := map[string]any{}
|
|
assert.NoError(t, json.Unmarshal([]byte(updated), &event))
|
|
assert.Equal(t, "*/5 * * * *", event["schedule"])
|
|
assert.Equal(t, "refs/heads/main", event["ref"])
|
|
})
|
|
|
|
t.Run("adds schedule to null payload", func(t *testing.T) {
|
|
updated := withScheduleInEventPayload("null", "37 12 5 1 2")
|
|
|
|
event := map[string]any{}
|
|
assert.NoError(t, json.Unmarshal([]byte(updated), &event))
|
|
assert.Equal(t, "37 12 5 1 2", event["schedule"])
|
|
})
|
|
|
|
t.Run("adds schedule to empty payload", func(t *testing.T) {
|
|
updated := withScheduleInEventPayload("", "37 12 5 1 2")
|
|
|
|
event := map[string]any{}
|
|
assert.NoError(t, json.Unmarshal([]byte(updated), &event))
|
|
assert.Equal(t, "37 12 5 1 2", event["schedule"])
|
|
})
|
|
|
|
t.Run("keeps payload when schedule empty", func(t *testing.T) {
|
|
payload := `{"ref":"refs/heads/main"}`
|
|
updated := withScheduleInEventPayload(payload, "")
|
|
assert.Equal(t, payload, updated)
|
|
})
|
|
|
|
t.Run("keeps payload when malformed JSON", func(t *testing.T) {
|
|
payload := `not a json object`
|
|
updated := withScheduleInEventPayload(payload, "*/5 * * * *")
|
|
assert.Equal(t, payload, updated)
|
|
})
|
|
}
|