[release-12.1.1] Alerting: Fix subpath handling in the alerting package (#109505)

Alerting: Fix subpath handling in the alerting package (#109448)

Take subpath into account when building contact points APIs URL

(cherry picked from commit a0cf529465)
This commit is contained in:
Konrad Lalik 2025-08-12 10:54:20 +02:00 committed by GitHub
parent 3b64a0b431
commit df5de8219b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 71 additions and 2 deletions

View File

@ -0,0 +1,67 @@
import { config } from '@grafana/runtime';
import { getAPIBaseURL, getAPINamespace, getAPIReducerPath } from './util';
describe('API utilities', () => {
const originalAppSubUrl = config.appSubUrl;
const originalNamespace = config.namespace;
afterEach(() => {
// Restore original config after each test
config.appSubUrl = originalAppSubUrl;
config.namespace = originalNamespace;
});
describe('getAPIBaseURL', () => {
const group = 'notifications.alerting.grafana.app';
const version = 'v0alpha1';
it('should generate correct API base URL without subpath', () => {
config.appSubUrl = '';
config.namespace = 'default';
const result = getAPIBaseURL(group, version);
expect(result).toBe('/apis/notifications.alerting.grafana.app/v0alpha1/namespaces/default');
});
it('should generate correct API base URL with subpath', () => {
config.appSubUrl = '/grafana';
config.namespace = 'default';
const result = getAPIBaseURL(group, version);
expect(result).toBe('/grafana/apis/notifications.alerting.grafana.app/v0alpha1/namespaces/default');
});
it('should handle different namespace', () => {
config.appSubUrl = '/grafana';
config.namespace = 'custom-namespace';
const result = getAPIBaseURL(group, version);
expect(result).toBe('/grafana/apis/notifications.alerting.grafana.app/v0alpha1/namespaces/custom-namespace');
});
});
describe('getAPINamespace', () => {
it('should return configured namespace', () => {
config.namespace = 'test-namespace';
const result = getAPINamespace();
expect(result).toBe('test-namespace');
});
});
describe('getAPIReducerPath', () => {
it('should generate correct reducer path', () => {
const group = 'notifications.alerting.grafana.app';
const version = 'v0alpha1';
const result = getAPIReducerPath(group, version);
expect(result).toBe('notifications.alerting.grafana.app/v0alpha1');
});
});
});

View File

@ -6,8 +6,10 @@ import { config } from '@grafana/runtime';
export const getAPINamespace = () => config.namespace;
export const getAPIBaseURL = (group: string, version: string) =>
`/apis/${group}/${version}/namespaces/${getAPINamespace()}` as const;
export const getAPIBaseURL = (group: string, version: string) => {
const subPath = config.appSubUrl || '';
return `${subPath}/apis/${group}/${version}/namespaces/${getAPINamespace()}` as const;
};
// By including the version in the reducer path we can prevent cache bugs when different versions of the API are used for the same entities
export const getAPIReducerPath = (group: string, version: string) => `${group}/${version}` as const;