From 499402062561b8ac1731fa862d8c5a5609f5e8fd Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Wed, 11 Aug 2021 14:26:45 -0400 Subject: [PATCH] [Security Solution, Timelines] Replace more legacy elasticsearch types (#108087) (#108229) * Replace more legacy elasticsearch types * Handle possibly undefined response fields These are both number | undefined, so we default to 0 if we need a value. Fixes the type errors resulting from the previous type changes. Co-authored-by: Ryland Herrick --- .../detection_engine/alerts/use_fetch_ecs_alerts_data.ts | 8 ++++---- .../timelines/public/container/use_update_alerts.ts | 5 +++-- .../public/hooks/use_status_bulk_action_items.tsx | 4 ++-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/alerts/use_fetch_ecs_alerts_data.ts b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/alerts/use_fetch_ecs_alerts_data.ts index 8af47812849248..b082d90fc14889 100644 --- a/x-pack/plugins/security_solution/public/detections/containers/detection_engine/alerts/use_fetch_ecs_alerts_data.ts +++ b/x-pack/plugins/security_solution/public/detections/containers/detection_engine/alerts/use_fetch_ecs_alerts_data.ts @@ -5,7 +5,7 @@ * 2.0. */ import { useEffect, useState } from 'react'; -import { SearchResponse } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; import { isEmpty } from 'lodash'; import { @@ -37,7 +37,7 @@ export const useFetchEcsAlertsData = ({ try { setIsLoading(true); const alertResponse = await KibanaServices.get().http.fetch< - SearchResponse<{ '@timestamp': string; [key: string]: unknown }> + estypes.SearchResponse<{ '@timestamp': string; [key: string]: unknown }> >(DETECTION_ENGINE_QUERY_SIGNALS_URL, { method: 'POST', body: JSON.stringify(buildAlertsQuery(alertIds ?? [])), @@ -45,10 +45,10 @@ export const useFetchEcsAlertsData = ({ setAlertEcsData( alertResponse?.hits.hits.reduce( - (acc, { _id, _index, _source }) => [ + (acc, { _id, _index, _source = {} }) => [ ...acc, { - ...formatAlertToEcsSignal(_source as {}), + ...formatAlertToEcsSignal(_source), _id, _index, timestamp: _source['@timestamp'], diff --git a/x-pack/plugins/timelines/public/container/use_update_alerts.ts b/x-pack/plugins/timelines/public/container/use_update_alerts.ts index 7576c831554cd3..8e18281e788241 100644 --- a/x-pack/plugins/timelines/public/container/use_update_alerts.ts +++ b/x-pack/plugins/timelines/public/container/use_update_alerts.ts @@ -5,7 +5,8 @@ * 2.0. */ -import { UpdateDocumentByQueryResponse } from 'elasticsearch'; +import type { estypes } from '@elastic/elasticsearch'; + import { useKibana } from '../../../../../src/plugins/kibana_react/public'; import { AlertStatus } from '../../../timelines/common'; @@ -24,7 +25,7 @@ export const useUpdateAlertsStatus = (): { updateAlertStatus: (params: { query: object; status: AlertStatus; - }) => Promise; + }) => Promise; } => { const { http } = useKibana().services; diff --git a/x-pack/plugins/timelines/public/hooks/use_status_bulk_action_items.tsx b/x-pack/plugins/timelines/public/hooks/use_status_bulk_action_items.tsx index 69d7ad36324de7..426884ef8caae0 100644 --- a/x-pack/plugins/timelines/public/hooks/use_status_bulk_action_items.tsx +++ b/x-pack/plugins/timelines/public/hooks/use_status_bulk_action_items.tsx @@ -57,11 +57,11 @@ export const useStatusBulkActionItems = ({ // TODO: Only delete those that were successfully updated from updatedRules setEventsDeleted({ eventIds, isDeleted: true }); - if (response.version_conflicts > 0 && eventIds.length === 1) { + if (response.version_conflicts && eventIds.length === 1) { throw new Error(i18n.BULK_ACTION_FAILED_SINGLE_ALERT); } - onUpdateSuccess(response.updated, response.version_conflicts, status); + onUpdateSuccess(response.updated ?? 0, response.version_conflicts ?? 0, status); } catch (error) { onUpdateFailure(status, error); } finally {