From 878dd21d220dbf78d0f4643772e29c2994edd113 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Ferna=CC=81ndez=20Haro?= Date: Mon, 11 May 2020 18:15:58 +0100 Subject: [PATCH 1/6] Remove Newsfeed leftovers from the Legacy codebase --- .github/CODEOWNERS | 1 + src/legacy/core_plugins/newsfeed/index.ts | 71 ----------------- src/legacy/core_plugins/newsfeed/package.json | 4 - .../core_plugins/newsfeed/public/index.scss | 3 - .../components/header_alert/_index.scss | 27 ------- .../components/header_alert/header_alert.tsx | 76 ------------------- .../newsfeed/common}/constants.ts | 11 ++- src/plugins/newsfeed/kibana.json | 2 +- .../public/components/flyout_list.tsx | 2 +- src/plugins/newsfeed/public/lib/api.test.ts | 39 +++++----- src/plugins/newsfeed/public/lib/api.ts | 8 +- src/plugins/newsfeed/public/plugin.tsx | 18 ++--- src/plugins/newsfeed/server/config.ts | 43 +++++++++++ src/plugins/newsfeed/server/index.ts | 36 +++++++++ .../{constants.ts => server/plugin.ts} | 12 ++- src/plugins/newsfeed/types.ts | 17 ++--- 16 files changed, 136 insertions(+), 234 deletions(-) delete mode 100644 src/legacy/core_plugins/newsfeed/index.ts delete mode 100644 src/legacy/core_plugins/newsfeed/package.json delete mode 100644 src/legacy/core_plugins/newsfeed/public/index.scss delete mode 100644 src/legacy/core_plugins/newsfeed/public/np_ready/components/header_alert/_index.scss delete mode 100644 src/legacy/core_plugins/newsfeed/public/np_ready/components/header_alert/header_alert.tsx rename src/{legacy/core_plugins/newsfeed => plugins/newsfeed/common}/constants.ts (65%) create mode 100644 src/plugins/newsfeed/server/config.ts create mode 100644 src/plugins/newsfeed/server/index.ts rename src/plugins/newsfeed/{constants.ts => server/plugin.ts} (81%) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 638e86ef375fe..406b75c12b2e0 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -162,6 +162,7 @@ # Pulse /packages/kbn-analytics/ @elastic/pulse /src/legacy/core_plugins/ui_metric/ @elastic/pulse +/src/plugins/newsfeed/ @elastic/pulse /src/plugins/telemetry/ @elastic/pulse /src/plugins/telemetry_collection_manager/ @elastic/pulse /src/plugins/telemetry_management_section/ @elastic/pulse diff --git a/src/legacy/core_plugins/newsfeed/index.ts b/src/legacy/core_plugins/newsfeed/index.ts deleted file mode 100644 index cf8852be09a1e..0000000000000 --- a/src/legacy/core_plugins/newsfeed/index.ts +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import { resolve } from 'path'; -import { LegacyPluginApi, LegacyPluginSpec, ArrayOrItem } from 'src/legacy/plugin_discovery/types'; -import { Legacy } from 'kibana'; -import { NewsfeedPluginInjectedConfig } from '../../../plugins/newsfeed/types'; -import { - PLUGIN_ID, - DEFAULT_SERVICE_URLROOT, - DEV_SERVICE_URLROOT, - DEFAULT_SERVICE_PATH, -} from './constants'; - -// eslint-disable-next-line import/no-default-export -export default function(kibana: LegacyPluginApi): ArrayOrItem { - const pluginSpec: Legacy.PluginSpecOptions = { - id: PLUGIN_ID, - config(Joi: any) { - // NewsfeedPluginInjectedConfig in Joi form - return Joi.object({ - enabled: Joi.boolean().default(true), - service: Joi.object({ - pathTemplate: Joi.string().default(DEFAULT_SERVICE_PATH), - urlRoot: Joi.when('$prod', { - is: true, - then: Joi.string().default(DEFAULT_SERVICE_URLROOT), - otherwise: Joi.string().default(DEV_SERVICE_URLROOT), - }), - }).default(), - defaultLanguage: Joi.string().default('en'), - mainInterval: Joi.number().default(120 * 1000), // (2min) How often to retry failed fetches, and/or check if newsfeed items need to be refreshed from remote - fetchInterval: Joi.number().default(86400 * 1000), // (1day) How often to fetch remote and reset the last fetched time - }).default(); - }, - uiExports: { - styleSheetPaths: resolve(__dirname, 'public/index.scss'), - injectDefaultVars(server): NewsfeedPluginInjectedConfig { - const config = server.config(); - return { - newsfeed: { - service: { - pathTemplate: config.get('newsfeed.service.pathTemplate') as string, - urlRoot: config.get('newsfeed.service.urlRoot') as string, - }, - defaultLanguage: config.get('newsfeed.defaultLanguage') as string, - mainInterval: config.get('newsfeed.mainInterval') as number, - fetchInterval: config.get('newsfeed.fetchInterval') as number, - }, - }; - }, - }, - }; - return new kibana.Plugin(pluginSpec); -} diff --git a/src/legacy/core_plugins/newsfeed/package.json b/src/legacy/core_plugins/newsfeed/package.json deleted file mode 100644 index d4d753f32b0f9..0000000000000 --- a/src/legacy/core_plugins/newsfeed/package.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "name": "newsfeed", - "version": "kibana" -} diff --git a/src/legacy/core_plugins/newsfeed/public/index.scss b/src/legacy/core_plugins/newsfeed/public/index.scss deleted file mode 100644 index a77132379041c..0000000000000 --- a/src/legacy/core_plugins/newsfeed/public/index.scss +++ /dev/null @@ -1,3 +0,0 @@ -@import 'src/legacy/ui/public/styles/styling_constants'; - -@import './np_ready/components/header_alert/_index'; diff --git a/src/legacy/core_plugins/newsfeed/public/np_ready/components/header_alert/_index.scss b/src/legacy/core_plugins/newsfeed/public/np_ready/components/header_alert/_index.scss deleted file mode 100644 index e25dbd25daaf5..0000000000000 --- a/src/legacy/core_plugins/newsfeed/public/np_ready/components/header_alert/_index.scss +++ /dev/null @@ -1,27 +0,0 @@ -@import '@elastic/eui/src/components/header/variables'; - -.kbnNews__flyout { - top: $euiHeaderChildSize + 1px; - height: calc(100% - #{$euiHeaderChildSize}); -} - -.kbnNewsFeed__headerAlert.euiHeaderAlert { - margin-bottom: $euiSizeL; - padding: 0 $euiSizeS $euiSizeL; - border-bottom: $euiBorderThin; - border-top: none; - - .euiHeaderAlert__title { - @include euiTitle('xs'); - margin-bottom: $euiSizeS; - } - - .euiHeaderAlert__text { - @include euiFontSizeS; - margin-bottom: $euiSize; - } - - .euiHeaderAlert__action { - @include euiFontSizeS; - } -} diff --git a/src/legacy/core_plugins/newsfeed/public/np_ready/components/header_alert/header_alert.tsx b/src/legacy/core_plugins/newsfeed/public/np_ready/components/header_alert/header_alert.tsx deleted file mode 100644 index c3c3e4144fca8..0000000000000 --- a/src/legacy/core_plugins/newsfeed/public/np_ready/components/header_alert/header_alert.tsx +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import React from 'react'; -import PropTypes from 'prop-types'; -import classNames from 'classnames'; - -import { EuiFlexGroup, EuiFlexItem, EuiI18n } from '@elastic/eui'; - -interface IEuiHeaderAlertProps { - action: JSX.Element; - className?: string; - date: string; - text: string; - title: string; - badge?: JSX.Element; - rest?: string[]; -} - -export const EuiHeaderAlert = ({ - action, - className, - date, - text, - title, - badge, - ...rest -}: IEuiHeaderAlertProps) => { - const classes = classNames('euiHeaderAlert', 'kbnNewsFeed__headerAlert', className); - - const badgeContent = badge || null; - - return ( - - {(dismiss: any) => ( -
- - -
{date}
-
- {badgeContent} -
- -
{title}
-
{text}
-
{action}
-
- )} -
- ); -}; - -EuiHeaderAlert.propTypes = { - action: PropTypes.node, - className: PropTypes.string, - date: PropTypes.node.isRequired, - text: PropTypes.node, - title: PropTypes.node.isRequired, - badge: PropTypes.node, -}; diff --git a/src/legacy/core_plugins/newsfeed/constants.ts b/src/plugins/newsfeed/common/constants.ts similarity index 65% rename from src/legacy/core_plugins/newsfeed/constants.ts rename to src/plugins/newsfeed/common/constants.ts index 55a0c51c2ac65..6bc95873a342d 100644 --- a/src/legacy/core_plugins/newsfeed/constants.ts +++ b/src/plugins/newsfeed/common/constants.ts @@ -17,7 +17,10 @@ * under the License. */ -export const PLUGIN_ID = 'newsfeed'; -export const DEFAULT_SERVICE_URLROOT = 'https://feeds.elastic.co'; -export const DEV_SERVICE_URLROOT = 'https://feeds-staging.elastic.co'; -export const DEFAULT_SERVICE_PATH = '/kibana/v{VERSION}.json'; +export const NEWSFEED_FALLBACK_LANGUAGE = 'en'; +export const NEWSFEED_LAST_FETCH_STORAGE_KEY = 'newsfeed.lastfetchtime'; +export const NEWSFEED_HASH_SET_STORAGE_KEY = 'newsfeed.hashes'; + +export const NEWSFEED_DEFAULT_SERVICE_BASE_URL = 'https://feeds.elastic.co'; +export const NEWSFEED_DEV_SERVICE_BASE_URL = 'https://feeds-staging.elastic.co'; +export const NEWSFEED_DEFAULT_SERVICE_PATH = '/kibana/v{VERSION}.json'; diff --git a/src/plugins/newsfeed/kibana.json b/src/plugins/newsfeed/kibana.json index 9d49b42424a06..b9f37b67f6921 100644 --- a/src/plugins/newsfeed/kibana.json +++ b/src/plugins/newsfeed/kibana.json @@ -1,6 +1,6 @@ { "id": "newsfeed", "version": "kibana", - "server": false, + "server": true, "ui": true } diff --git a/src/plugins/newsfeed/public/components/flyout_list.tsx b/src/plugins/newsfeed/public/components/flyout_list.tsx index bd554d7d98b7d..6fb5ee673678f 100644 --- a/src/plugins/newsfeed/public/components/flyout_list.tsx +++ b/src/plugins/newsfeed/public/components/flyout_list.tsx @@ -29,9 +29,9 @@ import { EuiButtonEmpty, EuiText, EuiBadge, + EuiHeaderAlert, } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n/react'; -import { EuiHeaderAlert } from '../../../../legacy/core_plugins/newsfeed/public/np_ready/components/header_alert/header_alert'; import { NewsfeedContext } from './newsfeed_header_nav_button'; import { NewsfeedItem } from '../../types'; import { NewsEmptyPrompt } from './empty_news'; diff --git a/src/plugins/newsfeed/public/lib/api.test.ts b/src/plugins/newsfeed/public/lib/api.test.ts index 9d64dd26da047..b77e63512c8c0 100644 --- a/src/plugins/newsfeed/public/lib/api.test.ts +++ b/src/plugins/newsfeed/public/lib/api.test.ts @@ -22,8 +22,11 @@ import { interval, race } from 'rxjs'; import sinon, { stub } from 'sinon'; import moment from 'moment'; import { HttpSetup } from 'src/core/public'; -import { NEWSFEED_HASH_SET_STORAGE_KEY, NEWSFEED_LAST_FETCH_STORAGE_KEY } from '../../constants'; -import { ApiItem, NewsfeedItem, NewsfeedPluginInjectedConfig } from '../../types'; +import { + NEWSFEED_HASH_SET_STORAGE_KEY, + NEWSFEED_LAST_FETCH_STORAGE_KEY, +} from '../../common/constants'; +import { ApiItem, NewsfeedItem, NewsfeedPluginBrowserConfig } from '../../types'; import { NewsfeedApiDriver, getApi } from './api'; const localStorageGet = sinon.stub(); @@ -458,7 +461,7 @@ describe('getApi', () => { } return Promise.reject('wrong args!'); }; - let configMock: NewsfeedPluginInjectedConfig; + let configMock: NewsfeedPluginBrowserConfig; afterEach(() => { jest.resetAllMocks(); @@ -466,15 +469,13 @@ describe('getApi', () => { beforeEach(() => { configMock = { - newsfeed: { - service: { - urlRoot: 'http://fakenews.co', - pathTemplate: '/kibana-test/v{VERSION}.json', - }, - defaultLanguage: 'en', - mainInterval: 86400000, - fetchInterval: 86400000, + service: { + urlRoot: 'http://fakenews.co', + pathTemplate: '/kibana-test/v{VERSION}.json', }, + defaultLanguage: 'en', + mainInterval: 86400000, + fetchInterval: 86400000, }; httpMock = ({ fetch: mockHttpGet, @@ -483,7 +484,7 @@ describe('getApi', () => { it('creates a result', done => { mockHttpGet.mockImplementationOnce(() => Promise.resolve({ items: [] })); - getApi(httpMock, configMock.newsfeed, '6.8.2').subscribe(result => { + getApi(httpMock, configMock, '6.8.2').subscribe(result => { expect(result).toMatchInlineSnapshot(` Object { "error": null, @@ -528,7 +529,7 @@ describe('getApi', () => { mockHttpGet.mockImplementationOnce(getHttpMockWithItems(mockApiItems)); - getApi(httpMock, configMock.newsfeed, '6.8.2').subscribe(result => { + getApi(httpMock, configMock, '6.8.2').subscribe(result => { expect(result).toMatchInlineSnapshot(` Object { "error": null, @@ -568,7 +569,7 @@ describe('getApi', () => { }, ]; mockHttpGet.mockImplementationOnce(getHttpMockWithItems(mockApiItems)); - getApi(httpMock, configMock.newsfeed, '6.8.2').subscribe(result => { + getApi(httpMock, configMock, '6.8.2').subscribe(result => { expect(result).toMatchInlineSnapshot(` Object { "error": null, @@ -595,7 +596,7 @@ describe('getApi', () => { it('forwards an error', done => { mockHttpGet.mockImplementationOnce((arg1, arg2) => Promise.reject('sorry, try again later!')); - getApi(httpMock, configMock.newsfeed, '6.8.2').subscribe(result => { + getApi(httpMock, configMock, '6.8.2').subscribe(result => { expect(result).toMatchInlineSnapshot(` Object { "error": "sorry, try again later!", @@ -623,14 +624,14 @@ describe('getApi', () => { ]; it("retries until fetch doesn't error", done => { - configMock.newsfeed.mainInterval = 10; // fast retry for testing + configMock.mainInterval = 10; // fast retry for testing mockHttpGet .mockImplementationOnce(() => Promise.reject('Sorry, try again later!')) .mockImplementationOnce(() => Promise.reject('Sorry, internal server error!')) .mockImplementationOnce(() => Promise.reject("Sorry, it's too cold to go outside!")) .mockImplementationOnce(getHttpMockWithItems(successItems)); - getApi(httpMock, configMock.newsfeed, '6.8.2') + getApi(httpMock, configMock, '6.8.2') .pipe(take(4), toArray()) .subscribe(result => { expect(result).toMatchInlineSnapshot(` @@ -677,13 +678,13 @@ describe('getApi', () => { }); it("doesn't retry if fetch succeeds", done => { - configMock.newsfeed.mainInterval = 10; // fast retry for testing + configMock.mainInterval = 10; // fast retry for testing mockHttpGet.mockImplementation(getHttpMockWithItems(successItems)); const timeout$ = interval(1000); // lets us capture some results after a short time let timesFetched = 0; - const get$ = getApi(httpMock, configMock.newsfeed, '6.8.2').pipe( + const get$ = getApi(httpMock, configMock, '6.8.2').pipe( tap(() => { timesFetched++; }) diff --git a/src/plugins/newsfeed/public/lib/api.ts b/src/plugins/newsfeed/public/lib/api.ts index bfeff4aa3e37b..5088ddaec81ea 100644 --- a/src/plugins/newsfeed/public/lib/api.ts +++ b/src/plugins/newsfeed/public/lib/api.ts @@ -26,10 +26,10 @@ import { NEWSFEED_FALLBACK_LANGUAGE, NEWSFEED_LAST_FETCH_STORAGE_KEY, NEWSFEED_HASH_SET_STORAGE_KEY, -} from '../../constants'; -import { NewsfeedPluginInjectedConfig, ApiItem, NewsfeedItem, FetchResult } from '../../types'; +} from '../../common/constants'; +import { ApiItem, NewsfeedItem, FetchResult, NewsfeedPluginBrowserConfig } from '../../types'; -type ApiConfig = NewsfeedPluginInjectedConfig['newsfeed']['service']; +type ApiConfig = NewsfeedPluginBrowserConfig['service']; export class NewsfeedApiDriver { private readonly loadedTime = moment().utc(); // the date is compared to time in UTC format coming from the service @@ -167,7 +167,7 @@ export class NewsfeedApiDriver { */ export function getApi( http: HttpSetup, - config: NewsfeedPluginInjectedConfig['newsfeed'], + config: NewsfeedPluginBrowserConfig, kibanaVersion: string ): Rx.Observable { const userLanguage = i18n.getLocale() || config.defaultLanguage; diff --git a/src/plugins/newsfeed/public/plugin.tsx b/src/plugins/newsfeed/public/plugin.tsx index d21cf75a1a65e..a1b2b265853e0 100644 --- a/src/plugins/newsfeed/public/plugin.tsx +++ b/src/plugins/newsfeed/public/plugin.tsx @@ -23,7 +23,7 @@ import ReactDOM from 'react-dom'; import React from 'react'; import { I18nProvider } from '@kbn/i18n/react'; import { PluginInitializerContext, CoreSetup, CoreStart, Plugin } from 'src/core/public'; -import { FetchResult, NewsfeedPluginInjectedConfig } from '../types'; +import { NewsfeedPluginBrowserConfig } from '../types'; import { NewsfeedNavButton, NewsfeedApiFetchResult } from './components/newsfeed_header_nav_button'; import { getApi } from './lib/api'; @@ -32,9 +32,11 @@ export type Start = object; export class NewsfeedPublicPlugin implements Plugin { private readonly kibanaVersion: string; + private readonly config: NewsfeedPluginBrowserConfig; private readonly stop$ = new Rx.ReplaySubject(1); - constructor(initializerContext: PluginInitializerContext) { + constructor(initializerContext: PluginInitializerContext) { + this.config = initializerContext.config.get(); this.kibanaVersion = initializerContext.env.packageInfo.version; } @@ -57,16 +59,8 @@ export class NewsfeedPublicPlugin implements Plugin { } private fetchNewsfeed(core: CoreStart) { - const { http, injectedMetadata } = core; - const config = injectedMetadata.getInjectedVar('newsfeed') as - | NewsfeedPluginInjectedConfig['newsfeed'] - | undefined; - - if (!config) { - // running in new platform, injected metadata not available - return new Rx.Observable(); - } - return getApi(http, config, this.kibanaVersion).pipe( + const { http } = core; + return getApi(http, this.config, this.kibanaVersion).pipe( takeUntil(this.stop$), // stop the interval when stop method is called catchError(() => Rx.of(null)) // do not throw error ); diff --git a/src/plugins/newsfeed/server/config.ts b/src/plugins/newsfeed/server/config.ts new file mode 100644 index 0000000000000..234bc2d83c467 --- /dev/null +++ b/src/plugins/newsfeed/server/config.ts @@ -0,0 +1,43 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { schema, TypeOf } from '@kbn/config-schema'; +import { + NEWSFEED_DEFAULT_SERVICE_PATH, + NEWSFEED_DEFAULT_SERVICE_BASE_URL, + NEWSFEED_DEV_SERVICE_BASE_URL, +} from '../common/constants'; + +export const configSchema = schema.object({ + enabled: schema.boolean({ defaultValue: true }), + service: schema.object({ + pathTemplate: schema.string({ defaultValue: NEWSFEED_DEFAULT_SERVICE_PATH }), + urlRoot: schema.conditional( + schema.contextRef('prod'), + schema.literal(true), // Point to staging if it's not a production release + schema.string({ defaultValue: NEWSFEED_DEFAULT_SERVICE_BASE_URL }), + schema.string({ defaultValue: NEWSFEED_DEV_SERVICE_BASE_URL }) + ), + }), + defaultLanguage: schema.string({ defaultValue: 'en' }), + mainInterval: schema.number({ defaultValue: 120 * 1000 }), // (2min) How often to retry failed fetches, and/or check if newsfeed items need to be refreshed from remote + fetchInterval: schema.number({ defaultValue: 86400 * 1000 }), // (1day) How often to fetch remote and reset the last fetched time +}); + +export type NewsfeedConfigType = TypeOf; diff --git a/src/plugins/newsfeed/server/index.ts b/src/plugins/newsfeed/server/index.ts new file mode 100644 index 0000000000000..71852f38d41d3 --- /dev/null +++ b/src/plugins/newsfeed/server/index.ts @@ -0,0 +1,36 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { PluginConfigDescriptor } from 'kibana/server'; +import { NewsfeedPlugin } from './plugin'; +import { configSchema, NewsfeedConfigType } from './config'; + +export const config: PluginConfigDescriptor = { + schema: configSchema, + exposeToBrowser: { + service: true, + defaultLanguage: true, + mainInterval: true, + fetchInterval: true, + }, +}; + +export function plugin() { + return new NewsfeedPlugin(); +} diff --git a/src/plugins/newsfeed/constants.ts b/src/plugins/newsfeed/server/plugin.ts similarity index 81% rename from src/plugins/newsfeed/constants.ts rename to src/plugins/newsfeed/server/plugin.ts index ddcbbb6cb1dbe..60b69b3977bf5 100644 --- a/src/plugins/newsfeed/constants.ts +++ b/src/plugins/newsfeed/server/plugin.ts @@ -17,6 +17,12 @@ * under the License. */ -export const NEWSFEED_FALLBACK_LANGUAGE = 'en'; -export const NEWSFEED_LAST_FETCH_STORAGE_KEY = 'newsfeed.lastfetchtime'; -export const NEWSFEED_HASH_SET_STORAGE_KEY = 'newsfeed.hashes'; +import { Plugin } from 'kibana/server'; + +export class NewsfeedPlugin implements Plugin { + public setup() {} + + public start() {} + + public stop() {} +} diff --git a/src/plugins/newsfeed/types.ts b/src/plugins/newsfeed/types.ts index 78485c6ee4f59..8eb5af1bf8f4b 100644 --- a/src/plugins/newsfeed/types.ts +++ b/src/plugins/newsfeed/types.ts @@ -19,16 +19,15 @@ import { Moment } from 'moment'; -export interface NewsfeedPluginInjectedConfig { - newsfeed: { - service: { - urlRoot: string; - pathTemplate: string; - }; - defaultLanguage: string; - mainInterval: number; // how often to check last updated time - fetchInterval: number; // how often to fetch remote service and set last updated +// Ideally, we may want to obtain the type from the configSchema and exposeToBrowser keys... +export interface NewsfeedPluginBrowserConfig { + service: { + urlRoot: string; + pathTemplate: string; }; + defaultLanguage: string; + mainInterval: number; // how often to check last updated time + fetchInterval: number; // how often to fetch remote service and set last updated } export interface ApiItem { From 7eb688dfb52b103a4f6188370d9593ec2f4a7529 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Ferna=CC=81ndez=20Haro?= Date: Tue, 12 May 2020 15:19:05 +0100 Subject: [PATCH 2/6] Migrate Newsfeed test plugin (#66059) --- .../fixtures/plugins/newsfeed/kibana.json | 6 + .../plugins/newsfeed/newsfeed_simulation.ts | 126 ------------------ .../fixtures/plugins/newsfeed/package.json | 7 - .../plugins/newsfeed/{ => server}/index.ts | 15 +-- .../plugins/newsfeed/server/plugin.ts | 96 +++++++++++++ 5 files changed, 105 insertions(+), 145 deletions(-) create mode 100644 test/common/fixtures/plugins/newsfeed/kibana.json delete mode 100644 test/common/fixtures/plugins/newsfeed/newsfeed_simulation.ts delete mode 100644 test/common/fixtures/plugins/newsfeed/package.json rename test/common/fixtures/plugins/newsfeed/{ => server}/index.ts (67%) create mode 100644 test/common/fixtures/plugins/newsfeed/server/plugin.ts diff --git a/test/common/fixtures/plugins/newsfeed/kibana.json b/test/common/fixtures/plugins/newsfeed/kibana.json new file mode 100644 index 0000000000000..110b53fc6b2e9 --- /dev/null +++ b/test/common/fixtures/plugins/newsfeed/kibana.json @@ -0,0 +1,6 @@ +{ + "id": "newsfeed-fixtures", + "version": "kibana", + "server": true, + "ui": false +} diff --git a/test/common/fixtures/plugins/newsfeed/newsfeed_simulation.ts b/test/common/fixtures/plugins/newsfeed/newsfeed_simulation.ts deleted file mode 100644 index 5aa44b48f9d59..0000000000000 --- a/test/common/fixtures/plugins/newsfeed/newsfeed_simulation.ts +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Licensed to Elasticsearch B.V. under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch B.V. licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import Hapi from 'hapi'; - -interface WebhookRequest extends Hapi.Request { - payload: string; -} - -export async function initPlugin(server: Hapi.Server, path: string) { - server.route({ - method: ['GET'], - path: `${path}/kibana/v{version}.json`, - options: { - cors: { - origin: ['*'], - additionalHeaders: [ - 'Sec-Fetch-Mode', - 'Access-Control-Request-Method', - 'Access-Control-Request-Headers', - 'cache-control', - 'x-requested-with', - 'Origin', - 'User-Agent', - 'DNT', - 'content-type', - 'kbn-version', - ], - }, - }, - handler: newsfeedHandler as Hapi.Lifecycle.Method, - }); - - server.route({ - method: ['GET'], - path: `${path}/kibana/crash.json`, - options: { - cors: { - origin: ['*'], - additionalHeaders: [ - 'Sec-Fetch-Mode', - 'Access-Control-Request-Method', - 'Access-Control-Request-Headers', - 'cache-control', - 'x-requested-with', - 'Origin', - 'User-Agent', - 'DNT', - 'content-type', - 'kbn-version', - ], - }, - }, - handler() { - throw new Error('Internal server error'); - }, - }); -} - -function newsfeedHandler(request: WebhookRequest, h: any) { - return htmlResponse(h, 200, JSON.stringify(mockNewsfeed(request.params.version))); -} - -const mockNewsfeed = (version: string) => ({ - items: [ - { - title: { en: `You are functionally testing the newsfeed widget with fixtures!` }, - description: { en: 'See test/common/fixtures/plugins/newsfeed/newsfeed_simulation' }, - link_text: { en: 'Generic feed-viewer could go here' }, - link_url: { en: 'https://feeds.elastic.co' }, - languages: null, - badge: null, - image_url: null, - publish_on: '2019-06-21T00:00:00', - expire_on: '2040-01-31T00:00:00', - hash: '39ca7d409c7eb25f4c69a5a6a11309b2f5ced7ca3f9b3a0109517126e0fd91ca', - }, - { - title: { en: 'Staging too!' }, - description: { en: 'Hello world' }, - link_text: { en: 'Generic feed-viewer could go here' }, - link_url: { en: 'https://feeds-staging.elastic.co' }, - languages: null, - badge: null, - image_url: null, - publish_on: '2019-06-21T00:00:00', - expire_on: '2040-01-31T00:00:00', - hash: 'db445c9443eb50ea2eb15f20edf89cf0f7dac2b058b11cafc2c8c288b6e4ce2a', - }, - { - title: { en: 'This item is expired!' }, - description: { en: 'This should not show up.' }, - link_text: { en: 'Generic feed-viewer could go here' }, - link_url: { en: 'https://feeds-staging.elastic.co' }, - languages: null, - badge: null, - image_url: null, - publish_on: '2019-06-21T00:00:00', - expire_on: '2019-12-31T00:00:00', - hash: 'db445c9443eb50ea2eb15f20edf89cf0f7dac2b058b11cafc2c8c288b6e4ce2a', - }, - ], -}); - -function htmlResponse(h: any, code: number, text: string) { - return h - .response(text) - .type('application/json') - .code(code); -} diff --git a/test/common/fixtures/plugins/newsfeed/package.json b/test/common/fixtures/plugins/newsfeed/package.json deleted file mode 100644 index 5291b1031b0a9..0000000000000 --- a/test/common/fixtures/plugins/newsfeed/package.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "name": "newsfeed-fixtures", - "version": "0.0.0", - "kibana": { - "version": "kibana" - } -} diff --git a/test/common/fixtures/plugins/newsfeed/index.ts b/test/common/fixtures/plugins/newsfeed/server/index.ts similarity index 67% rename from test/common/fixtures/plugins/newsfeed/index.ts rename to test/common/fixtures/plugins/newsfeed/server/index.ts index beee9bb5c6069..4974669e6a30c 100644 --- a/test/common/fixtures/plugins/newsfeed/index.ts +++ b/test/common/fixtures/plugins/newsfeed/server/index.ts @@ -17,17 +17,8 @@ * under the License. */ -import Hapi from 'hapi'; -import { initPlugin as initNewsfeed } from './newsfeed_simulation'; +import { NewsFeedSimulatorPlugin } from './plugin'; -const NAME = 'newsfeed-FTS-external-service-simulators'; - -// eslint-disable-next-line import/no-default-export -export default function(kibana: any) { - return new kibana.Plugin({ - name: NAME, - init: (server: Hapi.Server) => { - initNewsfeed(server, `/api/_${NAME}`); - }, - }); +export function plugin() { + return new NewsFeedSimulatorPlugin(); } diff --git a/test/common/fixtures/plugins/newsfeed/server/plugin.ts b/test/common/fixtures/plugins/newsfeed/server/plugin.ts new file mode 100644 index 0000000000000..d4b84e7e92dde --- /dev/null +++ b/test/common/fixtures/plugins/newsfeed/server/plugin.ts @@ -0,0 +1,96 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { schema } from '@kbn/config-schema'; +import { CoreSetup, Plugin } from 'kibana/server'; + +const PATH = '/api/_newsfeed-FTS-external-service-simulators'; + +export class NewsFeedSimulatorPlugin implements Plugin { + public setup({ http }: CoreSetup) { + const router = http.createRouter(); + + router.get( + { + path: `${PATH}/kibana/v{version}.json`, + validate: { params: schema.object({ version: schema.string() }) }, + options: { authRequired: false }, + }, + (context, req, res) => { + return res.ok({ body: this.mockNewsfeed(req.params.version) }); + } + ); + + router.get( + { + path: `${PATH}/kibana/crash.json`, + validate: false, + options: { authRequired: false }, + }, + (context, req, res) => { + return res.internalError({ body: new Error('Internal server error') }); + } + ); + } + + public start() {} + + private mockNewsfeed(version: string) { + return { + items: [ + { + title: { en: `You are functionally testing the newsfeed widget with fixtures!` }, + description: { en: 'See test/common/fixtures/plugins/newsfeed/newsfeed_simulation' }, + link_text: { en: 'Generic feed-viewer could go here' }, + link_url: { en: 'https://feeds.elastic.co' }, + languages: null, + badge: null, + image_url: null, + publish_on: '2019-06-21T00:00:00', + expire_on: '2040-01-31T00:00:00', + hash: '39ca7d409c7eb25f4c69a5a6a11309b2f5ced7ca3f9b3a0109517126e0fd91ca', + }, + { + title: { en: 'Staging too!' }, + description: { en: 'Hello world' }, + link_text: { en: 'Generic feed-viewer could go here' }, + link_url: { en: 'https://feeds-staging.elastic.co' }, + languages: null, + badge: null, + image_url: null, + publish_on: '2019-06-21T00:00:00', + expire_on: '2040-01-31T00:00:00', + hash: 'db445c9443eb50ea2eb15f20edf89cf0f7dac2b058b11cafc2c8c288b6e4ce2a', + }, + { + title: { en: 'This item is expired!' }, + description: { en: 'This should not show up.' }, + link_text: { en: 'Generic feed-viewer could go here' }, + link_url: { en: 'https://feeds-staging.elastic.co' }, + languages: null, + badge: null, + image_url: null, + publish_on: '2019-06-21T00:00:00', + expire_on: '2019-12-31T00:00:00', + hash: 'db445c9443eb50ea2eb15f20edf89cf0f7dac2b058b11cafc2c8c288b6e4ce2a', + }, + ], + }; + } +} From fe17e8eb554262c7e3a7eb8c22c21d760bdd2504 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Ferna=CC=81ndez=20Haro?= Date: Tue, 12 May 2020 21:27:11 +0100 Subject: [PATCH 3/6] PR feedback --- src/plugins/newsfeed/public/lib/api.test.ts | 1 - src/plugins/newsfeed/public/lib/api.ts | 2 +- src/plugins/newsfeed/server/config.ts | 7 ++++--- src/plugins/newsfeed/server/index.ts | 2 +- src/plugins/newsfeed/types.ts | 1 - test/common/fixtures/plugins/newsfeed/server/plugin.ts | 10 ++++------ 6 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/plugins/newsfeed/public/lib/api.test.ts b/src/plugins/newsfeed/public/lib/api.test.ts index b77e63512c8c0..9c61327c974fa 100644 --- a/src/plugins/newsfeed/public/lib/api.test.ts +++ b/src/plugins/newsfeed/public/lib/api.test.ts @@ -473,7 +473,6 @@ describe('getApi', () => { urlRoot: 'http://fakenews.co', pathTemplate: '/kibana-test/v{VERSION}.json', }, - defaultLanguage: 'en', mainInterval: 86400000, fetchInterval: 86400000, }; diff --git a/src/plugins/newsfeed/public/lib/api.ts b/src/plugins/newsfeed/public/lib/api.ts index 5088ddaec81ea..c934982e29265 100644 --- a/src/plugins/newsfeed/public/lib/api.ts +++ b/src/plugins/newsfeed/public/lib/api.ts @@ -170,7 +170,7 @@ export function getApi( config: NewsfeedPluginBrowserConfig, kibanaVersion: string ): Rx.Observable { - const userLanguage = i18n.getLocale() || config.defaultLanguage; + const userLanguage = i18n.getLocale(); const fetchInterval = config.fetchInterval; const driver = new NewsfeedApiDriver(kibanaVersion, userLanguage, fetchInterval); diff --git a/src/plugins/newsfeed/server/config.ts b/src/plugins/newsfeed/server/config.ts index 234bc2d83c467..e42066f4d20ec 100644 --- a/src/plugins/newsfeed/server/config.ts +++ b/src/plugins/newsfeed/server/config.ts @@ -22,6 +22,7 @@ import { NEWSFEED_DEFAULT_SERVICE_PATH, NEWSFEED_DEFAULT_SERVICE_BASE_URL, NEWSFEED_DEV_SERVICE_BASE_URL, + NEWSFEED_FALLBACK_LANGUAGE, } from '../common/constants'; export const configSchema = schema.object({ @@ -35,9 +36,9 @@ export const configSchema = schema.object({ schema.string({ defaultValue: NEWSFEED_DEV_SERVICE_BASE_URL }) ), }), - defaultLanguage: schema.string({ defaultValue: 'en' }), - mainInterval: schema.number({ defaultValue: 120 * 1000 }), // (2min) How often to retry failed fetches, and/or check if newsfeed items need to be refreshed from remote - fetchInterval: schema.number({ defaultValue: 86400 * 1000 }), // (1day) How often to fetch remote and reset the last fetched time + defaultLanguage: schema.string({ defaultValue: NEWSFEED_FALLBACK_LANGUAGE }), // TODO: Deprecate since no longer used + mainInterval: schema.duration({ defaultValue: '2m' }), // (2min) How often to retry failed fetches, and/or check if newsfeed items need to be refreshed from remote + fetchInterval: schema.duration({ defaultValue: '1d' }), // (1day) How often to fetch remote and reset the last fetched time }); export type NewsfeedConfigType = TypeOf; diff --git a/src/plugins/newsfeed/server/index.ts b/src/plugins/newsfeed/server/index.ts index 71852f38d41d3..e62bb95df8862 100644 --- a/src/plugins/newsfeed/server/index.ts +++ b/src/plugins/newsfeed/server/index.ts @@ -25,10 +25,10 @@ export const config: PluginConfigDescriptor = { schema: configSchema, exposeToBrowser: { service: true, - defaultLanguage: true, mainInterval: true, fetchInterval: true, }, + deprecations: ({ unused }) => [unused('defaultLanguage')], }; export function plugin() { diff --git a/src/plugins/newsfeed/types.ts b/src/plugins/newsfeed/types.ts index 8eb5af1bf8f4b..113c3e7836b56 100644 --- a/src/plugins/newsfeed/types.ts +++ b/src/plugins/newsfeed/types.ts @@ -25,7 +25,6 @@ export interface NewsfeedPluginBrowserConfig { urlRoot: string; pathTemplate: string; }; - defaultLanguage: string; mainInterval: number; // how often to check last updated time fetchInterval: number; // how often to fetch remote service and set last updated } diff --git a/test/common/fixtures/plugins/newsfeed/server/plugin.ts b/test/common/fixtures/plugins/newsfeed/server/plugin.ts index d4b84e7e92dde..470db4e90c8fb 100644 --- a/test/common/fixtures/plugins/newsfeed/server/plugin.ts +++ b/test/common/fixtures/plugins/newsfeed/server/plugin.ts @@ -20,26 +20,24 @@ import { schema } from '@kbn/config-schema'; import { CoreSetup, Plugin } from 'kibana/server'; -const PATH = '/api/_newsfeed-FTS-external-service-simulators'; - export class NewsFeedSimulatorPlugin implements Plugin { public setup({ http }: CoreSetup) { const router = http.createRouter(); router.get( { - path: `${PATH}/kibana/v{version}.json`, + path: '/api/_newsfeed-FTS-external-service-simulators/kibana/v{version}.json', validate: { params: schema.object({ version: schema.string() }) }, options: { authRequired: false }, }, (context, req, res) => { - return res.ok({ body: this.mockNewsfeed(req.params.version) }); + return res.ok({ body: this.mockNewsfeed() }); } ); router.get( { - path: `${PATH}/kibana/crash.json`, + path: '/api/_newsfeed-FTS-external-service-simulators/kibana/crash.json', validate: false, options: { authRequired: false }, }, @@ -51,7 +49,7 @@ export class NewsFeedSimulatorPlugin implements Plugin { public start() {} - private mockNewsfeed(version: string) { + private mockNewsfeed() { return { items: [ { From d4a8d0b95d46bbb4aade92f21da80c68e757f258 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Ferna=CC=81ndez=20Haro?= Date: Wed, 13 May 2020 09:39:15 +0100 Subject: [PATCH 4/6] schema.duration returns Duration type :) --- .../newsfeed/public/components/flyout_list.tsx | 2 +- .../public/components/newsfeed_header_nav_button.tsx | 2 +- src/plugins/newsfeed/public/lib/api.test.ts | 2 +- src/plugins/newsfeed/public/lib/api.ts | 6 +++--- src/plugins/newsfeed/public/plugin.tsx | 11 +++++++++-- src/plugins/newsfeed/{ => public}/types.ts | 6 +++--- 6 files changed, 18 insertions(+), 11 deletions(-) rename src/plugins/newsfeed/{ => public}/types.ts (89%) diff --git a/src/plugins/newsfeed/public/components/flyout_list.tsx b/src/plugins/newsfeed/public/components/flyout_list.tsx index 6fb5ee673678f..6e9444c950107 100644 --- a/src/plugins/newsfeed/public/components/flyout_list.tsx +++ b/src/plugins/newsfeed/public/components/flyout_list.tsx @@ -33,7 +33,7 @@ import { } from '@elastic/eui'; import { FormattedMessage } from '@kbn/i18n/react'; import { NewsfeedContext } from './newsfeed_header_nav_button'; -import { NewsfeedItem } from '../../types'; +import { NewsfeedItem } from '../types'; import { NewsEmptyPrompt } from './empty_news'; import { NewsLoadingPrompt } from './loading_news'; diff --git a/src/plugins/newsfeed/public/components/newsfeed_header_nav_button.tsx b/src/plugins/newsfeed/public/components/newsfeed_header_nav_button.tsx index da042f0fce7b6..fd938e9071074 100644 --- a/src/plugins/newsfeed/public/components/newsfeed_header_nav_button.tsx +++ b/src/plugins/newsfeed/public/components/newsfeed_header_nav_button.tsx @@ -21,7 +21,7 @@ import React, { useState, Fragment, useEffect } from 'react'; import * as Rx from 'rxjs'; import { EuiHeaderSectionItemButton, EuiIcon, EuiNotificationBadge } from '@elastic/eui'; import { NewsfeedFlyout } from './flyout_list'; -import { FetchResult } from '../../types'; +import { FetchResult } from '../types'; export interface INewsfeedContext { setFlyoutVisible: React.Dispatch>; diff --git a/src/plugins/newsfeed/public/lib/api.test.ts b/src/plugins/newsfeed/public/lib/api.test.ts index 9c61327c974fa..df0f796cd4c3e 100644 --- a/src/plugins/newsfeed/public/lib/api.test.ts +++ b/src/plugins/newsfeed/public/lib/api.test.ts @@ -26,7 +26,7 @@ import { NEWSFEED_HASH_SET_STORAGE_KEY, NEWSFEED_LAST_FETCH_STORAGE_KEY, } from '../../common/constants'; -import { ApiItem, NewsfeedItem, NewsfeedPluginBrowserConfig } from '../../types'; +import { ApiItem, NewsfeedItem, NewsfeedPluginBrowserConfig } from '../types'; import { NewsfeedApiDriver, getApi } from './api'; const localStorageGet = sinon.stub(); diff --git a/src/plugins/newsfeed/public/lib/api.ts b/src/plugins/newsfeed/public/lib/api.ts index c934982e29265..2924f3d340662 100644 --- a/src/plugins/newsfeed/public/lib/api.ts +++ b/src/plugins/newsfeed/public/lib/api.ts @@ -27,7 +27,7 @@ import { NEWSFEED_LAST_FETCH_STORAGE_KEY, NEWSFEED_HASH_SET_STORAGE_KEY, } from '../../common/constants'; -import { ApiItem, NewsfeedItem, FetchResult, NewsfeedPluginBrowserConfig } from '../../types'; +import { ApiItem, NewsfeedItem, FetchResult, NewsfeedPluginBrowserConfig } from '../types'; type ApiConfig = NewsfeedPluginBrowserConfig['service']; @@ -171,10 +171,10 @@ export function getApi( kibanaVersion: string ): Rx.Observable { const userLanguage = i18n.getLocale(); - const fetchInterval = config.fetchInterval; + const fetchInterval = config.fetchInterval.asMilliseconds(); const driver = new NewsfeedApiDriver(kibanaVersion, userLanguage, fetchInterval); - return Rx.timer(0, config.mainInterval).pipe( + return Rx.timer(0, config.mainInterval.asMilliseconds()).pipe( filter(() => driver.shouldFetch()), mergeMap(() => driver.fetchNewsfeedItems(http, config.service).pipe( diff --git a/src/plugins/newsfeed/public/plugin.tsx b/src/plugins/newsfeed/public/plugin.tsx index a1b2b265853e0..e61070ab184f3 100644 --- a/src/plugins/newsfeed/public/plugin.tsx +++ b/src/plugins/newsfeed/public/plugin.tsx @@ -21,9 +21,10 @@ import * as Rx from 'rxjs'; import { catchError, takeUntil } from 'rxjs/operators'; import ReactDOM from 'react-dom'; import React from 'react'; +import moment from 'moment'; import { I18nProvider } from '@kbn/i18n/react'; import { PluginInitializerContext, CoreSetup, CoreStart, Plugin } from 'src/core/public'; -import { NewsfeedPluginBrowserConfig } from '../types'; +import { NewsfeedPluginBrowserConfig } from './types'; import { NewsfeedNavButton, NewsfeedApiFetchResult } from './components/newsfeed_header_nav_button'; import { getApi } from './lib/api'; @@ -36,8 +37,14 @@ export class NewsfeedPublicPlugin implements Plugin { private readonly stop$ = new Rx.ReplaySubject(1); constructor(initializerContext: PluginInitializerContext) { - this.config = initializerContext.config.get(); this.kibanaVersion = initializerContext.env.packageInfo.version; + const config = initializerContext.config.get(); + this.config = Object.freeze({ + ...config, + // We need wrap them in moment.duration because exposeToBrowser stringifies it. + mainInterval: moment.duration(config.mainInterval), + fetchInterval: moment.duration(config.fetchInterval), + }); } public setup(core: CoreSetup): Setup { diff --git a/src/plugins/newsfeed/types.ts b/src/plugins/newsfeed/public/types.ts similarity index 89% rename from src/plugins/newsfeed/types.ts rename to src/plugins/newsfeed/public/types.ts index 113c3e7836b56..a9dab6791d7aa 100644 --- a/src/plugins/newsfeed/types.ts +++ b/src/plugins/newsfeed/public/types.ts @@ -17,7 +17,7 @@ * under the License. */ -import { Moment } from 'moment'; +import { Duration, Moment } from 'moment'; // Ideally, we may want to obtain the type from the configSchema and exposeToBrowser keys... export interface NewsfeedPluginBrowserConfig { @@ -25,8 +25,8 @@ export interface NewsfeedPluginBrowserConfig { urlRoot: string; pathTemplate: string; }; - mainInterval: number; // how often to check last updated time - fetchInterval: number; // how often to fetch remote service and set last updated + mainInterval: Duration; // how often to check last updated time + fetchInterval: Duration; // how often to fetch remote service and set last updated } export interface ApiItem { From a2391428c40baf40be5418491216aae689c5b427 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Ferna=CC=81ndez=20Haro?= Date: Wed, 13 May 2020 10:08:53 +0100 Subject: [PATCH 5/6] Newsfeed test plugin: requests should match the current version only --- test/common/fixtures/plugins/newsfeed/server/index.ts | 5 +++-- test/common/fixtures/plugins/newsfeed/server/plugin.ts | 9 ++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/test/common/fixtures/plugins/newsfeed/server/index.ts b/test/common/fixtures/plugins/newsfeed/server/index.ts index 4974669e6a30c..979aadfe86606 100644 --- a/test/common/fixtures/plugins/newsfeed/server/index.ts +++ b/test/common/fixtures/plugins/newsfeed/server/index.ts @@ -17,8 +17,9 @@ * under the License. */ +import { PluginInitializerContext } from 'kibana/public'; import { NewsFeedSimulatorPlugin } from './plugin'; -export function plugin() { - return new NewsFeedSimulatorPlugin(); +export function plugin(initializerContext: PluginInitializerContext) { + return new NewsFeedSimulatorPlugin(initializerContext); } diff --git a/test/common/fixtures/plugins/newsfeed/server/plugin.ts b/test/common/fixtures/plugins/newsfeed/server/plugin.ts index 470db4e90c8fb..60044d4e0b2df 100644 --- a/test/common/fixtures/plugins/newsfeed/server/plugin.ts +++ b/test/common/fixtures/plugins/newsfeed/server/plugin.ts @@ -17,17 +17,20 @@ * under the License. */ -import { schema } from '@kbn/config-schema'; import { CoreSetup, Plugin } from 'kibana/server'; +import { PluginInitializerContext } from 'kibana/public'; export class NewsFeedSimulatorPlugin implements Plugin { + constructor(private readonly initializerContext: PluginInitializerContext) {} + public setup({ http }: CoreSetup) { const router = http.createRouter(); + const version = this.initializerContext.env.packageInfo.version; router.get( { - path: '/api/_newsfeed-FTS-external-service-simulators/kibana/v{version}.json', - validate: { params: schema.object({ version: schema.string() }) }, + path: `/api/_newsfeed-FTS-external-service-simulators/kibana/v${version}.json`, + validate: false, options: { authRequired: false }, }, (context, req, res) => { From 37f4fac579f79ee6005d05f387ab1ff3d37e8340 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Ferna=CC=81ndez=20Haro?= Date: Wed, 13 May 2020 14:17:08 +0100 Subject: [PATCH 6/6] Fix tests to match new types --- src/plugins/newsfeed/public/lib/api.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/plugins/newsfeed/public/lib/api.test.ts b/src/plugins/newsfeed/public/lib/api.test.ts index df0f796cd4c3e..5db578f1bd4e9 100644 --- a/src/plugins/newsfeed/public/lib/api.test.ts +++ b/src/plugins/newsfeed/public/lib/api.test.ts @@ -473,8 +473,8 @@ describe('getApi', () => { urlRoot: 'http://fakenews.co', pathTemplate: '/kibana-test/v{VERSION}.json', }, - mainInterval: 86400000, - fetchInterval: 86400000, + mainInterval: moment.duration(86400000), + fetchInterval: moment.duration(86400000), }; httpMock = ({ fetch: mockHttpGet, @@ -623,7 +623,7 @@ describe('getApi', () => { ]; it("retries until fetch doesn't error", done => { - configMock.mainInterval = 10; // fast retry for testing + configMock.mainInterval = moment.duration(10); // fast retry for testing mockHttpGet .mockImplementationOnce(() => Promise.reject('Sorry, try again later!')) .mockImplementationOnce(() => Promise.reject('Sorry, internal server error!')) @@ -677,7 +677,7 @@ describe('getApi', () => { }); it("doesn't retry if fetch succeeds", done => { - configMock.mainInterval = 10; // fast retry for testing + configMock.mainInterval = moment.duration(10); // fast retry for testing mockHttpGet.mockImplementation(getHttpMockWithItems(successItems)); const timeout$ = interval(1000); // lets us capture some results after a short time