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

[Cases] Migrate user actions connector ID #108272

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
001d5f8
Making progress
jonathan-buttner Aug 3, 2021
a693539
Fleshing out the extraction logic
jonathan-buttner Aug 4, 2021
8756963
Finishing migration logic and starting more tests
jonathan-buttner Aug 5, 2021
d2f6c63
Merge branch 'master' of github.com:elastic/kibana into migrate-ua-co…
jonathan-buttner Aug 11, 2021
a145ef6
Finishing migration unit tests
jonathan-buttner Aug 11, 2021
e2cd0e9
Making progress on services
jonathan-buttner Aug 11, 2021
fe673ad
Finishing transform to es schema
jonathan-buttner Aug 12, 2021
b4bd652
Finishing transform functionality and unit tests
jonathan-buttner Aug 13, 2021
79fe37f
reverting migration data updates
jonathan-buttner Aug 13, 2021
3c55ad9
Merge branch 'master' of github.com:elastic/kibana into migrate-ua-co…
jonathan-buttner Aug 17, 2021
0280916
Cleaning up type errors
jonathan-buttner Aug 17, 2021
4c3407c
fixing test error
jonathan-buttner Aug 17, 2021
8e10d92
Merge branch 'master' of github.com:elastic/kibana into migrate-ua-co…
jonathan-buttner Aug 18, 2021
01a42e9
Merge branch 'master' of github.com:elastic/kibana into migrate-ua-co…
jonathan-buttner Aug 19, 2021
510fd76
Working migration tests
jonathan-buttner Aug 19, 2021
311f21d
Refactoring retrieval of connector fields
jonathan-buttner Aug 19, 2021
7dbe51c
Merge branch 'master' of github.com:elastic/kibana into migrate-ua-co…
jonathan-buttner Aug 20, 2021
9051b14
Refactoring connector id in and tests in frontend
jonathan-buttner Aug 20, 2021
297c2d0
Merge branch 'master' of github.com:elastic/kibana into migrate-ua-co…
jonathan-buttner Aug 23, 2021
2bda621
Fixing tests and finished refactoring parse string
jonathan-buttner Aug 23, 2021
5f4f4f8
Merge branch 'master' of github.com:elastic/kibana into migrate-ua-co…
jonathan-buttner Aug 24, 2021
d08d495
Fixing integration test
jonathan-buttner Aug 24, 2021
a149ec8
Fixing integration tests
jonathan-buttner Aug 24, 2021
7b09642
Removing some duplicate code and updating test name
jonathan-buttner Aug 24, 2021
3fcf158
Merge branch 'master' of github.com:elastic/kibana into migrate-ua-co…
jonathan-buttner Aug 24, 2021
d45de7b
Fixing create connector user action bug
jonathan-buttner Aug 24, 2021
258c6c2
Addressing feedback and logging error
jonathan-buttner Aug 26, 2021
43cc5e8
Merge branch 'master' of github.com:elastic/kibana into migrate-ua-co…
jonathan-buttner Aug 26, 2021
463a9a0
Merge branch 'master' of github.com:elastic/kibana into migrate-ua-co…
jonathan-buttner Sep 1, 2021
5379a07
Merge branch 'master' of github.com:elastic/kibana into migrate-ua-co…
jonathan-buttner Sep 2, 2021
bd4ffe4
Merge branch 'master' into migrate-ua-connector-id
kibanamachine Sep 2, 2021
646cf65
Merge branch 'master' into migrate-ua-connector-id
kibanamachine Sep 7, 2021
02aa71b
Merge branch 'master' into migrate-ua-connector-id
kibanamachine Sep 8, 2021
64f0d56
Merge branch 'master' into migrate-ua-connector-id
kibanamachine Sep 9, 2021
785d7a8
Merge branch 'master' of github.com:elastic/kibana into migrate-ua-co…
jonathan-buttner Sep 13, 2021
ffd5d6f
Moving parsing function to common
jonathan-buttner Sep 13, 2021
a09fbd1
Fixing type errors
jonathan-buttner Sep 13, 2021
4d467b5
Merge branch 'master' of github.com:elastic/kibana into migrate-ua-co…
jonathan-buttner Sep 13, 2021
2b9efd0
Fixing type errors
jonathan-buttner Sep 13, 2021
cf7319b
Merge branch 'master' into migrate-ua-connector-id
kibanamachine Sep 15, 2021
ed559de
Addressing feedback
jonathan-buttner Sep 16, 2021
63545be
Merge branch 'master' of github.com:elastic/kibana into migrate-ua-co…
jonathan-buttner Sep 20, 2021
25c8fa4
Fixing lint errors
jonathan-buttner Sep 20, 2021
f3473b7
Merge branch 'master' into migrate-ua-connector-id
kibanamachine Sep 20, 2021
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
16 changes: 13 additions & 3 deletions x-pack/plugins/cases/common/api/cases/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ const CaseBasicRt = rt.type({
owner: rt.string,
});

