From d30ef3924bfb9eea9846ffb0097ba6d75efd5e62 Mon Sep 17 00:00:00 2001 From: blam Date: Thu, 8 Oct 2020 22:38:06 +0200 Subject: [PATCH] chore(catalog-backend): updating to use msw + cross-fetch --- plugins/catalog-backend/package.json | 10 ++++------ .../ingestion/processors/AzureApiReaderProcessor.ts | 8 +++----- .../processors/BitbucketApiReaderProcessor.ts | 6 +++--- .../src/ingestion/processors/GithubReaderProcessor.ts | 6 +++--- .../ingestion/processors/GitlabApiReaderProcessor.ts | 6 +++--- .../src/ingestion/processors/GitlabReaderProcessor.ts | 6 +++--- 6 files changed, 19 insertions(+), 23 deletions(-) diff --git a/plugins/catalog-backend/package.json b/plugins/catalog-backend/package.json index 83b9391732..77564d1dee 100644 --- a/plugins/catalog-backend/package.json +++ b/plugins/catalog-backend/package.json @@ -23,10 +23,8 @@ "@backstage/backend-common": "^0.1.1-alpha.24", "@backstage/catalog-model": "^0.1.1-alpha.24", "@backstage/config": "^0.1.1-alpha.24", - "@backstage/test-utils": "^0.1.1-alpha.24", "@octokit/graphql": "^4.5.6", "@types/express": "^4.17.6", - "@types/node-fetch": "^2.5.7", "codeowners-utils": "^1.0.2", "core-js": "^3.6.5", "express": "^4.17.1", @@ -36,8 +34,8 @@ "knex": "^0.21.1", "ldapjs": "^2.2.0", "lodash": "^4.17.15", + "cross-fetch": "^3.0.6", "morgan": "^1.10.0", - "node-fetch": "^2.6.0", "sqlite3": "^5.0.0", "uuid": "^8.0.0", "winston": "^3.2.1", @@ -54,9 +52,9 @@ "@types/supertest": "^2.0.8", "@types/uuid": "^8.0.0", "@types/yup": "^0.28.2", - "jest-fetch-mock": "^3.0.3", - "msw": "^0.20.5", - "supertest": "^4.0.2" + "@backstage/test-utils": "^0.1.1-alpha.24", + "supertest": "^4.0.2", + "msw": "^0.21.2" }, "files": [ "dist", diff --git a/plugins/catalog-backend/src/ingestion/processors/AzureApiReaderProcessor.ts b/plugins/catalog-backend/src/ingestion/processors/AzureApiReaderProcessor.ts index 2a15c18c84..2e28da1d93 100644 --- a/plugins/catalog-backend/src/ingestion/processors/AzureApiReaderProcessor.ts +++ b/plugins/catalog-backend/src/ingestion/processors/AzureApiReaderProcessor.ts @@ -15,7 +15,7 @@ */ import { LocationSpec } from '@backstage/catalog-model'; -import fetch, { RequestInit, HeadersInit } from 'node-fetch'; +import fetch from 'cross-fetch'; import * as result from './results'; import { LocationProcessor, LocationProcessorEmit } from './types'; import { Config } from '@backstage/config'; @@ -44,11 +44,9 @@ export class AzureApiReaderProcessor implements LocationProcessor { ).toString('base64')}`; } - const requestOptions: RequestInit = { + return { headers, }; - - return requestOptions; } async readLocation( @@ -67,7 +65,7 @@ export class AzureApiReaderProcessor implements LocationProcessor { // for private repos when PAT is not valid, Azure API returns a http status code 203 with sign in page html if (response.ok && response.status !== 203) { - const data = await response.buffer(); + const data = Buffer.from(await response.text()); emit(result.data(location, data)); } else { const message = `${location.target} could not be read as ${url}, ${response.status} ${response.statusText}`; diff --git a/plugins/catalog-backend/src/ingestion/processors/BitbucketApiReaderProcessor.ts b/plugins/catalog-backend/src/ingestion/processors/BitbucketApiReaderProcessor.ts index b3720a5b76..a982b4d268 100644 --- a/plugins/catalog-backend/src/ingestion/processors/BitbucketApiReaderProcessor.ts +++ b/plugins/catalog-backend/src/ingestion/processors/BitbucketApiReaderProcessor.ts @@ -15,7 +15,7 @@ */ import { LocationSpec } from '@backstage/catalog-model'; -import fetch, { RequestInit, HeadersInit } from 'node-fetch'; +import fetch from 'cross-fetch'; import * as result from './results'; import { LocationProcessor, LocationProcessorEmit } from './types'; import { Config } from '@backstage/config'; @@ -70,8 +70,8 @@ export class BitbucketApiReaderProcessor implements LocationProcessor { const response = await fetch(url.toString(), this.getRequestOptions()); if (response.ok) { - const data = await response.buffer(); - emit(result.data(location, data)); + const data = await response.text(); + emit(result.data(location, Buffer.from(data))); } else { const message = `${location.target} could not be read as ${url}, ${response.status} ${response.statusText}`; if (response.status === 404) { diff --git a/plugins/catalog-backend/src/ingestion/processors/GithubReaderProcessor.ts b/plugins/catalog-backend/src/ingestion/processors/GithubReaderProcessor.ts index 89d1f69860..372ef5d20b 100644 --- a/plugins/catalog-backend/src/ingestion/processors/GithubReaderProcessor.ts +++ b/plugins/catalog-backend/src/ingestion/processors/GithubReaderProcessor.ts @@ -17,7 +17,7 @@ import { LocationSpec } from '@backstage/catalog-model'; import { Config } from '@backstage/config'; import parseGitUri from 'git-url-parse'; -import fetch, { HeadersInit, RequestInit } from 'node-fetch'; +import fetch from 'cross-fetch'; import { Logger } from 'winston'; import * as result from './results'; import { LocationProcessor, LocationProcessorEmit } from './types'; @@ -252,8 +252,8 @@ export class GithubReaderProcessor implements LocationProcessor { const response = await fetch(url.toString(), options); if (response.ok) { - const data = await response.buffer(); - emit(result.data(location, data)); + const data = await response.text(); + emit(result.data(location, Buffer.from(data))); } else { const message = `${location.target} could not be read as ${url}, ${response.status} ${response.statusText}`; if (response.status === 404) { diff --git a/plugins/catalog-backend/src/ingestion/processors/GitlabApiReaderProcessor.ts b/plugins/catalog-backend/src/ingestion/processors/GitlabApiReaderProcessor.ts index aeba7a7ebe..c55c203b51 100644 --- a/plugins/catalog-backend/src/ingestion/processors/GitlabApiReaderProcessor.ts +++ b/plugins/catalog-backend/src/ingestion/processors/GitlabApiReaderProcessor.ts @@ -15,7 +15,7 @@ */ import { LocationSpec } from '@backstage/catalog-model'; -import fetch, { RequestInit, HeadersInit } from 'node-fetch'; +import fetch from 'cross-fetch'; import * as result from './results'; import { LocationProcessor, LocationProcessorEmit } from './types'; import { Config } from '@backstage/config'; @@ -61,8 +61,8 @@ export class GitlabApiReaderProcessor implements LocationProcessor { const url = this.buildRawUrl(location.target, projectID); const response = await fetch(url.toString(), this.getRequestOptions()); if (response.ok) { - const data = await response.buffer(); - emit(result.data(location, data)); + const data = await response.text(); + emit(result.data(location, Buffer.from(data))); } else { const message = `${location.target} could not be read as ${url}, ${response.status} ${response.statusText}`; if (response.status === 404) { diff --git a/plugins/catalog-backend/src/ingestion/processors/GitlabReaderProcessor.ts b/plugins/catalog-backend/src/ingestion/processors/GitlabReaderProcessor.ts index b51bf85a8e..c682300706 100644 --- a/plugins/catalog-backend/src/ingestion/processors/GitlabReaderProcessor.ts +++ b/plugins/catalog-backend/src/ingestion/processors/GitlabReaderProcessor.ts @@ -15,7 +15,7 @@ */ import { LocationSpec } from '@backstage/catalog-model'; -import fetch from 'node-fetch'; +import fetch from 'cross-fetch'; import * as result from './results'; import { LocationProcessor, LocationProcessorEmit } from './types'; @@ -40,8 +40,8 @@ export class GitlabReaderProcessor implements LocationProcessor { const response = await fetch(url.toString()); if (response.ok) { - const data = await response.buffer(); - emit(result.data(location, data)); + const data = await response.text(); + emit(result.data(location, Buffer.from(data))); } else { const message = `${location.target} could not be read as ${url}, ${response.status} ${response.statusText}`; if (response.status === 404) {