Fix possible panic (#34508)

This commit is contained in:
Lunny Xiao 2025-05-22 05:59:42 -07:00 committed by GitHub
parent 0d1d57c5bf
commit 06ccda06c4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 65 additions and 25 deletions

13
modules/util/map.go Normal file
View File

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

26
modules/util/map_test.go Normal file
View File

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

View File

@ -15,6 +15,7 @@ import (
"code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"github.com/nektos/act/pkg/model" "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 { func (g *GiteaContext) ToGitHubContext() *model.GithubContext {
return &model.GithubContext{ return &model.GithubContext{
Event: (*g)["event"].(map[string]any), Event: util.GetMapValueOrDefault(*g, "event", map[string]any(nil)),
EventPath: (*g)["event_path"].(string), EventPath: util.GetMapValueOrDefault(*g, "event_path", ""),
Workflow: (*g)["workflow"].(string), Workflow: util.GetMapValueOrDefault(*g, "workflow", ""),
RunID: (*g)["run_id"].(string), RunID: util.GetMapValueOrDefault(*g, "run_id", ""),
RunNumber: (*g)["run_number"].(string), RunNumber: util.GetMapValueOrDefault(*g, "run_number", ""),
Actor: (*g)["actor"].(string), Actor: util.GetMapValueOrDefault(*g, "actor", ""),
Repository: (*g)["repository"].(string), Repository: util.GetMapValueOrDefault(*g, "repository", ""),
EventName: (*g)["event_name"].(string), EventName: util.GetMapValueOrDefault(*g, "event_name", ""),
Sha: (*g)["sha"].(string), Sha: util.GetMapValueOrDefault(*g, "sha", ""),
Ref: (*g)["ref"].(string), Ref: util.GetMapValueOrDefault(*g, "ref", ""),
RefName: (*g)["ref_name"].(string), RefName: util.GetMapValueOrDefault(*g, "ref_name", ""),
RefType: (*g)["ref_type"].(string), RefType: util.GetMapValueOrDefault(*g, "ref_type", ""),
HeadRef: (*g)["head_ref"].(string), HeadRef: util.GetMapValueOrDefault(*g, "head_ref", ""),
BaseRef: (*g)["base_ref"].(string), BaseRef: util.GetMapValueOrDefault(*g, "base_ref", ""),
Token: "", // deliberately omitted for security Token: "", // deliberately omitted for security
Workspace: (*g)["workspace"].(string), Workspace: util.GetMapValueOrDefault(*g, "workspace", ""),
Action: (*g)["action"].(string), Action: util.GetMapValueOrDefault(*g, "action", ""),
ActionPath: (*g)["action_path"].(string), ActionPath: util.GetMapValueOrDefault(*g, "action_path", ""),
ActionRef: (*g)["action_ref"].(string), ActionRef: util.GetMapValueOrDefault(*g, "action_ref", ""),
ActionRepository: (*g)["action_repository"].(string), ActionRepository: util.GetMapValueOrDefault(*g, "action_repository", ""),
Job: (*g)["job"].(string), Job: util.GetMapValueOrDefault(*g, "job", ""),
JobName: "", // not present in GiteaContext JobName: "", // not present in GiteaContext
RepositoryOwner: (*g)["repository_owner"].(string), RepositoryOwner: util.GetMapValueOrDefault(*g, "repository_owner", ""),
RetentionDays: (*g)["retention_days"].(string), RetentionDays: util.GetMapValueOrDefault(*g, "retention_days", ""),
RunnerPerflog: "", // not present in GiteaContext RunnerPerflog: "", // not present in GiteaContext
RunnerTrackingID: "", // not present in GiteaContext RunnerTrackingID: "", // not present in GiteaContext
ServerURL: (*g)["server_url"].(string), ServerURL: util.GetMapValueOrDefault(*g, "server_url", ""),
APIURL: (*g)["api_url"].(string), APIURL: util.GetMapValueOrDefault(*g, "api_url", ""),
GraphQLURL: (*g)["graphql_url"].(string), GraphQLURL: util.GetMapValueOrDefault(*g, "graphql_url", ""),
} }
} }