From 1915d10980a1ac91fef6b3577432b47f7c744892 Mon Sep 17 00:00:00 2001 From: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> Date: Thu, 13 Aug 2020 19:15:34 +0200 Subject: [PATCH] Remove brackets from escaped value if just 1 value (#26995) --- .../app/plugins/datasource/prometheus/datasource.test.ts | 8 ++++++++ public/app/plugins/datasource/prometheus/datasource.ts | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/public/app/plugins/datasource/prometheus/datasource.test.ts b/public/app/plugins/datasource/prometheus/datasource.test.ts index 1b3610663b0..a012a8894d7 100644 --- a/public/app/plugins/datasource/prometheus/datasource.test.ts +++ b/public/app/plugins/datasource/prometheus/datasource.test.ts @@ -502,6 +502,10 @@ describe('PrometheusDatasource', () => { it('should return pipe separated values if the value is an array of strings', () => { expect(ds.interpolateQueryExpr(['a|bc', 'de|f'], customVariable)).toEqual('(a\\\\|bc|de\\\\|f)'); }); + + it('should return 1 regex escaped value if there is just 1 value in an array of strings', () => { + expect(ds.interpolateQueryExpr(['looking*glass'], customVariable)).toEqual('looking\\\\*glass'); + }); }); describe('and variable allows all', () => { @@ -516,6 +520,10 @@ describe('PrometheusDatasource', () => { it('should return pipe separated values if the value is an array of strings', () => { expect(ds.interpolateQueryExpr(['a|bc', 'de|f'], customVariable)).toEqual('(a\\\\|bc|de\\\\|f)'); }); + + it('should return 1 regex escaped value if there is just 1 value in an array of strings', () => { + expect(ds.interpolateQueryExpr(['looking*glass'], customVariable)).toEqual('looking\\\\*glass'); + }); }); }); diff --git a/public/app/plugins/datasource/prometheus/datasource.ts b/public/app/plugins/datasource/prometheus/datasource.ts index 9e2ce91adf0..bbb1eff6a58 100644 --- a/public/app/plugins/datasource/prometheus/datasource.ts +++ b/public/app/plugins/datasource/prometheus/datasource.ts @@ -159,6 +159,11 @@ export class PrometheusDatasource extends DataSourceApi } const escapedValues = value.map(val => prometheusSpecialRegexEscape(val)); + + if (escapedValues.length === 1) { + return escapedValues[0]; + } + return '(' + escapedValues.join('|') + ')'; }