From 0523eba2a9b3990a1796e4c4e687f8079d83fe74 Mon Sep 17 00:00:00 2001 From: Gilles De Mey Date: Wed, 17 Aug 2022 11:56:54 +0200 Subject: [PATCH] Alerting: Read group details before saving (#53586) --- .../unified/hooks/useCombinedRuleNamespaces.ts | 2 +- .../features/alerting/unified/utils/rulerClient.ts | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/public/app/features/alerting/unified/hooks/useCombinedRuleNamespaces.ts b/public/app/features/alerting/unified/hooks/useCombinedRuleNamespaces.ts index 127569f59de..de6ec286687 100644 --- a/public/app/features/alerting/unified/hooks/useCombinedRuleNamespaces.ts +++ b/public/app/features/alerting/unified/hooks/useCombinedRuleNamespaces.ts @@ -115,7 +115,7 @@ export function sortRulesByName(rules: CombinedRule[]) { return rules.sort((a, b) => a.name.localeCompare(b.name)); } -function addRulerGroupsToCombinedNamespace(namespace: CombinedRuleNamespace, groups: RulerRuleGroupDTO[]): void { +function addRulerGroupsToCombinedNamespace(namespace: CombinedRuleNamespace, groups: RulerRuleGroupDTO[] = []): void { namespace.groups = groups.map((group) => { const combinedGroup: CombinedRuleGroup = { name: group.name, diff --git a/public/app/features/alerting/unified/utils/rulerClient.ts b/public/app/features/alerting/unified/utils/rulerClient.ts index 73a7b296586..c7ed178ead0 100644 --- a/public/app/features/alerting/unified/utils/rulerClient.ts +++ b/public/app/features/alerting/unified/utils/rulerClient.ts @@ -157,16 +157,22 @@ export function getRulerClient(rulerConfig: RulerDataSourceConfig): RulerClient return addRuleToNamespaceAndGroup(namespace, groupSpec, newRule); } - const sameNamespace = existingRule.namespace === namespace; - const sameGroup = existingRule.group.name === values.group; + // we'll fetch the existing group again, someone might have updated it while we were editing a rule + const freshExisting = await findEditableRule(ruleId.fromRuleWithLocation(existingRule)); + if (!freshExisting) { + throw new Error('Rule not found.'); + } + + const sameNamespace = freshExisting.namespace === namespace; + const sameGroup = freshExisting.group.name === values.group; const sameLocation = sameNamespace && sameGroup; if (sameLocation) { // we're update a rule in the same namespace and group - return updateGrafanaRule(existingRule, newRule, evaluateEvery); + return updateGrafanaRule(freshExisting, newRule, evaluateEvery); } else { // we're moving a rule to either a different group or namespace - return moveGrafanaRule(namespace, groupSpec, existingRule, newRule); + return moveGrafanaRule(namespace, groupSpec, freshExisting, newRule); } };