Merge pull request #3010 from chengzhichao-xydt/codex/channel-hash-type-assertions
fix(channels): add ok checks for type assertions in toChannelHashes
This commit is contained in:
commit
8e7e910f67
2 changed files with 64 additions and 2 deletions
|
|
@ -17,7 +17,7 @@ func toChannelHashes(cfg *config.Config) map[string]string {
|
||||||
_ = json.Unmarshal(marshal, &channelConfig)
|
_ = json.Unmarshal(marshal, &channelConfig)
|
||||||
|
|
||||||
for key, value := range channelConfig {
|
for key, value := range channelConfig {
|
||||||
if !value["enabled"].(bool) {
|
if enabled, ok := value["enabled"].(bool); !ok || !enabled {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
hiddenValues(key, value, ch.Get(key))
|
hiddenValues(key, value, ch.Get(key))
|
||||||
|
|
@ -94,7 +94,15 @@ func hiddenValues(key string, value map[string]any, ch *config.Channel) {
|
||||||
vv := value["webhooks"]
|
vv := value["webhooks"]
|
||||||
webhooks := make(map[string]string)
|
webhooks := make(map[string]string)
|
||||||
if vv != nil {
|
if vv != nil {
|
||||||
webhooks = vv.(map[string]string)
|
if m, ok := vv.(map[string]string); ok {
|
||||||
|
webhooks = m
|
||||||
|
} else if m, ok := vv.(map[string]any); ok {
|
||||||
|
for k, w := range m {
|
||||||
|
if s, ok := w.(string); ok {
|
||||||
|
webhooks[k] = s
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if settings, ok := v.(*config.TeamsWebhookSettings); ok {
|
if settings, ok := v.(*config.TeamsWebhookSettings); ok {
|
||||||
for name, target := range settings.Webhooks {
|
for name, target := range settings.Webhooks {
|
||||||
|
|
|
||||||
|
|
@ -151,3 +151,57 @@ func TestToChannelHashes_RealWorldChannel(t *testing.T) {
|
||||||
assert.Equal(t, 1, len(h))
|
assert.Equal(t, 1, len(h))
|
||||||
assert.Contains(t, h, "telegram")
|
assert.Contains(t, h, "telegram")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestToChannelHashes_MissingEnabledKey(t *testing.T) {
|
||||||
|
cfg := config.DefaultConfig()
|
||||||
|
cfg.Channels["test"] = &config.Channel{
|
||||||
|
Settings: config.RawNode(`{"key":"value"}`),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should not panic — the ok check safely handles the missing/false case
|
||||||
|
assert.NotPanics(t, func() {
|
||||||
|
_ = toChannelHashes(cfg)
|
||||||
|
})
|
||||||
|
h := toChannelHashes(cfg)
|
||||||
|
assert.Equal(t, 0, len(h), "channel with Enabled=false (default) skipped")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToChannelHashes_EnabledNotBool(t *testing.T) {
|
||||||
|
cfg := config.DefaultConfig()
|
||||||
|
cfg.Channels["test"] = &config.Channel{
|
||||||
|
Enabled: false,
|
||||||
|
Settings: config.RawNode(`{"enabled":"yes","boolField":true}`),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should not panic — string "enabled" won't match bool assertion, ok=false
|
||||||
|
assert.NotPanics(t, func() {
|
||||||
|
_ = toChannelHashes(cfg)
|
||||||
|
})
|
||||||
|
h := toChannelHashes(cfg)
|
||||||
|
assert.Equal(t, 0, len(h), "string enabled not treated as true")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestToChannelHashes_TeamsWebhookWithWebhooks(t *testing.T) {
|
||||||
|
cfg := config.DefaultConfig()
|
||||||
|
// teams_webhook with configured webhooks — this is the real-world
|
||||||
|
// scenario where the map type from JSON unmarshal (map[string]any)
|
||||||
|
// would cause a panic on the old unchecked vv.(map[string]string)
|
||||||
|
settings, _ := json.Marshal(map[string]any{
|
||||||
|
"enabled": true,
|
||||||
|
"webhooks": map[string]any{
|
||||||
|
"hook1": "https://example.com/webhook",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
cfg.Channels["teams_webhook"] = &config.Channel{
|
||||||
|
Enabled: true,
|
||||||
|
Type: config.ChannelTeamsWebHook,
|
||||||
|
Settings: config.RawNode(settings),
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.NotPanics(t, func() {
|
||||||
|
_ = toChannelHashes(cfg)
|
||||||
|
})
|
||||||
|
h := toChannelHashes(cfg)
|
||||||
|
assert.Equal(t, 1, len(h))
|
||||||
|
assert.Contains(t, h, "teams_webhook")
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue