From daa3b4a3fa5771c2d16ed7d1b5cba6f22012480e Mon Sep 17 00:00:00 2001 From: Dao Lam Date: Mon, 10 Jul 2017 12:59:04 -0700 Subject: [PATCH] feat: support overriding steps for templates --- lib/phase/flatten.js | 7 +++- ...asic-job-with-template-override-steps.json | 36 +++++++++++++++++++ ...asic-job-with-template-override-steps.yaml | 6 ++++ test/index.test.js | 6 ++++ 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 test/data/basic-job-with-template-override-steps.json create mode 100644 test/data/basic-job-with-template-override-steps.yaml diff --git a/lib/phase/flatten.js b/lib/phase/flatten.js index 8268a59..240b971 100644 --- a/lib/phase/flatten.js +++ b/lib/phase/flatten.js @@ -57,7 +57,12 @@ function merge(newJob, oldJob, fromTemplate) { mergedSteps.push({ [preStepName]: oldSteps[preStepName] }); } - mergedSteps.push(newJob.steps[i]); + // override step + if (oldSteps[stepName]) { + mergedSteps.push({ [stepName]: oldSteps[stepName] }); + } else { + mergedSteps.push(newJob.steps[i]); + } // add post-step if (oldSteps[postStepName]) { diff --git a/test/data/basic-job-with-template-override-steps.json b/test/data/basic-job-with-template-override-steps.json new file mode 100644 index 0000000..ec22ba1 --- /dev/null +++ b/test/data/basic-job-with-template-override-steps.json @@ -0,0 +1,36 @@ +{ + "annotations": {}, + "jobs": { + "main": [{ + "annotations": {}, + "image": "node:4", + "commands": [ + { + "name": "install", + "command": "npm install" + }, + { + "name": "pretest", + "command": "echo pre-test" + }, + { + "name": "test", + "command": "echo 'my job is overriding this step'" + } + ], + "environment": { + "FOO": "from template", + "BAR": "foo" + }, + "secrets": [ + "GIT_KEY" + ], + "settings": { + "email": "foo@example.com" + } + }] + }, + "workflow": [ + "main" + ] +} diff --git a/test/data/basic-job-with-template-override-steps.yaml b/test/data/basic-job-with-template-override-steps.yaml new file mode 100644 index 0000000..6966840 --- /dev/null +++ b/test/data/basic-job-with-template-override-steps.yaml @@ -0,0 +1,6 @@ +jobs: + main: + template: mytemplate@1.2.3 + steps: + - pretest: echo pre-test + - test: echo 'my job is overriding this step' \ No newline at end of file diff --git a/test/index.test.js b/test/index.test.js index c8fcf01..0977036 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -179,6 +179,12 @@ describe('config parser', () => { data, JSON.parse(loadData('basic-job-with-template-wrapped-steps.json')))) ); + it('flattens templates with job steps ', () => + parser(loadData('basic-job-with-template-override-steps.yaml'), templateFactoryMock) + .then(data => assert.deepEqual( + data, JSON.parse(loadData('basic-job-with-template-override-steps.json')))) + ); + it('returns error if template does not exist', () => { templateFactoryMock.getTemplate.withArgs(firstTemplateConfig).resolves(null);