Skip to content

Commit

Permalink
Fix migration helper if nested object properties are not defined (#468)
Browse files Browse the repository at this point in the history
* Fix migration helper if nested object properties are not defined

* Update version

* Update docker-compose.yml

---------

Co-authored-by: Mikhail Volkov <[email protected]>
  • Loading branch information
asimonok and mikhail-vl authored Aug 16, 2024
1 parent 4833196 commit 3f8185a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## 4.3.1 (2024-08-16)

### Bugfixes

- Fixed migration helper if nested object properties are not defined (#468)

## 4.3.0 (2024-08-12)

### Features / Enhancements
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ services:
- GF_DEFAULT_APP_MODE=development
- GF_USERS_DEFAULT_THEME=light
- GF_INSTALL_PLUGINS=marcusolsson-static-datasource,marcusolsson-json-datasource,volkovlabs-variable-panel, volkovlabs-image-panel
# - GF_FEATURE_TOGGLES_ENABLE=dashboardScene
volumes:
- ./dist:/var/lib/grafana/plugins/volkovlabs-form-panel
- ./provisioning:/etc/grafana/provisioning
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,5 @@
"test:ci": "jest --maxWorkers 4 --coverage",
"upgrade": "npm upgrade --save"
},
"version": "4.3.0"
"version": "4.3.1"
}
51 changes: 31 additions & 20 deletions src/migration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { PanelModel } from '@grafana/data';
import { set } from 'lodash';
import semver from 'semver';

import { FormElementType, PayloadMode } from './constants';
Expand Down Expand Up @@ -36,10 +37,10 @@ interface OutdatedLayoutOptions extends Omit<LayoutOptions, 'sections'> {
/**
* Outdated Panel Options
*/
interface OutdatedPanelOptions extends Omit<PanelOptions, 'initial' | 'update' | 'layout'> {
initial: OutdatedRequestOptions;
update: OutdatedRequestOptions;
layout: OutdatedLayoutOptions;
interface OutdatedPanelOptions extends Partial<Omit<PanelOptions, 'initial' | 'update' | 'layout'>> {
initial?: OutdatedRequestOptions;
update?: OutdatedRequestOptions;
layout?: OutdatedLayoutOptions;
}

/**
Expand All @@ -55,7 +56,11 @@ const normalizeRequestOptions = ({ updatedOnly, payloadMode, ...actual }: Outdat
/**
* Normalize Code Options
*/
const normalizeCodeOptions = (code: string): string => {
const normalizeCodeOptions = (code: string | undefined): string => {
if (!code) {
return '';
}

const search =
/^(?!.*context\.)(?:.*)(options\.|data\.|response|elements\.|onChange\(|locationService|templateService|onOptionsChange\(|initialRequest\(|setInitial\(|initial\.|notifyError\(|notifySuccess\(|notifyWarning\(|toDataQueryResponse\(|replaceVariables\()/gm;

Expand Down Expand Up @@ -131,7 +136,7 @@ const normalizeCodeOptions = (code: string): string => {
* @param obj
*
*/
export const normalizePayloadOptions = (obj: Record<string, unknown>) => {
export const normalizePayloadOptions = (obj?: Record<string, unknown>) => {
/**
* Check passed object
*/
Expand Down Expand Up @@ -167,13 +172,13 @@ export const getMigratedOptions = (panel: PanelModel<OutdatedPanelOptions>): Pan
* Normalize non context code parameters before 4.0.0
*/
if (panel.pluginVersion && semver.lt(panel.pluginVersion, '4.0.0')) {
options.initial.code = normalizeCodeOptions(options.initial.code);
options.resetAction.code = normalizeCodeOptions(options.resetAction.code);
options.update.code = normalizeCodeOptions(options.update.code);
set(options, 'initial.code', normalizeCodeOptions(options.initial?.code));
set(options, 'resetAction.code', normalizeCodeOptions(options.resetAction?.code));
set(options, 'update.code', normalizeCodeOptions(options.update?.code));

options.initial.getPayload = normalizeCodeOptions(options.initial.getPayload);
options.resetAction.getPayload = normalizeCodeOptions(options.resetAction.getPayload);
options.update.getPayload = normalizeCodeOptions(options.update.getPayload);
set(options, 'initial.getPayload', normalizeCodeOptions(options.initial?.getPayload));
set(options, 'resetAction.getPayload', normalizeCodeOptions(options.resetAction?.getPayload));
set(options, 'update.getPayload', normalizeCodeOptions(options.update?.getPayload));

if (options.elements && options.elements.length > 0) {
options.elements.forEach((element) => {
Expand All @@ -200,23 +205,25 @@ export const getMigratedOptions = (panel: PanelModel<OutdatedPanelOptions>): Pan
/**
* Remove datasource payload code
*/
if (typeof options.initial.payload === 'string') {
if (typeof options.initial?.payload === 'string') {
options.initial.payload = {};
}

if (typeof options.update.payload === 'string') {
if (typeof options.update?.payload === 'string') {
options.update.payload = {};
}

if (typeof options.resetAction.payload === 'string') {
if (typeof options.resetAction?.payload === 'string') {
options.resetAction.payload = {};
}

/**
* Normalize request options
* Normalize initial request options
*/
if ('updatedOnly' in options.initial || 'updatedOnly' in options.update) {
if (options.initial && 'updatedOnly' in options.initial) {
options.initial = normalizeRequestOptions(options.initial);
}
if (options.update && 'updatedOnly' in options.update) {
options.update = normalizeRequestOptions(options.update);
}

Expand All @@ -237,9 +244,13 @@ export const getMigratedOptions = (panel: PanelModel<OutdatedPanelOptions>): Pan
* Normalize payload object
*/
if (panel.pluginVersion && semver.lt(panel.pluginVersion, '4.3.0')) {
options.initial.payload = normalizePayloadOptions(options.initial.payload as Record<string, unknown>);
options.update.payload = normalizePayloadOptions(options.update.payload as Record<string, unknown>);
options.resetAction.payload = normalizePayloadOptions(options.resetAction.payload as Record<string, unknown>);
set(options, 'initial.payload', normalizePayloadOptions(options.initial?.payload as Record<string, unknown>));
set(options, 'update.payload', normalizePayloadOptions(options.update?.payload as Record<string, unknown>));
set(
options,
'resetAction.payload',
normalizePayloadOptions(options.resetAction?.payload as Record<string, unknown>)
);
}

return options as PanelOptions;
Expand Down

0 comments on commit 3f8185a

Please sign in to comment.