Merge pull request #4956 from vinayvinay/vinay-fetch-cookiecutter-more-inputs
Adds inputs for fetch:cookiecutter action: copyWithoutRender, extensions, imageName
This commit is contained in:
@@ -108,6 +108,58 @@ 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',
|
||||
],
|
||||
imageName: 'foo/cookiecutter-image-with-extensions',
|
||||
},
|
||||
});
|
||||
|
||||
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',
|
||||
],
|
||||
imageName: 'foo/cookiecutter-image-with-extensions',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
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();
|
||||
|
||||
|
||||
@@ -37,6 +37,9 @@ export function createFetchCookiecutterAction(options: {
|
||||
url: string;
|
||||
targetPath?: string;
|
||||
values: JsonObject;
|
||||
copyWithoutRender?: string[];
|
||||
extensions?: string[];
|
||||
imageName?: string;
|
||||
}>({
|
||||
id: 'fetch:cookiecutter',
|
||||
description:
|
||||
@@ -63,6 +66,30 @@ 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',
|
||||
},
|
||||
},
|
||||
imageName: {
|
||||
title: 'Cookiecutter Docker image',
|
||||
description:
|
||||
"Specify a custom Docker image to run cookiecutter, to override the default: 'spotify/backstage-cookiecutter'. This can be used to execute cookiecutter with Template Extensions. Used only when a local cookiecutter is not found.",
|
||||
type: 'string',
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -76,6 +103,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 +124,19 @@ 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,
|
||||
imageName: ctx.input.imageName,
|
||||
};
|
||||
|
||||
// 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
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -52,9 +52,10 @@ export class CookieCutter implements TemplaterBase {
|
||||
// First lets grab the default cookiecutter.json file
|
||||
const cookieCutterJson = await this.fetchTemplateCookieCutter(templateDir);
|
||||
|
||||
const { imageName, ...valuesForCookieCutterJson } = values;
|
||||
const cookieInfo = {
|
||||
...cookieCutterJson,
|
||||
...values,
|
||||
...valuesForCookieCutterJson,
|
||||
};
|
||||
|
||||
await fs.writeJSON(path.join(templateDir, 'cookiecutter.json'), cookieInfo);
|
||||
@@ -74,7 +75,7 @@ export class CookieCutter implements TemplaterBase {
|
||||
});
|
||||
} else {
|
||||
await runDockerContainer({
|
||||
imageName: 'spotify/backstage-cookiecutter',
|
||||
imageName: imageName || 'spotify/backstage-cookiecutter',
|
||||
args: [
|
||||
'cookiecutter',
|
||||
'--no-input',
|
||||
|
||||
Reference in New Issue
Block a user