Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugins: Renamed parts of the UI extension APIs #63070

Merged
merged 5 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('getPluginExtensions', () => {
type: 'link',
title: 'Declare incident',
description: 'Declaring an incident in the app',
href: `/a/${pluginId}/declare-incident`,
path: `/a/${pluginId}/declare-incident`,
key: 1,
},
],
Expand All @@ -22,26 +22,26 @@ describe('getPluginExtensions', () => {

it('should return a collection of extensions to the plugin', () => {
const { extensions, error } = getPluginExtensions({
target: `plugins/${pluginId}/${linkId}`,
id: `plugins/${pluginId}/${linkId}`,
});

expect(extensions[0].href).toBe(`/a/${pluginId}/declare-incident`);
expect(extensions[0].path).toBe(`/a/${pluginId}/declare-incident`);
expect(error).toBeUndefined();
});

it('should return a description for the requested link', () => {
const { extensions, error } = getPluginExtensions({
target: `plugins/${pluginId}/${linkId}`,
id: `plugins/${pluginId}/${linkId}`,
});

expect(extensions[0].href).toBe(`/a/${pluginId}/declare-incident`);
expect(extensions[0].path).toBe(`/a/${pluginId}/declare-incident`);
expect(extensions[0].description).toBe('Declaring an incident in the app');
expect(error).toBeUndefined();
});

it('should return an empty array when no links can be found', () => {
const { extensions, error } = getPluginExtensions({
target: `an-unknown-app/${linkId}`,
id: `an-unknown-app/${linkId}`,
});

expect(extensions.length).toBe(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getPluginsExtensionRegistry, PluginsExtension } from './registry';

export type GetPluginExtensionsOptions = {
target: string;
id: string;
};

export type PluginExtensionsResult = {
Expand All @@ -10,23 +10,23 @@ export type PluginExtensionsResult = {
};

export class PluginExtensionsMissingError extends Error {
readonly target: string;
readonly id: string;

constructor(target: string) {
super(`Could not find extensions for '${target}'`);
this.target = target;
constructor(id: string) {
super(`Could not find extensions for '${id}'`);
this.id = id;
this.name = PluginExtensionsMissingError.name;
}
}

export function getPluginExtensions({ target }: GetPluginExtensionsOptions): PluginExtensionsResult {
export function getPluginExtensions({ id }: GetPluginExtensionsOptions): PluginExtensionsResult {
const registry = getPluginsExtensionRegistry();
const extensions = registry[target];
const extensions = registry[id];

if (!Array.isArray(extensions)) {
return {
extensions: [],
error: new PluginExtensionsMissingError(target),
error: new PluginExtensionsMissingError(id),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export type PluginsExtensionLink = {
type: 'link';
title: string;
description: string;
href: string;
path: string;
key: number;
};

Expand Down
6 changes: 3 additions & 3 deletions pkg/api/frontendsettings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func TestHTTPServer_GetFrontendSettings_apps(t *testing.T) {
return &plugins.FakePluginStore{
PluginList: newPlugins("test-app", []*plugindef.ExtensionsLink{
{
Target: "core/home/menu",
Id: "core/home/menu",
Type: plugindef.ExtensionsLinkTypeLink,
Title: "Title",
Description: "Home route of app",
Expand All @@ -267,7 +267,7 @@ func TestHTTPServer_GetFrontendSettings_apps(t *testing.T) {
Version: "0.5.0",
Extensions: []*plugindef.ExtensionsLink{
{
Target: "core/home/menu",
Id: "core/home/menu",
Type: plugindef.ExtensionsLinkTypeLink,
Title: "Title",
Description: "Home route of app",
Expand All @@ -284,7 +284,7 @@ func TestHTTPServer_GetFrontendSettings_apps(t *testing.T) {
return &plugins.FakePluginStore{
PluginList: newPlugins("test-app", []*plugindef.ExtensionsLink{
{
Target: "core/home/menu",
Id: "core/home/menu",
Type: plugindef.ExtensionsLinkTypeLink,
Title: "Title",
Description: "Home route of app",
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugins/manager/loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,14 +502,14 @@ func TestLoader_Load(t *testing.T) {
},
Extensions: []*plugindef.ExtensionsLink{
{
Target: "plugins/grafana-slo-app/slo-breach",
Id: "plugins/grafana-slo-app/slo-breach",
Title: "Declare incident",
Type: plugindef.ExtensionsLinkTypeLink,
Description: "Declares a new incident",
Path: "/incidents/declare",
},
{
Target: "plugins/grafana-slo-app/slo-breach",
Id: "plugins/grafana-slo-app/slo-breach",
Title: "Declare incident",
Type: plugindef.ExtensionsLinkTypeLink,
Description: "Declares a new incident (path without backslash)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@
],
"extensions": [
{
"target": "plugins/grafana-slo-app/slo-breach",
"id": "plugins/grafana-slo-app/slo-breach",
"type": "link",
"title": "Declare incident",
"description": "Declares a new incident",
"path": "/incidents/declare"
},
{
"target": "plugins/grafana-slo-app/slo-breach",
"id": "plugins/grafana-slo-app/slo-breach",
"type": "link",
"title": "Declare incident",
"description": "Declares a new incident (path without backslash)",
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugins/plugindef/plugindef.cue
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ seqs: [
component?: string

// The minimum role a user must have to see this page in the navigation menu.
role?: "Admin" | "Editor" | "Viewer"
role?: "Admin" | "Editor" | "Viewer"

// RBAC action the user must have to access the route
action?: string
Expand All @@ -124,7 +124,7 @@ seqs: [

#ExtensionsLink: {
// Target where the link will be rendered
target: =~"^(plugins|grafana)\/[a-z-/0-9]*$"
id: =~"^(plugins|grafana)\/[a-z-/0-9]*$"
// Type of extension
type: "link"
// Title that will be displayed for the rendered link
Expand Down
6 changes: 3 additions & 3 deletions pkg/plugins/plugindef/plugindef_types_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions public/app/features/plugins/extensions/registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('Plugin registry', () => {
title: 'The title',
type: 'link',
description: 'Incidents are occurring!',
href: '/a/belugacdn-app/incidents/declare',
path: '/a/belugacdn-app/incidents/declare',
key: 539074708,
});
});
Expand All @@ -68,15 +68,15 @@ describe('Plugin registry', () => {
title: 'The title',
type: 'link',
description: 'Incidents are occurring!',
href: '/a/belugacdn-app/incidents/declare',
path: '/a/belugacdn-app/incidents/declare',
key: 539074708,
});

expect(pluginBLink).toEqual({
title: 'The title',
type: 'link',
description: 'Incidents are occurring!',
href: '/a/strava-app/incidents/declare',
path: '/a/strava-app/incidents/declare',
key: -1637066384,
});
});
Expand Down
6 changes: 3 additions & 3 deletions public/app/features/plugins/extensions/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ export function createPluginExtensionsRegistry(apps: Record<string, AppPluginCon
}

function createRegistryItem(pluginId: string, extension: PluginsExtensionLinkConfig): PluginsExtensionLink {
const href = `/a/${pluginId}${extension.path}`;
const path = `/a/${pluginId}${extension.path}`;

return Object.freeze({
type: PluginExtensionTypes.link,
title: extension.title,
description: extension.description,
href: href,
key: hashKey(`${extension.title}${href}`),
path: path,
mckn marked this conversation as resolved.
Show resolved Hide resolved
key: hashKey(`${extension.title}${path}`),
});
}

Expand Down
8 changes: 4 additions & 4 deletions public/app/features/sandbox/TestStuffPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const TestStuffPage = () => {
<Page navModel={{ node: node, main: node }}>
<Page.Contents>
<HorizontalGroup>
<LinkToBasicApp target="grafana/sandbox/testing" />
<LinkToBasicApp id="grafana/sandbox/testing" />
</HorizontalGroup>
{data && (
<AutoSizer style={{ width: '100%', height: '600px' }}>
Expand Down Expand Up @@ -148,8 +148,8 @@ export function getDefaultState(): State {
};
}

function LinkToBasicApp({ target }: { target: string }) {
const { extensions, error } = getPluginExtensions({ target });
function LinkToBasicApp({ id }: { id: string }) {
const { extensions, error } = getPluginExtensions({ id });

if (error) {
return null;
Expand All @@ -159,7 +159,7 @@ function LinkToBasicApp({ target }: { target: string }) {
<div>
{extensions.map((extension) => {
return (
<LinkButton href={extension.href} title={extension.description} key={extension.key}>
<LinkButton href={extension.path} title={extension.description} key={extension.key}>
{extension.title}
</LinkButton>
);
Expand Down