Plugins: Extend panel menu with commands from plugins (#63802)

* feat(plugins): introduce dashboard panel menu placement for adding menu items

* test: add test for getPanelMenu()

* added an unique identifier for each extension.

* added context to getPluginExtensions.

* wip

* Wip

* wiwip

* Wip

* feat: WWWIIIIPPPP 🧨

* Wip

* Renamed some of the types to align a bit better.

* added limit to how many extensions a plugin can register per placement.

* decreased number of items to 2

* will trim the lenght of titles to max 25 chars.

* wrapping configure function with error handling.

* added error handling for all scenarios.

* moved extension menu items to the bottom of the more sub menu.

* added tests for configuring the title.

* minor refactorings.

* changed so you need to specify the full path in package.json.

* wip

* removed unused type.

* big refactor to make things simpler and to centralize all configure error/validation handling.

* added missing import.

* fixed failing tests.

* fixed tests.

* revert(extensions): remove static extensions config in favour of registering via AppPlugin APIs

* removed the compose that didn't work for some reason.

* added tests just to verify that validation and error handling is tied together in configuration function.

* adding some more values to the context.

* draft validation.

* added missing tests for getPanelMenu.

* added more tests.

* refactor(extensions): move logic for validating extension link config to function

* Fixed ts errors.

* Started to add structure for supporting commands.

* fixed tests.

* adding commands to the registry

* tests: group test cases in describe blocks

* tests: add a little bit more refactoring to the tests

* tests: add a test case for checking correct placements

* feat: first version of the command handler

* feat: register panel menu items with commands

* refactor: make the 'configure' function not optional on `PluginExtensionRegistryItem`

* Wip

* Wip

* Wip

* added test to verify the default configure function.

* added some more tests to verify that commands have the proper error handling for its configure function.

* tests: fix TS errors in tests

* tests: add auxiliary functions

* refactor: small refactoring in tests

* refactor: refactoring tests for registryFactory

* refactor: refactoring tests for registryFactory

* refactor: refactoring tests for registryFactory

* refactor: refactoring tests for registryFactory

* refactor: refactoring tests for registryFactory

* refactor: refactoring tests for registryFactory

* refactor: refactoring tests for registryFactory

* refactor: refactoring tests for registryFactory

* draft of wrapping command handler with error handling.

* refactor: refactoring tests for registryFactory

* added test for edge case.

* replaced the registry item with a configure function.

* renamed the configure function type.

* refactoring of the registryfactory.

* added tests for handler error handling.

* fixed issue with assert function.

* added comment about the limited type.

* Update public/app/features/plugins/extensions/errorHandling.test.ts

Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com>

* Update public/app/features/plugins/extensions/errorHandling.test.ts

Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com>

* Update public/app/features/plugins/extensions/errorHandling.test.ts

Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com>

* added missing tests.

---------

Co-authored-by: Jack Westbrook <jack.westbrook@gmail.com>
Co-authored-by: Levente Balogh <balogh.levente.hu@gmail.com>
This commit is contained in:
Marcus Andersson
2023-03-08 14:23:29 +01:00
committed by GitHub
parent 7aca818aae
commit b63c56903d
18 changed files with 855 additions and 484 deletions

View File

@@ -1,79 +1,122 @@
import { AppConfigureExtension, AppPluginExtensionLink } from '@grafana/data';
import { AppPluginExtensionLink } from '@grafana/data';
import { createErrorHandling } from './errorHandling';
import { handleErrorsInConfigure, handleErrorsInHandler } from './errorHandling';
import type { CommandHandlerFunc, ConfigureFunc } from './types';
describe('extension error handling', () => {
const pluginId = 'grafana-basic-app';
const errorHandler = createErrorHandling<AppPluginExtensionLink>({
pluginId: pluginId,
title: 'Go to page one',
logger: jest.fn(),
});
describe('error handling for extensions', () => {
describe('error handling for configure', () => {
const pluginId = 'grafana-basic-app';
const errorHandler = handleErrorsInConfigure<AppPluginExtensionLink>({
pluginId: pluginId,
title: 'Go to page one',
logger: jest.fn(),
});
const context = {};
const extension: AppPluginExtensionLink = {
title: 'Go to page one',
description: 'Will navigate the user to page one',
path: `/a/${pluginId}/one`,
};
const context = {};
const extension: AppPluginExtensionLink = {
title: 'Go to page one',
description: 'Will navigate the user to page one',
path: `/a/${pluginId}/one`,
};
it('should return configured link if configure is successful', () => {
const configureWithErrorHandling = errorHandler(() => {
return {
it('should return configured link if configure is successful', () => {
const configureWithErrorHandling = errorHandler(() => {
return {
title: 'This is a new title',
};
});
const configured = configureWithErrorHandling(extension, context);
expect(configured).toEqual({
title: 'This is a new title',
};
});
});
const configured = configureWithErrorHandling(extension, context);
it('should return undefined if configure throws error', () => {
const configureWithErrorHandling = errorHandler(() => {
throw new Error();
});
expect(configured).toEqual({
title: 'This is a new title',
const configured = configureWithErrorHandling(extension, context);
expect(configured).toBeUndefined();
});
it('should return undefined if configure is promise/async-based', () => {
const promisebased = (async () => {}) as ConfigureFunc<AppPluginExtensionLink>;
const configureWithErrorHandling = errorHandler(promisebased);
const configured = configureWithErrorHandling(extension, context);
expect(configured).toBeUndefined();
});
it('should return undefined if configure is not a function', () => {
const objectbased = {} as ConfigureFunc<AppPluginExtensionLink>;
const configureWithErrorHandling = errorHandler(objectbased);
const configured = configureWithErrorHandling(extension, context);
expect(configured).toBeUndefined();
});
it('should return undefined if configure returns other than an object', () => {
const returnString = (() => '') as ConfigureFunc<AppPluginExtensionLink>;
const configureWithErrorHandling = errorHandler(returnString);
const configured = configureWithErrorHandling(extension, context);
expect(configured).toBeUndefined();
});
it('should return undefined if configure returns undefined', () => {
const returnUndefined = () => undefined;
const configureWithErrorHandling = errorHandler(returnUndefined);
const configured = configureWithErrorHandling(extension, context);
expect(configured).toBeUndefined();
});
});
it('should return undefined if configure throws error', () => {
const configureWithErrorHandling = errorHandler(() => {
throw new Error();
describe('error handling for command handler', () => {
const pluginId = 'grafana-basic-app';
const errorHandler = handleErrorsInHandler({
pluginId: pluginId,
title: 'open modal',
logger: jest.fn(),
});
const configured = configureWithErrorHandling(extension, context);
it('should be called successfully when handler is a normal synchronous function', () => {
const handler = jest.fn();
const handlerWithErrorHandling = errorHandler(handler);
expect(configured).toBeUndefined();
});
handlerWithErrorHandling();
it('should return undefined if configure is promise/async-based', () => {
const promisebased = (async () => {}) as AppConfigureExtension<AppPluginExtensionLink>;
const configureWithErrorHandling = errorHandler(promisebased);
expect(handler).toBeCalled();
});
const configured = configureWithErrorHandling(extension, context);
it('should not error out even if the handler throws an error', () => {
const handlerWithErrorHandling = errorHandler(() => {
throw new Error();
});
expect(configured).toBeUndefined();
});
expect(handlerWithErrorHandling).not.toThrowError();
});
it('should return undefined if configure is not a function', () => {
const objectbased = {} as AppConfigureExtension<AppPluginExtensionLink>;
const configureWithErrorHandling = errorHandler(objectbased);
it('should be called successfully when handler is an async function / promise', () => {
const promisebased = (async () => {}) as CommandHandlerFunc;
const configureWithErrorHandling = errorHandler(promisebased);
const configured = configureWithErrorHandling(extension, context);
expect(configureWithErrorHandling).not.toThrowError();
});
expect(configured).toBeUndefined();
});
it('should be called successfully when handler is not a function', () => {
const objectbased = {} as CommandHandlerFunc;
const configureWithErrorHandling = errorHandler(objectbased);
it('should return undefined if configure returns other than an object', () => {
const returnString = (() => '') as AppConfigureExtension<AppPluginExtensionLink>;
const configureWithErrorHandling = errorHandler(returnString);
const configured = configureWithErrorHandling(extension, context);
expect(configured).toBeUndefined();
});
it('should return undefined if configure returns undefined', () => {
const returnUndefined = () => undefined;
const configureWithErrorHandling = errorHandler(returnUndefined);
const configured = configureWithErrorHandling(extension, context);
expect(configured).toBeUndefined();
expect(configureWithErrorHandling).not.toThrowError();
});
});
});

View File

@@ -1,6 +1,6 @@
import { isFunction, isObject } from 'lodash';
import type { AppConfigureExtension } from '@grafana/data';
import type { CommandHandlerFunc, ConfigureFunc } from './types';
type Options = {
pluginId: string;
@@ -8,10 +8,10 @@ type Options = {
logger: (msg: string, error?: unknown) => void;
};
export function createErrorHandling<T>(options: Options) {
export function handleErrorsInConfigure<T>(options: Options) {
const { pluginId, title, logger } = options;
return (configure: AppConfigureExtension<T>): AppConfigureExtension<T> => {
return (configure: ConfigureFunc<T>): ConfigureFunc<T> => {
return function handleErrors(extension, context) {
try {
if (!isFunction(configure)) {
@@ -41,3 +41,32 @@ export function createErrorHandling<T>(options: Options) {
};
};
}
export function handleErrorsInHandler(options: Options) {
const { pluginId, title, logger } = options;
return (handler: CommandHandlerFunc): CommandHandlerFunc => {
return function handleErrors(context) {
try {
if (!isFunction(handler)) {
logger(`[Plugins] ${pluginId} provided invalid handler function for command extension '${title}'.`);
return;
}
const result = handler(context);
if (result instanceof Promise) {
logger(
`[Plugins] ${pluginId} provided an unsupported async/promise-based handler function for command extension '${title}'.`
);
result.catch(() => {});
return;
}
return result;
} catch (error) {
logger(`[Plugins] ${pluginId} thow an error while handling command extension '${title}'`, error);
return;
}
};
};
}

View File

@@ -0,0 +1,15 @@
export class PlacementsPerPlugin {
private counter: Record<string, number> = {};
private limit = 2;
allowedToAdd(placement: string): boolean {
const count = this.counter[placement] ?? 0;
if (count >= this.limit) {
return false;
}
this.counter[placement] = count + 1;
return true;
}
}

View File

@@ -1,15 +1,27 @@
import { PluginExtensionTypes } from '@grafana/data';
import {
AppPluginExtensionCommandConfig,
AppPluginExtensionLinkConfig,
assertPluginExtensionCommand,
PluginExtensionTypes,
} from '@grafana/data';
import { PluginExtensionRegistry } from '@grafana/runtime';
import { createPluginExtensionRegistry } from './registryFactory';
const validateLink = jest.fn((configure, extension, context) => configure?.(extension, context));
const errorHandler = jest.fn((configure, extension, context) => configure?.(extension, context));
const configureErrorHandler = jest.fn((configure, extension, context) => configure?.(extension, context));
const commandErrorHandler = jest.fn((configure, context) => configure?.(context));
jest.mock('./errorHandling', () => ({
...jest.requireActual('./errorHandling'),
createErrorHandling: jest.fn(() => {
handleErrorsInConfigure: jest.fn(() => {
return jest.fn((configure) => {
return jest.fn((extension, context) => errorHandler(configure, extension, context));
return jest.fn((extension, context) => configureErrorHandler(configure, extension, context));
});
}),
handleErrorsInHandler: jest.fn(() => {
return jest.fn((configure) => {
return jest.fn((context) => commandErrorHandler(configure, context));
});
}),
}));
@@ -23,304 +35,489 @@ jest.mock('./validateLink', () => ({
}),
}));
describe('Creating extensions registry', () => {
describe('createPluginExtensionRegistry()', () => {
beforeEach(() => {
validateLink.mockClear();
errorHandler.mockClear();
configureErrorHandler.mockClear();
commandErrorHandler.mockClear();
});
it('should register an extension', () => {
const registry = createPluginExtensionRegistry([
{
pluginId: 'belugacdn-app',
linkExtensions: [
{
placement: 'grafana/dashboard/panel/menu',
title: 'Open incident',
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
},
],
},
]);
const numberOfPlacements = Object.keys(registry).length;
const extensions = registry['grafana/dashboard/panel/menu'];
expect(numberOfPlacements).toBe(1);
expect(extensions).toEqual([
{
configure: undefined,
extension: {
title: 'Open incident',
type: PluginExtensionTypes.link,
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
key: -68154691,
},
},
]);
});
it('should register extensions from one plugin with multiple placements', () => {
const registry = createPluginExtensionRegistry([
{
pluginId: 'belugacdn-app',
linkExtensions: [
{
placement: 'grafana/dashboard/panel/menu',
title: 'Open incident',
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
},
{
placement: 'plugins/grafana-slo-app/slo-breached',
title: 'Open incident',
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
},
],
},
]);
const numberOfPlacements = Object.keys(registry).length;
const panelExtensions = registry['grafana/dashboard/panel/menu'];
const sloExtensions = registry['plugins/grafana-slo-app/slo-breached'];
expect(numberOfPlacements).toBe(2);
expect(panelExtensions).toEqual([
{
configure: undefined,
extension: {
title: 'Open incident',
type: PluginExtensionTypes.link,
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
key: -68154691,
},
},
]);
expect(sloExtensions).toEqual([
{
configure: undefined,
extension: {
title: 'Open incident',
type: PluginExtensionTypes.link,
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
key: -1638987831,
},
},
]);
});
it('should register extensions from multiple plugins with multiple placements', () => {
const registry = createPluginExtensionRegistry([
{
pluginId: 'belugacdn-app',
linkExtensions: [
{
placement: 'grafana/dashboard/panel/menu',
title: 'Open incident',
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
},
{
placement: 'plugins/grafana-slo-app/slo-breached',
title: 'Open incident',
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
},
],
},
{
pluginId: 'grafana-monitoring-app',
linkExtensions: [
{
placement: 'grafana/dashboard/panel/menu',
title: 'Open Incident',
description: 'You can create an incident from this context',
path: '/a/grafana-monitoring-app/incidents/declare',
},
],
},
]);
const numberOfPlacements = Object.keys(registry).length;
const panelExtensions = registry['grafana/dashboard/panel/menu'];
const sloExtensions = registry['plugins/grafana-slo-app/slo-breached'];
expect(numberOfPlacements).toBe(2);
expect(panelExtensions).toEqual([
{
configure: undefined,
extension: {
title: 'Open incident',
type: PluginExtensionTypes.link,
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
key: -68154691,
},
},
{
configure: undefined,
extension: {
title: 'Open Incident',
type: PluginExtensionTypes.link,
description: 'You can create an incident from this context',
path: '/a/grafana-monitoring-app/incidents/declare',
key: -540306829,
},
},
]);
expect(sloExtensions).toEqual([
{
configure: undefined,
extension: {
title: 'Open incident',
type: PluginExtensionTypes.link,
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
key: -1638987831,
},
},
]);
});
it('should register maximum 2 extensions per plugin and placement', () => {
const registry = createPluginExtensionRegistry([
{
pluginId: 'belugacdn-app',
linkExtensions: [
{
placement: 'grafana/dashboard/panel/menu',
title: 'Open incident',
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
},
{
placement: 'grafana/dashboard/panel/menu',
title: 'Open incident 2',
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
},
{
placement: 'grafana/dashboard/panel/menu',
title: 'Open incident 3',
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
},
],
},
]);
const numberOfPlacements = Object.keys(registry).length;
const panelExtensions = registry['grafana/dashboard/panel/menu'];
expect(numberOfPlacements).toBe(1);
expect(panelExtensions).toEqual([
{
configure: undefined,
extension: {
title: 'Open incident',
type: PluginExtensionTypes.link,
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
key: -68154691,
},
},
{
configure: undefined,
extension: {
title: 'Open incident 2',
type: PluginExtensionTypes.link,
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
key: -1072147569,
},
},
]);
});
it('should not register extensions with invalid path configured', () => {
const registry = createPluginExtensionRegistry([
{
pluginId: 'belugacdn-app',
linkExtensions: [
{
placement: 'grafana/dashboard/panel/menu',
title: 'Open incident',
description: 'You can create an incident from this context',
path: '/incidents/declare',
},
],
},
]);
const numberOfPlacements = Object.keys(registry).length;
expect(numberOfPlacements).toBe(0);
});
it('should wrap configure function with link extension validator', () => {
const registry = createPluginExtensionRegistry([
{
pluginId: 'belugacdn-app',
linkExtensions: [
{
placement: 'grafana/dashboard/panel/menu',
title: 'Open incident',
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
configure: () => ({}),
},
],
},
]);
const extensions = registry['grafana/dashboard/panel/menu'];
const [extension] = extensions;
const context = {};
const configurable = {
describe('when registering links', () => {
const placement1 = 'grafana/dashboard/panel/menu';
const placement2 = 'plugins/grafana-slo-app/slo-breached';
const pluginId = 'belugacdn-app';
// Sample link configurations that can be used in tests
const linkConfig = {
placement: placement1,
title: 'Open incident',
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
};
extension?.configure?.(context);
it('should register a link extension', () => {
const registry = createPluginExtensionRegistry([
{
pluginId,
linkExtensions: [linkConfig],
commandExtensions: [],
},
]);
expect(validateLink).toBeCalledWith(expect.any(Function), configurable, context);
shouldHaveExtensionsAtPlacement({ configs: [linkConfig], placement: placement1, registry });
});
it('should only register a link extension to a single placement', () => {
const registry = createPluginExtensionRegistry([
{
pluginId,
linkExtensions: [linkConfig],
commandExtensions: [],
},
]);
shouldHaveNumberOfPlacements(registry, 1);
expect(registry[placement1]).toBeDefined();
});
it('should register link extensions from one plugin with multiple placements', () => {
const registry = createPluginExtensionRegistry([
{
pluginId,
linkExtensions: [
{ ...linkConfig, placement: placement1 },
{ ...linkConfig, placement: placement2 },
],
commandExtensions: [],
},
]);
shouldHaveNumberOfPlacements(registry, 2);
shouldHaveExtensionsAtPlacement({ placement: placement1, configs: [linkConfig], registry });
shouldHaveExtensionsAtPlacement({ placement: placement2, configs: [linkConfig], registry });
});
it('should register link extensions from multiple plugins with multiple placements', () => {
const registry = createPluginExtensionRegistry([
{
pluginId,
linkExtensions: [
{ ...linkConfig, placement: placement1 },
{ ...linkConfig, placement: placement2 },
],
commandExtensions: [],
},
{
pluginId: 'grafana-monitoring-app',
linkExtensions: [
{ ...linkConfig, placement: placement1, path: '/a/grafana-monitoring-app/incidents/declare' },
],
commandExtensions: [],
},
]);
shouldHaveNumberOfPlacements(registry, 2);
shouldHaveExtensionsAtPlacement({
placement: placement1,
configs: [linkConfig, { ...linkConfig, path: '/a/grafana-monitoring-app/incidents/declare' }],
registry,
});
shouldHaveExtensionsAtPlacement({ placement: placement2, configs: [linkConfig], registry });
});
it('should register maximum 2 extensions per plugin and placement', () => {
const registry = createPluginExtensionRegistry([
{
pluginId,
linkExtensions: [
{ ...linkConfig, title: 'Link 1' },
{ ...linkConfig, title: 'Link 2' },
{ ...linkConfig, title: 'Link 3' },
],
commandExtensions: [],
},
]);
shouldHaveNumberOfPlacements(registry, 1);
// The 3rd link is being ignored
shouldHaveExtensionsAtPlacement({
placement: linkConfig.placement,
configs: [
{ ...linkConfig, title: 'Link 1' },
{ ...linkConfig, title: 'Link 2' },
],
registry,
});
});
it('should not register link extensions with invalid path configured', () => {
const registry = createPluginExtensionRegistry([
{
pluginId,
linkExtensions: [
{
...linkConfig,
path: '/incidents/declare', // invalid path, should always be prefixed with the plugin id
},
],
commandExtensions: [],
},
]);
shouldHaveNumberOfPlacements(registry, 0);
});
it('should add default configure function when none provided via extension config', () => {
const registry = createPluginExtensionRegistry([
{
pluginId,
linkExtensions: [linkConfig],
commandExtensions: [],
},
]);
const [configure] = registry[linkConfig.placement];
const configured = configure();
// The default configure() function returns the same extension config
expect(configured).toEqual({
key: expect.any(Number),
type: PluginExtensionTypes.link,
title: linkConfig.title,
description: linkConfig.description,
path: linkConfig.path,
});
});
it('should wrap the configure function with link extension validator', () => {
const registry = createPluginExtensionRegistry([
{
pluginId,
linkExtensions: [
{
...linkConfig,
configure: () => ({}),
},
],
commandExtensions: [],
},
]);
const [configure] = registry[linkConfig.placement];
const context = {};
const configurable = {
title: linkConfig.title,
description: linkConfig.description,
path: linkConfig.path,
};
configure(context);
expect(validateLink).toBeCalledWith(expect.any(Function), configurable, context);
});
it('should wrap configure function with extension error handling', () => {
const registry = createPluginExtensionRegistry([
{
pluginId,
linkExtensions: [
{
...linkConfig,
configure: () => ({}),
},
],
commandExtensions: [],
},
]);
const [configure] = registry[linkConfig.placement];
const context = {};
const configurable = {
title: linkConfig.title,
description: linkConfig.description,
path: linkConfig.path,
};
configure(context);
expect(configureErrorHandler).toBeCalledWith(expect.any(Function), configurable, context);
});
it('should return undefined if returned by the provided extension config', () => {
const registry = createPluginExtensionRegistry([
{
pluginId,
linkExtensions: [
{
...linkConfig,
configure: () => undefined,
},
],
commandExtensions: [],
},
]);
const [configure] = registry[linkConfig.placement];
const context = {};
expect(configure(context)).toBeUndefined();
});
});
it('should wrap configure function with extension error handling', () => {
const registry = createPluginExtensionRegistry([
{
pluginId: 'belugacdn-app',
linkExtensions: [
{
placement: 'grafana/dashboard/panel/menu',
title: 'Open incident',
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
configure: () => ({}),
},
],
},
]);
const extensions = registry['grafana/dashboard/panel/menu'];
const [extension] = extensions;
const context = {};
const configurable = {
// Command extensions
// ------------------
describe('when registering commands', () => {
const pluginId = 'belugacdn-app';
// Sample command configurations to be used in tests
const commandConfig1 = {
placement: 'grafana/dashboard/panel/menu',
title: 'Open incident',
description: 'You can create an incident from this context',
path: '/a/belugacdn-app/incidents/declare',
handler: () => {},
};
const commandConfig2 = {
placement: 'plugins/grafana-slo-app/slo-breached',
title: 'Open incident',
description: 'You can create an incident from this context',
handler: () => {},
};
extension?.configure?.(context);
it('should register a command extension', () => {
const registry = createPluginExtensionRegistry([
{
pluginId,
linkExtensions: [],
commandExtensions: [commandConfig1],
},
]);
expect(errorHandler).toBeCalledWith(expect.any(Function), configurable, context);
shouldHaveNumberOfPlacements(registry, 1);
shouldHaveExtensionsAtPlacement({
placement: commandConfig1.placement,
configs: [commandConfig1],
registry,
});
});
it('should register command extensions from a SINGLE PLUGIN with MULTIPLE PLACEMENTS', () => {
const registry = createPluginExtensionRegistry([
{
pluginId,
linkExtensions: [],
commandExtensions: [commandConfig1, commandConfig2],
},
]);
shouldHaveNumberOfPlacements(registry, 2);
shouldHaveExtensionsAtPlacement({
placement: commandConfig1.placement,
configs: [commandConfig1],
registry,
});
shouldHaveExtensionsAtPlacement({
placement: commandConfig2.placement,
configs: [commandConfig2],
registry,
});
});
it('should register command extensions from MULTIPLE PLUGINS with MULTIPLE PLACEMENTS', () => {
const registry = createPluginExtensionRegistry([
{
pluginId,
linkExtensions: [],
commandExtensions: [commandConfig1, commandConfig2],
},
{
pluginId: 'grafana-monitoring-app',
linkExtensions: [],
commandExtensions: [commandConfig1],
},
]);
shouldHaveNumberOfPlacements(registry, 2);
// Both plugins register commands to the same placement
shouldHaveExtensionsAtPlacement({
placement: commandConfig1.placement,
configs: [commandConfig1, commandConfig1],
registry,
});
// The 'beluga-cdn-app' plugin registers a command to an other placement as well
shouldHaveExtensionsAtPlacement({
placement: commandConfig2.placement,
configs: [commandConfig2],
registry,
});
});
it('should add default configure function when none is provided via the extension config', () => {
const registry = createPluginExtensionRegistry([
{
pluginId,
linkExtensions: [],
commandExtensions: [commandConfig1],
},
]);
const [configure] = registry[commandConfig1.placement];
const configured = configure();
// The default configure() function returns the extension config as is
expect(configured).toEqual({
type: PluginExtensionTypes.command,
key: expect.any(Number),
title: commandConfig1.title,
description: commandConfig1.description,
callHandlerWithContext: expect.any(Function),
});
});
it('should wrap the configure function with error handling', () => {
const registry = createPluginExtensionRegistry([
{
pluginId,
linkExtensions: [],
commandExtensions: [
{
...commandConfig1,
configure: () => ({}),
},
],
},
]);
const [configure] = registry[commandConfig1.placement];
const context = {};
const configurable = {
title: commandConfig1.title,
description: commandConfig2.description,
};
configure(context);
// The error handler is wrapping (decorating) the configure function, so it can provide standard error messages
expect(configureErrorHandler).toBeCalledWith(expect.any(Function), configurable, context);
});
it('should return undefined if returned by the provided extension config', () => {
const registry = createPluginExtensionRegistry([
{
pluginId,
linkExtensions: [],
commandExtensions: [
{
...commandConfig1,
configure: () => undefined,
},
],
},
]);
const [configure] = registry[commandConfig1.placement];
const context = {};
expect(configure(context)).toBeUndefined();
});
it('should wrap handler function with extension error handling', () => {
const registry = createPluginExtensionRegistry([
{
pluginId,
linkExtensions: [],
commandExtensions: [
{
placement: 'grafana/dashboard/panel/menu',
title: 'Open incident',
description: 'You can create an incident from this context',
handler: () => {},
configure: () => ({}),
},
],
},
]);
const extensions = registry['grafana/dashboard/panel/menu'];
const [configure] = extensions;
const context = {};
const extension = configure?.(context);
assertPluginExtensionCommand(extension);
extension.callHandlerWithContext();
expect(commandErrorHandler).toBeCalledWith(expect.any(Function), context);
});
it('should wrap handler function with extension error handling when no configure function is added', () => {
const registry = createPluginExtensionRegistry([
{
pluginId,
linkExtensions: [],
commandExtensions: [
{
placement: 'grafana/dashboard/panel/menu',
title: 'Open incident',
description: 'You can create an incident from this context',
handler: () => {},
},
],
},
]);
const extensions = registry['grafana/dashboard/panel/menu'];
const [configure] = extensions;
const context = {};
const extension = configure?.(context);
assertPluginExtensionCommand(extension);
extension.callHandlerWithContext();
expect(commandErrorHandler).toBeCalledWith(expect.any(Function), context);
});
});
});
// Checks the number of total placements in the registry
function shouldHaveNumberOfPlacements(registry: PluginExtensionRegistry, numberOfPlacements: number) {
expect(Object.keys(registry).length).toBe(numberOfPlacements);
}
// Checks if the registry has exactly the same extensions at the expected placement
function shouldHaveExtensionsAtPlacement({
configs,
placement,
registry,
}: {
configs: Array<AppPluginExtensionLinkConfig | AppPluginExtensionCommandConfig>;
placement: string;
registry: PluginExtensionRegistry;
}) {
const extensions = registry[placement].map((configure) => configure());
expect(extensions).toEqual(
configs.map((extension) => {
// Command extension
if ('handler' in extension) {
return {
key: expect.any(Number),
title: extension.title,
description: extension.description,
type: PluginExtensionTypes.command,
callHandlerWithContext: expect.any(Function),
};
}
// Link extension
return {
key: expect.any(Number),
title: extension.title,
description: extension.description,
type: PluginExtensionTypes.link,
path: extension.path,
};
})
);
}

View File

@@ -1,41 +1,40 @@
import {
AppConfigureExtension,
AppPluginExtensionLink,
AppPluginExtensionLinkConfig,
PluginExtensionLink,
type AppPluginExtensionCommand,
type AppPluginExtensionCommandConfig,
type AppPluginExtensionLink,
type AppPluginExtensionLinkConfig,
type PluginExtension,
type PluginExtensionCommand,
type PluginExtensionLink,
PluginExtensionTypes,
} from '@grafana/data';
import type {
PluginExtensionRegistry,
PluginExtensionRegistryItem,
RegistryConfigureExtension,
} from '@grafana/runtime';
import type { PluginExtensionRegistry, PluginExtensionRegistryItem } from '@grafana/runtime';
import { PluginPreloadResult } from '../pluginPreloader';
import type { PluginPreloadResult } from '../pluginPreloader';
import { createErrorHandling } from './errorHandling';
import { handleErrorsInHandler, handleErrorsInConfigure } from './errorHandling';
import { PlacementsPerPlugin } from './placementsPerPlugin';
import { ConfigureFunc } from './types';
import { createLinkValidator, isValidLinkPath } from './validateLink';
export function createPluginExtensionRegistry(preloadResults: PluginPreloadResult[]): PluginExtensionRegistry {
const registry: PluginExtensionRegistry = {};
for (const result of preloadResults) {
const pluginPlacementCount: Record<string, number> = {};
const { pluginId, linkExtensions, error } = result;
const { pluginId, linkExtensions, commandExtensions, error } = result;
if (!Array.isArray(linkExtensions) || error) {
if (error) {
continue;
}
for (const extension of linkExtensions) {
const placement = extension.placement;
const placementsPerPlugin = new PlacementsPerPlugin();
const configs = [...linkExtensions, ...commandExtensions];
pluginPlacementCount[placement] = (pluginPlacementCount[placement] ?? 0) + 1;
const item = createRegistryLink(pluginId, extension);
for (const config of configs) {
const placement = config.placement;
const item = createRegistryItem(pluginId, config);
// If there was an issue initialising the plugin, skip adding its extensions to the registry
// or if the plugin already have placed 2 items at the extension point.
if (!item || pluginPlacementCount[placement] > 2) {
if (!item || !placementsPerPlugin.allowedToAdd(placement)) {
continue;
}
@@ -55,41 +54,21 @@ export function createPluginExtensionRegistry(preloadResults: PluginPreloadResul
return Object.freeze(registry);
}
function createRegistryLink(
function createRegistryItem(
pluginId: string,
config: AppPluginExtensionLinkConfig
): PluginExtensionRegistryItem<PluginExtensionLink> | undefined {
if (!isValidLinkPath(pluginId, config.path)) {
return undefined;
config: AppPluginExtensionCommandConfig | AppPluginExtensionLinkConfig
): PluginExtensionRegistryItem | undefined {
if ('handler' in config) {
return createCommandRegistryItem(pluginId, config);
}
const id = `${pluginId}${config.placement}${config.title}`;
const extension = Object.freeze({
type: PluginExtensionTypes.link,
title: config.title,
description: config.description,
key: hashKey(id),
path: config.path,
});
return Object.freeze({
extension: extension,
configure: createLinkConfigure(pluginId, config, extension),
});
return createLinkRegistryItem(pluginId, config);
}
function hashKey(key: string): number {
return Array.from(key).reduce((s, c) => (Math.imul(31, s) + c.charCodeAt(0)) | 0, 0);
}
function createLinkConfigure(
function createCommandRegistryItem(
pluginId: string,
config: AppPluginExtensionLinkConfig,
extension: PluginExtensionLink
): RegistryConfigureExtension<PluginExtensionLink> | undefined {
if (!config.configure) {
return undefined;
}
config: AppPluginExtensionCommandConfig
): PluginExtensionRegistryItem<PluginExtensionCommand> | undefined {
const configure = config.configure ?? defaultConfigure;
const options = {
pluginId: pluginId,
@@ -97,36 +76,102 @@ function createLinkConfigure(
logger: console.warn,
};
const mapper = mapToRegistryType(extension);
const validator = createLinkValidator(options);
const errorHandler = createErrorHandling<AppPluginExtensionLink>(options);
const catchErrorsInHandler = handleErrorsInHandler(options);
const handler = catchErrorsInHandler(config.handler);
return mapper(validator(errorHandler(config.configure)));
}
const extensionFactory = createCommandFactory(pluginId, config, handler);
function mapToRegistryType(
extension: PluginExtensionLink
): (configure: AppConfigureExtension<AppPluginExtensionLink>) => RegistryConfigureExtension<PluginExtensionLink> {
const configurable: AppPluginExtensionLink = {
title: extension.title,
description: extension.description,
path: extension.path,
const configurable: AppPluginExtensionCommand = {
title: config.title,
description: config.description,
};
return (configure) => {
return function mapper(context: object): PluginExtensionLink | undefined {
const configured = configure(configurable, context);
const mapper = mapToConfigure<PluginExtensionCommand, AppPluginExtensionCommand>(extensionFactory, configurable);
const catchErrorsInConfigure = handleErrorsInConfigure<AppPluginExtensionCommand>(options);
if (!configured) {
return mapper(catchErrorsInConfigure(configure));
}
function createLinkRegistryItem(
pluginId: string,
config: AppPluginExtensionLinkConfig
): PluginExtensionRegistryItem<PluginExtensionLink> | undefined {
if (!isValidLinkPath(pluginId, config.path)) {
return undefined;
}
const configure = config.configure ?? defaultConfigure;
const options = { pluginId: pluginId, title: config.title, logger: console.warn };
const extensionFactory = createLinkFactory(pluginId, config);
const configurable: AppPluginExtensionLink = {
title: config.title,
description: config.description,
path: config.path,
};
const mapper = mapToConfigure<PluginExtensionLink, AppPluginExtensionLink>(extensionFactory, configurable);
const withConfigureErrorHandling = handleErrorsInConfigure<AppPluginExtensionLink>(options);
const validateLink = createLinkValidator(options);
return mapper(validateLink(withConfigureErrorHandling(configure)));
}
function createLinkFactory(pluginId: string, config: AppPluginExtensionLinkConfig) {
return (override: Partial<AppPluginExtensionLink>, context?: object): PluginExtensionLink => {
const title = override?.title ?? config.title;
const description = override?.description ?? config.description;
const path = override?.path ?? config.path;
return Object.freeze({
type: PluginExtensionTypes.link,
title: title,
description: description,
path: path,
key: hashKey(`${pluginId}${config.placement}${title}`),
});
};
}
function createCommandFactory(
pluginId: string,
config: AppPluginExtensionCommandConfig,
handler: (context?: object) => void
) {
return (override: Partial<AppPluginExtensionCommand>, context?: object): PluginExtensionCommand => {
const title = override?.title ?? config.title;
const description = override?.description ?? config.description;
return Object.freeze({
type: PluginExtensionTypes.command,
title: title,
description: description,
key: hashKey(`${pluginId}${config.placement}${title}`),
callHandlerWithContext: () => handler(context),
});
};
}
function mapToConfigure<T extends PluginExtension, C>(
commandFactory: (override: Partial<C>, context?: object) => T | undefined,
configurable: C
): (configure: ConfigureFunc<C>) => PluginExtensionRegistryItem<T> {
return (configure) => {
return function mapToExtension(context?: object): T | undefined {
const override = configure(configurable, context);
if (!override) {
return undefined;
}
return {
...extension,
title: configured.title ?? extension.title,
description: configured.description ?? extension.description,
path: configured.path ?? extension.path,
};
return commandFactory(override, context);
};
};
}
function hashKey(key: string): number {
return Array.from(key).reduce((s, c) => (Math.imul(31, s) + c.charCodeAt(0)) | 0, 0);
}
function defaultConfigure() {
return {};
}

View File

@@ -0,0 +1,4 @@
import type { AppPluginExtensionCommandConfig } from '@grafana/data';
export type CommandHandlerFunc = AppPluginExtensionCommandConfig['handler'];
export type ConfigureFunc<T> = (extension: T, context?: object) => Partial<T> | undefined;

View File

@@ -1,4 +1,6 @@
import type { AppConfigureExtension, AppPluginExtensionLink } from '@grafana/data';
import type { AppPluginExtensionLink } from '@grafana/data';
import type { ConfigureFunc } from './types';
type Options = {
pluginId: string;
@@ -9,7 +11,7 @@ type Options = {
export function createLinkValidator(options: Options) {
const { pluginId, title, logger } = options;
return (configure: AppConfigureExtension<AppPluginExtensionLink>): AppConfigureExtension<AppPluginExtensionLink> => {
return (configure: ConfigureFunc<AppPluginExtensionLink>): ConfigureFunc<AppPluginExtensionLink> => {
return function validateLink(link, context) {
const configured = configure(link, context);

View File

@@ -1,4 +1,4 @@
import { AppPluginExtensionLinkConfig } from '@grafana/data';
import type { AppPluginExtensionCommandConfig, AppPluginExtensionLinkConfig } from '@grafana/data';
import type { AppPluginConfig } from '@grafana/runtime';
import * as pluginLoader from './plugin_loader';
@@ -6,6 +6,7 @@ import * as pluginLoader from './plugin_loader';
export type PluginPreloadResult = {
pluginId: string;
linkExtensions: AppPluginExtensionLinkConfig[];
commandExtensions: AppPluginExtensionCommandConfig[];
error?: unknown;
};
@@ -18,10 +19,10 @@ async function preload(config: AppPluginConfig): Promise<PluginPreloadResult> {
const { path, version, id: pluginId } = config;
try {
const { plugin } = await pluginLoader.importPluginModule(path, version);
const { linkExtensions = [] } = plugin;
return { pluginId, linkExtensions };
const { linkExtensions = [], commandExtensions = [] } = plugin;
return { pluginId, linkExtensions, commandExtensions };
} catch (error) {
console.error(`[Plugins] Failed to preload plugin: ${path} (version: ${version})`, error);
return { pluginId, linkExtensions: [], error };
return { pluginId, linkExtensions: [], commandExtensions: [], error };
}
}