From 6cb121d4e8aeda3ffbb423872bde2c002a47e24e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ida=20=C5=A0tambuk?= Date: Thu, 12 Jun 2025 12:42:06 +0200 Subject: [PATCH] [release-12.0.2] Dashboard: Support TemplateSrv.containsTemplate in scenes context (#106535) Dashboard: Support TemplateSrv.containsTemplate in scenes context (#104072) (cherry picked from commit 82184686dc5b6f5f4b0844c4235687fe3282df08) Co-authored-by: Haris Rozajac <58232930+harisrozajac@users.noreply.github.com> --- .../features/templating/template_srv.test.ts | 23 +++++++++++++++++++ .../app/features/templating/template_srv.ts | 9 ++++++++ 2 files changed, 32 insertions(+) diff --git a/public/app/features/templating/template_srv.test.ts b/public/app/features/templating/template_srv.test.ts index 58da8d87c04..f8d3f836e5f 100644 --- a/public/app/features/templating/template_srv.test.ts +++ b/public/app/features/templating/template_srv.test.ts @@ -904,6 +904,29 @@ describe('templateSrv', () => { expect(podVar.current.text).toEqual(['podA', 'podB']); }); + it('Can use containsTemplate to check if a variable exists', () => { + window.__grafanaSceneContext = new EmbeddedScene({ + $variables: new SceneVariableSet({ + variables: [ + new QueryVariable({ name: 'server', value: 'serverA', text: 'Server A', query: { refId: 'A' } }), + new QueryVariable({ name: 'pods', value: ['pA', 'pB'], text: ['podA', 'podB'], query: { refId: 'A' } }), + new DataSourceVariable({ name: 'ds', value: 'dsA', text: 'dsA', pluginId: 'prometheus' }), + new CustomVariable({ name: 'custom', value: 'A', text: 'A', query: 'A, B, C' }), + new IntervalVariable({ name: 'interval', value: '1m', intervals: ['1m', '2m'] }), + ], + }), + body: new SceneCanvasText({ text: 'hello' }), + }); + + window.__grafanaSceneContext.activate(); + + expect(_templateSrv.containsTemplate('${server}')).toBe(true); + expect(_templateSrv.containsTemplate('${pods}')).toBe(true); + expect(_templateSrv.containsTemplate('${ds}')).toBe(true); + expect(_templateSrv.containsTemplate('${custom}')).toBe(true); + expect(_templateSrv.containsTemplate('${interval}')).toBe(true); + }); + it('Should return timeRange from scenes context', () => { window.__grafanaSceneContext = new EmbeddedScene({ body: new SceneCanvasText({ text: 'hello' }), diff --git a/public/app/features/templating/template_srv.ts b/public/app/features/templating/template_srv.ts index babc6d2cbd4..9e0a22adb53 100644 --- a/public/app/features/templating/template_srv.ts +++ b/public/app/features/templating/template_srv.ts @@ -200,6 +200,15 @@ export class TemplateSrv implements BaseTemplateSrv { if (!target) { return false; } + + // Scenes compatability + if (window.__grafanaSceneContext && window.__grafanaSceneContext.isActive) { + // We are just checking that this is a valid variable reference, and we are not looking up the variable + this.regex.lastIndex = 0; + const match = this.regex.exec(target); + return !!match; + } + const name = this.getVariableName(target); const variable = name && this.getVariableAtIndex(name); return variable !== null && variable !== undefined;