Skip to content

Commit 733f947

Browse files
authored
Merge pull request #400 from lewgordon/support-wait-seconds-number
fix: handles issue when Ref value is passed to Seconds for Wait task
2 parents b9ff754 + fe24c64 commit 733f947

File tree

2 files changed

+118
-3
lines changed

2 files changed

+118
-3
lines changed

lib/deploy/stepFunctions/compileStateMachines.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function* getIntrinsicFunctions(obj) {
5858
const paramName = generateSubVariableName(value);
5959
// eslint-disable-next-line no-param-reassign
6060
obj[key] = `\${${paramName}}`;
61-
yield [paramName, value];
61+
yield [paramName, value, (obj.Type === 'Wait' && key === 'Seconds')];
6262
} else if (typeof value === 'object') {
6363
const innerFuncs = Array.from(getIntrinsicFunctions(value));
6464
for (const x of innerFuncs) {
@@ -144,18 +144,27 @@ module.exports = {
144144
};
145145
} else {
146146
const f = translateLocalFunctionNames.bind(this);
147+
let processedDefinitionString = definitionString;
148+
functionMappings.forEach((functionMapping) => {
149+
if (functionMapping[2]) {
150+
processedDefinitionString = processedDefinitionString.replace(
151+
// eslint-disable-next-line no-useless-escape
152+
new RegExp(`\\\"(\\\$\\\{${functionMapping[0]}\\\})\\\"`, 'g'),
153+
'$1',
154+
);
155+
}
156+
});
147157
const params = _.fromPairs(functionMappings.map(([k, v]) => [k, f(v)]));
148158

149159
DefinitionString = {
150160
'Fn::Sub': [
151-
definitionString,
161+
processedDefinitionString,
152162
params,
153163
],
154164
};
155165
}
156166
}
157167
}
158-
159168
if (stateMachineObj.useExactVersion === true && DefinitionString['Fn::Sub']) {
160169
const params = DefinitionString['Fn::Sub'][1];
161170
const f = convertToFunctionVersion.bind(this);

lib/deploy/stepFunctions/compileStateMachines.test.js

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,112 @@ describe('#compileStateMachines', () => {
370370
expect(actual).to.equal(JSON.stringify(definition, undefined, 2));
371371
});
372372

373+
it('should use raw values for Seconds for Wait task', () => {
374+
const definition = {
375+
Comment: 'Hello World',
376+
StartAt: 'HelloWorld',
377+
States: {
378+
HelloWorld: {
379+
Type: 'Wait',
380+
Seconds: { Ref: 'SomeSeconds' },
381+
End: true,
382+
},
383+
},
384+
};
385+
386+
serverless.service.stepFunctions = {
387+
stateMachines: {
388+
myStateMachine1: {
389+
name: 'stateMachineBeta1',
390+
definition,
391+
},
392+
},
393+
};
394+
395+
serverlessStepFunctions.compileStateMachines();
396+
const actual = serverlessStepFunctions
397+
.serverless
398+
.service
399+
.provider
400+
.compiledCloudFormationTemplate
401+
.Resources
402+
.StateMachineBeta1
403+
.Properties
404+
.DefinitionString;
405+
406+
expect(actual['Fn::Sub'][0]).to.equal(`
407+
{
408+
"Comment": "Hello World",
409+
"StartAt": "HelloWorld",
410+
"States": {
411+
"HelloWorld": {
412+
"Type": "Wait",
413+
"Seconds": \${4dfb8832166d083d5c26a32fbfcaebf9},
414+
"End": true
415+
}
416+
}
417+
}
418+
`.trim());
419+
});
420+
421+
it('should not use raw values for Seconds in other task', () => {
422+
const definition = {
423+
Comment: 'Hello World',
424+
StartAt: 'HelloWorld',
425+
States: {
426+
HelloWorld: {
427+
Type: 'Task',
428+
Resource: 'arn:aws:states:::glue:startJobRun.sync',
429+
Parameters: {
430+
Seconds: { Ref: 'SomeSeconds' },
431+
},
432+
TimeoutSecondsPath: '$.params.maxTime',
433+
HeartbeatSecondsPath: '$.params.heartbeat',
434+
End: true,
435+
},
436+
},
437+
};
438+
439+
serverless.service.stepFunctions = {
440+
stateMachines: {
441+
myStateMachine1: {
442+
name: 'stateMachineBeta1',
443+
definition,
444+
},
445+
},
446+
};
447+
448+
serverlessStepFunctions.compileStateMachines();
449+
const actual = serverlessStepFunctions
450+
.serverless
451+
.service
452+
.provider
453+
.compiledCloudFormationTemplate
454+
.Resources
455+
.StateMachineBeta1
456+
.Properties
457+
.DefinitionString;
458+
459+
expect(actual['Fn::Sub'][0]).to.equal(`
460+
{
461+
"Comment": "Hello World",
462+
"StartAt": "HelloWorld",
463+
"States": {
464+
"HelloWorld": {
465+
"Type": "Task",
466+
"Resource": "arn:aws:states:::glue:startJobRun.sync",
467+
"Parameters": {
468+
"Seconds": "\${4dfb8832166d083d5c26a32fbfcaebf9}"
469+
},
470+
"TimeoutSecondsPath": "$.params.maxTime",
471+
"HeartbeatSecondsPath": "$.params.heartbeat",
472+
"End": true
473+
}
474+
}
475+
}
476+
`.trim());
477+
});
478+
373479
it('should add dependsOn resources', () => {
374480
serverless.service.stepFunctions = {
375481
stateMachines: {

0 commit comments

Comments
 (0)