Skip to content

Commit c245ed9

Browse files
committed
Add api and service
1 parent 624a2bf commit c245ed9

File tree

12 files changed

+608
-20
lines changed

12 files changed

+608
-20
lines changed

x-pack/plugins/actions/server/builtin_action_types/case/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const pushToServiceHandler = async ({
4141
}
4242

4343
const fields = prepareFieldsForTransformation({
44-
params,
44+
externalCase: params.externalCase,
4545
mapping,
4646
defaultPipes,
4747
});

x-pack/plugins/actions/server/builtin_action_types/case/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export const ExecutorSubActionSchema = schema.oneOf([
6767
]);
6868

6969
export const ExecutorSubActionPushParamsSchema = schema.object({
70-
caseId: schema.string(),
70+
savedObjectId: schema.string(),
7171
title: schema.string(),
7272
description: schema.nullable(schema.string()),
7373
comments: schema.nullable(schema.arrayOf(CommentSchema)),

x-pack/plugins/actions/server/builtin_action_types/case/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export interface PipedField {
144144
}
145145

146146
export interface PrepareFieldsForTransformArgs {
147-
params: PushToServiceApiParams;
147+
externalCase: Record<string, any>;
148148
mapping: Map<string, MapRecord>;
149149
defaultPipes?: string[];
150150
}

x-pack/plugins/actions/server/builtin_action_types/case/utils.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const maliciousMapping: MapRecord[] = [
6363
];
6464

6565
const fullParams: PushToServiceApiParams = {
66-
caseId: 'd4387ac5-0899-4dc2-bbfa-0dd605c934aa',
66+
savedObjectId: 'd4387ac5-0899-4dc2-bbfa-0dd605c934aa',
6767
title: 'a title',
6868
description: 'a description',
6969
createdAt: '2020-03-13T08:34:53.450Z',
@@ -132,7 +132,7 @@ describe('buildMap', () => {
132132
describe('mapParams', () => {
133133
test('maps params correctly', () => {
134134
const params = {
135-
caseId: '123',
135+
savedObjectId: '123',
136136
incidentId: '456',
137137
title: 'Incident title',
138138
description: 'Incident description',
@@ -148,7 +148,7 @@ describe('mapParams', () => {
148148

149149
test('do not add fields not in mapping', () => {
150150
const params = {
151-
caseId: '123',
151+
savedObjectId: '123',
152152
incidentId: '456',
153153
title: 'Incident title',
154154
description: 'Incident description',
@@ -164,7 +164,7 @@ describe('mapParams', () => {
164164
describe('prepareFieldsForTransformation', () => {
165165
test('prepare fields with defaults', () => {
166166
const res = prepareFieldsForTransformation({
167-
params: fullParams,
167+
externalCase: fullParams.externalCase,
168168
mapping: finalMapping,
169169
});
170170
expect(res).toEqual([
@@ -185,7 +185,7 @@ describe('prepareFieldsForTransformation', () => {
185185

186186
test('prepare fields with default pipes', () => {
187187
const res = prepareFieldsForTransformation({
188-
params: fullParams,
188+
externalCase: fullParams.externalCase,
189189
mapping: finalMapping,
190190
defaultPipes: ['myTestPipe'],
191191
});
@@ -209,7 +209,7 @@ describe('prepareFieldsForTransformation', () => {
209209
describe('transformFields', () => {
210210
test('transform fields for creation correctly', () => {
211211
const fields = prepareFieldsForTransformation({
212-
params: fullParams,
212+
externalCase: fullParams.externalCase,
213213
mapping: finalMapping,
214214
});
215215

@@ -226,8 +226,8 @@ describe('transformFields', () => {
226226

227227
test('transform fields for update correctly', () => {
228228
const fields = prepareFieldsForTransformation({
229-
params: {
230-
...fullParams,
229+
externalCase: {
230+
...fullParams.externalCase,
231231
updatedAt: '2020-03-15T08:34:53.450Z',
232232
updatedBy: {
233233
username: 'anotherUser',
@@ -262,7 +262,7 @@ describe('transformFields', () => {
262262

263263
test('add newline character to descripton', () => {
264264
const fields = prepareFieldsForTransformation({
265-
params: fullParams,
265+
externalCase: fullParams.externalCase,
266266
mapping: finalMapping,
267267
defaultPipes: ['informationUpdated'],
268268
});
@@ -280,7 +280,7 @@ describe('transformFields', () => {
280280

281281
test('append username if fullname is undefined when create', () => {
282282
const fields = prepareFieldsForTransformation({
283-
params: fullParams,
283+
externalCase: fullParams.externalCase,
284284
mapping: finalMapping,
285285
});
286286

@@ -300,8 +300,8 @@ describe('transformFields', () => {
300300

301301
test('append username if fullname is undefined when update', () => {
302302
const fields = prepareFieldsForTransformation({
303-
params: {
304-
...fullParams,
303+
externalCase: {
304+
...fullParams.externalCase,
305305
updatedAt: '2020-03-15T08:34:53.450Z',
306306
updatedBy: {
307307
username: 'anotherUser',

x-pack/plugins/actions/server/builtin_action_types/case/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,17 @@ export const addTimeZoneToDate = (date: string, timezone = 'GMT'): string => {
182182
};
183183

184184
export const prepareFieldsForTransformation = ({
185-
params,
185+
externalCase,
186186
mapping,
187187
defaultPipes = ['informationCreated'],
188188
}: PrepareFieldsForTransformArgs): PipedField[] => {
189-
return Object.keys(params.externalCase)
189+
return Object.keys(externalCase)
190190
.filter((p) => mapping.get(p)?.actionType != null && mapping.get(p)?.actionType !== 'nothing')
191191
.map((p) => {
192192
const actionType = mapping.get(p)?.actionType ?? 'nothing';
193193
return {
194194
key: p,
195-
value: params.externalCase[p],
195+
value: externalCase[p],
196196
actionType,
197197
pipes: actionType === 'append' ? [...defaultPipes, 'append'] : defaultPipes,
198198
};

x-pack/plugins/actions/server/builtin_action_types/jira/mocks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ mapping.set('summary', {
8888
});
8989

9090
const executorParams: ExecutorSubActionPushParams = {
91-
caseId: 'd4387ac5-0899-4dc2-bbfa-0dd605c934aa',
91+
savedObjectId: 'd4387ac5-0899-4dc2-bbfa-0dd605c934aa',
9292
externalId: 'incident-3',
9393
createdAt: '2020-04-27T10:59:46.202Z',
9494
createdBy: { fullName: 'Elastic User', username: 'elastic' },
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
import { flow } from 'lodash';
7+
import {
8+
ExternalServiceParams,
9+
PushToServiceApiHandlerArgs,
10+
HandshakeApiHandlerArgs,
11+
GetIncidentApiHandlerArgs,
12+
ExternalServiceApi,
13+
} from './types';
14+
15+
// TODO: to remove, need to support Case
16+
import { transformers, Transformer } from '../case/transformers';
17+
import { PushToServiceResponse, TransformFieldsArgs } from './case_types';
18+
import { prepareFieldsForTransformation } from '../case/utils';
19+
20+
const handshakeHandler = async ({
21+
externalService,
22+
mapping,
23+
params,
24+
}: HandshakeApiHandlerArgs) => {};
25+
const getIncidentHandler = async ({
26+
externalService,
27+
mapping,
28+
params,
29+
}: GetIncidentApiHandlerArgs) => {};
30+
31+
const pushToServiceHandler = async ({
32+
externalService,
33+
mapping,
34+
params,
35+
secrets,
36+
}: PushToServiceApiHandlerArgs): Promise<PushToServiceResponse> => {
37+
const { externalId, comments } = params;
38+
const updateIncident = externalId ? true : false;
39+
const defaultPipes = updateIncident ? ['informationUpdated'] : ['informationCreated'];
40+
let currentIncident: ExternalServiceParams | undefined;
41+
let res: PushToServiceResponse;
42+
43+
if (externalId) {
44+
currentIncident = await externalService.getIncident(externalId);
45+
}
46+
47+
let incident = {};
48+
// TODO: should be removed later but currently keep it for the Case implementation support
49+
if (mapping) {
50+
const fields = prepareFieldsForTransformation({
51+
externalCase: params.externalObject,
52+
mapping,
53+
defaultPipes,
54+
});
55+
56+
incident = transformFields({
57+
params,
58+
fields,
59+
currentIncident,
60+
});
61+
} else {
62+
incident = { ...params, name: params.title };
63+
}
64+
65+
if (updateIncident) {
66+
res = await externalService.updateIncident({
67+
incidentId: externalId,
68+
incident,
69+
});
70+
} else {
71+
res = await externalService.createIncident({
72+
incident: {
73+
...incident,
74+
caller_id: secrets.username,
75+
},
76+
});
77+
}
78+
79+
// TODO: should temporary keep comments for a Case usage
80+
if (
81+
comments &&
82+
Array.isArray(comments) &&
83+
comments.length > 0 &&
84+
mapping &&
85+
mapping.get('comments')?.actionType !== 'nothing'
86+
) {
87+
res.comments = [];
88+
89+
const fieldsKey = mapping.get('comments')?.target ?? 'comments';
90+
for (const currentComment of comments) {
91+
await externalService.updateIncident({
92+
incidentId: res.id,
93+
incident: {
94+
...incident,
95+
[fieldsKey]: currentComment.comment,
96+
},
97+
});
98+
res.comments = [
99+
...(res.comments ?? []),
100+
{
101+
commentId: currentComment.commentId,
102+
pushedDate: res.pushedDate,
103+
},
104+
];
105+
}
106+
}
107+
return res;
108+
};
109+
110+
export const transformFields = ({
111+
params,
112+
fields,
113+
currentIncident,
114+
}: TransformFieldsArgs): Record<string, string> => {
115+
return fields.reduce((prev, cur) => {
116+
const transform = flow<Transformer>(...cur.pipes.map((p) => transformers[p]));
117+
return {
118+
...prev,
119+
[cur.key]: transform({
120+
value: cur.value,
121+
date: params.updatedAt ?? params.createdAt,
122+
user:
123+
(params.updatedBy != null
124+
? params.updatedBy.fullName
125+
? params.updatedBy.fullName
126+
: params.updatedBy.username
127+
: params.createdBy.fullName
128+
? params.createdBy.fullName
129+
: params.createdBy.username) ?? '',
130+
previousValue: currentIncident ? currentIncident[cur.key] : '',
131+
}).value,
132+
};
133+
}, {});
134+
};
135+
136+
export const api: ExternalServiceApi = {
137+
handshake: handshakeHandler,
138+
pushToService: pushToServiceHandler,
139+
getIncident: getIncidentHandler,
140+
};

0 commit comments

Comments
 (0)