diff --git a/public/app/features/alerting/unified/components/rules/RulesGroup.tsx b/public/app/features/alerting/unified/components/rules/RulesGroup.tsx index 953d7075908..83f0ebde0fb 100644 --- a/public/app/features/alerting/unified/components/rules/RulesGroup.tsx +++ b/public/app/features/alerting/unified/components/rules/RulesGroup.tsx @@ -13,7 +13,7 @@ import { useRulesAccess } from '../../utils/accessControlHooks'; import { GRAFANA_RULES_SOURCE_NAME, getRulesSourceName, isCloudRulesSource } from '../../utils/datasource'; import { makeFolderLink } from '../../utils/misc'; import { groups } from '../../utils/navigation'; -import { isFederatedRuleGroup, isPluginProvidedRule, rulerRuleType } from '../../utils/rules'; +import { isFederatedRuleGroup, isPluginProvidedRule, isUngroupedRuleGroup, rulerRuleType } from '../../utils/rules'; import { CollapseToggle } from '../CollapseToggle'; import { RuleLocation } from '../RuleLocation'; import { GrafanaRuleFolderExporter } from '../export/GrafanaRuleFolderExporter'; @@ -33,9 +33,6 @@ interface Props { viewMode: ViewMode; } -const NoGroupPrefix = 'no_group_for_rule_'; -const isNoGroup = (group: string) => group.startsWith(NoGroupPrefix); - export const RulesGroup = React.memo(({ group, namespace, expandAll, viewMode }: Props) => { const { rulesSource } = namespace; const rulesSourceName = getRulesSourceName(rulesSource); @@ -170,7 +167,7 @@ export const RulesGroup = React.memo(({ group, namespace, expandAll, viewMode }: let groupName = ; if (isListView) { groupName = ; - } else if (isNoGroup(group.name)) { + } else if (isUngroupedRuleGroup(group.name)) { groupName = ( ); diff --git a/public/app/features/alerting/unified/utils/rules.test.ts b/public/app/features/alerting/unified/utils/rules.test.ts index 63c0cb62f04..33d26e2b707 100644 --- a/public/app/features/alerting/unified/utils/rules.test.ts +++ b/public/app/features/alerting/unified/utils/rules.test.ts @@ -14,9 +14,11 @@ import { import { GRAFANA_ORIGIN_LABEL } from './labels'; import { + NO_GROUP_PREFIX, getRuleGroupLocationFromCombinedRule, getRuleGroupLocationFromRuleWithLocation, getRulePluginOrigin, + isUngroupedRuleGroup, } from './rules'; describe('getRuleOrigin', () => { @@ -123,3 +125,22 @@ describe('ruleGroupLocation', () => { }); }); }); + +describe('isUngroupedRuleGroup', () => { + it('should return true for group names starting with NO_GROUP_PREFIX', () => { + expect(isUngroupedRuleGroup('no_group_for_rule_abc123')).toBe(true); + expect(isUngroupedRuleGroup('no_group_for_rule_')).toBe(true); + expect(isUngroupedRuleGroup('no_group_for_rule_test-rule-uid')).toBe(true); + }); + + it('should return false for group names not starting with NO_GROUP_PREFIX', () => { + expect(isUngroupedRuleGroup('MyGroup')).toBe(false); + expect(isUngroupedRuleGroup('group-1')).toBe(false); + expect(isUngroupedRuleGroup('')).toBe(false); + }); + + it('should return false for group names that contain but do not start with NO_GROUP_PREFIX', () => { + expect(isUngroupedRuleGroup('prefix_no_group_for_rule_abc123')).toBe(false); + expect(isUngroupedRuleGroup('MyGroup_no_group_for_rule_')).toBe(false); + }); +}); diff --git a/public/app/features/alerting/unified/utils/rules.ts b/public/app/features/alerting/unified/utils/rules.ts index e3d35972be5..efd85a0b85e 100644 --- a/public/app/features/alerting/unified/utils/rules.ts +++ b/public/app/features/alerting/unified/utils/rules.ts @@ -563,3 +563,6 @@ export function getRuleUID(rule?: RulerRuleDTO | Rule) { return ruleUid; } + +export const NO_GROUP_PREFIX = 'no_group_for_rule_'; +export const isUngroupedRuleGroup = (group: string): boolean => group.startsWith(NO_GROUP_PREFIX);