From 0cb28c22e30eb6bce66520149a5b5fb86d98ad92 Mon Sep 17 00:00:00 2001 From: blam Date: Tue, 7 Sep 2021 11:55:14 +0200 Subject: [PATCH 1/7] feat: when reading from AzureDevOps strip the root directory as that is how the ZipArchive is returned Signed-off-by: blam --- .../writing-custom-field-extensions.md | 34 ++++++++++++++++ .../src/reading/AzureUrlReader.ts | 1 + .../reading/tree/ReadTreeResponseFactory.ts | 1 + packages/backend-common/src/reading/types.ts | 3 ++ .../src/test-integrations.ts | 40 +++++++++++++++++++ 5 files changed, 79 insertions(+) create mode 100644 docs/features/software-templates/writing-custom-field-extensions.md create mode 100644 plugins/scaffolder-backend/src/test-integrations.ts diff --git a/docs/features/software-templates/writing-custom-field-extensions.md b/docs/features/software-templates/writing-custom-field-extensions.md new file mode 100644 index 0000000000..e32aa4e78d --- /dev/null +++ b/docs/features/software-templates/writing-custom-field-extensions.md @@ -0,0 +1,34 @@ +--- +id: writing-custom-field-extensions +title: Writing Custom Field Extensions +description: How to write your own field extensions +--- + +Collecting input from the user is a very large part of the scaffolding process +and Software Templates as a whole. Sometimes the built in components and fields +just aren't good enough, and sometimes you want to enrich the form that the +users sees with better inputs that fit better. + +This is where `Custom Field Extensions` come in. + +With them you can show your own `React` Components and use them to control the +state of the JSON schema, as well as provide your own validation functions to +validate the data too. + +## Creating a Field Extension + +Field extensions are a way to combine an ID, a `React` Component and a +`validation` function together in a modular way that you can then use to pass to +the `Scaffolder` frontend plugin in your own `App.tsx`. + +You can create your own Field Extension by using the +`createScaffolderFieldExtension` `API` like below: + +```tsx +//packages/app/scaffolder/MyCustomExtension/MyCustomExtension.tsx +export const MyCustomExtension = () => {}; +``` + +```tsx +// packages/app/scaffolder/MyCustomExtension/validation.ts +``` diff --git a/packages/backend-common/src/reading/AzureUrlReader.ts b/packages/backend-common/src/reading/AzureUrlReader.ts index 910e8e04a5..5369306702 100644 --- a/packages/backend-common/src/reading/AzureUrlReader.ts +++ b/packages/backend-common/src/reading/AzureUrlReader.ts @@ -133,6 +133,7 @@ export class AzureUrlReader implements UrlReader { stream: archiveAzureResponse.body as unknown as Readable, etag: commitSha, filter: options?.filter, + stripFirstDirectory: true, }); } diff --git a/packages/backend-common/src/reading/tree/ReadTreeResponseFactory.ts b/packages/backend-common/src/reading/tree/ReadTreeResponseFactory.ts index 5fdd633102..70d8f63af8 100644 --- a/packages/backend-common/src/reading/tree/ReadTreeResponseFactory.ts +++ b/packages/backend-common/src/reading/tree/ReadTreeResponseFactory.ts @@ -55,6 +55,7 @@ export class DefaultReadTreeResponseFactory implements ReadTreeResponseFactory { this.workDir, options.etag, options.filter, + options.stripFirstDirectory, ); } } diff --git a/packages/backend-common/src/reading/types.ts b/packages/backend-common/src/reading/types.ts index b7e1ff823f..2d9c305b85 100644 --- a/packages/backend-common/src/reading/types.ts +++ b/packages/backend-common/src/reading/types.ts @@ -182,6 +182,9 @@ export type ReadTreeResponseFactoryOptions = { etag: string; // Filter passed on from the ReadTreeOptions filter?: (path: string, info?: { size: number }) => boolean; + + // Strip the first directory in the readTree response + stripFirstDirectory?: boolean; }; /** @public */ diff --git a/plugins/scaffolder-backend/src/test-integrations.ts b/plugins/scaffolder-backend/src/test-integrations.ts new file mode 100644 index 0000000000..65d84768aa --- /dev/null +++ b/plugins/scaffolder-backend/src/test-integrations.ts @@ -0,0 +1,40 @@ +/* + * Copyright 2021 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { + loadBackendConfig, + getRootLogger, + UrlReaders, +} from '@backstage/backend-common'; +// import { ScmIntegrations } from '@backstage/integration'; + +const run = async () => { + const root = getRootLogger(); + const config = await loadBackendConfig({ + argv: process.argv, + logger: root, + }); + + // const integrations = ScmIntegrations.fromConfig(config); + const reader = UrlReaders.default({ logger: root, config }); + + console.log( + await reader.readTree( + 'https://dev.azure.com/backstage-verification/test-template-fetch/_git/test-template-fetch', + ), + ); +}; + +run(); From e86adc689210c7cabdc73d90306797e71d2d680d Mon Sep 17 00:00:00 2001 From: blam Date: Tue, 7 Sep 2021 12:00:33 +0200 Subject: [PATCH 2/7] chore: remove unwanted files Signed-off-by: blam --- .../writing-custom-field-extensions.md | 34 ---------------- .../src/test-integrations.ts | 40 ------------------- 2 files changed, 74 deletions(-) delete mode 100644 docs/features/software-templates/writing-custom-field-extensions.md delete mode 100644 plugins/scaffolder-backend/src/test-integrations.ts diff --git a/docs/features/software-templates/writing-custom-field-extensions.md b/docs/features/software-templates/writing-custom-field-extensions.md deleted file mode 100644 index e32aa4e78d..0000000000 --- a/docs/features/software-templates/writing-custom-field-extensions.md +++ /dev/null @@ -1,34 +0,0 @@ ---- -id: writing-custom-field-extensions -title: Writing Custom Field Extensions -description: How to write your own field extensions ---- - -Collecting input from the user is a very large part of the scaffolding process -and Software Templates as a whole. Sometimes the built in components and fields -just aren't good enough, and sometimes you want to enrich the form that the -users sees with better inputs that fit better. - -This is where `Custom Field Extensions` come in. - -With them you can show your own `React` Components and use them to control the -state of the JSON schema, as well as provide your own validation functions to -validate the data too. - -## Creating a Field Extension - -Field extensions are a way to combine an ID, a `React` Component and a -`validation` function together in a modular way that you can then use to pass to -the `Scaffolder` frontend plugin in your own `App.tsx`. - -You can create your own Field Extension by using the -`createScaffolderFieldExtension` `API` like below: - -```tsx -//packages/app/scaffolder/MyCustomExtension/MyCustomExtension.tsx -export const MyCustomExtension = () => {}; -``` - -```tsx -// packages/app/scaffolder/MyCustomExtension/validation.ts -``` diff --git a/plugins/scaffolder-backend/src/test-integrations.ts b/plugins/scaffolder-backend/src/test-integrations.ts deleted file mode 100644 index 65d84768aa..0000000000 --- a/plugins/scaffolder-backend/src/test-integrations.ts +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 2021 The Backstage Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -import { - loadBackendConfig, - getRootLogger, - UrlReaders, -} from '@backstage/backend-common'; -// import { ScmIntegrations } from '@backstage/integration'; - -const run = async () => { - const root = getRootLogger(); - const config = await loadBackendConfig({ - argv: process.argv, - logger: root, - }); - - // const integrations = ScmIntegrations.fromConfig(config); - const reader = UrlReaders.default({ logger: root, config }); - - console.log( - await reader.readTree( - 'https://dev.azure.com/backstage-verification/test-template-fetch/_git/test-template-fetch', - ), - ); -}; - -run(); From a316cd838da728b051deafc018dd2f444159114c Mon Sep 17 00:00:00 2001 From: blam Date: Wed, 8 Sep 2021 10:59:40 +0200 Subject: [PATCH 3/7] chore: reverting stripping as we don't need it Signed-off-by: blam --- packages/backend-common/src/reading/AzureUrlReader.ts | 6 +++--- packages/backend-common/src/reading/tree/util.ts | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/backend-common/src/reading/AzureUrlReader.ts b/packages/backend-common/src/reading/AzureUrlReader.ts index 5369306702..2269393cbf 100644 --- a/packages/backend-common/src/reading/AzureUrlReader.ts +++ b/packages/backend-common/src/reading/AzureUrlReader.ts @@ -94,8 +94,6 @@ export class AzureUrlReader implements UrlReader { url: string, options?: ReadTreeOptions, ): Promise { - // TODO: Support filepath based reading tree feature like other providers - // Get latest commit SHA const commitsAzureResponse = await fetch( @@ -129,11 +127,13 @@ export class AzureUrlReader implements UrlReader { throw new Error(message); } + const { filepath } = parseGitUrl(url); + return await this.deps.treeResponseFactory.fromZipArchive({ stream: archiveAzureResponse.body as unknown as Readable, etag: commitSha, filter: options?.filter, - stripFirstDirectory: true, + subpath: filepath, }); } diff --git a/packages/backend-common/src/reading/tree/util.ts b/packages/backend-common/src/reading/tree/util.ts index cfb986a0b0..5ddc5a3766 100644 --- a/packages/backend-common/src/reading/tree/util.ts +++ b/packages/backend-common/src/reading/tree/util.ts @@ -21,5 +21,6 @@ const directoryNameRegex = /^[^\/]+\//; // Removes the first segment of a forward-slash-separated path export function stripFirstDirectoryFromPath(path: string): string { + console.log('stripFirstDirectoryPath path:', path); return path.replace(directoryNameRegex, ''); } From e7b596fdae5922ca08d1104638bd4f769cd25ce1 Mon Sep 17 00:00:00 2001 From: blam Date: Wed, 8 Sep 2021 11:00:59 +0200 Subject: [PATCH 4/7] chore: removing stripFirstDirectory it's not needed Signed-off-by: blam --- .../backend-common/src/reading/tree/ReadTreeResponseFactory.ts | 1 - packages/backend-common/src/reading/types.ts | 3 --- 2 files changed, 4 deletions(-) diff --git a/packages/backend-common/src/reading/tree/ReadTreeResponseFactory.ts b/packages/backend-common/src/reading/tree/ReadTreeResponseFactory.ts index 70d8f63af8..5fdd633102 100644 --- a/packages/backend-common/src/reading/tree/ReadTreeResponseFactory.ts +++ b/packages/backend-common/src/reading/tree/ReadTreeResponseFactory.ts @@ -55,7 +55,6 @@ export class DefaultReadTreeResponseFactory implements ReadTreeResponseFactory { this.workDir, options.etag, options.filter, - options.stripFirstDirectory, ); } } diff --git a/packages/backend-common/src/reading/types.ts b/packages/backend-common/src/reading/types.ts index 2d9c305b85..b7e1ff823f 100644 --- a/packages/backend-common/src/reading/types.ts +++ b/packages/backend-common/src/reading/types.ts @@ -182,9 +182,6 @@ export type ReadTreeResponseFactoryOptions = { etag: string; // Filter passed on from the ReadTreeOptions filter?: (path: string, info?: { size: number }) => boolean; - - // Strip the first directory in the readTree response - stripFirstDirectory?: boolean; }; /** @public */ From bb7ce1c64c16623f11d047431b915a5538422a3e Mon Sep 17 00:00:00 2001 From: blam Date: Wed, 8 Sep 2021 11:06:24 +0200 Subject: [PATCH 5/7] chore: added test for subpath azure Signed-off-by: blam --- .../src/reading/AzureUrlReader.test.ts | 15 +++++++++++++++ packages/backend-common/src/reading/tree/util.ts | 1 - 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/backend-common/src/reading/AzureUrlReader.test.ts b/packages/backend-common/src/reading/AzureUrlReader.test.ts index c7c8853929..fc1f6a03f3 100644 --- a/packages/backend-common/src/reading/AzureUrlReader.test.ts +++ b/packages/backend-common/src/reading/AzureUrlReader.test.ts @@ -220,6 +220,21 @@ describe('AzureUrlReader', () => { expect(indexMarkdownFile.toString()).toBe('# Test\n'); }); + it('returns the wanted files from an archive when a subpath is passed through', async () => { + const response = await processor.readTree( + 'https://dev.azure.com/organization/project/_git/repository?path=%2Fdocs', + ); + + expect(response.etag).toBe('123abc2'); + + const files = await response.files(); + + expect(files.length).toBe(1); + const indexMarkdownFile = await files[0].content(); + + expect(indexMarkdownFile.toString()).toBe('# Test\n'); + }); + it('creates a directory with the wanted files', async () => { const response = await processor.readTree( 'https://dev.azure.com/organization/project/_git/repository', diff --git a/packages/backend-common/src/reading/tree/util.ts b/packages/backend-common/src/reading/tree/util.ts index 5ddc5a3766..cfb986a0b0 100644 --- a/packages/backend-common/src/reading/tree/util.ts +++ b/packages/backend-common/src/reading/tree/util.ts @@ -21,6 +21,5 @@ const directoryNameRegex = /^[^\/]+\//; // Removes the first segment of a forward-slash-separated path export function stripFirstDirectoryFromPath(path: string): string { - console.log('stripFirstDirectoryPath path:', path); return path.replace(directoryNameRegex, ''); } From fdaa61a87b2d25f565476db13c7aa2c79779ed7f Mon Sep 17 00:00:00 2001 From: blam Date: Wed, 8 Sep 2021 11:08:01 +0200 Subject: [PATCH 6/7] chore: added changeset Signed-off-by: blam --- .changeset/early-seahorses-stare.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/early-seahorses-stare.md diff --git a/.changeset/early-seahorses-stare.md b/.changeset/early-seahorses-stare.md new file mode 100644 index 0000000000..963058a26f --- /dev/null +++ b/.changeset/early-seahorses-stare.md @@ -0,0 +1,5 @@ +--- +'@backstage/backend-common': patch +--- + +Fixing issue with `AzureUrlReader` that doesn't do `subpath` directories correctly From 8e6e95fc434fa5b0544774a7f5233e19da71162b Mon Sep 17 00:00:00 2001 From: blam Date: Wed, 8 Sep 2021 11:17:13 +0200 Subject: [PATCH 7/7] chore: remove the `stripFirstDirectory` option to the `ZipArchiveResponse` Signed-off-by: blam --- .../reading/tree/ZipArchiveResponse.test.ts | 30 ------------------- .../src/reading/tree/ZipArchiveResponse.ts | 20 ++----------- 2 files changed, 3 insertions(+), 47 deletions(-) diff --git a/packages/backend-common/src/reading/tree/ZipArchiveResponse.test.ts b/packages/backend-common/src/reading/tree/ZipArchiveResponse.test.ts index f6f9a7845a..a54bac0c4a 100644 --- a/packages/backend-common/src/reading/tree/ZipArchiveResponse.test.ts +++ b/packages/backend-common/src/reading/tree/ZipArchiveResponse.test.ts @@ -62,36 +62,6 @@ describe('ZipArchiveResponse', () => { ]); }); - it('should read files and strip root dir if requested', async () => { - const stream = fs.createReadStream('/test-archive-with-extra-root-dir.zip'); - - const res = new ZipArchiveResponse( - stream, - '', - '/tmp', - 'etag', - undefined, - true, - ); - const files = await res.files(); - - expect(files).toEqual([ - { - path: 'mkdocs.yml', - content: expect.any(Function), - }, - { - path: 'docs/index.md', - content: expect.any(Function), - }, - ]); - const contents = await Promise.all(files.map(f => f.content())); - expect(contents.map(c => c.toString('utf8').trim())).toEqual([ - 'site_name: Test', - '# Test', - ]); - }); - it('should read files with filter', async () => { const stream = fs.createReadStream('/test-archive.zip'); diff --git a/packages/backend-common/src/reading/tree/ZipArchiveResponse.ts b/packages/backend-common/src/reading/tree/ZipArchiveResponse.ts index fb22f24b94..e07cedde87 100644 --- a/packages/backend-common/src/reading/tree/ZipArchiveResponse.ts +++ b/packages/backend-common/src/reading/tree/ZipArchiveResponse.ts @@ -24,7 +24,6 @@ import { ReadTreeResponseDirOptions, ReadTreeResponseFile, } from '../types'; -import { stripFirstDirectoryFromPath } from './util'; /** * Wraps a zip archive stream into a tree response reader. @@ -38,7 +37,6 @@ export class ZipArchiveResponse implements ReadTreeResponse { private readonly workDir: string, public readonly etag: string, private readonly filter?: (path: string, info: { size: number }) => boolean, - private readonly stripFirstDirectory?: boolean, ) { if (subPath) { if (!subPath.endsWith('/')) { @@ -68,12 +66,8 @@ export class ZipArchiveResponse implements ReadTreeResponse { } private shouldBeIncluded(entry: Entry): boolean { - const strippedPath = this.stripFirstDirectory - ? stripFirstDirectoryFromPath(entry.path) - : entry.path; - if (this.subPath) { - if (!strippedPath.startsWith(this.subPath)) { + if (!entry.path.startsWith(this.subPath)) { return false; } } @@ -102,11 +96,7 @@ export class ZipArchiveResponse implements ReadTreeResponse { if (this.shouldBeIncluded(entry)) { files.push({ - path: this.getInnerPath( - this.stripFirstDirectory - ? stripFirstDirectoryFromPath(entry.path) - : entry.path, - ), + path: this.getInnerPath(entry.path), content: () => entry.buffer(), }); } else { @@ -154,11 +144,7 @@ export class ZipArchiveResponse implements ReadTreeResponse { // Ignore directory entries since we handle that with the file entries // as a zip can have files with directories without directory entries if (entry.type === 'File' && this.shouldBeIncluded(entry)) { - const entryPath = this.getInnerPath( - this.stripFirstDirectory - ? stripFirstDirectoryFromPath(entry.path) - : entry.path, - ); + const entryPath = this.getInnerPath(entry.path); const dirname = platformPath.dirname(entryPath); if (dirname) { await fs.mkdirp(platformPath.join(dir, dirname));