Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ResponseOps][Cases] Setting rule info to null for 8.0 #123096

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions x-pack/plugins/cases/common/utils/user_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,12 @@ export function isUpdateConnector(action?: string, actionFields?: string[]): boo
export function isPush(action?: string, actionFields?: string[]): boolean {
return action === 'push-to-service' && actionFields != null && actionFields.includes('pushed');
}

export function isCreateComment(action?: string, actionFields?: string[]): boolean {
return (
action === 'create' &&
actionFields !== null &&
actionFields !== undefined &&
actionFields.includes('comment')
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
import React from 'react';
import { mount } from 'enzyme';

import { CaseStatuses, ConnectorTypes } from '../../../common/api';
import { CaseStatuses, CommentRequestAlertType, ConnectorTypes } from '../../../common/api';
import { basicPush, getUserAction } from '../../containers/mock';
import {
getLabelTitle,
getPushedServiceLabelTitle,
getConnectorLabelTitle,
toStringArray,
getRuleId,
getRuleName,
} from './helpers';
import { connectorsMock } from '../../containers/configure/mock';
import * as i18n from './translations';
import { Ecs } from '../../containers/types';

describe('User action tree helpers', () => {
const connectors = connectorsMock;
Expand Down Expand Up @@ -245,4 +248,131 @@ describe('User action tree helpers', () => {
expect(res).toEqual(['100']);
});
});

describe('rule getters', () => {
describe.each([
['getRuleId', getRuleId],
['getRuleName', getRuleName],
])('%s null checks', (name, funcToExec) => {
it('returns null if the comment field is an empty string', () => {
const comment = {
rule: {
id: '',
name: '',
},
} as unknown as CommentRequestAlertType;

expect(funcToExec(comment)).toBeNull();
});

it('returns null if the comment field is an empty string in an array', () => {
const comment = {
rule: {
id: [''],
name: [''],
},
} as unknown as CommentRequestAlertType;

expect(funcToExec(comment)).toBeNull();
});

it('returns null if the comment does not have a rule field', () => {
const comment = {} as unknown as CommentRequestAlertType;

expect(funcToExec(comment)).toBeNull();
});

it('returns null if the signals and alert field is an empty string', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = {
signal: { rule: { id: '', name: '' } },
kibana: { alert: { rule: { uuid: '', name: '' } } },
} as unknown as Ecs;

expect(funcToExec(comment, alert)).toBeNull();
});
});

describe.each([
['getRuleId', getRuleId, '1'],
['getRuleName', getRuleName, 'Rule name1'],
])('%s', (name, funcToExec, expectedResult) => {
it('returns the first entry in the comment field', () => {
const comment = {
rule: {
id: ['1', '2'],
name: ['Rule name1', 'Rule name2'],
},
} as unknown as CommentRequestAlertType;

expect(funcToExec(comment)).toEqual(expectedResult);
});

it('returns signal field', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = { signal: { rule: { id: '1', name: 'Rule name1' } } } as unknown as Ecs;

expect(funcToExec(comment, alert)).toEqual(expectedResult);
});

it('returns kibana alert field', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = {
kibana: { alert: { rule: { uuid: '1', name: 'Rule name1' } } },
} as unknown as Ecs;

expect(funcToExec(comment, alert)).toEqual(expectedResult);
});

it('returns signal field even when kibana alert field is defined', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = {
signal: { rule: { id: '1', name: 'Rule name1' } },
kibana: { alert: { rule: { uuid: 'rule id1', name: 'other rule name1' } } },
} as unknown as Ecs;

expect(funcToExec(comment, alert)).toEqual(expectedResult);
});

it('returns the first entry in the signals field', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = {
signal: { rule: { id: '1', name: 'Rule name1' } },
kibana: { alert: { rule: { uuid: 'rule id1', name: 'other rule name1' } } },
} as unknown as Ecs;

expect(funcToExec(comment, alert)).toEqual(expectedResult);
});

it('returns the alert field if the signals field is an empty string', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = {
signal: { rule: { id: '', name: '' } },
kibana: { alert: { rule: { uuid: '1', name: 'Rule name1' } } },
} as unknown as Ecs;

expect(funcToExec(comment, alert)).toEqual(expectedResult);
});

it('returns the alert field if the signals field is an empty string in an array', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = {
signal: { rule: { id: [''], name: [''] } },
kibana: { alert: { rule: { uuid: '1', name: 'Rule name1' } } },
} as unknown as Ecs;

expect(funcToExec(comment, alert)).toEqual(expectedResult);
});

