From 2d41b6993fb2d4d65727d28fdb34cf24032da1c7 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 1 Jul 2021 14:47:48 +0200 Subject: [PATCH] catalog-backend: make use of new readUrl method if available Co-authored-by: Johan Haals Signed-off-by: Patrik Oldsberg --- .changeset/rotten-walls-cross.md | 5 +++++ .../src/ingestion/processors/PlaceholderProcessor.ts | 11 ++++++++++- .../src/ingestion/processors/UrlReaderProcessor.ts | 7 ++++++- .../src/ingestion/processors/codeowners/read.ts | 6 ++++++ 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 .changeset/rotten-walls-cross.md diff --git a/.changeset/rotten-walls-cross.md b/.changeset/rotten-walls-cross.md new file mode 100644 index 0000000000..498c10b203 --- /dev/null +++ b/.changeset/rotten-walls-cross.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend': patch +--- + +Make use of the new `readUrl` method on `UrlReader` from `@backstage/backend-common`. diff --git a/plugins/catalog-backend/src/ingestion/processors/PlaceholderProcessor.ts b/plugins/catalog-backend/src/ingestion/processors/PlaceholderProcessor.ts index 8c06ef419e..1b015d7d93 100644 --- a/plugins/catalog-backend/src/ingestion/processors/PlaceholderProcessor.ts +++ b/plugins/catalog-backend/src/ingestion/processors/PlaceholderProcessor.ts @@ -94,12 +94,21 @@ export class PlaceholderProcessor implements CatalogProcessor { return [data, false]; } + const read = async (url: string): Promise => { + if (this.options.reader.readUrl) { + const response = await this.options.reader.readUrl(url); + const buffer = await response.buffer(); + return buffer; + } + return this.options.reader.read(url); + }; + return [ await resolver({ key: resolverKey, value: resolverValue, baseUrl: location.target, - read: this.options.reader.read.bind(this.options.reader), + read, }), true, ]; diff --git a/plugins/catalog-backend/src/ingestion/processors/UrlReaderProcessor.ts b/plugins/catalog-backend/src/ingestion/processors/UrlReaderProcessor.ts index aff126414f..da7bee12d8 100644 --- a/plugins/catalog-backend/src/ingestion/processors/UrlReaderProcessor.ts +++ b/plugins/catalog-backend/src/ingestion/processors/UrlReaderProcessor.ts @@ -101,7 +101,12 @@ export class UrlReaderProcessor implements CatalogProcessor { return Promise.all(output); } - // Otherwise do a plain read + // Otherwise do a plain read, prioritizing readUrl if available + if (this.options.reader.readUrl) { + const data = await this.options.reader.readUrl(location); + return [{ url: location, data: await data.buffer() }]; + } + const data = await this.options.reader.read(location); return [{ url: location, data }]; } diff --git a/plugins/catalog-backend/src/ingestion/processors/codeowners/read.ts b/plugins/catalog-backend/src/ingestion/processors/codeowners/read.ts index ae4255555a..c6b89efcc9 100644 --- a/plugins/catalog-backend/src/ingestion/processors/codeowners/read.ts +++ b/plugins/catalog-backend/src/ingestion/processors/codeowners/read.ts @@ -28,6 +28,12 @@ export async function readCodeOwners( ): Promise { const readOwnerLocation = async (path: string): Promise => { const url = `${sourceUrl}${path}`; + + if (reader.readUrl) { + const data = await reader.readUrl(url); + const buffer = await data.buffer(); + return buffer.toString(); + } const data = await reader.read(url); return data.toString(); };