[8.x] [Watcher] Fix code scanning alert (#194564) (#194913)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Watcher] Fix code scanning alert
(#194564)](https://github.com/elastic/kibana/pull/194564)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Ignacio
Rivas","email":"rivasign@gmail.com"},"sourceCommit":{"committedDate":"2024-10-04T08:18:17Z","message":"[Watcher]
Fix code scanning alert
(#194564)","sha":"1f77490ac5417204c088d0bccb09e0eb5ffa1dfa","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Feature:Watcher","Team:Kibana
Management","release_note:skip","v9.0.0","backport:prev-minor"],"title":"[Watcher]
Fix code scanning
alert","number":194564,"url":"https://github.com/elastic/kibana/pull/194564","mergeCommit":{"message":"[Watcher]
Fix code scanning alert
(#194564)","sha":"1f77490ac5417204c088d0bccb09e0eb5ffa1dfa"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/194564","number":194564,"mergeCommit":{"message":"[Watcher]
Fix code scanning alert
(#194564)","sha":"1f77490ac5417204c088d0bccb09e0eb5ffa1dfa"}}]}]
BACKPORT-->

Co-authored-by: Ignacio Rivas <rivasign@gmail.com>
This commit is contained in:
Kibana Machine 2024-10-04 20:01:56 +10:00 committed by GitHub
parent c483318790
commit 7e77855c4e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -6,7 +6,7 @@
*/
import React, { useEffect, useReducer } from 'react';
import { isEqual } from 'lodash';
import { isEqual, has, isFunction } from 'lodash';
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';
@ -45,6 +45,12 @@ const watchReducer = (state: any, action: any) => {
const { command, payload } = action;
const { watch } = state;
const watchTypes = Watch.getWatchTypes();
const WatchType =
watch && has(watchTypes, watch.type) && isFunction(watchTypes[watch.type])
? watchTypes[watch.type]
: null;
switch (command) {
case 'setWatch':
return {
@ -57,23 +63,33 @@ const watchReducer = (state: any, action: any) => {
if (isEqual(watch[property], value)) {
return state;
} else {
return {
...state,
watch: new (Watch.getWatchTypes()[watch.type])({
...watch,
[property]: value,
}),
};
if (WatchType) {
return {
...state,
watch: new WatchType({
...watch,
[property]: value,
}),
};
}
return state;
}
case 'addAction':
const { type, defaults } = payload;
const newWatch = new (Watch.getWatchTypes()[watch.type])(watch);
newWatch.createAction(type, defaults);
return {
...state,
watch: newWatch,
};
if (WatchType) {
const newWatch = new WatchType(watch);
newWatch.createAction(type, defaults);
return {
...state,
watch: newWatch,
};
} else {
return state;
}
case 'setError':
return {
@ -117,9 +133,13 @@ export const WatchEditPage = ({
dispatch({ command: 'setError', payload: error.body });
}
} else if (type) {
const WatchType = Watch.getWatchTypes()[type];
if (WatchType) {
const watchTypes = Watch.getWatchTypes();
if (has(watchTypes, type) && isFunction(watchTypes[type])) {
const WatchType = watchTypes[type];
dispatch({ command: 'setWatch', payload: new WatchType() });
} else {
dispatch({ command: 'setError', payload: { message: 'Invalid watch type' } });
}
}
};