Alerting: Default pending period (for) to 0s when the rule is created using file provisioning (#94646)

Alerting: default for to 0s when the rule is created using file provisioning
This commit is contained in:
Alexander Akhmetov 2024-10-16 10:15:10 +02:00 committed by GitHub
parent 384562e1ad
commit bfbbdf5efb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 6 deletions

View File

@ -97,11 +97,17 @@ func (rule *AlertRuleV1) mapToModel(orgID int64) (models.AlertRule, error) {
return models.AlertRule{}, fmt.Errorf("rule '%s' failed to parse: no UID set", alertRule.Title)
}
alertRule.OrgID = orgID
duration, err := model.ParseDuration(rule.For.Value())
if err != nil {
return models.AlertRule{}, fmt.Errorf("rule '%s' failed to parse: %w", alertRule.Title, err)
duration := model.Duration(0)
if rule.For.Value() != "" {
var err error
duration, err = model.ParseDuration(rule.For.Value())
if err != nil {
return models.AlertRule{}, fmt.Errorf("rule '%s' failed to parse 'for' field: %w", alertRule.Title, err)
}
}
alertRule.For = time.Duration(duration)
dasboardUID := rule.DasboardUID.Value()
dashboardUID := rule.DashboardUID.Value()
alertRule.DashboardUID = withFallback(dashboardUID, dasboardUID) // Use correct spelling over supported typo.

View File

@ -102,11 +102,12 @@ func TestRules(t *testing.T) {
_, err := rule.mapToModel(1)
require.Error(t, err)
})
t.Run("a rule with out a for duration should error", func(t *testing.T) {
t.Run("a rule without a for duration should default to 0s", func(t *testing.T) {
rule := validRuleV1(t)
rule.For = values.StringValue{}
_, err := rule.mapToModel(1)
require.Error(t, err)
ruleMapped, err := rule.mapToModel(1)
require.NoError(t, err)
require.Equal(t, time.Duration(0), ruleMapped.For)
})
t.Run("a rule with an invalid for duration should error", func(t *testing.T) {
rule := validRuleV1(t)