it('returns the alert field first item if the signals field is an empty string in an array', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = {
signal: { rule: { id: [''], name: [''] } },
kibana: { alert: { rule: { uuid: ['1', '2'], name: ['Rule name1', 'Rule name2'] } } },
} as unknown as Ecs;

expect(funcToExec(comment, alert)).toEqual(expectedResult);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { get, isEmpty } from 'lodash';
import {
EuiFlexGroup,
EuiFlexItem,
Expand All @@ -13,17 +14,19 @@ import {
EuiCommentProps,
EuiToken,
} from '@elastic/eui';
import { ALERT_RULE_NAME, ALERT_RULE_UUID } from '@kbn/rule-data-utils/technical_field_names';
import React, { useContext } from 'react';
import classNames from 'classnames';
import { ThemeContext } from 'styled-components';
import { Comment } from '../../../common/ui/types';
import { Comment, Ecs } from '../../../common/ui/types';
import {
CaseFullExternalService,
ActionConnector,
CaseStatuses,
CommentType,
CommentRequestActionsType,
noneConnectorId,
CommentRequestAlertType,
} from '../../../common/api';
import { CaseUserActions } from '../../containers/types';
import { CaseServices } from '../../containers/use_get_case_user_actions';
Expand Down Expand Up @@ -458,3 +461,51 @@ export interface Alert {
signal: Signal;
[key: string]: unknown;
}

export const getFirstItem = (items?: string | string[] | null): string | null => {
return Array.isArray(items) ? items[0] : items ?? null;
};

export const getRuleId = (comment: CommentRequestAlertType, alertData?: Ecs): string | null =>
getRuleField({
commentRuleField: comment?.rule?.id,
alertData,
signalRuleFieldPath: 'signal.rule.id',
kibanaAlertFieldPath: ALERT_RULE_UUID,
});

export const getRuleName = (comment: CommentRequestAlertType, alertData?: Ecs): string | null =>
getRuleField({
commentRuleField: comment?.rule?.name,
alertData,
signalRuleFieldPath: 'signal.rule.name',
kibanaAlertFieldPath: ALERT_RULE_NAME,
});

const getRuleField = ({
commentRuleField,
alertData,
signalRuleFieldPath,
kibanaAlertFieldPath,
}: {
commentRuleField: string | string[] | null | undefined;
alertData: Ecs | undefined;
signalRuleFieldPath: string;
kibanaAlertFieldPath: string;
}): string | null => {
const field =
getNonEmptyField(commentRuleField) ??
getNonEmptyField(get(alertData, signalRuleFieldPath)) ??
getNonEmptyField(get(alertData, kibanaAlertFieldPath));

return field;
};

export const getNonEmptyField = (field: string | string[] | undefined | null): string | null => {
const firstItem = getFirstItem(field);
if (firstItem == null || isEmpty(firstItem)) {
return null;
}

return firstItem;
};
37 changes: 11 additions & 26 deletions x-pack/plugins/cases/public/components/user_action_tree/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import {
EuiCommentList,
EuiCommentProps,
} from '@elastic/eui';
import { ALERT_RULE_NAME, ALERT_RULE_UUID } from '@kbn/rule-data-utils/technical_field_names';

import classNames from 'classnames';
import { get, isEmpty } from 'lodash';
import { isEmpty } from 'lodash';
import React, { useCallback, useMemo, useRef, useState, useEffect } from 'react';
import { useParams } from 'react-router-dom';
import styled from 'styled-components';
Expand Down Expand Up @@ -49,6 +48,9 @@ import {
RuleDetailsNavigation,
ActionsNavigation,
getActionAttachment,
getNonEmptyField,
getRuleId,
getRuleName,
} from './helpers';
import { UserActionAvatar } from './user_action_avatar';
import { UserActionMarkdown } from './user_action_markdown';
Expand Down Expand Up @@ -404,33 +406,16 @@ export const UserActionTree = React.memo(
isRight(AlertCommentRequestRt.decode(comment)) &&
comment.type === CommentType.alert
) {
// TODO: clean this up
const alertId = Array.isArray(comment.alertId)
? comment.alertId.length > 0
? comment.alertId[0]
: ''
: comment.alertId;

const alertIndex = Array.isArray(comment.index)
? comment.index.length > 0
? comment.index[0]
: ''
: comment.index;

if (isEmpty(alertId)) {
const alertId = getNonEmptyField(comment.alertId);
const alertIndex = getNonEmptyField(comment.index);

if (!alertId || !alertIndex) {
return comments;
}

const ruleId =
comment?.rule?.id ??
manualAlertsData[alertId]?.signal?.rule?.id?.[0] ??
get(manualAlertsData[alertId], ALERT_RULE_UUID)[0] ??
null;
const ruleName =
comment?.rule?.name ??
manualAlertsData[alertId]?.signal?.rule?.name?.[0] ??
get(manualAlertsData[alertId], ALERT_RULE_NAME)[0] ??
null;
const alertField: Ecs | undefined = manualAlertsData[alertId];
const ruleId = getRuleId(comment, alertField);
const ruleName = getRuleName(comment, alertField);

return [
...comments,
Expand Down
Loading