Skip to content

Commit

Permalink
ServiceNow action improvements (elastic#60052)
Browse files Browse the repository at this point in the history
* Apply action types to fields

* Add information to each field

* Do not create or update comments when actionType is set to nothing

* Improve helpers tests

* Improve tests

* Refactor: Use transformers and pipes

* Better types

* Refactor tests to new changes

* Better error messages

* Improve field formatting and display

* Improve integration tests

* Make username mandatory field

* Translate transformers

* Refactor schema

* Translate appendInformationToField helper

* Improve intergration tests

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
  • Loading branch information
cnasikas and elasticmachine committed Mar 18, 2020
1 parent 4e5aa93 commit 24534e8
Show file tree
Hide file tree
Showing 16 changed files with 1,714 additions and 254 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@
*/

import { zipWith } from 'lodash';
import { Incident, CommentResponse } from './lib/types';
import { CommentResponse } from './lib/types';
import {
ActionHandlerArguments,
UpdateParamsType,
UpdateActionHandlerArguments,
IncidentCreationResponse,
CommentType,
CommentsZipped,
HandlerResponse,
Comment,
SimpleComment,
CreateHandlerArguments,
UpdateHandlerArguments,
IncidentHandlerArguments,
} from './types';
import { ServiceNow } from './lib';
import { transformFields, prepareFieldsForTransformation, transformComments } from './helpers';

const createComments = async (
export const createComments = async (
serviceNow: ServiceNow,
incidentId: string,
key: string,
comments: CommentType[]
): Promise<CommentsZipped[]> => {
comments: Comment[]
): Promise<SimpleComment[]> => {
const createdComments = await serviceNow.batchCreateComments(incidentId, comments, key);

return zipWith(comments, createdComments, (a: CommentType, b: CommentResponse) => ({
return zipWith(comments, createdComments, (a: Comment, b: CommentResponse) => ({
commentId: a.commentId,
pushedDate: b.pushedDate,
}));
Expand All @@ -35,16 +36,30 @@ export const handleCreateIncident = async ({
params,
comments,
mapping,
}: ActionHandlerArguments): Promise<IncidentCreationResponse> => {
const paramsAsIncident = params as Incident;
}: CreateHandlerArguments): Promise<HandlerResponse> => {
const fields = prepareFieldsForTransformation({
params,
mapping,
});

const incident = transformFields({
params,
fields,
});

const { incidentId, number, pushedDate } = await serviceNow.createIncident({
...paramsAsIncident,
...incident,
});

const res: IncidentCreationResponse = { incidentId, number, pushedDate };
const res: HandlerResponse = { incidentId, number, pushedDate };

if (comments && Array.isArray(comments) && comments.length > 0) {
if (
comments &&
Array.isArray(comments) &&
comments.length > 0 &&
mapping.get('comments').actionType !== 'nothing'
) {
comments = transformComments(comments, params, ['informationAdded']);
res.comments = [
...(await createComments(serviceNow, incidentId, mapping.get('comments').target, comments)),
];
Expand All @@ -59,20 +74,51 @@ export const handleUpdateIncident = async ({
params,
comments,
mapping,
}: UpdateActionHandlerArguments): Promise<IncidentCreationResponse> => {
const paramsAsIncident = params as UpdateParamsType;
}: UpdateHandlerArguments): Promise<HandlerResponse> => {
const currentIncident = await serviceNow.getIncident(incidentId);
const fields = prepareFieldsForTransformation({
params,
mapping,
defaultPipes: ['informationUpdated'],
});

const incident = transformFields({
params,
fields,
currentIncident,
});

const { number, pushedDate } = await serviceNow.updateIncident(incidentId, {
...paramsAsIncident,
...incident,
});

const res: IncidentCreationResponse = { incidentId, number, pushedDate };
const res: HandlerResponse = { incidentId, number, pushedDate };

if (comments && Array.isArray(comments) && comments.length > 0) {
if (
comments &&
Array.isArray(comments) &&
comments.length > 0 &&
mapping.get('comments').actionType !== 'nothing'
) {
comments = transformComments(comments, params, ['informationAdded']);
res.comments = [
...(await createComments(serviceNow, incidentId, mapping.get('comments').target, comments)),
];
}

return { ...res };
};

export const handleIncident = async ({
incidentId,
serviceNow,
params,
comments,
mapping,
}: IncidentHandlerArguments): Promise<HandlerResponse> => {
if (!incidentId) {
return await handleCreateIncident({ serviceNow, params, comments, mapping });
} else {
return await handleUpdateIncident({ incidentId, serviceNow, params, comments, mapping });
}
};
Loading

0 comments on commit 24534e8

Please sign in to comment.