From b1dc16baa14863d974aa627067b9f19462831ce4 Mon Sep 17 00:00:00 2001 From: Vinay P Date: Mon, 15 Mar 2021 12:45:06 +0000 Subject: [PATCH] copyWithoutRender, extensions inputs for fetch:cookiecutter as discussed in this GitHub issue: https://github.com/backstage/backstage/issues/4920 Signed-off-by: Vinay P --- .../builtin/fetch/cookiecutter.test.ts | 50 +++++++++++++++++++ .../actions/builtin/fetch/cookiecutter.ts | 39 ++++++++++++++- .../stages/templater/cookiecutter.test.ts | 23 +++++++++ 3 files changed, 111 insertions(+), 1 deletion(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/cookiecutter.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/cookiecutter.test.ts index 0de7d94fda..940c0f1e08 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/cookiecutter.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/cookiecutter.test.ts @@ -108,6 +108,56 @@ describe('fetch:cookiecutter', () => { }); }); + it('should execute the cookiecutter templater with optional inputs if they are present and valid', async () => { + await action.handler({ + ...mockContext, + input: { + ...mockContext.input, + copyWithoutRender: ['goreleaser.yml'], + extensions: [ + 'jinja2_custom_filters_extension.string_filters_extension.StringFilterExtension', + ], + }, + }); + + expect(cookiecutterTemplater.run).toHaveBeenCalledWith({ + workspacePath: mockTmpDir, + dockerClient: mockDockerClient, + logStream: mockContext.logStream, + values: { + ...mockContext.input.values, + _copy_without_render: ['goreleaser.yml'], + _extensions: [ + 'jinja2_custom_filters_extension.string_filters_extension.StringFilterExtension', + ], + }, + }); + }); + + it('should throw if copyWithoutRender is not an Array', async () => { + await expect( + action.handler({ + ...mockContext, + input: { + ...mockContext.input, + copyWithoutRender: 'xyz', + }, + }), + ).rejects.toThrow(/copyWithoutRender must be an Array/); + }); + + it('should throw if extensions is not an Array', async () => { + await expect( + action.handler({ + ...mockContext, + input: { + ...mockContext.input, + extensions: 'xyz', + }, + }), + ).rejects.toThrow(/extensions must be an Array/); + }); + it('should throw if there is no cookiecutter templater initialized', async () => { const templatersWithoutCookiecutter = new Templaters(); diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/cookiecutter.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/cookiecutter.ts index 814146cc50..404d134f6d 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/cookiecutter.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/fetch/cookiecutter.ts @@ -37,6 +37,8 @@ export function createFetchCookiecutterAction(options: { url: string; targetPath?: string; values: JsonObject; + copyWithoutRender?: string[]; + extensions?: string[]; }>({ id: 'fetch:cookiecutter', description: @@ -63,6 +65,24 @@ export function createFetchCookiecutterAction(options: { description: 'Values to pass on to cookiecutter for templating', type: 'object', }, + copyWithoutRender: { + title: 'Copy Without Render', + description: + 'Avoid rendering directories and files in the template', + type: 'array', + items: { + type: 'string', + }, + }, + extensions: { + title: 'Template Extensions', + description: + "Jinja2 extensions to add filters, tests, globals or extend the parser. Extensions must be installed in the container or on the host where Cookiecutter executes. See the contrib directory in Backstage's repo for more information", + type: 'array', + items: { + type: 'string', + }, + }, }, }, }, @@ -76,6 +96,18 @@ export function createFetchCookiecutterAction(options: { ); const resultDir = resolvePath(workDir, 'result'); + if ( + ctx.input.copyWithoutRender && + !Array.isArray(ctx.input.copyWithoutRender) + ) { + throw new InputError( + 'Fetch action input copyWithoutRender must be an Array', + ); + } + if (ctx.input.extensions && !Array.isArray(ctx.input.extensions)) { + throw new InputError('Fetch action input extensions must be an Array'); + } + await fetchContents({ reader, integrations, @@ -85,13 +117,18 @@ export function createFetchCookiecutterAction(options: { }); const cookiecutter = templaters.get('cookiecutter'); + const values = { + ...(ctx.input.values as TemplaterValues), + _copy_without_render: ctx.input.copyWithoutRender, + _extensions: ctx.input.extensions, + }; // Will execute the template in ./template and put the result in ./result await cookiecutter.run({ workspacePath: workDir, dockerClient, logStream: ctx.logStream, - values: ctx.input.values as TemplaterValues, + values, }); // Finally move the template result into the task workspace diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/templater/cookiecutter.test.ts b/plugins/scaffolder-backend/src/scaffolder/stages/templater/cookiecutter.test.ts index 763572fda5..7806f089ff 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/templater/cookiecutter.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/templater/cookiecutter.test.ts @@ -168,6 +168,29 @@ describe('CookieCutter Templater', () => { }); }); + it('should run the docker container mentioned in configs, overriding the default', async () => { + const values = { + owner: 'blobby', + storePath: 'https://github.com/org/repo', + imageName: 'foo/cookiecutter-image-with-extensions', + }; + + jest.spyOn(fs, 'readdir').mockResolvedValueOnce(['newthing'] as any); + + const templater = new CookieCutter(); + await templater.run({ + workspacePath: 'tempdir', + values, + dockerClient: mockDocker, + }); + + expect(runDockerContainer).toHaveBeenCalledWith( + expect.objectContaining({ + imageName: 'foo/cookiecutter-image-with-extensions', + }), + ); + }); + it('should pass through the streamer to the run docker helper', async () => { const stream = new PassThrough();