diff --git a/modules/util/map.go b/modules/util/map.go new file mode 100644 index 0000000000000..f307faad1f539 --- /dev/null +++ b/modules/util/map.go @@ -0,0 +1,13 @@ +// Copyright 2025 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package util + +func GetMapValueOrDefault[T any](m map[string]any, key string, defaultValue T) T { + if value, ok := m[key]; ok { + if v, ok := value.(T); ok { + return v + } + } + return defaultValue +} diff --git a/modules/util/map_test.go b/modules/util/map_test.go new file mode 100644 index 0000000000000..1a141cec88f5f --- /dev/null +++ b/modules/util/map_test.go @@ -0,0 +1,26 @@ +// Copyright 2025 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package util + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGetMapValueOrDefault(t *testing.T) { + testMap := map[string]any{ + "key1": "value1", + "key2": 42, + "key3": nil, + } + + assert.Equal(t, "value1", GetMapValueOrDefault(testMap, "key1", "default")) + assert.Equal(t, 42, GetMapValueOrDefault(testMap, "key2", 0)) + + assert.Equal(t, "default", GetMapValueOrDefault(testMap, "key4", "default")) + assert.Equal(t, 100, GetMapValueOrDefault(testMap, "key5", 100)) + + assert.Equal(t, "default", GetMapValueOrDefault(testMap, "key3", "default")) +} diff --git a/services/actions/context.go b/services/actions/context.go index 8f686a688a42b..b6de429ccf5f0 100644 --- a/services/actions/context.go +++ b/services/actions/context.go @@ -15,6 +15,7 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" "github.com/nektos/act/pkg/model" ) @@ -167,34 +168,34 @@ func mergeTwoOutputs(o1, o2 map[string]string) map[string]string { func (g *GiteaContext) ToGitHubContext() *model.GithubContext { return &model.GithubContext{ - Event: (*g)["event"].(map[string]any), - EventPath: (*g)["event_path"].(string), - Workflow: (*g)["workflow"].(string), - RunID: (*g)["run_id"].(string), - RunNumber: (*g)["run_number"].(string), - Actor: (*g)["actor"].(string), - Repository: (*g)["repository"].(string), - EventName: (*g)["event_name"].(string), - Sha: (*g)["sha"].(string), - Ref: (*g)["ref"].(string), - RefName: (*g)["ref_name"].(string), - RefType: (*g)["ref_type"].(string), - HeadRef: (*g)["head_ref"].(string), - BaseRef: (*g)["base_ref"].(string), + Event: util.GetMapValueOrDefault(*g, "event", map[string]any(nil)), + EventPath: util.GetMapValueOrDefault(*g, "event_path", ""), + Workflow: util.GetMapValueOrDefault(*g, "workflow", ""), + RunID: util.GetMapValueOrDefault(*g, "run_id", ""), + RunNumber: util.GetMapValueOrDefault(*g, "run_number", ""), + Actor: util.GetMapValueOrDefault(*g, "actor", ""), + Repository: util.GetMapValueOrDefault(*g, "repository", ""), + EventName: util.GetMapValueOrDefault(*g, "event_name", ""), + Sha: util.GetMapValueOrDefault(*g, "sha", ""), + Ref: util.GetMapValueOrDefault(*g, "ref", ""), + RefName: util.GetMapValueOrDefault(*g, "ref_name", ""), + RefType: util.GetMapValueOrDefault(*g, "ref_type", ""), + HeadRef: util.GetMapValueOrDefault(*g, "head_ref", ""), + BaseRef: util.GetMapValueOrDefault(*g, "base_ref", ""), Token: "", // deliberately omitted for security - Workspace: (*g)["workspace"].(string), - Action: (*g)["action"].(string), - ActionPath: (*g)["action_path"].(string), - ActionRef: (*g)["action_ref"].(string), - ActionRepository: (*g)["action_repository"].(string), - Job: (*g)["job"].(string), + Workspace: util.GetMapValueOrDefault(*g, "workspace", ""), + Action: util.GetMapValueOrDefault(*g, "action", ""), + ActionPath: util.GetMapValueOrDefault(*g, "action_path", ""), + ActionRef: util.GetMapValueOrDefault(*g, "action_ref", ""), + ActionRepository: util.GetMapValueOrDefault(*g, "action_repository", ""), + Job: util.GetMapValueOrDefault(*g, "job", ""), JobName: "", // not present in GiteaContext - RepositoryOwner: (*g)["repository_owner"].(string), - RetentionDays: (*g)["retention_days"].(string), + RepositoryOwner: util.GetMapValueOrDefault(*g, "repository_owner", ""), + RetentionDays: util.GetMapValueOrDefault(*g, "retention_days", ""), RunnerPerflog: "", // not present in GiteaContext RunnerTrackingID: "", // not present in GiteaContext - ServerURL: (*g)["server_url"].(string), - APIURL: (*g)["api_url"].(string), - GraphQLURL: (*g)["graphql_url"].(string), + ServerURL: util.GetMapValueOrDefault(*g, "server_url", ""), + APIURL: util.GetMapValueOrDefault(*g, "api_url", ""), + GraphQLURL: util.GetMapValueOrDefault(*g, "graphql_url", ""), } }