From c902b074c8f64eff2d05da9dee6e9c686362c1b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez=20Haro?= Date: Thu, 3 Sep 2020 12:06:49 +0100 Subject: [PATCH] [Newsfeed] Ensure the version format when calling the API (#76381) Co-authored-by: Elastic Machine --- src/plugins/newsfeed/public/lib/api.test.ts | 14 +++++++------- src/plugins/newsfeed/public/lib/api.ts | 8 ++++++-- .../fixtures/plugins/newsfeed/server/plugin.ts | 2 +- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/plugins/newsfeed/public/lib/api.test.ts b/src/plugins/newsfeed/public/lib/api.test.ts index be4274e8bc054..21331b9d82575 100644 --- a/src/plugins/newsfeed/public/lib/api.test.ts +++ b/src/plugins/newsfeed/public/lib/api.test.ts @@ -48,7 +48,7 @@ Object.defineProperty(window, 'sessionStorage', { }); describe('NewsfeedApiDriver', () => { - const kibanaVersion = 'test_version'; + const kibanaVersion = '99.999.9-test_version'; // It'll remove the `-test_version` bit const userLanguage = 'en'; const fetchInterval = 2000; const getDriver = () => new NewsfeedApiDriver(kibanaVersion, userLanguage, fetchInterval); @@ -179,7 +179,7 @@ describe('NewsfeedApiDriver', () => { "error": null, "feedItems": Array [], "hasNew": false, - "kibanaVersion": "test_version", + "kibanaVersion": "99.999.9", } `); }); @@ -227,7 +227,7 @@ describe('NewsfeedApiDriver', () => { }, ], hasNew: true, - kibanaVersion: 'test_version', + kibanaVersion: '99.999.9', }); }); @@ -309,7 +309,7 @@ describe('NewsfeedApiDriver', () => { }, ], hasNew: true, - kibanaVersion: 'test_version', + kibanaVersion: '99.999.9', }); }); @@ -375,7 +375,7 @@ describe('NewsfeedApiDriver', () => { }, ], hasNew: true, - kibanaVersion: 'test_version', + kibanaVersion: '99.999.9', }); }); @@ -405,7 +405,7 @@ describe('NewsfeedApiDriver', () => { "error": null, "feedItems": Array [], "hasNew": false, - "kibanaVersion": "test_version", + "kibanaVersion": "99.999.9", } `); }); @@ -436,7 +436,7 @@ describe('NewsfeedApiDriver', () => { "error": null, "feedItems": Array [], "hasNew": false, - "kibanaVersion": "test_version", + "kibanaVersion": "99.999.9", } `); }); diff --git a/src/plugins/newsfeed/public/lib/api.ts b/src/plugins/newsfeed/public/lib/api.ts index ac38e064d2686..7253c0f3e294c 100644 --- a/src/plugins/newsfeed/public/lib/api.ts +++ b/src/plugins/newsfeed/public/lib/api.ts @@ -32,13 +32,17 @@ import { ApiItem, NewsfeedItem, FetchResult, NewsfeedPluginBrowserConfig } from type ApiConfig = NewsfeedPluginBrowserConfig['service']; export class NewsfeedApiDriver { + private readonly kibanaVersion: string; private readonly loadedTime = moment().utc(); // the date is compared to time in UTC format coming from the service constructor( - private readonly kibanaVersion: string, + kibanaVersion: string, private readonly userLanguage: string, private readonly fetchInterval: number - ) {} + ) { + // The API only accepts versions in the format `X.Y.Z`, so we need to drop the `-SNAPSHOT` or any other label after it + this.kibanaVersion = kibanaVersion.replace(/^(\d+\.\d+\.\d+).*/, '$1'); + } shouldFetch(): boolean { const lastFetchUtc: string | null = sessionStorage.getItem(NEWSFEED_LAST_FETCH_STORAGE_KEY); diff --git a/test/common/fixtures/plugins/newsfeed/server/plugin.ts b/test/common/fixtures/plugins/newsfeed/server/plugin.ts index 60044d4e0b2df..3d64c7ff58e5d 100644 --- a/test/common/fixtures/plugins/newsfeed/server/plugin.ts +++ b/test/common/fixtures/plugins/newsfeed/server/plugin.ts @@ -25,7 +25,7 @@ export class NewsFeedSimulatorPlugin implements Plugin { public setup({ http }: CoreSetup) { const router = http.createRouter(); - const version = this.initializerContext.env.packageInfo.version; + const version = this.initializerContext.env.packageInfo.version.replace('-SNAPSHOT', ''); router.get( {