Skip to content

Commit

Permalink
[Cases] Case action UI implementation (#179409)
Browse files Browse the repository at this point in the history
## Summary

Implements #179433

This PR adds case connector with params fields in the UI for case action

### Checklist

Delete any items that are not applicable to this PR.

- [x] Any text added follows [EUI's writing
guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses
sentence case text and includes [i18n
support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md)
- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios
- [x] This renders correctly on smaller devices using a responsive
layout. (You can test this [in your
browser](https://www.browserstack.com/guide/responsive-testing-on-local-server))

---------

Co-authored-by: kibanamachine <[email protected]>
  • Loading branch information
js-jankisalvi and kibanamachine authored Apr 3, 2024
1 parent b8765aa commit 3453cb9
Show file tree
Hide file tree
Showing 27 changed files with 1,223 additions and 14 deletions.
8 changes: 8 additions & 0 deletions x-pack/plugins/cases/common/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,15 @@ export const LOCAL_STORAGE_KEYS = {
* Connectors
*/

export enum CASES_CONNECTOR_SUB_ACTION {
RUN = 'run',
}

export const NONE_CONNECTOR_ID: string = 'none';
export const CASES_CONNECTOR_ID = '.cases';
export const CASES_CONNECTOR_TITLE = 'Cases';

export const CASES_CONNECTOR_TIME_WINDOW_REGEX = '^[1-9][0-9]*[d,w,M,y]$';

/**
* This field is used for authorization of the entities within the cases plugin. Each entity within Cases will have the owner field
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { ActionTypeModel } from '@kbn/triggers-actions-ui-plugin/public/types';
import { getConnectorType } from './cases';
const CONNECTOR_TYPE_ID = '.cases';
let connectorTypeModel: ActionTypeModel;

beforeAll(() => {
connectorTypeModel = getConnectorType();
});

describe('has correct connector id', () => {
test('connector type static data is as expected', () => {
expect(connectorTypeModel.id).toEqual(CONNECTOR_TYPE_ID);
});
});

describe('action params validation', () => {
test('action params validation succeeds when action params is valid', async () => {
const actionParams = {
subActionParams: {
timeWindow: '7d',
reopenClosedCases: false,
groupingBy: [],
owner: 'cases',
},
};

expect(await connectorTypeModel.validateParams(actionParams)).toEqual({
errors: { timeWindow: [] },
});
});

test('params validation succeeds when valid timeWindow', async () => {
const actionParams = { subActionParams: { timeWindow: '17w' } };

expect(await connectorTypeModel.validateParams(actionParams)).toEqual({
errors: { timeWindow: [] },
});
});

test('params validation fails when timeWindow is empty', async () => {
const actionParams = { subActionParams: { timeWindow: '' } };

expect(await connectorTypeModel.validateParams(actionParams)).toEqual({
errors: { timeWindow: ['Invalid time window.'] },
});
});

test('params validation fails when timeWindow is undefined', async () => {
const actionParams = { subActionParams: { timeWindow: undefined } };

expect(await connectorTypeModel.validateParams(actionParams)).toEqual({
errors: { timeWindow: ['Invalid time window.'] },
});
});

test('params validation fails when timeWindow is null', async () => {
const actionParams = { subActionParams: { timeWindow: null } };

expect(await connectorTypeModel.validateParams(actionParams)).toEqual({
errors: { timeWindow: ['Invalid time window.'] },
});
});

test('params validation fails when timeWindow size is 0', async () => {
const actionParams = { subActionParams: { timeWindow: '0d' } };

expect(await connectorTypeModel.validateParams(actionParams)).toEqual({
errors: { timeWindow: ['Invalid time window.'] },
});
});

test('params validation fails when timeWindow size is negative', async () => {
const actionParams = { subActionParams: { timeWindow: '-5w' } };

expect(await connectorTypeModel.validateParams(actionParams)).toEqual({
errors: { timeWindow: ['Invalid time window.'] },
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { lazy } from 'react';
import type {
GenericValidationResult,
ActionTypeModel as ConnectorTypeModel,
} from '@kbn/triggers-actions-ui-plugin/public';

import {
CASES_CONNECTOR_ID,
CASES_CONNECTOR_TITLE,
CASES_CONNECTOR_TIME_WINDOW_REGEX,
} from '../../../../common/constants';
import type { CasesActionParams } from './types';
import * as i18n from './translations';

interface ValidationErrors {
timeWindow: string[];
}

export function getConnectorType(): ConnectorTypeModel<{}, {}, CasesActionParams> {
return {
id: CASES_CONNECTOR_ID,
iconClass: 'casesApp',
selectMessage: i18n.CASE_ACTION_DESC,
actionTypeTitle: CASES_CONNECTOR_TITLE,
actionConnectorFields: null,
isExperimental: true,
validateParams: async (
actionParams: CasesActionParams
): Promise<GenericValidationResult<unknown>> => {
const errors: ValidationErrors = {
timeWindow: [],
};
const validationResult = {
errors,
};
const timeWindowRegex = new RegExp(CASES_CONNECTOR_TIME_WINDOW_REGEX, 'g');

if (
actionParams.subActionParams &&
(!actionParams.subActionParams.timeWindow ||
!actionParams.subActionParams.timeWindow.length ||
!timeWindowRegex.test(actionParams.subActionParams.timeWindow))
) {
errors.timeWindow.push(i18n.TIME_WINDOW_SIZE_ERROR);
}
return validationResult;
},
actionParamsFields: lazy(() => import('./cases_params')),
};
}
Loading

0 comments on commit 3453cb9

Please sign in to comment.