chore: moving the storePath to it's own variable as source-ref and fixing templates to use cookiecutter vars

This commit is contained in:
blam
2021-01-21 18:15:31 +01:00
parent 5a91087e2d
commit 51b2c3999a
9 changed files with 90 additions and 31 deletions
@@ -3,8 +3,8 @@ kind: Component
metadata:
name: {{cookiecutter.component_id | jsonify}}
description: {{cookiecutter.description | jsonify}}
annotations:
github.com/project-slug: {{cookiecutter.storePath | jsonify}}
github.com/project-slug: {{cookiecutter.destination.git.owner + "/" + cookiecutter.destination.git.name}}
backstage.io/source-ref: {{cookiecutter.storePath | jsonify}}
backstage.io/techdocs-ref: url:{{cookiecutter.storePath}}
spec:
type: documentation
@@ -4,7 +4,8 @@ metadata:
name: {{cookiecutter.component_id | jsonify}}
description: {{cookiecutter.description | jsonify}}
annotations:
github.com/project-slug: {{cookiecutter.storePath | jsonify}}
github.com/project-slug: {{cookiecutter.destination.git.owner + "/" + cookiecutter.destination.git.name}}
backstage.io/source-url: {{cookiecutter.storePath | jsonify}}
backstage.io/techdocs-ref: url:{{cookiecutter.storePath}}
spec:
type: website
@@ -4,7 +4,8 @@ metadata:
name: {{cookiecutter.component_id | jsonify}}
description: {{cookiecutter.description | jsonify}}
annotations:
github.com/project-slug: {{cookiecutter.storePath | jsonify}}
github.com/project-slug: {{cookiecutter.destination.git.owner + "/" + cookiecutter.destination.git.name}}
backstage.io/source-url: {{cookiecutter.storePath | jsonify}}
backstage.io/techdocs-ref: url:{{cookiecutter.storePath}}
spec:
type: service
@@ -26,6 +26,7 @@ import path from 'path';
import { RunDockerContainerOptions, RunCommandOptions } from './helpers';
import { PassThrough } from 'stream';
import Docker from 'dockerode';
import parseGitUrl from 'git-url-parse';
const commandExists = require('command-exists-promise');
@@ -56,16 +57,21 @@ describe('CookieCutter Templater', () => {
const values = {
owner: 'blobby',
storePath: 'backstage/end-repo',
storePath: 'https://github.com/org/repo',
description: 'description',
component_id: 'newthing',
destination: {
git: parseGitUrl('https://github.com/org/repo'),
},
};
await cookie.run({ directory: tempdir, values, dockerClient: mockDocker });
const cookieCutterJson = await fs.readJSON(`${tempdir}/cookiecutter.json`);
expect(cookieCutterJson).toEqual(expect.objectContaining(values));
expect(cookieCutterJson).toEqual(
expect.objectContaining(JSON.parse(JSON.stringify(values))),
);
});
it('should merge any value that is in the cookiecutter.json path already', async () => {
@@ -78,15 +84,24 @@ describe('CookieCutter Templater', () => {
const values = {
owner: 'blobby',
storePath: 'backstage/end-repo',
storePath: 'https://github.com/org/repo',
component_id: 'something',
destination: {
git: parseGitUrl('https://github.com/org/repo'),
},
};
await cookie.run({ directory: tempdir, values, dockerClient: mockDocker });
const cookieCutterJson = await fs.readJSON(`${tempdir}/cookiecutter.json`);
expect(cookieCutterJson).toEqual({ ...existingJson, ...values });
expect(cookieCutterJson).toEqual({
...existingJson,
...values,
destination: {
git: expect.objectContaining({ organization: 'org', name: 'repo' }),
},
});
});
it('should throw an error if the cookiecutter json is malformed and not missing', async () => {
@@ -96,7 +111,10 @@ describe('CookieCutter Templater', () => {
const values = {
owner: 'blobby',
storePath: 'backstage/end-repo',
storePath: 'https://github.com/org/repo',
destination: {
git: parseGitUrl('https://github.com/org/repo'),
},
};
await expect(
@@ -109,8 +127,11 @@ describe('CookieCutter Templater', () => {
const values = {
owner: 'blobby',
storePath: 'backstage/end-repo',
storePath: 'https://github.com/org/repo',
component_id: 'newthing',
destination: {
git: parseGitUrl('https://github.com/org/repo'),
},
};
await cookie.run({ directory: tempdir, values, dockerClient: mockDocker });
@@ -137,8 +158,11 @@ describe('CookieCutter Templater', () => {
const values = {
owner: 'blobby',
storePath: 'backstage/end-repo',
storePath: 'https://github.com/org/repo',
component_id: 'newthing',
destination: {
git: parseGitUrl('https://github.com/org/repo'),
},
};
const { resultDir } = await cookie.run({
@@ -157,8 +181,11 @@ describe('CookieCutter Templater', () => {
const values = {
owner: 'blobby',
storePath: 'backstage/end-repo',
storePath: 'https://github.com/org/repo',
component_id: 'newthing',
destination: {
git: parseGitUrl('https://github.com/org/repo'),
},
};
await cookie.run({
@@ -203,8 +230,11 @@ describe('CookieCutter Templater', () => {
const values = {
owner: 'blobby',
storePath: 'backstage/end-repo',
storePath: 'https://github.com/org/repo',
component_id: 'newthing',
destination: {
git: parseGitUrl('https://github.com/org/repo'),
},
};
await cookie.run({
@@ -243,7 +273,10 @@ describe('CookieCutter Templater', () => {
directory: tempdir,
values: {
owner: 'blobby',
storePath: 'backstage/end-repo',
storePath: 'https://github.com/org/repo',
destination: {
git: parseGitUrl('https://github.com/org/repo'),
},
},
logStream: stream,
dockerClient: mockDocker,
@@ -18,6 +18,7 @@ import { JsonValue } from '@backstage/config';
import { runDockerContainer, runCommand } from './helpers';
import { TemplaterBase, TemplaterRunOptions } from '.';
import path from 'path';
import { TemplaterRunResult } from './types';
const commandExists = require('command-exists-promise');
@@ -70,7 +70,9 @@ export class CreateReactAppTemplater implements TemplaterBase {
`${finalDir}/.github/workflows/main.yml`,
);
extraAnnotations[GITHUB_ACTIONS_ANNOTATION] = options.values.storePath;
extraAnnotations[
GITHUB_ACTIONS_ANNOTATION
] = `${options.values.destination.git.owner}/${options.values.destination.git.name}`;
}
const componentInfo = {
@@ -17,6 +17,7 @@ import type { Writable } from 'stream';
import Docker from 'dockerode';
import { JsonValue } from '@backstage/config';
import { TemplateEntityV1alpha1 } from '@backstage/catalog-model';
import gitUrlParse from 'git-url-parse';
/**
* Currently the required template values. The owner
@@ -25,6 +26,9 @@ import { TemplateEntityV1alpha1 } from '@backstage/catalog-model';
export type RequiredTemplateValues = {
owner: string;
storePath: string;
destination: {
git: gitUrlParse.GitUrl;
};
};
/**
@@ -42,7 +46,7 @@ export type TemplaterRunResult = {
*/
export type TemplaterRunOptions = {
directory: string;
values: RequiredTemplateValues & Record<string, JsonValue>;
values: RequiredTemplateValues & Record<string, any>;
logStream?: Writable;
dockerClient: Docker;
};
@@ -107,10 +107,14 @@ describe('createRouter - working directory', () => {
});
const app = express().use(router);
await request(app).post('/v1/jobs').send({
templateName: '',
values: {},
});
await request(app)
.post('/v1/jobs')
.send({
templateName: '',
values: {
storePath: 'https://github.com/backstage/good',
},
});
expect(mockPrepare).toBeCalledWith(expect.anything(), {
logger: expect.anything(),
@@ -130,10 +134,14 @@ describe('createRouter - working directory', () => {
});
const app = express().use(router);
await request(app).post('/v1/jobs').send({
templateName: '',
values: {},
});
await request(app)
.post('/v1/jobs')
.send({
templateName: '',
values: {
storePath: 'https://github.com/backstage/goodrepo',
},
});
expect(mockPrepare).toBeCalledWith(expect.anything(), {
logger: expect.anything(),
@@ -200,10 +208,14 @@ describe('createRouter', () => {
describe('POST /v1/jobs', () => {
it('rejects template values which do not match the template schema definition', async () => {
const response = await request(app).post('/v1/jobs').send({
templateName: '',
values: {},
});
const response = await request(app)
.post('/v1/jobs')
.send({
templateName: '',
values: {
storePath: 'https://github.com/backstage/backstage',
},
});
expect(response.status).toEqual(400);
});
@@ -32,6 +32,7 @@ import {
} from '../scaffolder';
import { CatalogEntityClient } from '../lib/catalog';
import { validate, ValidatorResult } from 'jsonschema';
import parseGitUrl from 'git-url-parse';
export interface RouterOptions {
preparers: PreparerBuilder;
@@ -109,8 +110,13 @@ export async function createRouter(
})
.post('/v1/jobs', async (req, res) => {
const templateName: string = req.body.templateName;
const values: RequiredTemplateValues & Record<string, JsonValue> =
req.body.values;
const values: RequiredTemplateValues & Record<string, JsonValue> = {
...req.body.values,
destination: {
git: parseGitUrl(req.body.values.storePath),
},
};
req.body.values;
const template = await entityClient.findTemplate(templateName);
@@ -123,7 +129,6 @@ export async function createRouter(
res.status(400).json({ errors: validationResult.errors });
return;
}
const job = jobProcessor.create({
entity: template,
values,