Alerting: Hide DMA options when no manageAlerts datasources exist (#115952)

* hide data source managed options in the more menu in the list view

* Hide type selector in the new alert form when no data source has mangeAlerts enabled
This commit is contained in:
Sonia Aguilar
2026-01-08 13:17:37 +01:00
committed by GitHub
parent bb5bb00e4d
commit 9e8bdee283
4 changed files with 110 additions and 7 deletions
@@ -36,7 +36,11 @@ import {
isExpressionQueryInAlert, isExpressionQueryInAlert,
} from '../../../rule-editor/formProcessing'; } from '../../../rule-editor/formProcessing';
import { RuleFormType, RuleFormValues } from '../../../types/rule-form'; import { RuleFormType, RuleFormValues } from '../../../types/rule-form';
import { GRAFANA_RULES_SOURCE_NAME, getDefaultOrFirstCompatibleDataSource } from '../../../utils/datasource'; import {
GRAFANA_RULES_SOURCE_NAME,
getDefaultOrFirstCompatibleDataSource,
getRulesDataSources,
} from '../../../utils/datasource';
import { PromOrLokiQuery, isPromOrLokiQuery } from '../../../utils/rule-form'; import { PromOrLokiQuery, isPromOrLokiQuery } from '../../../utils/rule-form';
import { import {
isCloudAlertingRuleByType, isCloudAlertingRuleByType,
@@ -417,7 +421,9 @@ export const QueryAndExpressionsStep = ({ editingExistingRule, onDataChange, mod
]); ]);
const { sectionTitle, helpLabel, helpContent, helpLink } = DESCRIPTIONS[type ?? RuleFormType.grafana]; const { sectionTitle, helpLabel, helpContent, helpLink } = DESCRIPTIONS[type ?? RuleFormType.grafana];
// Only show the data source managed option if there are data sources with manageAlerts enabled
const hasAlertEnabledDataSources = useMemo(() => getRulesDataSources().length > 0, []);
const canSelectDataSourceManaged = onlyOneDSInQueries(queries) && hasAlertEnabledDataSources;
if (!type) { if (!type) {
return null; return null;
} }
@@ -437,8 +443,6 @@ export const QueryAndExpressionsStep = ({ editingExistingRule, onDataChange, mod
} }
: undefined; : undefined;
const canSelectDataSourceManaged = onlyOneDSInQueries(queries);
return ( return (
<> <>
<RuleEditorSection <RuleEditorSection
@@ -506,7 +510,7 @@ export const QueryAndExpressionsStep = ({ editingExistingRule, onDataChange, mod
}} }}
/> />
</Field> </Field>
{mode === 'edit' && ( {mode === 'edit' && hasAlertEnabledDataSources && (
<> <>
<Divider /> <Divider />
<SmartAlertTypeDetector <SmartAlertTypeDetector
@@ -194,4 +194,53 @@ describe('RuleEditor grafana managed rules', () => {
]), ]),
}); });
}); });
it('should not show rule type switch when no data sources have manageAlerts enabled', async () => {
// Setup data source with manageAlerts explicitly disabled
setupDataSources(
mockDataSource(
{
type: 'prometheus',
name: 'Prom-disabled',
uid: 'prometheus-disabled',
isDefault: true,
jsonData: { manageAlerts: false },
},
{ alerting: true, module: 'core:plugin/prometheus' }
)
);
renderRuleEditor();
// Wait for the form to load
await screen.findByRole('textbox', { name: 'name' });
// The rule type switch should NOT be visible
expect(screen.queryByText('Rule type')).not.toBeInTheDocument();
expect(screen.queryByTestId('rule-type-radio-group')).not.toBeInTheDocument();
});
it('should show rule type switch when data sources have manageAlerts enabled', async () => {
// Setup data source with manageAlerts enabled
setupDataSources(
mockDataSource(
{
type: 'prometheus',
name: 'Prom-enabled',
uid: 'prometheus-enabled',
isDefault: true,
jsonData: { manageAlerts: true },
},
{ alerting: true, module: 'core:plugin/prometheus' }
)
);
renderRuleEditor();
// Wait for the form to load
await screen.findByRole('textbox', { name: 'name' });
// The rule type section should be visible
expect(await screen.findByText('Rule type')).toBeInTheDocument();
});
}); });
@@ -7,10 +7,11 @@ import { setPluginComponentsHook, setPluginLinksHook } from '@grafana/runtime';
import { AccessControlAction } from 'app/types/accessControl'; import { AccessControlAction } from 'app/types/accessControl';
import { setupMswServer } from '../mockApi'; import { setupMswServer } from '../mockApi';
import { grantUserPermissions, grantUserRole } from '../mocks'; import { grantUserPermissions, grantUserRole, mockDataSource } from '../mocks';
import { setGrafanaRuleGroupExportResolver } from '../mocks/server/configure'; import { setGrafanaRuleGroupExportResolver } from '../mocks/server/configure';
import { alertingFactory } from '../mocks/server/db'; import { alertingFactory } from '../mocks/server/db';
import { RulesFilter } from '../search/rulesSearchParser'; import { RulesFilter } from '../search/rulesSearchParser';
import { setupDataSources } from '../testSetup/datasources';
import RuleListPage, { RuleListActions } from './RuleList.v2'; import RuleListPage, { RuleListActions } from './RuleList.v2';
import { loadDefaultSavedSearch } from './filter/useSavedSearches'; import { loadDefaultSavedSearch } from './filter/useSavedSearches';
@@ -365,6 +366,51 @@ describe('RuleListActions', () => {
expect(ui.exportDrawer.query()).toBeInTheDocument(); expect(ui.exportDrawer.query()).toBeInTheDocument();
}); });
}); });
describe('Data source options visibility', () => {
it('should not show "New Data source recording rule" option when no data sources have manageAlerts enabled', async () => {
// Set up only data sources with manageAlerts explicitly set to false
// This replaces the default data sources that have manageAlerts defaulting to true
setupDataSources(
mockDataSource({
name: 'Prometheus-disabled',
uid: 'prometheus-disabled',
type: 'prometheus',
jsonData: { manageAlerts: false },
})
);
grantUserPermissions([AccessControlAction.AlertingRuleExternalWrite]);
const { user } = render(<RuleListActions />);
await user.click(ui.moreButton.get());
const menu = await ui.moreMenu.find();
expect(ui.menuOptions.newDataSourceRecordingRule.query(menu)).not.toBeInTheDocument();
});
it('should show "New Data source recording rule" option when data sources have manageAlerts enabled', async () => {
// Set up data source with manageAlerts enabled
setupDataSources(
mockDataSource({
name: 'Prometheus-enabled',
uid: 'prometheus-enabled',
type: 'prometheus',
jsonData: { manageAlerts: true },
})
);
grantUserPermissions([AccessControlAction.AlertingRuleExternalWrite]);
const { user } = render(<RuleListActions />);
await user.click(ui.moreButton.get());
const menu = await ui.moreMenu.find();
expect(ui.menuOptions.newDataSourceRecordingRule.query(menu)).toBeInTheDocument();
});
});
}); });
describe('RuleListPage v2 - View switching', () => { describe('RuleListPage v2 - View switching', () => {
@@ -13,6 +13,7 @@ import { useListViewMode } from '../components/rules/Filter/RulesViewModeSelecto
import { AIAlertRuleButtonComponent } from '../enterprise-components/AI/AIGenAlertRuleButton/addAIAlertRuleButton'; import { AIAlertRuleButtonComponent } from '../enterprise-components/AI/AIGenAlertRuleButton/addAIAlertRuleButton';
import { AlertingAction, useAlertingAbility } from '../hooks/useAbilities'; import { AlertingAction, useAlertingAbility } from '../hooks/useAbilities';
import { useRulesFilter } from '../hooks/useFilteredRules'; import { useRulesFilter } from '../hooks/useFilteredRules';
import { getRulesDataSources } from '../utils/datasource';
import { FilterView } from './FilterView'; import { FilterView } from './FilterView';
import { GroupedView } from './GroupedView'; import { GroupedView } from './GroupedView';
@@ -41,8 +42,11 @@ export function RuleListActions() {
const [createCloudRuleSupported, createCloudRuleAllowed] = useAlertingAbility(AlertingAction.CreateExternalAlertRule); const [createCloudRuleSupported, createCloudRuleAllowed] = useAlertingAbility(AlertingAction.CreateExternalAlertRule);
const [exportRulesSupported, exportRulesAllowed] = useAlertingAbility(AlertingAction.ExportGrafanaManagedRules); const [exportRulesSupported, exportRulesAllowed] = useAlertingAbility(AlertingAction.ExportGrafanaManagedRules);
// Check if there are any data sources with manageAlerts enabled
const hasAlertEnabledDataSources = useMemo(() => getRulesDataSources().length > 0, []);
const canCreateGrafanaRules = createGrafanaRuleSupported && createGrafanaRuleAllowed; const canCreateGrafanaRules = createGrafanaRuleSupported && createGrafanaRuleAllowed;
const canCreateCloudRules = createCloudRuleSupported && createCloudRuleAllowed; const canCreateCloudRules = createCloudRuleSupported && createCloudRuleAllowed && hasAlertEnabledDataSources;
const canExportRules = exportRulesSupported && exportRulesAllowed; const canExportRules = exportRulesSupported && exportRulesAllowed;
const canCreateRules = canCreateGrafanaRules || canCreateCloudRules; const canCreateRules = canCreateGrafanaRules || canCreateCloudRules;