diff --git a/public/app/features/alerting/unified/api/alertRuleApi.ts b/public/app/features/alerting/unified/api/alertRuleApi.ts index ef657ef857a..c1c4e7272a9 100644 --- a/public/app/features/alerting/unified/api/alertRuleApi.ts +++ b/public/app/features/alerting/unified/api/alertRuleApi.ts @@ -20,7 +20,7 @@ import { ExportFormats } from '../components/export/providers'; import { Folder } from '../types/rule-form'; import { getDatasourceAPIUid, GRAFANA_RULES_SOURCE_NAME, isGrafanaRulesSource } from '../utils/datasource'; import { arrayKeyValuesToObject } from '../utils/labels'; -import { isCloudRuleIdentifier, isPrometheusRuleIdentifier } from '../utils/rules'; +import { isCloudRuleIdentifier, isGrafanaRulerRule, isPrometheusRuleIdentifier } from '../utils/rules'; import { alertingApi, WithNotificationOptions } from './alertingApi'; import { @@ -323,6 +323,9 @@ export const alertRuleApi = alertingApi.injectEndpoints({ type: 'RuleGroup', id: `${namespace}/${payload.name}`, }, + ...payload.rules + .filter((rule) => isGrafanaRulerRule(rule)) + .map((rule) => ({ type: 'GrafanaRulerRule', id: rule.grafana_alert.uid }) as const), ], }), diff --git a/public/app/features/alerting/unified/components/rule-editor/alert-rule-form/AlertRuleForm.tsx b/public/app/features/alerting/unified/components/rule-editor/alert-rule-form/AlertRuleForm.tsx index 30f21ce9017..185406b26ef 100644 --- a/public/app/features/alerting/unified/components/rule-editor/alert-rule-form/AlertRuleForm.tsx +++ b/public/app/features/alerting/unified/components/rule-editor/alert-rule-form/AlertRuleForm.tsx @@ -163,6 +163,7 @@ export const AlertRuleForm = ({ existing, prefill }: Props) => { ? getRuleGroupLocationFromRuleWithLocation(existing) : getRuleGroupLocationFromFormValues(values); + const targetRuleGroupIdentifier = getRuleGroupLocationFromFormValues(values); // @TODO move this to a hook too to make sure the logic here is tested for regressions? if (!existing) { // when creating a new rule, we save the manual routing setting , and editorSettings.simplifiedQueryEditor to the local storage @@ -171,7 +172,6 @@ export const AlertRuleForm = ({ existing, prefill }: Props) => { grafanaTypeRule && trackNewGrafanaAlertRuleFormSavedSuccess(); // new Grafana-managed rule } else { const ruleIdentifier = fromRulerRuleAndRuleGroupIdentifier(ruleGroupIdentifier, existing.rule); - const targetRuleGroupIdentifier = getRuleGroupLocationFromFormValues(values); await updateRuleInRuleGroup.execute( ruleGroupIdentifier, ruleIdentifier, @@ -181,19 +181,21 @@ export const AlertRuleForm = ({ existing, prefill }: Props) => { ); } - const { dataSourceName, namespaceName, groupName } = ruleGroupIdentifier; + const { dataSourceName, namespaceName, groupName } = targetRuleGroupIdentifier; if (exitOnSave) { - const returnToUrl = returnTo || getReturnToUrl(ruleGroupIdentifier, ruleDefinition); + const returnToUrl = returnTo || getReturnToUrl(targetRuleGroupIdentifier, ruleDefinition); locationService.push(returnToUrl); return; - } + } else { + // we stay in the same page - // Cloud Ruler rules identifier changes on update due to containing rule name and hash components - // After successful update we need to update the URL to avoid displaying 404 errors - if (isCloudRulerRule(ruleDefinition)) { - const updatedRuleIdentifier = fromRulerRule(dataSourceName, namespaceName, groupName, ruleDefinition); - locationService.replace(`/alerting/${encodeURIComponent(stringifyIdentifier(updatedRuleIdentifier))}/edit`); + // Cloud Ruler rules identifier changes on update due to containing rule name and hash components + // After successful update we need to update the URL to avoid displaying 404 errors + if (isCloudRulerRule(ruleDefinition)) { + const updatedRuleIdentifier = fromRulerRule(dataSourceName, namespaceName, groupName, ruleDefinition); + locationService.replace(`/alerting/${encodeURIComponent(stringifyIdentifier(updatedRuleIdentifier))}/edit`); + } } };