Alerting: Fix missing instances and history when Grafana rule is stored in folder with / (#97956)

This commit is contained in:
Gilles De Mey 2024-12-16 13:26:55 +01:00 committed by GitHub
parent ccb2be5962
commit d7e07d9993
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 1 deletions

View File

@ -0,0 +1,27 @@
import { mockFolder } from '../mocks';
import { stringifyFolder } from './useFolder';
describe('with slashes', () => {
it('should correctly stringify a folder', () => {
const folder = mockFolder({ title: 'my/folder' });
expect(stringifyFolder(folder)).toEqual('my\\/folder');
});
it('should correctly stringify a nested folder', () => {
const folder = mockFolder({ title: 'my/folder', parents: [mockFolder({ title: 'parent/slash' })] });
expect(stringifyFolder(folder)).toEqual('parent\\/slash/my\\/folder');
});
});
describe('without slashes', () => {
it('should correctly stringify a folder', () => {
const folder = mockFolder({ title: 'my folder' });
expect(stringifyFolder(folder)).toEqual('my folder');
});
it('should correctly stringify a nested folder', () => {
const folder = mockFolder({ title: 'my folder', parents: [mockFolder({ title: 'my parent' })] });
expect(stringifyFolder(folder)).toEqual('my parent/my folder');
});
});

View File

@ -34,5 +34,11 @@ export function useFolder(uid?: string): ReturnBag {
}
export function stringifyFolder({ title, parents }: FolderDTO) {
return parents && parents?.length ? [...parents.map((p) => p.title), title].join('/') : title;
return parents && parents?.length
? [...parents.map((p) => p.title), title].map(encodeTitle).join('/')
: encodeTitle(title);
}
export function encodeTitle(title: string): string {
return title.replaceAll('/', '\\/');
}