From 77ccbeb543f6a810bb88376b735ae6f9d19e60cc Mon Sep 17 00:00:00 2001 From: Sonia Aguilar Date: Wed, 14 Jan 2026 16:36:40 +0100 Subject: [PATCH] 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 --- .../MuteTimingsSelector.test.tsx | 12 ++++++------ .../alertmanager-entities/MuteTimingsSelector.tsx | 5 ++--- .../components/mute-timings/MuteTimingForm.test.tsx | 8 ++++---- .../components/mute-timings/MuteTimingForm.tsx | 8 +++----- .../app/features/alerting/unified/utils/k8s/utils.ts | 4 ++++ 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/public/app/features/alerting/unified/components/alertmanager-entities/MuteTimingsSelector.test.tsx b/public/app/features/alerting/unified/components/alertmanager-entities/MuteTimingsSelector.test.tsx index 5b5cd905e16..2c333d0ce7b 100644 --- a/public/app/features/alerting/unified/components/alertmanager-entities/MuteTimingsSelector.test.tsx +++ b/public/app/features/alerting/unified/components/alertmanager-entities/MuteTimingsSelector.test.tsx @@ -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(); diff --git a/public/app/features/alerting/unified/components/alertmanager-entities/MuteTimingsSelector.tsx b/public/app/features/alerting/unified/components/alertmanager-entities/MuteTimingsSelector.tsx index 19f0afa9d8f..2c49a741424 100644 --- a/public/app/features/alerting/unified/components/alertmanager-entities/MuteTimingsSelector.tsx +++ b/public/app/features/alerting/unified/components/alertmanager-entities/MuteTimingsSelector.tsx @@ -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 => ({ 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 */ diff --git a/public/app/features/alerting/unified/components/mute-timings/MuteTimingForm.test.tsx b/public/app/features/alerting/unified/components/mute-timings/MuteTimingForm.test.tsx index c1e77620238..19bd04c36f9 100644 --- a/public/app/features/alerting/unified/components/mute-timings/MuteTimingForm.test.tsx +++ b/public/app/features/alerting/unified/components/mute-timings/MuteTimingForm.test.tsx @@ -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(); diff --git a/public/app/features/alerting/unified/components/mute-timings/MuteTimingForm.tsx b/public/app/features/alerting/unified/components/mute-timings/MuteTimingForm.tsx index ba978a345f0..208057439ef 100644 --- a/public/app/features/alerting/unified/components/mute-timings/MuteTimingForm.tsx +++ b/public/app/features/alerting/unified/components/mute-timings/MuteTimingForm.tsx @@ -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 ( <> diff --git a/public/app/features/alerting/unified/utils/k8s/utils.ts b/public/app/features/alerting/unified/utils/k8s/utils.ts index 015ba8f17a2..409298bf9bb 100644 --- a/public/app/features/alerting/unified/utils/k8s/utils.ts +++ b/public/app/features/alerting/unified/utils/k8s/utils.ts @@ -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; +}