Skip to content

Commit

Permalink
Fixes alert mustache templating with arrays
Browse files Browse the repository at this point in the history
fixes elastic#44057

Prior to this fix, values inside an array were not subject to the
mustache transform.
  • Loading branch information
pmuellr committed Aug 27, 2019
1 parent dedfd62 commit 962a377
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ test('skips non string parameters', () => {
};
const result = transformActionParams(params, {}, {});
expect(result).toMatchInlineSnapshot(`
Object {
"boolean": true,
"date": "2019-02-12T21:01:22.479Z",
"empty1": null,
"empty2": undefined,
"number": 1,
}
`);
Object {
"boolean": true,
"date": "2019-02-12T21:01:22.479Z",
"empty1": null,
"empty2": undefined,
"number": 1,
}
`);
});

test('missing parameters get emptied out', () => {
Expand All @@ -33,11 +33,11 @@ test('missing parameters get emptied out', () => {
};
const result = transformActionParams(params, {}, {});
expect(result).toMatchInlineSnapshot(`
Object {
"message1": "",
"message2": "This message \\"\\" is missing",
}
`);
Object {
"message1": "",
"message2": "This message \\"\\" is missing",
}
`);
});

test('context parameters are passed to templates', () => {
Expand All @@ -46,10 +46,10 @@ test('context parameters are passed to templates', () => {
};
const result = transformActionParams(params, {}, { foo: 'fooVal' });
expect(result).toMatchInlineSnapshot(`
Object {
"message": "Value \\"fooVal\\" exists",
}
`);
Object {
"message": "Value \\"fooVal\\" exists",
}
`);
});

test('state parameters are passed to templates', () => {
Expand All @@ -58,10 +58,10 @@ test('state parameters are passed to templates', () => {
};
const result = transformActionParams(params, { bar: 'barVal' }, {});
expect(result).toMatchInlineSnapshot(`
Object {
"message": "Value \\"barVal\\" exists",
}
`);
Object {
"message": "Value \\"barVal\\" exists",
}
`);
});

test('works recursively', () => {
Expand All @@ -72,10 +72,28 @@ test('works recursively', () => {
};
const result = transformActionParams(params, { value: 'state' }, { value: 'context' });
expect(result).toMatchInlineSnapshot(`
Object {
"body": Object {
"message": "State: \\"state\\", Context: \\"context\\"",
},
}
`);
Object {
"body": Object {
"message": "State: \\"state\\", Context: \\"context\\"",
},
}
`);
});

test('works recursively with arrays', () => {
const params = {
body: {
messages: ['State: "{{state.value}}", Context: "{{context.value}}"'],
},
};
const result = transformActionParams(params, { value: 'state' }, { value: 'context' });
expect(result).toMatchInlineSnapshot(`
Object {
"body": Object {
"messages": Array [
"State: \\"state\\", Context: \\"context\\"",
],
},
}
`);
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@
*/

import Mustache from 'mustache';
import { isPlainObject } from 'lodash';
import { isString, cloneDeep } from 'lodash';
import { AlertActionParams, State, Context } from '../types';

export function transformActionParams(params: AlertActionParams, state: State, context: Context) {
const result: AlertActionParams = {};
for (const [key, value] of Object.entries(params)) {
if (isPlainObject(value)) {
result[key] = transformActionParams(value as AlertActionParams, state, context);
} else if (typeof value !== 'string') {
result[key] = value;
} else {
result[key] = Mustache.render(value, { context, state });
}
}
return result;
return cloneDeep(params, value => {
if (!isString(value)) return;

return Mustache.render(value, { context, state });
});
}

0 comments on commit 962a377

Please sign in to comment.