Skip to content

Commit 02451b8

Browse files
committed
added check for mustache tempalte in timestamp
1 parent 4f2e734 commit 02451b8

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

x-pack/plugins/triggers_actions_ui/public/application/components/builtin_action_types/pagerduty.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
import { PagerDutyActionParams, PagerDutyActionConnector } from './types';
2424
import pagerDutySvg from './pagerduty.svg';
2525
import { AddMessageVariables } from '../add_message_variables';
26+
import { hasMustacheTokens } from '../../lib/has_mustache_tokens';
2627

2728
export function getActionType(): ActionTypeModel {
2829
return {
@@ -62,6 +63,7 @@ export function getActionType(): ActionTypeModel {
6263
const validationResult = { errors: {} };
6364
const errors = {
6465
summary: new Array<string>(),
66+
timestamp: new Array<string>(),
6567
};
6668
validationResult.errors = errors;
6769
if (!actionParams.summary?.length) {
@@ -74,6 +76,21 @@ export function getActionType(): ActionTypeModel {
7476
)
7577
);
7678
}
79+
if (actionParams.timestamp && !hasMustacheTokens(actionParams.timestamp)) {
80+
if (isNaN(Date.parse(actionParams.timestamp))) {
81+
errors.timestamp.push(
82+
i18n.translate(
83+
'xpack.triggersActionsUI.components.builtinActionTypes.pagerDutyAction.error.invalidTimestamp',
84+
{
85+
defaultMessage: 'Timestamp must be formatted as a date, such as {now}.',
86+
values: {
87+
now: new Date().toISOString(),
88+
},
89+
}
90+
)
91+
);
92+
}
93+
}
7794
return validationResult;
7895
},
7996
actionConnectorFields: PagerDutyActionConnectorFields,
@@ -334,6 +351,8 @@ const PagerDutyParamsFields: React.FunctionComponent<ActionParamsProps<PagerDuty
334351
<EuiFlexItem>
335352
<EuiFormRow
336353
fullWidth
354+
error={errors.timestamp}
355+
isInvalid={errors.timestamp.length > 0 && timestamp !== undefined}
337356
label={i18n.translate(
338357
'xpack.triggersActionsUI.components.builtinActionTypes.pagerDutyAction.timestampTextFieldLabel',
339358
{
@@ -355,8 +374,9 @@ const PagerDutyParamsFields: React.FunctionComponent<ActionParamsProps<PagerDuty
355374
name="timestamp"
356375
data-test-subj="timestampInput"
357376
value={timestamp || ''}
377+
isInvalid={errors.timestamp.length > 0 && timestamp !== undefined}
358378
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
359-
editAction('timestamp', e.target.value, index);
379+
editAction('timestamp', e.target.value.trim(), index);
360380
}}
361381
onBlur={() => {
362382
if (timestamp?.trim()) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
7+
import uuid from 'uuid';
8+
9+
import { hasMustacheTokens } from './has_mustache_tokens';
10+
11+
describe('hasMustacheTokens', () => {
12+
test('returns false for empty string', () => {
13+
expect(hasMustacheTokens('')).toBe(false);
14+
});
15+
16+
test('returns false for string without tokens', () => {
17+
expect(hasMustacheTokens(`some random string ${uuid.v4()}`)).toBe(false);
18+
});
19+
20+
test('returns true when a template token is present', () => {
21+
expect(hasMustacheTokens('{{context.timestamp}}')).toBe(true);
22+
});
23+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
7+
export function hasMustacheTokens(str: string): boolean {
8+
return null !== str.match(/{{.*}}/);
9+
}

0 commit comments

Comments
 (0)