mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-18 11:51:16 +00:00
chore: enable forcetypeassert linter, fix issues (#38804)
Enable [`forcetypeassert`](https://github.com/gostaticanalysis/forcetypeassert) linter to prevent unchecked type assertions. ~650 issues fixed, most fixes were clean, some use `setting.PanicInDevOrTesting`. The only behaviour changes are where code would previously send a 500 error or panic, a 4xx error is now emitted. Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
+34
-26
@@ -20,7 +20,6 @@ import (
|
||||
"gitea.dev/modules/optional"
|
||||
"gitea.dev/modules/setting"
|
||||
api "gitea.dev/modules/structs"
|
||||
"gitea.dev/modules/util"
|
||||
)
|
||||
|
||||
type GiteaContext map[string]any
|
||||
@@ -318,36 +317,45 @@ func mergeTwoOutputs(o1, o2 map[string]string) map[string]string {
|
||||
return ret
|
||||
}
|
||||
|
||||
func contextMapValueOrDefault[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
|
||||
}
|
||||
|
||||
func (g *GiteaContext) ToGitHubContext() *model.GithubContext {
|
||||
return &model.GithubContext{
|
||||
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", ""),
|
||||
Event: contextMapValueOrDefault(*g, "event", map[string]any(nil)),
|
||||
EventPath: contextMapValueOrDefault(*g, "event_path", ""),
|
||||
Workflow: contextMapValueOrDefault(*g, "workflow", ""),
|
||||
RunID: contextMapValueOrDefault(*g, "run_id", ""),
|
||||
RunNumber: contextMapValueOrDefault(*g, "run_number", ""),
|
||||
Actor: contextMapValueOrDefault(*g, "actor", ""),
|
||||
Repository: contextMapValueOrDefault(*g, "repository", ""),
|
||||
EventName: contextMapValueOrDefault(*g, "event_name", ""),
|
||||
Sha: contextMapValueOrDefault(*g, "sha", ""),
|
||||
Ref: contextMapValueOrDefault(*g, "ref", ""),
|
||||
RefName: contextMapValueOrDefault(*g, "ref_name", ""),
|
||||
RefType: contextMapValueOrDefault(*g, "ref_type", ""),
|
||||
HeadRef: contextMapValueOrDefault(*g, "head_ref", ""),
|
||||
BaseRef: contextMapValueOrDefault(*g, "base_ref", ""),
|
||||
Token: "", // deliberately omitted for security
|
||||
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", ""),
|
||||
Workspace: contextMapValueOrDefault(*g, "workspace", ""),
|
||||
Action: contextMapValueOrDefault(*g, "action", ""),
|
||||
ActionPath: contextMapValueOrDefault(*g, "action_path", ""),
|
||||
ActionRef: contextMapValueOrDefault(*g, "action_ref", ""),
|
||||
ActionRepository: contextMapValueOrDefault(*g, "action_repository", ""),
|
||||
Job: contextMapValueOrDefault(*g, "job", ""),
|
||||
JobName: "", // not present in GiteaContext
|
||||
RepositoryOwner: util.GetMapValueOrDefault(*g, "repository_owner", ""),
|
||||
RetentionDays: util.GetMapValueOrDefault(*g, "retention_days", ""),
|
||||
RepositoryOwner: contextMapValueOrDefault(*g, "repository_owner", ""),
|
||||
RetentionDays: contextMapValueOrDefault(*g, "retention_days", ""),
|
||||
RunnerPerflog: "", // not present in GiteaContext
|
||||
RunnerTrackingID: "", // not present in GiteaContext
|
||||
ServerURL: util.GetMapValueOrDefault(*g, "server_url", ""),
|
||||
APIURL: util.GetMapValueOrDefault(*g, "api_url", ""),
|
||||
GraphQLURL: util.GetMapValueOrDefault(*g, "graphql_url", ""),
|
||||
ServerURL: contextMapValueOrDefault(*g, "server_url", ""),
|
||||
APIURL: contextMapValueOrDefault(*g, "api_url", ""),
|
||||
GraphQLURL: contextMapValueOrDefault(*g, "graphql_url", ""),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
api "gitea.dev/modules/structs"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestWithScheduleInEventPayload(t *testing.T) {
|
||||
@@ -49,9 +50,15 @@ func TestWithScheduleInEventPayload(t *testing.T) {
|
||||
event := map[string]any{}
|
||||
assert.NoError(t, json.Unmarshal([]byte(updated), &event))
|
||||
assert.Equal(t, "@weekly", event["schedule"])
|
||||
assert.Equal(t, "test-repo", event["repository"].(map[string]any)["name"])
|
||||
assert.Equal(t, "test-user", event["sender"].(map[string]any)["login"])
|
||||
assert.Equal(t, "test-org", event["organization"].(map[string]any)["name"])
|
||||
repository, ok := event["repository"].(map[string]any)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, "test-repo", repository["name"])
|
||||
sender, ok := event["sender"].(map[string]any)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, "test-user", sender["login"])
|
||||
organization, ok := event["organization"].(map[string]any)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, "test-org", organization["name"])
|
||||
})
|
||||
|
||||
t.Run("keeps payload when schedule empty", func(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user