Skip to content

Commit 06ccda0

Browse files
authored
Fix possible panic (#34508)
1 parent 0d1d57c commit 06ccda0

File tree

3 files changed

+65
-25
lines changed

3 files changed

+65
-25
lines changed

modules/util/map.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package util
5+
6+
func GetMapValueOrDefault[T any](m map[string]any, key string, defaultValue T) T {
7+
if value, ok := m[key]; ok {
8+
if v, ok := value.(T); ok {
9+
return v
10+
}
11+
}
12+
return defaultValue
13+
}

modules/util/map_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package util
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestGetMapValueOrDefault(t *testing.T) {
13+
testMap := map[string]any{
14+
"key1": "value1",
15+
"key2": 42,
16+
"key3": nil,
17+
}
18+
19+
assert.Equal(t, "value1", GetMapValueOrDefault(testMap, "key1", "default"))
20+
assert.Equal(t, 42, GetMapValueOrDefault(testMap, "key2", 0))
21+
22+
assert.Equal(t, "default", GetMapValueOrDefault(testMap, "key4", "default"))
23+
assert.Equal(t, 100, GetMapValueOrDefault(testMap, "key5", 100))
24+
25+
assert.Equal(t, "default", GetMapValueOrDefault(testMap, "key3", "default"))
26+
}

services/actions/context.go

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"code.gitea.io/gitea/modules/git"
1616
"code.gitea.io/gitea/modules/json"
1717
"code.gitea.io/gitea/modules/setting"
18+
"code.gitea.io/gitea/modules/util"
1819

1920
"github.com/nektos/act/pkg/model"
2021
)
@@ -167,34 +168,34 @@ func mergeTwoOutputs(o1, o2 map[string]string) map[string]string {
167168

168169
func (g *GiteaContext) ToGitHubContext() *model.GithubContext {
169170
return &model.GithubContext{
170-
Event: (*g)["event"].(map[string]any),
171-
EventPath: (*g)["event_path"].(string),
172-
Workflow: (*g)["workflow"].(string),
173-
RunID: (*g)["run_id"].(string),
174-
RunNumber: (*g)["run_number"].(string),
175-
Actor: (*g)["actor"].(string),
176-
Repository: (*g)["repository"].(string),
177-
EventName: (*g)["event_name"].(string),
178-
Sha: (*g)["sha"].(string),
179-
Ref: (*g)["ref"].(string),
180-
RefName: (*g)["ref_name"].(string),
181-
RefType: (*g)["ref_type"].(string),
182-
HeadRef: (*g)["head_ref"].(string),
183-
BaseRef: (*g)["base_ref"].(string),
171+
Event: util.GetMapValueOrDefault(*g, "event", map[string]any(nil)),
172+
EventPath: util.GetMapValueOrDefault(*g, "event_path", ""),
173+
Workflow: util.GetMapValueOrDefault(*g, "workflow", ""),
174+
RunID: util.GetMapValueOrDefault(*g, "run_id", ""),
175+
RunNumber: util.GetMapValueOrDefault(*g, "run_number", ""),
176+
Actor: util.GetMapValueOrDefault(*g, "actor", ""),
177+
Repository: util.GetMapValueOrDefault(*g, "repository", ""),
178+
EventName: util.GetMapValueOrDefault(*g, "event_name", ""),
179+
Sha: util.GetMapValueOrDefault(*g, "sha", ""),
180+
Ref: util.GetMapValueOrDefault(*g, "ref", ""),
181+
RefName: util.GetMapValueOrDefault(*g, "ref_name", ""),
182+
RefType: util.GetMapValueOrDefault(*g, "ref_type", ""),
183+
HeadRef: util.GetMapValueOrDefault(*g, "head_ref", ""),
184+
BaseRef: util.GetMapValueOrDefault(*g, "base_ref", ""),
184185
Token: "", // deliberately omitted for security
185-
Workspace: (*g)["workspace"].(string),
186-
Action: (*g)["action"].(string),
187-
ActionPath: (*g)["action_path"].(string),
188-
ActionRef: (*g)["action_ref"].(string),
189-
ActionRepository: (*g)["action_repository"].(string),
190-
Job: (*g)["job"].(string),
186+
Workspace: util.GetMapValueOrDefault(*g, "workspace", ""),
187+
Action: util.GetMapValueOrDefault(*g, "action", ""),
188+
ActionPath: util.GetMapValueOrDefault(*g, "action_path", ""),
189+
ActionRef: util.GetMapValueOrDefault(*g, "action_ref", ""),
190+
ActionRepository: util.GetMapValueOrDefault(*g, "action_repository", ""),
191+
Job: util.GetMapValueOrDefault(*g, "job", ""),
191192
JobName: "", // not present in GiteaContext
192-
RepositoryOwner: (*g)["repository_owner"].(string),
193-
RetentionDays: (*g)["retention_days"].(string),
193+
RepositoryOwner: util.GetMapValueOrDefault(*g, "repository_owner", ""),
194+
RetentionDays: util.GetMapValueOrDefault(*g, "retention_days", ""),
194195
RunnerPerflog: "", // not present in GiteaContext
195196
RunnerTrackingID: "", // not present in GiteaContext
196-
ServerURL: (*g)["server_url"].(string),
197-
APIURL: (*g)["api_url"].(string),
198-
GraphQLURL: (*g)["graphql_url"].(string),
197+
ServerURL: util.GetMapValueOrDefault(*g, "server_url", ""),
198+
APIURL: util.GetMapValueOrDefault(*g, "api_url", ""),
199+
GraphQLURL: util.GetMapValueOrDefault(*g, "graphql_url", ""),
199200
}
200201
}

0 commit comments

Comments
 (0)