From 4ce1abc6f99484604c6f9b3c680c94a6cce43f33 Mon Sep 17 00:00:00 2001 From: Alexander Akhmetov Date: Wed, 6 Nov 2024 18:39:15 +0100 Subject: [PATCH] Alerting: Fix saving advanced mode toggle state in the alert rule editor (#95924) --- .../ngalert/api/api_ruler_validation.go | 6 ++++- pkg/services/ngalert/models/alert_rule.go | 5 ++-- .../ngalert/models/alert_rule_test.go | 1 + pkg/tests/api/alerting/api_ruler_test.go | 25 +++++++++++++++++-- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/pkg/services/ngalert/api/api_ruler_validation.go b/pkg/services/ngalert/api/api_ruler_validation.go index bd35d0e6375..ae5bd72b840 100644 --- a/pkg/services/ngalert/api/api_ruler_validation.go +++ b/pkg/services/ngalert/api/api_ruler_validation.go @@ -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) } diff --git a/pkg/services/ngalert/models/alert_rule.go b/pkg/services/ngalert/models/alert_rule.go index ee6e7062098..32ca13a33d4 100644 --- a/pkg/services/ngalert/models/alert_rule.go +++ b/pkg/services/ngalert/models/alert_rule.go @@ -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 } } diff --git a/pkg/services/ngalert/models/alert_rule_test.go b/pkg/services/ngalert/models/alert_rule_test.go index 5aa76c513d1..619d50d90df 100644 --- a/pkg/services/ngalert/models/alert_rule_test.go +++ b/pkg/services/ngalert/models/alert_rule_test.go @@ -257,6 +257,7 @@ func TestPatchPartialAlertRule(t *testing.T) { name: "No metadata", mutator: func(r *AlertRuleWithOptionals) { r.Metadata = AlertRuleMetadata{} + r.HasMetadata = false }, }, } diff --git a/pkg/tests/api/alerting/api_ruler_test.go b/pkg/tests/api/alerting/api_ruler_test.go index 9301edeb1c4..f96d6facd43 100644 --- a/pkg/tests/api/alerting/api_ruler_test.go +++ b/pkg/tests/api/alerting/api_ruler_test.go @@ -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) }) }