chore: allow undefined containerRunner in cookiecutter

this is only needed if the cookiecutter command is not available.

Signed-off-by: Heikki Hellgren <heikki.hellgren@op.fi>
This commit is contained in:
Heikki Hellgren
2023-03-01 14:51:58 +02:00
parent 9c61bdaf40
commit 62414770ea
5 changed files with 28 additions and 4 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend-module-cookiecutter': patch
---
allow container runner to be undefined in cookiecutter plugin
@@ -161,3 +161,5 @@ You can do so by including the following lines in the last step of your Dockerfi
RUN apt-get update && apt-get install -y python3 python3-pip
RUN pip3 install cookiecutter
```
In this case, you don't have to include `containerRunner` in the action configuration.
@@ -15,7 +15,7 @@ import { UrlReader } from '@backstage/backend-common';
export function createFetchCookiecutterAction(options: {
reader: UrlReader;
integrations: ScmIntegrations;
containerRunner: ContainerRunner;
containerRunner?: ContainerRunner;
}): TemplateAction<{
url: string;
targetPath?: string | undefined;
@@ -223,4 +223,16 @@ describe('fetch:cookiecutter', () => {
}),
);
});
it('should throw error if cookiecutter is not installed and containerRunner is undefined', async () => {
commandExists.mockResolvedValue(false);
const ccAction = createFetchCookiecutterAction({
integrations,
reader: mockReader,
});
await expect(ccAction.handler(mockContext)).rejects.toThrow(
/Invalid state: containerRunner cannot be undefined when cookiecutter is not installed/,
);
});
});
@@ -33,9 +33,9 @@ import {
import { createTemplateAction } from '@backstage/plugin-scaffolder-node';
export class CookiecutterRunner {
private readonly containerRunner: ContainerRunner;
private readonly containerRunner?: ContainerRunner;
constructor({ containerRunner }: { containerRunner: ContainerRunner }) {
constructor({ containerRunner }: { containerRunner?: ContainerRunner }) {
this.containerRunner = containerRunner;
}
@@ -101,6 +101,11 @@ export class CookiecutterRunner {
logStream,
});
} else {
if (this.containerRunner === undefined) {
throw new Error(
'Invalid state: containerRunner cannot be undefined when cookiecutter is not installed',
);
}
await this.containerRunner.runContainer({
imageName: imageName ?? 'spotify/backstage-cookiecutter',
command: 'cookiecutter',
@@ -139,7 +144,7 @@ export class CookiecutterRunner {
export function createFetchCookiecutterAction(options: {
reader: UrlReader;
integrations: ScmIntegrations;
containerRunner: ContainerRunner;
containerRunner?: ContainerRunner;
}) {
const { reader, containerRunner, integrations } = options;