Refactor: Use isImportedResource and isProvisionedResource utilities

- Replace manual provenance comparisons with existing utility functions
- Use isImportedResource() instead of comparing with 'prometheus_convert' directly
- Use isProvisionedResource() instead of manual PROVENANCE_NONE checks
- Update tests to use correct provenance value 'converted_prometheus' (from KnownProvenance enum)
- Remove redundant constant definitions
This commit is contained in:
Sonia Aguilar
2026-01-14 16:36:40 +01:00
parent 11835de49f
commit 77ccbeb543
5 changed files with 19 additions and 18 deletions
@@ -52,11 +52,11 @@ describe('MuteTimingsSelector', () => {
expect(screen.getByText('another-regular')).toBeInTheDocument();
});
it('should filter out imported time intervals (provenance: prometheus_convert)', async () => {
it('should filter out imported time intervals (provenance: converted_prometheus)', async () => {
const user = userEvent.setup();
setTimeIntervalsList([
{ name: 'regular-interval', provenance: 'none' },
{ name: 'imported-interval', provenance: 'prometheus_convert' },
{ name: 'imported-interval', provenance: 'converted_prometheus' },
{ name: 'file-provisioned', provenance: 'file' },
]);
@@ -78,9 +78,9 @@ describe('MuteTimingsSelector', () => {
const user = userEvent.setup();
setTimeIntervalsList([
{ name: 'normal-1', provenance: 'none' },
{ name: 'imported-1', provenance: 'prometheus_convert' },
{ name: 'imported-1', provenance: 'converted_prometheus' },
{ name: 'normal-2', provenance: 'none' },
{ name: 'imported-2', provenance: 'prometheus_convert' },
{ name: 'imported-2', provenance: 'converted_prometheus' },
{ name: 'file-1', provenance: 'file' },
]);
@@ -113,8 +113,8 @@ describe('MuteTimingsSelector', () => {
it('should handle list with only imported intervals', async () => {
const user = userEvent.setup();
setTimeIntervalsList([
{ name: 'imported-1', provenance: 'prometheus_convert' },
{ name: 'imported-2', provenance: 'prometheus_convert' },
{ name: 'imported-1', provenance: 'converted_prometheus' },
{ name: 'imported-2', provenance: 'converted_prometheus' },
]);
renderWithProvider();
@@ -5,8 +5,7 @@ import { MuteTiming, useMuteTimings } from 'app/features/alerting/unified/compon
import { BaseAlertmanagerArgs } from 'app/features/alerting/unified/types/hooks';
import { timeIntervalToString } from 'app/features/alerting/unified/utils/alertmanager';
import { K8sAnnotations } from 'app/features/alerting/unified/utils/k8s/constants';
const PROMETHEUS_CONVERT_PROVENANCE = 'prometheus_convert';
import { isImportedResource } from 'app/features/alerting/unified/utils/k8s/utils';
const mapTimeInterval = ({ name, time_intervals }: MuteTiming): SelectableValue<string> => ({
value: name,
@@ -17,7 +16,7 @@ const mapTimeInterval = ({ name, time_intervals }: MuteTiming): SelectableValue<
/** Check if a time interval was imported from an external Alertmanager */
const isImportedTimeInterval = (timing: MuteTiming): boolean => {
const provenance = timing.metadata?.annotations?.[K8sAnnotations.Provenance];
return provenance === PROMETHEUS_CONVERT_PROVENANCE;
return isImportedResource(provenance);
};
/** Provides a MultiSelect with available time intervals for the given alertmanager */
@@ -47,8 +47,8 @@ describe('MuteTimingForm', () => {
expect(screen.queryByText(/provisioned/i)).not.toBeInTheDocument();
});
it('should show imported alert when provenance is prometheus_convert', async () => {
renderWithProvider('prometheus_convert');
it('should show imported alert when provenance is converted_prometheus', async () => {
renderWithProvider('converted_prometheus');
expect(
await screen.findByText(/This time interval was imported and cannot be edited through the UI/i)
@@ -73,8 +73,8 @@ describe('MuteTimingForm', () => {
expect(await screen.findByText(/This time interval cannot be edited through the UI/i)).toBeInTheDocument();
});
it('should disable form when provenance is prometheus_convert', async () => {
renderWithProvider('prometheus_convert', true);
it('should disable form when provenance is converted_prometheus', async () => {
renderWithProvider('converted_prometheus', true);
const nameInput = await screen.findByTestId('mute-timing-name');
expect(nameInput).toBeDisabled();
@@ -14,15 +14,13 @@ import {
import { useAlertmanager } from '../../state/AlertmanagerContext';
import { MuteTimingFields } from '../../types/mute-timing-form';
import { PROVENANCE_NONE } from '../../utils/k8s/constants';
import { isImportedResource, isProvisionedResource } from '../../utils/k8s/utils';
import { makeAMLink } from '../../utils/misc';
import { createMuteTiming, defaultTimeInterval, isTimeIntervalDisabled } from '../../utils/mute-timings';
import { ImportedTimeIntervalAlert, ProvisionedResource, ProvisioningAlert } from '../Provisioning';
import { MuteTimingTimeInterval } from './MuteTimingTimeInterval';
const PROMETHEUS_CONVERT_PROVENANCE = 'prometheus_convert';
interface Props {
muteTiming?: MuteTiming;
showError?: boolean;
@@ -108,8 +106,8 @@ const MuteTimingForm = ({ muteTiming, showError, loading, provenance, editMode }
);
}
const isProvisioned = Boolean(provenance && provenance !== PROVENANCE_NONE);
const isImported = provenance === PROMETHEUS_CONVERT_PROVENANCE;
const isProvisioned = isProvisionedResource(provenance);
const isImported = isImportedResource(provenance);
return (
<>
@@ -65,3 +65,7 @@ export const stringifyFieldSelector = (fieldSelectors: FieldSelector[]): string
export function isProvisionedResource(provenance?: string): boolean {
return Boolean(provenance && provenance !== KnownProvenance.None);
}
export function isImportedResource(provenance?: string): boolean {
return provenance === KnownProvenance.ConvertedPrometheus;
}