From 24c3c3a0ef36a128ede921afb27eb9f49575f856 Mon Sep 17 00:00:00 2001 From: Sonia Aguilar <33540275+soniaAguilarPeiron@users.noreply.github.com> Date: Thu, 12 Jan 2023 19:52:36 +0100 Subject: [PATCH] Alerting: Use pluginBridge to check if plugin is installed (#61356) * Use getPluginSettings (/api/plugins//settings) to check if plugin is installed * Use usePluginBridge hook instead of getPluginSettings to check if plugin is installed --- .../features/alerting/unified/AmRoutes.test.tsx | 3 +++ .../receivers/grafanaAppReceivers/grafanaApp.ts | 14 +++----------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/public/app/features/alerting/unified/AmRoutes.test.tsx b/public/app/features/alerting/unified/AmRoutes.test.tsx index 01462fdc863..f45cd551dd6 100644 --- a/public/app/features/alerting/unified/AmRoutes.test.tsx +++ b/public/app/features/alerting/unified/AmRoutes.test.tsx @@ -21,6 +21,7 @@ import { AccessControlAction } from 'app/types'; import AmRoutes from './AmRoutes'; import { fetchAlertManagerConfig, fetchStatus, updateAlertManagerConfig } from './api/alertmanager'; import { discoverAlertmanagerFeatures } from './api/buildInfo'; +import * as grafanaApp from './components/receivers/grafanaAppReceivers/grafanaApp'; import { mockDataSource, MockDataSourceSrv, someCloudAlertManagerConfig, someCloudAlertManagerStatus } from './mocks'; import { defaultGroupBy } from './utils/amroutes'; import { getAllDataSources } from './utils/config'; @@ -43,6 +44,7 @@ const mocks = { }, contextSrv: jest.mocked(contextSrv), }; +const useGetGrafanaReceiverTypeCheckerMock = jest.spyOn(grafanaApp, 'useGetGrafanaReceiverTypeChecker'); const renderAmRoutes = (alertManagerSourceName?: string) => { const store = configureStore(); @@ -199,6 +201,7 @@ describe('AmRoutes', () => { mocks.contextSrv.evaluatePermission.mockImplementation(() => []); mocks.api.discoverAlertmanagerFeatures.mockResolvedValue({ lazyConfigInit: false }); setDataSourceSrv(new MockDataSourceSrv(dataSources)); + useGetGrafanaReceiverTypeCheckerMock.mockReturnValue(() => undefined); }); afterEach(() => { diff --git a/public/app/features/alerting/unified/components/receivers/grafanaAppReceivers/grafanaApp.ts b/public/app/features/alerting/unified/components/receivers/grafanaAppReceivers/grafanaApp.ts index 90ffec98190..7ae1a348ced 100644 --- a/public/app/features/alerting/unified/components/receivers/grafanaAppReceivers/grafanaApp.ts +++ b/public/app/features/alerting/unified/components/receivers/grafanaAppReceivers/grafanaApp.ts @@ -1,24 +1,16 @@ -import { useGetSingleLocalWithoutDetails } from 'app/features/plugins/admin/state/hooks'; -import { CatalogPlugin } from 'app/features/plugins/admin/types'; import { Receiver } from 'app/plugins/datasource/alertmanager/types'; import { useGetOnCallIntegrationsQuery } from '../../../api/onCallApi'; +import { SupportedPlugin, usePluginBridge } from '../../PluginBridge'; import { isOnCallReceiver } from './onCall/onCall'; -import { AmRouteReceiver, GrafanaAppReceiverEnum, GRAFANA_APP_PLUGIN_IDS, ReceiverWithTypes } from './types'; - -export const useGetAppIsInstalledAndEnabled = (grafanaAppType: GrafanaAppReceiverEnum) => { - // fetches the plugin settings for this Grafana instance - const plugin: CatalogPlugin | undefined = useGetSingleLocalWithoutDetails(GRAFANA_APP_PLUGIN_IDS[grafanaAppType]); - return plugin?.isInstalled && !plugin?.isDisabled && plugin?.type === 'app'; -}; +import { AmRouteReceiver, GrafanaAppReceiverEnum, ReceiverWithTypes } from './types'; export const useGetGrafanaReceiverTypeChecker = () => { - const isOnCallEnabled = useGetAppIsInstalledAndEnabled(GrafanaAppReceiverEnum.GRAFANA_ONCALL); + const { installed: isOnCallEnabled } = usePluginBridge(SupportedPlugin.OnCall); const { data } = useGetOnCallIntegrationsQuery(undefined, { skip: !isOnCallEnabled, }); - const getGrafanaReceiverType = (receiver: Receiver): GrafanaAppReceiverEnum | undefined => { //CHECK FOR ONCALL PLUGIN const onCallIntegrations = data ?? [];