[v9.1.x] Alerting: Read group details before saving (#53809)

Co-authored-by: Gilles De Mey <gilles.de.mey@gmail.com>
This commit is contained in:
Grot (@grafanabot)
2022-08-17 13:26:14 +02:00
committed by GitHub
co-authored by Gilles De Mey
parent 5116f740ab
commit 5242c26b3b
2 changed files with 11 additions and 5 deletions
@@ -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,
@@ -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);
}
};