Alerting: Fix saving advanced mode toggle state in the alert rule editor (#95924)

This commit is contained in:
Alexander Akhmetov 2024-11-06 18:39:15 +01:00 committed by GitHub
parent 70c21a2e4d
commit 4ce1abc6f9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 32 additions and 5 deletions

View File

@ -313,13 +313,16 @@ func ValidateRuleGroup(
uids[rule.UID] = idx
}
var hasPause, isPaused bool
var hasPause, isPaused, hasMetadata bool
original := ruleGroupConfig.Rules[idx]
if alert := original.GrafanaManagedAlert; alert != nil {
if alert.IsPaused != nil {
isPaused = *alert.IsPaused
hasPause = true
}
if alert.Metadata != nil {
hasMetadata = true
}
}
ruleWithOptionals := ngmodels.AlertRuleWithOptionals{}
@ -327,6 +330,7 @@ func ValidateRuleGroup(
rule.RuleGroupIndex = idx + 1
ruleWithOptionals.AlertRule = *rule
ruleWithOptionals.HasPause = hasPause
ruleWithOptionals.HasMetadata = hasMetadata
result = append(result, &ruleWithOptionals)
}

View File

@ -299,7 +299,8 @@ type AlertRuleWithOptionals struct {
AlertRule
// This parameter is to know if an optional API field was sent and, therefore, patch it with the current field from
// DB in case it was not sent.
HasPause bool
HasPause bool
HasMetadata bool
}
// AlertsRulesBy is a function that defines the ordering of alert rules.
@ -809,7 +810,7 @@ func PatchPartialAlertRule(existingRule *AlertRule, ruleToPatch *AlertRuleWithOp
// Currently metadata contains only editor settings, so we can just copy it.
// If we add more fields to metadata, we might need to handle them separately,
// and/or merge or update their values.
if ruleToPatch.Metadata == (AlertRuleMetadata{}) {
if !ruleToPatch.HasMetadata {
ruleToPatch.Metadata = existingRule.Metadata
}
}

View File

@ -257,6 +257,7 @@ func TestPatchPartialAlertRule(t *testing.T) {
name: "No metadata",
mutator: func(r *AlertRuleWithOptionals) {
r.Metadata = AlertRuleMetadata{}
r.HasMetadata = false
},
},
}

View File

@ -895,7 +895,28 @@ func TestIntegrationAlertRuleEditorSettings(t *testing.T) {
updatedRuleGroup := apiClient.GetRulesGroup(t, folderName, groupName).GettableRuleGroupConfig
require.Len(t, updatedRuleGroup.Rules, 1)
require.False(t, false, updatedRuleGroup.Rules[0].GrafanaManagedAlert.Metadata.EditorSettings.SimplifiedQueryAndExpressionsSection)
require.True(t, updatedRuleGroup.Rules[0].GrafanaManagedAlert.Metadata.EditorSettings.SimplifiedQueryAndExpressionsSection)
})
t.Run("disable simplified query editor in editor settings", func(t *testing.T) {
metadata := &apimodels.AlertRuleMetadata{
EditorSettings: apimodels.AlertRuleEditorSettings{
SimplifiedQueryAndExpressionsSection: true,
},
}
createdRuleGroup := createAlertInGrafana(metadata)
rulesWithUID := convertGettableRuleGroupToPostable(createdRuleGroup)
// disabling the editor
rulesWithUID.Rules[0].GrafanaManagedAlert.Metadata.EditorSettings.SimplifiedQueryAndExpressionsSection = false
_, status, _ := apiClient.PostRulesGroupWithStatus(t, folderName, &rulesWithUID)
assert.Equal(t, http.StatusAccepted, status)
updatedRuleGroup := apiClient.GetRulesGroup(t, folderName, groupName).GettableRuleGroupConfig
require.Len(t, updatedRuleGroup.Rules, 1)
require.False(t, updatedRuleGroup.Rules[0].GrafanaManagedAlert.Metadata.EditorSettings.SimplifiedQueryAndExpressionsSection)
})
t.Run("post alert without metadata", func(t *testing.T) {
@ -903,7 +924,7 @@ func TestIntegrationAlertRuleEditorSettings(t *testing.T) {
createdRuleGroup := apiClient.GetRulesGroup(t, folderName, groupName).GettableRuleGroupConfig
require.Len(t, createdRuleGroup.Rules, 1)
require.False(t, false, createdRuleGroup.Rules[0].GrafanaManagedAlert.Metadata.EditorSettings.SimplifiedQueryAndExpressionsSection)
require.False(t, createdRuleGroup.Rules[0].GrafanaManagedAlert.Metadata.EditorSettings.SimplifiedQueryAndExpressionsSection)
})
}