Extract utility method isUngroupedRuleGroup

This commit is contained in:
rodrigopk
2026-01-06 14:34:30 -05:00
parent 35b43aae84
commit 138000a80d
3 changed files with 26 additions and 5 deletions
@@ -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 = <RuleLocation namespace={decodeGrafanaNamespace(namespace).name} group={group.name} />;
if (isListView) {
groupName = <RuleLocation namespace={decodeGrafanaNamespace(namespace).name} />;
} else if (isNoGroup(group.name)) {
} else if (isUngroupedRuleGroup(group.name)) {
groupName = (
<RuleLocation namespace={decodeGrafanaNamespace(namespace).name} group={`${group.rules[0].name} (Ungrouped)`} />
);
@@ -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);
});
});
@@ -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);