Merge pull request #3137 from mfrinnstrom/scaffolder-update-working-directory

[Scaffolder] introduce options type for the preparers
This commit is contained in:
Ben Lambert
2020-10-28 10:50:54 +01:00
committed by GitHub
10 changed files with 45 additions and 21 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend': patch
---
Introduce PreparerOptions for PreparerBase
@@ -30,6 +30,7 @@ import {
TemplateEntityV1alpha1,
LOCATION_ANNOTATION,
} from '@backstage/catalog-model';
import { getVoidLogger } from '@backstage/backend-common';
import { ConfigReader } from '@backstage/config';
describe('AzurePreparer', () => {
@@ -78,7 +79,7 @@ describe('AzurePreparer', () => {
it('calls the clone command with the correct arguments for a repository', async () => {
const preparer = new AzurePreparer(ConfigReader.fromConfigs([]));
await preparer.prepare(mockEntity);
await preparer.prepare(mockEntity, { logger: getVoidLogger() });
expect(mocks.Clone.clone).toHaveBeenNthCalledWith(
1,
'https://dev.azure.com/backstage-org/backstage-project/_git/template-repo',
@@ -104,7 +105,7 @@ describe('AzurePreparer', () => {
},
]),
);
await preparer.prepare(mockEntity);
await preparer.prepare(mockEntity, { logger: getVoidLogger() });
expect(mocks.Clone.clone).toHaveBeenNthCalledWith(
1,
'https://dev.azure.com/backstage-org/backstage-project/_git/template-repo',
@@ -122,7 +123,7 @@ describe('AzurePreparer', () => {
it('calls the clone command with the correct arguments for a repository when no path is provided', async () => {
const preparer = new AzurePreparer(ConfigReader.fromConfigs([]));
delete mockEntity.spec.path;
await preparer.prepare(mockEntity);
await preparer.prepare(mockEntity, { logger: getVoidLogger() });
expect(mocks.Clone.clone).toHaveBeenNthCalledWith(
1,
'https://dev.azure.com/backstage-org/backstage-project/_git/template-repo',
@@ -134,7 +135,9 @@ describe('AzurePreparer', () => {
it('return the temp directory with the path to the folder if it is specified', async () => {
const preparer = new AzurePreparer(ConfigReader.fromConfigs([]));
mockEntity.spec.path = './template/test/1/2/3';
const response = await preparer.prepare(mockEntity);
const response = await preparer.prepare(mockEntity, {
logger: getVoidLogger(),
});
expect(response.split('\\').join('/')).toMatch(
/\/template\/test\/1\/2\/3$/,
@@ -145,6 +148,7 @@ describe('AzurePreparer', () => {
const preparer = new AzurePreparer(ConfigReader.fromConfigs([]));
mockEntity.spec.path = './template/test/1/2/3';
const response = await preparer.prepare(mockEntity, {
logger: getVoidLogger(),
workingDirectory: '/workDir',
});
@@ -19,7 +19,7 @@ import path from 'path';
import { TemplateEntityV1alpha1 } from '@backstage/catalog-model';
import { parseLocationAnnotation } from '../helpers';
import { InputError } from '@backstage/backend-common';
import { PreparerBase } from './types';
import { PreparerBase, PreparerOptions } from './types';
import GitUriParser from 'git-url-parse';
import { Clone, Cred } from 'nodegit';
import { Config } from '@backstage/config';
@@ -34,7 +34,7 @@ export class AzurePreparer implements PreparerBase {
async prepare(
template: TemplateEntityV1alpha1,
opts?: { workingDirectory?: string },
opts: PreparerOptions,
): Promise<string> {
const { protocol, location } = parseLocationAnnotation(template);
const workingDirectory = opts?.workingDirectory ?? os.tmpdir();
@@ -23,6 +23,7 @@ import {
TemplateEntityV1alpha1,
LOCATION_ANNOTATION,
} from '@backstage/catalog-model';
import { getVoidLogger } from '@backstage/backend-common';
const setupTest = async (fixturePath: string) => {
const locationForTemplateYaml = path.resolve(
@@ -44,6 +45,7 @@ const setupTest = async (fixturePath: string) => {
const filePreparer = new FilePreparer();
const resultDir = await filePreparer.prepare(template, {
logger: getVoidLogger(),
workingDirectory: os.tmpdir(),
});
@@ -19,12 +19,12 @@ import path from 'path';
import { TemplateEntityV1alpha1 } from '@backstage/catalog-model';
import { parseLocationAnnotation } from '../helpers';
import { InputError } from '@backstage/backend-common';
import { PreparerBase } from './types';
import { PreparerBase, PreparerOptions } from './types';
export class FilePreparer implements PreparerBase {
async prepare(
template: TemplateEntityV1alpha1,
opts?: { workingDirectory?: string },
opts: PreparerOptions,
): Promise<string> {
const { protocol, location } = parseLocationAnnotation(template);
const workingDirectory = opts?.workingDirectory ?? os.tmpdir();
@@ -30,6 +30,7 @@ import {
TemplateEntityV1alpha1,
LOCATION_ANNOTATION,
} from '@backstage/catalog-model';
import { getVoidLogger } from '@backstage/backend-common';
describe('GitHubPreparer', () => {
let mockEntity: TemplateEntityV1alpha1;
@@ -76,7 +77,7 @@ describe('GitHubPreparer', () => {
});
it('calls the clone command with the correct arguments for a repository', async () => {
const preparer = new GithubPreparer();
await preparer.prepare(mockEntity);
await preparer.prepare(mockEntity, { logger: getVoidLogger() });
expect(mocks.Clone.clone).toHaveBeenNthCalledWith(
1,
'https://github.com/benjdlambert/backstage-graphql-template',
@@ -89,7 +90,7 @@ describe('GitHubPreparer', () => {
it('calls the clone command with the correct arguments for a repository when no path is provided', async () => {
const preparer = new GithubPreparer();
delete mockEntity.spec.path;
await preparer.prepare(mockEntity);
await preparer.prepare(mockEntity, { logger: getVoidLogger() });
expect(mocks.Clone.clone).toHaveBeenNthCalledWith(
1,
'https://github.com/benjdlambert/backstage-graphql-template',
@@ -103,7 +104,9 @@ describe('GitHubPreparer', () => {
it('return the temp directory with the path to the folder if it is specified', async () => {
const preparer = new GithubPreparer();
mockEntity.spec.path = './template/test/1/2/3';
const response = await preparer.prepare(mockEntity);
const response = await preparer.prepare(mockEntity, {
logger: getVoidLogger(),
});
expect(response.split('\\').join('/')).toMatch(
/\/template\/test\/1\/2\/3$/,
@@ -114,6 +117,7 @@ describe('GitHubPreparer', () => {
const preparer = new GithubPreparer();
mockEntity.spec.path = './template/test/1/2/3';
const response = await preparer.prepare(mockEntity, {
logger: getVoidLogger(),
workingDirectory: '/workDir',
});
@@ -124,7 +128,7 @@ describe('GitHubPreparer', () => {
it('calls the clone command with the token when provided', async () => {
const preparer = new GithubPreparer({ token: 'abc' });
await preparer.prepare(mockEntity);
await preparer.prepare(mockEntity, { logger: getVoidLogger() });
expect(mocks.Clone.clone).toHaveBeenNthCalledWith(
1,
'https://github.com/benjdlambert/backstage-graphql-template',
@@ -19,7 +19,7 @@ import path from 'path';
import { TemplateEntityV1alpha1 } from '@backstage/catalog-model';
import { parseLocationAnnotation } from '../helpers';
import { InputError } from '@backstage/backend-common';
import { PreparerBase } from './types';
import { PreparerBase, PreparerOptions } from './types';
import GitUriParser from 'git-url-parse';
import { Clone, CloneOptions, Cred } from 'nodegit';
@@ -32,7 +32,7 @@ export class GithubPreparer implements PreparerBase {
async prepare(
template: TemplateEntityV1alpha1,
opts?: { workingDirectory?: string },
opts: PreparerOptions,
): Promise<string> {
const { protocol, location } = parseLocationAnnotation(template);
const workingDirectory = opts?.workingDirectory ?? os.tmpdir();
@@ -30,6 +30,7 @@ import {
LOCATION_ANNOTATION,
} from '@backstage/catalog-model';
import { ConfigReader } from '@backstage/config';
import { getVoidLogger } from '@backstage/backend-common';
const mockEntityWithProtocol = (protocol: string): TemplateEntityV1alpha1 => ({
apiVersion: 'backstage.io/v1alpha1',
@@ -79,7 +80,7 @@ describe('GitLabPreparer', () => {
it(`calls the clone command with the correct arguments for a repository using the ${protocol} protocol`, async () => {
const preparer = new GitlabPreparer(ConfigReader.fromConfigs([]));
mockEntity = mockEntityWithProtocol(protocol);
await preparer.prepare(mockEntity);
await preparer.prepare(mockEntity, { logger: getVoidLogger() });
expect(mocks.Clone.clone).toHaveBeenNthCalledWith(
1,
'https://gitlab.com/benjdlambert/backstage-graphql-template',
@@ -106,7 +107,7 @@ describe('GitLabPreparer', () => {
]),
);
mockEntity = mockEntityWithProtocol(protocol);
await preparer.prepare(mockEntity);
await preparer.prepare(mockEntity, { logger: getVoidLogger() });
expect(mocks.Clone.clone).toHaveBeenNthCalledWith(
1,
'https://gitlab.com/benjdlambert/backstage-graphql-template',
@@ -125,7 +126,7 @@ describe('GitLabPreparer', () => {
const preparer = new GitlabPreparer(ConfigReader.fromConfigs([]));
mockEntity = mockEntityWithProtocol(protocol);
delete mockEntity.spec.path;
await preparer.prepare(mockEntity);
await preparer.prepare(mockEntity, { logger: getVoidLogger() });
expect(mocks.Clone.clone).toHaveBeenNthCalledWith(
1,
'https://gitlab.com/benjdlambert/backstage-graphql-template',
@@ -138,7 +139,9 @@ describe('GitLabPreparer', () => {
const preparer = new GitlabPreparer(ConfigReader.fromConfigs([]));
mockEntity = mockEntityWithProtocol(protocol);
mockEntity.spec.path = './template/test/1/2/3';
const response = await preparer.prepare(mockEntity);
const response = await preparer.prepare(mockEntity, {
logger: getVoidLogger(),
});
expect(response.split('\\').join('/')).toMatch(
/\/template\/test\/1\/2\/3$/,
@@ -149,6 +152,7 @@ describe('GitLabPreparer', () => {
const preparer = new GitlabPreparer(ConfigReader.fromConfigs([]));
mockEntity.spec.path = './template/test/1/2/3';
const response = await preparer.prepare(mockEntity, {
logger: getVoidLogger(),
workingDirectory: '/workDir',
});
@@ -19,7 +19,7 @@ import path from 'path';
import { TemplateEntityV1alpha1 } from '@backstage/catalog-model';
import { parseLocationAnnotation } from '../helpers';
import { InputError } from '@backstage/backend-common';
import { PreparerBase } from './types';
import { PreparerBase, PreparerOptions } from './types';
import GitUriParser from 'git-url-parse';
import { Clone, Cred } from 'nodegit';
import { Config } from '@backstage/config';
@@ -35,7 +35,7 @@ export class GitlabPreparer implements PreparerBase {
async prepare(
template: TemplateEntityV1alpha1,
opts?: { workingDirectory?: string },
opts: PreparerOptions,
): Promise<string> {
const { protocol, location } = parseLocationAnnotation(template);
const workingDirectory = opts?.workingDirectory ?? os.tmpdir();
@@ -17,6 +17,11 @@ import type { TemplateEntityV1alpha1 } from '@backstage/catalog-model';
import { Logger } from 'winston';
import { RemoteProtocol } from '../types';
export type PreparerOptions = {
logger: Logger;
workingDirectory?: string;
};
export type PreparerBase = {
/**
* Given an Entity definition from the Service Catalog, go and prepare a directory
@@ -25,7 +30,7 @@ export type PreparerBase = {
*/
prepare(
template: TemplateEntityV1alpha1,
opts?: { logger: Logger; workingDirectory?: string },
opts: PreparerOptions,
): Promise<string>;
};