From feedd3fd260a47ddc1c04cea2a75276359c90077 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Wed, 20 Apr 2022 15:17:05 +0100 Subject: [PATCH 1/4] fix(NA): update codeowners for kibana-cloud-security-posture team on cloud_security_posture plugin (#130630) From 4818c4056c795d9cd3c25296966bdda146663567 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Wed, 15 Jun 2022 13:03:07 +0200 Subject: [PATCH 2/4] Handle legacy sort conversion --- .../main/services/discover_state.ts | 27 ++++----- .../main/utils/cleanup_url_state.test.ts | 58 +++++++++++++++++++ .../main/utils/cleanup_url_state.ts | 35 +++++++++++ .../public/utils/migrate_legacy_url_state.ts | 0 4 files changed, 105 insertions(+), 15 deletions(-) create mode 100644 src/plugins/discover/public/application/main/utils/cleanup_url_state.test.ts create mode 100644 src/plugins/discover/public/application/main/utils/cleanup_url_state.ts create mode 100644 src/plugins/discover/public/utils/migrate_legacy_url_state.ts diff --git a/src/plugins/discover/public/application/main/services/discover_state.ts b/src/plugins/discover/public/application/main/services/discover_state.ts index 81f25e6151c99..81dd6e3ad6ea7 100644 --- a/src/plugins/discover/public/application/main/services/discover_state.ts +++ b/src/plugins/discover/public/application/main/services/discover_state.ts @@ -6,11 +6,11 @@ * Side Public License, v 1. */ -import { isEqual, cloneDeep } from 'lodash'; +import { cloneDeep, isEqual } from 'lodash'; import { i18n } from '@kbn/i18n'; import { History } from 'history'; -import { NotificationsStart, IUiSettingsClient } from '@kbn/core/public'; -import { Filter, FilterStateStore, compareFilters, COMPARE_ALL_OPTIONS } from '@kbn/es-query'; +import { IUiSettingsClient, NotificationsStart } from '@kbn/core/public'; +import { COMPARE_ALL_OPTIONS, compareFilters, Filter, FilterStateStore } from '@kbn/es-query'; import { createKbnUrlStateStorage, createStateContainer, @@ -29,12 +29,12 @@ import { syncQueryStateWithUrl, } from '@kbn/data-plugin/public'; import { DataView } from '@kbn/data-views-plugin/public'; -import { migrateLegacyQuery } from '../../../utils/migrate_legacy_query'; import { DiscoverGridSettings } from '../../../components/discover_grid/types'; import { SavedSearch } from '../../../services/saved_searches'; import { handleSourceColumnState } from '../../../utils/state_helpers'; import { DISCOVER_APP_LOCATOR, DiscoverAppLocatorParams } from '../../../locator'; import { VIEW_MODE } from '../../../components/view_mode_toggle'; +import { cleanupUrlState } from '../utils/cleanup_url_state'; export interface AppState { /** @@ -87,6 +87,13 @@ export interface AppState { rowHeight?: number; } +export interface AppStateUrl extends Omit { + /** + * Necessary to take care of legacy links [fieldName,direction] + */ + sort?: string[][] | [string, string]; +} + interface GetStateParams { /** * Default state used for merging with with URL state to get the initial state @@ -188,17 +195,7 @@ export function getState({ ...(toasts && withNotifyOnErrors(toasts)), }); - const appStateFromUrl = stateStorage.get(APP_STATE_URL_KEY) as AppState; - - if (appStateFromUrl && appStateFromUrl.query && !appStateFromUrl.query.language) { - appStateFromUrl.query = migrateLegacyQuery(appStateFromUrl.query); - } - - if (appStateFromUrl?.sort && !appStateFromUrl.sort.length) { - // If there's an empty array given in the URL, the sort prop should be removed - // This allows the sort prop to be overwritten with the default sorting - delete appStateFromUrl.sort; - } + const appStateFromUrl = cleanupUrlState(stateStorage.get(APP_STATE_URL_KEY) as AppStateUrl); let initialAppState = handleSourceColumnState( { diff --git a/src/plugins/discover/public/application/main/utils/cleanup_url_state.test.ts b/src/plugins/discover/public/application/main/utils/cleanup_url_state.test.ts new file mode 100644 index 0000000000000..e76e40a45a316 --- /dev/null +++ b/src/plugins/discover/public/application/main/utils/cleanup_url_state.test.ts @@ -0,0 +1,58 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { cleanupUrlState } from './cleanup_url_state'; +import { AppStateUrl } from '../services/discover_state'; + +describe('cleanupUrlState', () => { + test('cleaning up legacy sort', async () => { + const state = { sort: ['batman', 'desc'] } as AppStateUrl; + expect(cleanupUrlState(state)).toMatchInlineSnapshot(` + Object { + "sort": Array [ + Array [ + "batman", + "desc", + ], + ], + } + `); + }); + test('not cleaning up broken legacy sort', async () => { + const state = { sort: ['batman'] } as unknown as AppStateUrl; + expect(cleanupUrlState(state)).toMatchInlineSnapshot(`Object {}`); + }); + test('not cleaning up regular sort', async () => { + const state = { + sort: [ + ['batman', 'desc'], + ['robin', 'asc'], + ], + } as AppStateUrl; + expect(cleanupUrlState(state)).toMatchInlineSnapshot(` + Object { + "sort": Array [ + Array [ + "batman", + "desc", + ], + Array [ + "robin", + "asc", + ], + ], + } + `); + }); + test('removing empty sort', async () => { + const state = { + sort: [], + } as AppStateUrl; + expect(cleanupUrlState(state)).toMatchInlineSnapshot(`Object {}`); + }); +}); diff --git a/src/plugins/discover/public/application/main/utils/cleanup_url_state.ts b/src/plugins/discover/public/application/main/utils/cleanup_url_state.ts new file mode 100644 index 0000000000000..0148931f73c58 --- /dev/null +++ b/src/plugins/discover/public/application/main/utils/cleanup_url_state.ts @@ -0,0 +1,35 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ +import { migrateLegacyQuery } from '../../../utils/migrate_legacy_query'; +import { AppState, AppStateUrl } from '../services/discover_state'; + +/** + * Takes care of the given url state, migrates legacy props and cleans up empty props + * @param appStateFromUrl + */ +export function cleanupUrlState(appStateFromUrl: AppStateUrl): AppState { + if (appStateFromUrl && appStateFromUrl.query && !appStateFromUrl.query.language) { + appStateFromUrl.query = migrateLegacyQuery(appStateFromUrl.query); + } + + if (typeof appStateFromUrl?.sort?.[0] === 'string') { + if (appStateFromUrl?.sort?.[1] === 'asc' || appStateFromUrl.sort[1] === 'desc') { + // handling sort props like this[fieldName,direction] + appStateFromUrl.sort = [[appStateFromUrl.sort[0], appStateFromUrl.sort[1]]]; + } else { + delete appStateFromUrl.sort; + } + } + + if (appStateFromUrl?.sort && !appStateFromUrl.sort.length) { + // If there's an empty array given in the URL, the sort prop should be removed + // This allows the sort prop to be overwritten with the default sorting + delete appStateFromUrl.sort; + } + return appStateFromUrl as AppState; +} diff --git a/src/plugins/discover/public/utils/migrate_legacy_url_state.ts b/src/plugins/discover/public/utils/migrate_legacy_url_state.ts new file mode 100644 index 0000000000000..e69de29bb2d1d From c60a169aa1fa812b0bc2b147facf9f8210a48652 Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Wed, 15 Jun 2022 11:12:00 +0000 Subject: [PATCH 3/4] [CI] Auto-commit changed files from 'node scripts/precommit_hook.js --ref HEAD~1..HEAD --fix' --- .../discover/public/utils/migrate_legacy_url_state.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/plugins/discover/public/utils/migrate_legacy_url_state.ts b/src/plugins/discover/public/utils/migrate_legacy_url_state.ts index e69de29bb2d1d..5c2d5b68ae2e0 100644 --- a/src/plugins/discover/public/utils/migrate_legacy_url_state.ts +++ b/src/plugins/discover/public/utils/migrate_legacy_url_state.ts @@ -0,0 +1,7 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ From 922995ce16fb72a54bda073c31502943c5b7db0d Mon Sep 17 00:00:00 2001 From: kertal Date: Fri, 24 Jun 2022 07:09:04 +0000 Subject: [PATCH 4/4] remove empty file --- .../discover/public/utils/migrate_legacy_url_state.ts | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 src/plugins/discover/public/utils/migrate_legacy_url_state.ts diff --git a/src/plugins/discover/public/utils/migrate_legacy_url_state.ts b/src/plugins/discover/public/utils/migrate_legacy_url_state.ts deleted file mode 100644 index 5c2d5b68ae2e0..0000000000000 --- a/src/plugins/discover/public/utils/migrate_legacy_url_state.ts +++ /dev/null @@ -1,7 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */