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 1 commit
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
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,122 @@ describe('User action tree helpers', () => {
expect(res).toEqual(['100']);
});
});

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

expect(funcToExec(comment)).toEqual('1');
});

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 signal field', () => {
const comment = {} as unknown as CommentRequestAlertType;
const alert = { signal: { rule: { id: '1', name: '1' } } } as unknown as Ecs;

expect(funcToExec(comment, alert)).toEqual('1');
});

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

expect(funcToExec(comment, alert)).toEqual('1');
});

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

expect(funcToExec(comment, alert)).toEqual('signal');
});

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

expect(funcToExec(comment, alert)).toEqual('signal1');
});

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: '1' } } },
} as unknown as Ecs;

expect(funcToExec(comment, alert)).toEqual('1');
});

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: '1' } } },
} as unknown as Ecs;

expect(funcToExec(comment, alert)).toEqual('1');
});

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: ['1', '2'] } } },
} as unknown as Ecs;

expect(funcToExec(comment, alert)).toEqual('1');
});

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();
});
});
});
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,55 @@ export interface Alert {
signal: Signal;
[key: string]: unknown;
}

export const getFirstItem = (items?: string | string[] | null): string | null => {
if (items == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: this kind of code can be confusing as items could be undefined and can also be null. I'd recommend the more verbose but clear and type safe way:

if (item === undefined || item === null) {

return null;
}

return Array.isArray(items) ? (items.length > 0 ? items[0] : null) : items;
};

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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
createCommentsMigrations,
mergeMigrationFunctionMaps,
migrateByValueLensVisualizations,
removeRuleInformation,
stringifyCommentWithoutTrailingNewline,
} from './comments';
import {
Expand All @@ -29,6 +30,7 @@ import {
MigrateFunctionsObject,
} from '../../../../../../src/plugins/kibana_utils/common';
import { SerializableRecord } from '@kbn/utility-types';
import { CommentType } from '../../../common/api';

describe('comments migrations', () => {
const migrations = createCommentsMigrations({
Expand Down Expand Up @@ -396,4 +398,79 @@ describe('comments migrations', () => {
);
});
});

describe('removeRuleInformation', () => {
it('does not modify non-alert comment', () => {
const doc = {
id: '123',
attributes: {
type: 'user',
},
type: 'abc',
references: [],
};

expect(removeRuleInformation(doc)).toEqual(doc);
});

it('sets the rule fields to null', () => {
const doc = {
id: '123',
type: 'abc',
attributes: {
type: CommentType.alert,
rule: {
id: '123',
name: 'hello',
},
},
};

expect(removeRuleInformation(doc)).toEqual({
...doc,
attributes: { ...doc.attributes, rule: { id: null, name: null } },
references: [],
});
});

it('sets the rule fields to null for a generated alert', () => {
const doc = {
id: '123',
type: 'abc',
attributes: {
type: CommentType.generatedAlert,
rule: {
id: '123',
name: 'hello',
},
},
};

expect(removeRuleInformation(doc)).toEqual({
...doc,
attributes: { ...doc.attributes, rule: { id: null, name: null } },
references: [],
});
});

it('preserves the references field', () => {
const doc = {
id: '123',
type: 'abc',
attributes: {
type: CommentType.alert,
rule: {
id: '123',
name: 'hello',
},
},
references: [{ id: '123', name: 'hi', type: 'awesome' }],
};

expect(removeRuleInformation(doc)).toEqual({
...doc,
attributes: { ...doc.attributes, rule: { id: null, name: null } },
});
});
});
});
Loading