Add Kibana version to URL (#112528)

* add utility to set current kibana version

* set kibana version on dashboard

* move setVersion function into separate file

* improve setVersion method

* add test mocks

* remove unnecessary any

* narrow down history type

* use new formatting settings

* remove version integration on dashboard

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Vadim Kibana 2021-09-23 13:23:30 +02:00 committed by GitHub
parent 26d19e7fd1
commit 7f7c0b4ed5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 155 additions and 6 deletions

View file

@ -6,6 +6,9 @@
* Side Public License, v 1.
*/
import { PluginInitializerContext } from 'src/core/public';
import { KibanaUtilsPublicPlugin } from './plugin';
// TODO: https://github.com/elastic/kibana/issues/109893
/* eslint-disable @kbn/eslint/no_export_all */
@ -78,10 +81,8 @@ export {
export { applyDiff } from './state_management/utils/diff_object';
export { createStartServicesGetter, StartServicesGetter } from './core/create_start_service_getter';
/** dummy plugin, we just want kibanaUtils to have its own bundle */
export function plugin() {
return new (class KibanaUtilsPlugin {
setup() {}
start() {}
})();
export { KibanaUtilsSetup } from './plugin';
export function plugin(initializerContext: PluginInitializerContext) {
return new KibanaUtilsPublicPlugin(initializerContext);
}

View file

@ -0,0 +1,27 @@
/*
* 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 { KibanaUtilsSetup, KibanaUtilsStart } from './plugin';
export type Setup = jest.Mocked<KibanaUtilsSetup>;
export type Start = jest.Mocked<KibanaUtilsStart>;
const createSetupContract = (): Setup => {
return {
setVersion: jest.fn(),
};
};
const createStartContract = (): Start => {
return undefined;
};
export const kibanaUtilsPluginMock = {
createSetupContract,
createStartContract,
};

View file

@ -0,0 +1,41 @@
/*
* 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 { PluginInitializerContext, CoreSetup, CoreStart, Plugin } from 'src/core/public';
import { History } from 'history';
import { setVersion } from './set_version';
export interface KibanaUtilsSetup {
setVersion: (history: Pick<History, 'location' | 'replace'>) => void;
}
export type KibanaUtilsStart = undefined;
export class KibanaUtilsPublicPlugin implements Plugin<KibanaUtilsSetup, KibanaUtilsStart> {
private readonly version: string;
constructor(initializerContext: PluginInitializerContext) {
this.version = initializerContext.env.packageInfo.version;
}
public setup(core: CoreSetup): KibanaUtilsSetup {
return {
setVersion: this.setVersion,
};
}
public start(core: CoreStart): KibanaUtilsStart {
return undefined;
}
public stop() {}
private setVersion = (history: Pick<History, 'location' | 'replace'>) => {
setVersion(history, this.version);
};
}

View file

@ -0,0 +1,59 @@
/*
* 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 { History } from 'history';
import { setVersion } from './set_version';
describe('setVersion', () => {
test('sets version, if one is not set', () => {
const history: Pick<History, 'location' | 'replace'> = {
location: {
hash: '',
search: '',
pathname: '/',
state: {},
},
replace: jest.fn(),
};
setVersion(history, '1.2.3');
expect(history.replace).toHaveBeenCalledTimes(1);
expect(history.replace).toHaveBeenCalledWith('/?_v=1.2.3');
});
test('overwrites, if version already set to a different value', () => {
const history: Pick<History, 'location' | 'replace'> = {
location: {
hash: '/view/dashboards',
search: 'a=b&_v=7.16.6',
pathname: '/foo/bar',
state: {},
},
replace: jest.fn(),
};
setVersion(history, '8.0.0');
expect(history.replace).toHaveBeenCalledTimes(1);
expect(history.replace).toHaveBeenCalledWith('/foo/bar?a=b&_v=8.0.0#/view/dashboards');
});
test('does nothing, if version already set to correct value', () => {
const history: Pick<History, 'location' | 'replace'> = {
location: {
hash: '/view/dashboards',
search: 'a=b&_v=8.0.0',
pathname: '/foo/bar',
state: {},
},
replace: jest.fn(),
};
setVersion(history, '8.0.0');
expect(history.replace).toHaveBeenCalledTimes(0);
});
});

View file

@ -0,0 +1,21 @@
/*
* 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 { History } from 'history';
export const setVersion = (history: Pick<History, 'location' | 'replace'>, version: string) => {
const search = new URLSearchParams(history.location.search);
if (search.get('_v') === version) return;
search.set('_v', version);
const path =
history.location.pathname +
'?' +
search.toString() +
(history.location.hash ? '#' + history.location.hash : '');
history.replace(path);
};