diff --git a/public/app/features/alerting/unified/api/alertmanagerApi.ts b/public/app/features/alerting/unified/api/alertmanagerApi.ts index c7ede90eea5..a6d1a718d83 100644 --- a/public/app/features/alerting/unified/api/alertmanagerApi.ts +++ b/public/app/features/alerting/unified/api/alertmanagerApi.ts @@ -1,5 +1,6 @@ import { isEmpty } from 'lodash'; +import { encodeMatcher } from 'app/features/alerting/unified/utils/matchers'; import { dispatch } from 'app/store/store'; import { ReceiversStateDTO } from 'app/types/alerting'; @@ -17,13 +18,13 @@ import { } from '../../../../plugins/datasource/alertmanager/types'; import { NotifierDTO } from '../../../../types'; import { withPerformanceLogging } from '../Analytics'; -import { matcherToOperator } from '../utils/alertmanager'; +import { matcherToMatcherField } from '../utils/alertmanager'; import { GRAFANA_RULES_SOURCE_NAME, getDatasourceAPIUid, isVanillaPrometheusAlertManagerDataSource, } from '../utils/datasource'; -import { retryWhile, wrapWithQuotes } from '../utils/misc'; +import { retryWhile } from '../utils/misc'; import { messageFromError, withSerializedError } from '../utils/redux'; import { alertingApi } from './alertingApi'; @@ -72,9 +73,9 @@ export const alertmanagerApi = alertingApi.injectEndpoints({ // TODO Add support for active, silenced, inhibited, unprocessed filters const filterMatchers = filter?.matchers ?.filter((matcher) => matcher.name && matcher.value) - .map( - (matcher) => `${wrapWithQuotes(matcher.name)}${matcherToOperator(matcher)}${wrapWithQuotes(matcher.value)}` - ); + .map((matcher) => { + return encodeMatcher(matcherToMatcherField(matcher)); + }); const { silenced, inhibited, unprocessed, active } = filter || {}; diff --git a/public/app/features/alerting/unified/utils/misc.test.ts b/public/app/features/alerting/unified/utils/misc.test.ts index af2fc085996..913de7b92b4 100644 --- a/public/app/features/alerting/unified/utils/misc.test.ts +++ b/public/app/features/alerting/unified/utils/misc.test.ts @@ -1,7 +1,5 @@ import { sortAlerts, - wrapWithQuotes, - escapeQuotes, createExploreLink, makeLabelBasedSilenceLink, makeDataSourceLink, @@ -46,24 +44,6 @@ function permute(inputArray: any[]): any[] { }, []); } -describe('wrapWithQuotes', () => { - it('should work as expected', () => { - expect(wrapWithQuotes('"hello, world!"')).toBe('\\"hello, world!\\"'); - expect(wrapWithQuotes('hello, world!')).toBe('"hello, world!"'); - expect(wrapWithQuotes('hello, "world"!')).toBe('"hello, \\"world\\"!"'); - expect(wrapWithQuotes('"hello""')).toBe('\\"hello\\"\\"'); - }); -}); - -describe('escapeQuotes', () => { - it('should escape all quotes', () => { - expect(escapeQuotes('"hello, world!"')).toBe('\\"hello, world!\\"'); - expect(escapeQuotes('hello, world!')).toBe('hello, world!'); - expect(escapeQuotes('hello, "world"!')).toBe('hello, \\"world\\"!'); - expect(escapeQuotes('hello"')).toBe('hello\\"'); - }); -}); - describe('Unified Altering misc', () => { describe('sortAlerts', () => { describe('when using any sortOrder with a list of alert instances', () => { diff --git a/public/app/features/alerting/unified/utils/misc.ts b/public/app/features/alerting/unified/utils/misc.ts index d15871f8fae..965be198308 100644 --- a/public/app/features/alerting/unified/utils/misc.ts +++ b/public/app/features/alerting/unified/utils/misc.ts @@ -111,13 +111,6 @@ export function makeAMLink(path: string, alertManagerName?: string, options?: UR return `${path}?${search.toString()}`; } -export const escapeQuotes = (input: string) => input.replace(/\"/g, '\\"'); - -export function wrapWithQuotes(input: string) { - const alreadyWrapped = input.startsWith('"') && input.endsWith('"'); - return alreadyWrapped ? escapeQuotes(input) : `"${escapeQuotes(input)}"`; -} - export function makeLabelBasedSilenceLink(alertManagerSourceName: string, labels: Labels) { const silenceUrlParams = new URLSearchParams(); silenceUrlParams.append('alertmanager', alertManagerSourceName);