Skip to content

Commit

Permalink
Fix bug (#136812)
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Jul 21, 2022
1 parent 2bda8aa commit d95ac72
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 33 deletions.
41 changes: 25 additions & 16 deletions x-pack/plugins/actions/server/sub_action_framework/case.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { TestCaseConnector } from './mocks';
import { ActionsConfigurationUtilities } from '../actions_config';

describe('CaseConnector', () => {
const pushToServiceParams = { externalId: null, comments: [] };
const pushToServiceParams = { incident: { externalId: null }, comments: [] };
let logger: MockedLogger;
let services: ReturnType<typeof actionsMock.createServices>;
let mockedActionsConfig: jest.Mocked<ActionsConfigurationUtilities>;
Expand Down Expand Up @@ -57,19 +57,19 @@ describe('CaseConnector', () => {
const subAction = subActions.get('pushToService');
expect(
subAction?.schema?.validate({
externalId: 'test',
incident: { externalId: 'test' },
comments: [{ comment: 'comment', commentId: 'comment-id' }],
})
).toEqual({
externalId: 'test',
incident: { externalId: 'test' },
comments: [{ comment: 'comment', commentId: 'comment-id' }],
});
});

it('should accept null for externalId', async () => {
const subActions = service.getSubActions();
const subAction = subActions.get('pushToService');
expect(subAction?.schema?.validate({ externalId: null, comments: [] }));
expect(subAction?.schema?.validate({ incident: { externalId: null }, comments: [] }));
});

it.each([[undefined], [1], [false], [{ test: 'hello' }], [['test']], [{ test: 'hello' }]])(
Expand All @@ -84,7 +84,7 @@ describe('CaseConnector', () => {
it('should accept null for comments', async () => {
const subActions = service.getSubActions();
const subAction = subActions.get('pushToService');
expect(subAction?.schema?.validate({ externalId: 'test', comments: null }));
expect(subAction?.schema?.validate({ incident: { externalId: 'test' }, comments: null }));
});

it.each([
Expand All @@ -98,30 +98,35 @@ describe('CaseConnector', () => {
])('should throw if comments %p', async (comments) => {
const subActions = service.getSubActions();
const subAction = subActions.get('pushToService');
expect(() => subAction?.schema?.validate({ externalId: 'test', comments }));
expect(() => subAction?.schema?.validate({ incident: { externalId: 'test' }, comments }));
});

it('should allow any field in the params', async () => {
const subActions = service.getSubActions();
const subAction = subActions.get('pushToService');

expect(
subAction?.schema?.validate({
externalId: 'test',
incident: {
externalId: 'test',
foo: 'foo',
bar: 1,
baz: [{ test: 'hello' }, 1, 'test', false],
isValid: false,
val: null,
},
comments: [{ comment: 'comment', commentId: 'comment-id' }],
})
).toEqual({
incident: {
externalId: 'test',
foo: 'foo',
bar: 1,
baz: [{ test: 'hello' }, 1, 'test', false],
isValid: false,
val: null,
})
).toEqual({
externalId: 'test',
},
comments: [{ comment: 'comment', commentId: 'comment-id' }],
foo: 'foo',
bar: 1,
baz: [{ test: 'hello' }, 1, 'test', false],
isValid: false,
val: null,
});
});
});
Expand All @@ -138,7 +143,11 @@ describe('CaseConnector', () => {
});

it('should update an incident if externalId is not null', async () => {
const res = await service.pushToService({ ...pushToServiceParams, externalId: 'test-id' });
const res = await service.pushToService({
...pushToServiceParams,
incident: { externalId: 'test-id' },
});

expect(res).toEqual({
id: 'update-incident',
title: 'Test incident',
Expand Down
31 changes: 16 additions & 15 deletions x-pack/plugins/actions/server/sub_action_framework/case.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,20 @@ export abstract class CaseConnector<Config, Secrets>
this.registerSubAction({
name: 'pushToService',
method: 'pushToService',
schema: schema.object(
{
externalId: schema.nullable(schema.string()),
comments: schema.nullable(
schema.arrayOf(
schema.object({
comment: schema.string(),
commentId: schema.string(),
})
)
),
},
{ unknowns: 'allow' }
),
schema: schema.object({
incident: schema.object(
{ externalId: schema.nullable(schema.string()) },
{ unknowns: 'allow' }
),
comments: schema.nullable(
schema.arrayOf(
schema.object({
comment: schema.string(),
commentId: schema.string(),
})
)
),
}),
});
}

Expand All @@ -82,7 +82,8 @@ export abstract class CaseConnector<Config, Secrets>
public abstract getIncident({ id }: { id: string }): Promise<ExternalServiceIncidentResponse>;

public async pushToService(params: PushToServiceParams) {
const { externalId, comments, ...rest } = params;
const { incident, comments } = params;
const { externalId, ...rest } = incident;

let res: PushToServiceResponse;

Expand Down
6 changes: 4 additions & 2 deletions x-pack/plugins/actions/server/sub_action_framework/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ export interface SubAction {
}

export interface PushToServiceParams {
externalId: string | null;
incident: {
externalId: string | null;
[x: string]: unknown;
};
comments: Array<{ commentId: string; comment: string }>;
[x: string]: unknown;
}

export interface ExternalServiceIncidentResponse {
Expand Down

0 comments on commit d95ac72

Please sign in to comment.