export const CaseExternalServiceBasicRt = rt.type({
connector_id: rt.union([rt.string, rt.null]),
/**
* This represents the push to service UserAction. It lacks the connector_id because that is stored in a different field
* within the user action object in the API response.
*/
export const CaseUserActionExternalServiceRt = rt.type({
connector_name: rt.string,
external_id: rt.string,
external_title: rt.string,
Expand All @@ -97,7 +100,14 @@ export const CaseExternalServiceBasicRt = rt.type({
pushed_by: UserRT,
});

const CaseFullExternalServiceRt = rt.union([CaseExternalServiceBasicRt, rt.null]);
export const CaseExternalServiceBasicRt = rt.intersection([
rt.type({
connector_id: rt.union([rt.string, rt.null]),
}),
CaseUserActionExternalServiceRt,
]);

export const CaseFullExternalServiceRt = rt.union([CaseExternalServiceBasicRt, rt.null]);

export const CaseAttributesRt = rt.intersection([
CaseBasicRt,
Expand Down
3 changes: 2 additions & 1 deletion x-pack/plugins/cases/common/api/cases/user_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const UserActionRt = rt.union([
rt.literal('push-to-service'),
]);

// TO DO change state to status
const CaseUserActionBasicRT = rt.type({
action_field: UserActionFieldRt,
action: UserActionRt,
Expand All @@ -51,6 +50,8 @@ const CaseUserActionResponseRT = rt.intersection([
action_id: rt.string,
case_id: rt.string,
comment_id: rt.union([rt.string, rt.null]),
new_val_connector_id: rt.union([rt.string, rt.null]),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These will be populated with the connector ids extracted from the newValue and oldValue fields.

old_val_connector_id: rt.union([rt.string, rt.null]),
}),
rt.partial({ sub_case_id: rt.string }),
]);
Expand Down
12 changes: 10 additions & 2 deletions x-pack/plugins/cases/common/api/connectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,22 @@ export const ConnectorTypeFieldsRt = rt.union([
ConnectorSwimlaneTypeFieldsRt,
]);

/**
* This type represents the connector's format when it is encoded within a user action.
*/
export const CaseUserActionConnectorRt = rt.intersection([
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This type just lacks the id field since that wont exist in the encoded data any longer.

rt.type({ name: rt.string }),
ConnectorTypeFieldsRt,
]);

export const CaseConnectorRt = rt.intersection([
rt.type({
id: rt.string,
name: rt.string,
}),
ConnectorTypeFieldsRt,
CaseUserActionConnectorRt,
]);

export type CaseUserActionConnector = rt.TypeOf<typeof CaseUserActionConnectorRt>;
export type CaseConnector = rt.TypeOf<typeof CaseConnectorRt>;
export type ConnectorTypeFields = rt.TypeOf<typeof ConnectorTypeFieldsRt>;
export type ConnectorJiraTypeFields = rt.TypeOf<typeof ConnectorJiraTypeFieldsRt>;
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/cases/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export * from './constants';
export * from './api';
export * from './ui/types';
export * from './utils/connectors_api';
export * from './utils/user_actions';
2 changes: 2 additions & 0 deletions x-pack/plugins/cases/common/ui/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ export interface CaseUserActions {
caseId: string;
commentId: string | null;
newValue: string | null;
newValConnectorId: string | null;
oldValue: string | null;
oldValConnectorId: string | null;
}

export interface CaseExternalService {
Expand Down
18 changes: 18 additions & 0 deletions x-pack/plugins/cases/common/utils/user_actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* 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.
*/

export function isCreateConnector(action?: string, actionFields?: string[]): boolean {
return action === 'create' && actionFields != null && actionFields.includes('connector');
}

export function isUpdateConnector(action?: string, actionFields?: string[]): boolean {
return action === 'update' && actionFields != null && actionFields.includes('connector');
}

export function isPush(action?: string, actionFields?: string[]): boolean {
return action === 'push-to-service' && actionFields != null && actionFields.includes('pushed');
}
8 changes: 8 additions & 0 deletions x-pack/plugins/cases/public/common/user_actions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* 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.
*/

export * from './parsers';
86 changes: 86 additions & 0 deletions x-pack/plugins/cases/public/common/user_actions/parsers.test.ts
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 { ConnectorTypes, noneConnectorId } from '../../../common';
import { parseStringAsConnector, parseStringAsExternalService } from './parsers';

describe('user actions utility functions', () => {
describe('parseStringAsConnector', () => {
it('return null if the data is null', () => {
expect(parseStringAsConnector('', null)).toBeNull();
});

it('return null if the data is not a json object', () => {
expect(parseStringAsConnector('', 'blah')).toBeNull();
});

it('return null if the data is not a valid connector', () => {
expect(parseStringAsConnector('', JSON.stringify({ a: '1' }))).toBeNull();
});

it('return null if id is null but the data is a connector other than none', () => {
expect(
parseStringAsConnector(
null,
JSON.stringify({ type: ConnectorTypes.jira, name: '', fields: null })
)
).toBeNull();
});

it('return the id as the none connector if the data is the none connector', () => {
expect(
parseStringAsConnector(
null,
JSON.stringify({ type: ConnectorTypes.none, name: '', fields: null })
)
).toEqual({ id: noneConnectorId, type: ConnectorTypes.none, name: '', fields: null });
});

it('returns a decoded connector with the specified id', () => {
expect(
parseStringAsConnector(
'a',
JSON.stringify({ type: ConnectorTypes.jira, name: 'hi', fields: null })
)
).toEqual({ id: 'a', type: ConnectorTypes.jira, name: 'hi', fields: null });
});
});

describe('parseStringAsExternalService', () => {
it('returns null when the data is null', () => {
expect(parseStringAsExternalService('', null)).toBeNull();
});

it('returns null when the data is not valid json', () => {
expect(parseStringAsExternalService('', 'blah')).toBeNull();
});

it('returns null when the data is not a valid external service object', () => {
expect(parseStringAsExternalService('', JSON.stringify({ a: '1' }))).toBeNull();
});

it('returns the decoded external service with the connector_id field added', () => {
const externalServiceInfo = {
connector_name: 'name',
external_id: '1',
external_title: 'title',
external_url: 'abc',
pushed_at: '1',
pushed_by: {
username: 'a',
email: '[email protected]',
full_name: 'a',
},
};

expect(parseStringAsExternalService('500', JSON.stringify(externalServiceInfo))).toEqual({
...externalServiceInfo,
connector_id: '500',
});
});
});
});
77 changes: 77 additions & 0 deletions x-pack/plugins/cases/public/common/user_actions/parsers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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 {
CaseUserActionConnectorRt,
CaseConnector,
ConnectorTypes,
noneConnectorId,
CaseFullExternalService,
CaseUserActionExternalServiceRt,
} from '../../../common';

export const parseStringAsConnector = (
id: string | null,
encodedData: string | null
): CaseConnector | null => {
if (encodedData == null) {
return null;
}

const decodedConnector = parseString(encodedData);

if (!CaseUserActionConnectorRt.is(decodedConnector)) {
return null;
}

if (id == null && decodedConnector.type === ConnectorTypes.none) {
return {
...decodedConnector,
id: noneConnectorId,
};
} else if (id == null) {
return null;
} else {
// id does not equal null or undefined and the connector type does not equal none
// so return the connector with its id
return {
...decodedConnector,
id,
};
}
};

const parseString = (params: string | null): unknown | null => {
if (params == null) {
return null;
}

try {
return JSON.parse(params);
} catch {
return null;
}
};

export const parseStringAsExternalService = (
id: string | null,
encodedData: string | null
): CaseFullExternalService => {
if (encodedData == null) {
return null;
}

const decodedExternalService = parseString(encodedData);
if (!CaseUserActionExternalServiceRt.is(decodedExternalService)) {
return null;
}

return {
...decodedExternalService,
connector_id: id,
};
};
Loading