diff --git a/.changeset/dull-icons-share.md b/.changeset/dull-icons-share.md new file mode 100644 index 0000000000..aa4c01e422 --- /dev/null +++ b/.changeset/dull-icons-share.md @@ -0,0 +1,21 @@ +--- +'@backstage/backend-common': minor +'@backstage/cli': minor +'@backstage/config-loader': minor +'example-backend': patch +'@backstage/create-app': patch +--- + +**BREAKING CHANGE** + +The existing loading of additional config files like `app-config.development.yaml` using APP_ENV or NODE_ENV has been removed. +Instead, the CLI and backend process now accept one or more `--config` flags to load config files. + +Without passing any flags, `app-config.yaml` and, if it exists, `app-config.local.yaml` will be loaded. +If passing any `--config ` flags, only those files will be loaded, **NOT** the default `app-config.yaml` one. + +The old behaviour of for example `APP_ENV=development` can be replicated using the following flags: + +```bash +--config ../../app-config.yaml --config ../../app-config.development.yaml +``` diff --git a/.changeset/popular-jars-serve.md b/.changeset/popular-jars-serve.md new file mode 100644 index 0000000000..6ee40195fc --- /dev/null +++ b/.changeset/popular-jars-serve.md @@ -0,0 +1,5 @@ +--- +'@backstage/catalog-model': minor +--- + +Enable adding locations for config files that does not yet exist by adding a flag to api request diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bcce2966f..f445c189f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,16 @@ If you encounter issues while upgrading to a newer version, don't hesitate to re ## Next Release +### @backstage/cli + +- Configuration files are no longer selected through `APP_ENV` or `NODE_ENV`. The default configuration files are `app-config.yaml` and, fix it exists, `app-config.local.yaml` in the repo root. To load a different set of files, use `--config ` arguments. + +### @backstage/backend-common + +- Configuration files are no longer selected through `APP_ENV` or `NODE_ENV`. The default configuration files are `app-config.yaml` and, fix it exists, `app-config.local.yaml` in the repo root. To load a different set of files, use `--config ` arguments. + +## v0.1.1-alpha.25 + > Collect changes for the next release below ### @backstage/cli diff --git a/app-config.development.yaml b/app-config.development.yaml deleted file mode 100644 index 817847c6d6..0000000000 --- a/app-config.development.yaml +++ /dev/null @@ -1,13 +0,0 @@ -app: - baseUrl: http://localhost:3000 - -backend: - baseUrl: http://localhost:7000 - listen: - port: 7000 - cors: - origin: http://localhost:3000 - methods: [GET, POST, PUT, DELETE] - credentials: true - csp: - connect-src: ["'self'", 'http:', 'https:'] diff --git a/app-config.yaml b/app-config.yaml index 6e49e765cd..05bf39b5b3 100644 --- a/app-config.yaml +++ b/app-config.yaml @@ -1,6 +1,6 @@ app: title: Backstage Example App - baseUrl: http://localhost:7000 + baseUrl: http://localhost:3000 googleAnalyticsTrackingId: # UA-000000-0 backend: @@ -10,8 +10,12 @@ backend: database: client: sqlite3 connection: ':memory:' + cors: + origin: http://localhost:3000 + methods: [GET, POST, PUT, DELETE] + credentials: true csp: - connect-src: ["'self'", 'https:'] + connect-src: ["'self'", 'http:', 'https:'] # See README.md in the proxy-backend plugin for information on the configuration format proxy: diff --git a/contrib/kubernetes/plain_single_backend_deplyoment/deployment.yaml b/contrib/kubernetes/plain_single_backend_deplyoment/deployment.yaml index 87dee94895..a20c47229d 100644 --- a/contrib/kubernetes/plain_single_backend_deplyoment/deployment.yaml +++ b/contrib/kubernetes/plain_single_backend_deplyoment/deployment.yaml @@ -28,6 +28,9 @@ spec: image: example-backend:latest imagePullPolicy: Never + command: [node, packages/backend] + args: [--config, app-config.yaml, --config, k8s-config.yaml] + env: # We set this to development to make the backend start with incomplete configuration. In a production # deployment you will want to make sure that you have a full configuration, and remove any plugins that @@ -35,10 +38,6 @@ spec: - name: NODE_ENV value: development - # This makes us load in `app-config.production.yaml` if there is one. - - name: APP_ENV - value: production - # This makes it possible for the app to reach the backend when serving through `kubectl proxy` # If you expose the service using for example an ingress controller, you should # switch this out or remove it. @@ -54,8 +53,8 @@ spec: volumeMounts: - name: config-volume - mountPath: /usr/src/app/app-config.local.yaml - subPath: app-config.local.yaml + mountPath: /usr/src/app/k8s-config.yaml + subPath: k8s-config.yaml resources: limits: @@ -77,7 +76,7 @@ spec: name: backstage-config items: - key: app-config - path: app-config.local.yaml + path: k8s-config.yaml --- apiVersion: v1 kind: ConfigMap diff --git a/docs/conf/index.md b/docs/conf/index.md index e1dfc30582..2c3ee06ba6 100644 --- a/docs/conf/index.md +++ b/docs/conf/index.md @@ -15,11 +15,11 @@ allowing for customization. ## Supplying Configuration -Configuration is stored in `app-config.yaml` files, with support for suffixes -such as `app-config.production.yaml` to override values for specific -environments. The configuration files themselves contain plain YAML, but with -support for loading in secrets from various sources using for example `$env` and -`$file` keys. +Configuration is stored in YAML files where the defaults are `app-config.yaml` +and `app-config.local.yaml` for local overrides. Other sets of files can by +loaded by passing `--config ` flags. The configuration files themselves +contain plain YAML, but with support for loading in secrets from various sources +using for example `$env` and `$file` keys. It is also possible to supply configuration through environment variables, for example `APP_CONFIG_app_baseUrl=https://staging.example.com`. However these diff --git a/docs/conf/writing.md b/docs/conf/writing.md index bb4d0beb75..06d4b52f3f 100644 --- a/docs/conf/writing.md +++ b/docs/conf/writing.md @@ -55,20 +55,28 @@ picked up by the serve tasks of `@backstage/cli` for local development, and are injected by the entrypoint of the nginx container serving the frontend in a production build. -## File Resolution +## Configuration Files It is possible to have multiple configuration files, both to support different environments, but also to define configuration that is local to specific -packages. +packages. The configuration files to load are selected using a `--config ` +flag, and it is possible to load any number of files. Paths are relative to the +working directory of the executed process, for example `package/backend`. This +means that to select a config file in the repo root when running the backend, +you would use `--config ../../my-config.yaml`. -All `app-config.yaml` files inside the monorepo root and package root are -considered, as are files with additional `local` and environment affixes such as -`development`, for example `app-config.local.yaml`, -`app-config.production.yaml`, and `app-config.development.local.yaml`. Which -environment config files are loaded is determined by the `APP_ENV` environment -variable, or `NODE_ENV` if it is not set. Local configuration files are always -loaded, but are meant for local development overrides and should typically be -`.gitignore`'d. +If no `config` flags are specified, the default behavior is to load +`app-config.yaml` and, if it exists, `app-config.local.yaml` from the repo root. +In the provided project setup, `app-config.local.yaml` is `.gitignore`'d, making +it a good place to add config overrides and secrets for local development. + +Note that if any config flags are provided, the default `app-config.yaml` files +are NOT loaded. To include them you need to explicitly include them with a flag, +for example: + +``` +yarn start --config ../../app-config.yaml --config ../../app-config.staging.yaml +``` All loaded configuration files are merged together using the following rules: @@ -84,10 +92,10 @@ order: - Configuration from the `APP_CONFIG_` environment variables has the highest priority, followed by files. -- Files inside package directories have higher priority than those in the root - directory. -- Files with environment affixes have higher priority than ones without. -- Files with the `local` affix have higher priority than ones without. +- Files loaded with config flags are ordered by priority, where the last flag + has the highest priority. +- If no config flags are provided, `app-config.local.yaml` has higher priority + than `app-config.yaml`. ## Secrets diff --git a/docs/features/techdocs/README.md b/docs/features/techdocs/README.md index cec5b63cc7..e2b81b4a53 100644 --- a/docs/features/techdocs/README.md +++ b/docs/features/techdocs/README.md @@ -82,7 +82,9 @@ for companies to adopt. This involves (something like) the following work items. - “Solidify” work and “Mkdocs stabilization” work that has come out of our Q3 end-to-end work. - Improve/simplify the get up and running process. -- Introduce doc template Software Templates. +- Introduce new documentation templates. +- Extend the already existing docs-template to have options of different + documentation types. - Enable companies to choose their own storage (S3 for example). - Enable companies to choose their own source code hosting provider (GitHub, GitLab, and so). diff --git a/docs/support/project-structure.md b/docs/support/project-structure.md index 277ca3929f..987d6a8ff3 100644 --- a/docs/support/project-structure.md +++ b/docs/support/project-structure.md @@ -228,9 +228,6 @@ future. - [`app-config.yaml`](https://github.com/spotify/backstage/tree/master/app-config.yaml) - Configuration for the app, both frontend and backend -- [`app-config.development.yaml`](https://github.com/spotify/backstage/tree/master/app-config.development.yaml) - - Used for overriding configuration when developing locally. - - [`catalog-info.yaml`](https://github.com/spotify/backstage/tree/master/catalog-info.yaml) - Description of Backstage in the Backstage Entity format. diff --git a/microsite/blog/2020-10-22-cost-insights-plugin.md b/microsite/blog/2020-10-22-cost-insights-plugin.md new file mode 100644 index 0000000000..421ebacdd2 --- /dev/null +++ b/microsite/blog/2020-10-22-cost-insights-plugin.md @@ -0,0 +1,79 @@ +--- +title: New Cost Insights plugin: The engineer’s solution to taming cloud costs +author: Janisa Anandamohan +authorURL: https://twitter.com/janisa_a +--- + +How did Spotify save millions on cloud costs within a matter of months?? We made cost optimization just another part of the daily development process. Our newly open sourced [Cost Insights plugin](https://github.com/spotify/backstage/tree/master/plugins/cost-insights) makes a team’s cloud costs visible — and actionable — right inside Backstage. So engineers can see the impact of their cloud usage (down to a product and resource level) and make optimizations wherever and whenever it makes sense. By managing cloud costs from the ground up, you can make smarter decisions that let you continue to build and scale quickly, without wasting resources. + + + +Are we turning engineers into accountants? Nope, we’re just letting engineers do what they do best, in the place that feels natural to them: inside Backstage. + + + +## Why put a cost management tool in the hands of engineers? + +Engineers are closest to the metal in terms of knowing why a specific feature, product, or service is using cloud resources. So they’re in the best position to understand how costs impact ongoing development (and vice versa). + +If you manage costs top-down from a 10,000-foot view of your cloud infrastructure, you’re likely making decisions far removed from products, especially in larger organizations. Set a broad cost-cutting goal, and you could be creating unintended consequences — curtailing spending at the expense of growth or experimentation. + +## Ground-level intelligence, data-driven solutions + +Our hypothesis at Spotify was, if you bring spending data into an engineer’s everyday development workflow, they’ll naturally look for cost optimizations just like they look for any other optimization. And the cost optimizations will be more efficient and effective, because the decisions are informed at the ground level. + +The problem is that most cloud platforms don’t provide cost data at a granular enough level to make those decisions. And the bigger your organization (say, two-thousand-microservices and four-thousand-data-pipelines big, like Spotify), then the less you can attribute these large, fuzzy numbers to the right team, let alone a shipping product or internal service. + +That’s where Cost Insights comes in. Instead of making cost management and product development separate departments on the org chart, Backstage brings them together — with a level of detail and specificity engineers relate and respond to. + +## How to turn dollars into sense + +It’s not enough to make costs visible. To be useful, the numbers need to be relevant, relatable, and actionable. In other words, not just cost information, but insights. There are several ways the plugin puts data from your cloud provider in a more helpful context. + +### Use business metrics to evaluate costs + +Cost Insights will show you trends at a glance and also let you compare costs quarter over quarter. More importantly, you can also evaluate costs against business metrics that you care most about. In the example below, should the upward slope shown in the first screen be cause for worry? Perhaps not — if you switch views, you’ll see that cost per daily average user (DAU) is actually going down. Exactly what you hope to see as you scale. + +![Comparing costs to DAU](assets/20-10-22/cost-insights-1-dau.gif) +_(Note: Screens are examples; they do not show real data.)_ + +### Illustrate costs with relatable, real-world comparisons + +In addition to dollar amounts, Cost Insights allows teams to visualize and convert cost overages into more relatable terms. In the example below, we equate the growth in costs for virtual machine instances (100% increase) to developer time spent (about 1 engineer). We use this particular comparison in the plugin because we found it resonated with our own engineers — providing a useful perspective for spending increases. You can configure what the “cost of an engineer” means to your organization. Or engineers can build in their own comparisons — cups of coffee, carbon offset credits, electric luxury vehicles — whatever makes costs more tangible for them. + +![Cost growth as engineering time](assets/20-10-22/cost-insights-2-engineer.png) +_(Note: Screens are examples; they do not show real data.)_ + +### Tie spending to specific products and resources + +The more detailed the cost data, the more relevant, actionable, and helpful it is. Cost Insights allows you to attribute costs to products and resources in a way that makes sense to your engineers. For example, here we see a breakdown of data processing costs by individual pipelines. This allows your team to target optimizations more precisely. + +![Data Processing costs by pipeline](assets/20-10-22/cost-insights-3-data.png) +_(Note: Screens are examples; they do not show real data.)_ + +## Driving down costs without slowing down development + +When it comes to cutting costs, we actually want to guard against over-optimization. Growth and costs can go hand in hand. The trick is knowing when one is out of balance and needs addressing. Our product highlights when there’s been a large increase in spending, so that engineers are thinking about cost only when they must and aren’t distracted from their set goals and priorities. + +Engineers can then determine for themselves if the time invested in an optimization was valuable compared to the costs saved. Cost Insights puts the decision in our engineers’ hands for them to choose when to focus on growth efforts and when to focus on cost. Control, as ever, remains with our developers, where we think it belongs. + +## Getting started + +You can begin working with the Cost Insights plugin today on [GitHub](https://github.com/spotify/backstage/tree/master/plugins/cost-insights). We include an example client with static data in the expected format. The `CostInsightsApi` should talk with a cloud billing backend that aggregates billing data from your cloud provider. + +The current release of Cost Insights includes: + +- Daily cost graph by team or billing account +- Cost comparisons against configurable business metrics (including an option for Daily Active Users) +- Insights panels — configurable for the cloud products your company uses +- Cost alerts and recommendations +- Selectable time periods for month-over-month or quarter-over-quarter comparisons +- Conversion of cost growth into “cost of average engineer” to help optimization trade-off decisions + +Our hope is to help other companies translate their cloud cost in a relatable way for their engineers to better understand their impact and accurately identify their opportunities for optimizations. + +And if you’re interested in contributing to our outstanding issues, you can find them in the issues queue, filtered under the [‘cost-insights’ label](https://github.com/spotify/backstage/labels/cost-insights). + +## Ready for DevSecCostOpsPlus (and whatever’s next) + +There’s DevOps, there’s DevSecOps, and then there’s Backstage: one frontend for all your infrastructure. From building, testing, and deploying to monitoring and security — Backstage helps you manage your entire tech organization and provides a seamless developer experience for engineers, from end to end to end. And now that also extends to cost management for your cloud infrastructure and tooling. Happy building and [happy optimizing](https://github.com/spotify/backstage/tree/master/plugins/cost-insights). diff --git a/microsite/blog/assets/20-10-22/cost-insights-1-dau.gif b/microsite/blog/assets/20-10-22/cost-insights-1-dau.gif new file mode 100644 index 0000000000..df9396439d Binary files /dev/null and b/microsite/blog/assets/20-10-22/cost-insights-1-dau.gif differ diff --git a/microsite/blog/assets/20-10-22/cost-insights-2-engineer.png b/microsite/blog/assets/20-10-22/cost-insights-2-engineer.png new file mode 100644 index 0000000000..1e9293f5c1 Binary files /dev/null and b/microsite/blog/assets/20-10-22/cost-insights-2-engineer.png differ diff --git a/microsite/blog/assets/20-10-22/cost-insights-3-data.png b/microsite/blog/assets/20-10-22/cost-insights-3-data.png new file mode 100644 index 0000000000..941f0487da Binary files /dev/null and b/microsite/blog/assets/20-10-22/cost-insights-3-data.png differ diff --git a/microsite/pages/en/demos.js b/microsite/pages/en/demos.js index c6657c6a31..63c15ab698 100644 --- a/microsite/pages/en/demos.js +++ b/microsite/pages/en/demos.js @@ -78,7 +78,41 @@ const Background = props => { - + + + + Control cloud costs + + How do you control cloud costs while maintaining the speed and + independence of your development teams? With the{' '} + Cost Insights plugin{' '} + for Backstage, managing cloud costs becomes just another part of + an engineer’s daily development process. They get a clear view of + their spending — and can decide for themselves how they want to + optimize it. Learn more about the{' '} + + Cost Insights plugin + + . + + + Watch now + + + + + + + + + @@ -96,7 +130,7 @@ const Background = props => { . - + Watch now diff --git a/packages/backend-common/package.json b/packages/backend-common/package.json index 249fa9e72c..5a2357f74e 100644 --- a/packages/backend-common/package.json +++ b/packages/backend-common/package.json @@ -32,10 +32,12 @@ "@backstage/cli-common": "^0.1.1-alpha.25", "@backstage/config": "^0.1.1-alpha.25", "@backstage/config-loader": "^0.1.1-alpha.25", + "@backstage/test-utils": "^0.1.1-alpha.25", "@types/cors": "^2.8.6", "@types/express": "^4.17.6", "compression": "^1.7.4", "cors": "^2.8.5", + "cross-fetch": "^3.0.6", "express": "^4.17.1", "express-prom-bundle": "^6.1.0", "express-promise-router": "^3.0.3", @@ -44,8 +46,8 @@ "knex": "^0.21.1", "lodash": "^4.17.15", "logform": "^2.1.1", + "minimist": "^1.2.5", "morgan": "^1.10.0", - "node-fetch": "^2.6.0", "prom-client": "^12.0.0", "selfsigned": "^1.10.7", "stoppable": "^1.1.0", @@ -63,8 +65,8 @@ "@backstage/cli": "^0.1.1-alpha.25", "@types/compression": "^1.7.0", "@types/http-errors": "^1.6.3", + "@types/minimist": "^1.2.0", "@types/morgan": "^1.9.0", - "@types/node-fetch": "^2.5.7", "@types/stoppable": "^1.1.0", "@types/supertest": "^2.0.8", "@types/webpack-env": "^1.15.2", @@ -72,8 +74,7 @@ "get-port": "^5.1.1", "http-errors": "^1.7.3", "jest": "^26.0.1", - "jest-fetch-mock": "^3.0.3", - "msw": "^0.20.5", + "msw": "^0.21.2", "supertest": "^4.0.2" }, "files": [ diff --git a/packages/backend-common/src/config.ts b/packages/backend-common/src/config.ts index c86793fc08..69e46bd5b4 100644 --- a/packages/backend-common/src/config.ts +++ b/packages/backend-common/src/config.ts @@ -14,24 +14,32 @@ * limitations under the License. */ +import { resolve as resolvePath } from 'path'; +import parseArgs from 'minimist'; +import { Logger } from 'winston'; import { findPaths } from '@backstage/cli-common'; import { Config, ConfigReader } from '@backstage/config'; import { loadConfig } from '@backstage/config-loader'; -import { Logger } from 'winston'; type Options = { logger: Logger; + // process.argv or any other overrides + argv: string[]; }; /** * Load configuration for a Backend */ export async function loadBackendConfig(options: Options): Promise { + const args = parseArgs(options.argv); + const configOpts: string[] = [args.config ?? []].flat(); + /* eslint-disable-next-line no-restricted-syntax */ const paths = findPaths(__dirname); const configs = await loadConfig({ env: process.env.APP_ENV ?? process.env.NODE_ENV ?? 'development', - rootPaths: [paths.targetRoot, paths.targetDir], + configRoot: paths.targetRoot, + configPaths: configOpts.map(opt => resolvePath(opt)), shouldReadSecrets: true, }); diff --git a/packages/backend-common/src/reading/AzureUrlReader.test.ts b/packages/backend-common/src/reading/AzureUrlReader.test.ts index c9e1fc5bc7..24b435006a 100644 --- a/packages/backend-common/src/reading/AzureUrlReader.test.ts +++ b/packages/backend-common/src/reading/AzureUrlReader.test.ts @@ -19,14 +19,13 @@ import { setupServer } from 'msw/node'; import { ConfigReader } from '@backstage/config'; import { getVoidLogger } from '../logging'; import { AzureUrlReader } from './AzureUrlReader'; +import { msw } from '@backstage/test-utils'; const logger = getVoidLogger(); describe('AzureUrlReader', () => { const worker = setupServer(); - - beforeAll(() => worker.listen({ onUnhandledRequest: 'error' })); - afterAll(() => worker.close()); + msw.setupDefaultHandlers(worker); beforeEach(() => { worker.use( @@ -41,7 +40,6 @@ describe('AzureUrlReader', () => { ), ); }); - afterEach(() => worker.resetHandlers()); const createConfig = (token?: string) => new ConfigReader( diff --git a/packages/backend-common/src/reading/AzureUrlReader.ts b/packages/backend-common/src/reading/AzureUrlReader.ts index 28fbf25eea..8e6e6a75df 100644 --- a/packages/backend-common/src/reading/AzureUrlReader.ts +++ b/packages/backend-common/src/reading/AzureUrlReader.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import fetch, { RequestInit, HeadersInit, Response } from 'node-fetch'; +import fetch from 'cross-fetch'; import { Config } from '@backstage/config'; import { NotFoundError } from '../errors'; import { ReaderFactory, UrlReader } from './types'; @@ -76,7 +76,7 @@ export class AzureUrlReader implements UrlReader { // 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) { - return response.buffer(); + return Buffer.from(await response.text()); } const message = `${url} could not be read as ${builtUrl}, ${response.status} ${response.statusText}`; diff --git a/packages/backend-common/src/reading/BitbucketUrlReader.test.ts b/packages/backend-common/src/reading/BitbucketUrlReader.test.ts index c3e61fb821..bd7c43d887 100644 --- a/packages/backend-common/src/reading/BitbucketUrlReader.test.ts +++ b/packages/backend-common/src/reading/BitbucketUrlReader.test.ts @@ -19,14 +19,14 @@ import { setupServer } from 'msw/node'; import { ConfigReader } from '@backstage/config'; import { getVoidLogger } from '../logging'; import { BitbucketUrlReader } from './BitbucketUrlReader'; +import { msw } from '@backstage/test-utils'; const logger = getVoidLogger(); describe('BitbucketUrlReader', () => { const worker = setupServer(); - beforeAll(() => worker.listen({ onUnhandledRequest: 'error' })); - afterAll(() => worker.close()); + msw.setupDefaultHandlers(worker); beforeEach(() => { worker.use( @@ -41,7 +41,6 @@ describe('BitbucketUrlReader', () => { ), ); }); - afterEach(() => worker.resetHandlers()); const createConfig = (username?: string, appPassword?: string) => new ConfigReader( diff --git a/packages/backend-common/src/reading/BitbucketUrlReader.ts b/packages/backend-common/src/reading/BitbucketUrlReader.ts index e2576eed47..bf07dc18a6 100644 --- a/packages/backend-common/src/reading/BitbucketUrlReader.ts +++ b/packages/backend-common/src/reading/BitbucketUrlReader.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import fetch, { RequestInit, HeadersInit, Response } from 'node-fetch'; +import fetch from 'cross-fetch'; import { Config } from '@backstage/config'; import { ReaderFactory, UrlReader } from './types'; import { NotFoundError } from '../errors'; @@ -84,7 +84,7 @@ export class BitbucketUrlReader implements UrlReader { } if (response.ok) { - return response.buffer(); + return Buffer.from(await response.text()); } const message = `${url} could not be read as ${builtUrl}, ${response.status} ${response.statusText}`; diff --git a/packages/backend-common/src/reading/FetchUrlReader.ts b/packages/backend-common/src/reading/FetchUrlReader.ts index ea56f2182d..d2e5c45620 100644 --- a/packages/backend-common/src/reading/FetchUrlReader.ts +++ b/packages/backend-common/src/reading/FetchUrlReader.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import fetch, { Response } from 'node-fetch'; +import fetch from 'cross-fetch'; import { NotFoundError } from '../errors'; import { UrlReader } from './types'; @@ -31,7 +31,7 @@ export class FetchUrlReader implements UrlReader { } if (response.ok) { - return response.buffer(); + return Buffer.from(await response.text()); } const message = `could not read ${url}, ${response.status} ${response.statusText}`; diff --git a/packages/backend-common/src/reading/GithubUrlReader.ts b/packages/backend-common/src/reading/GithubUrlReader.ts index e5bed6dd26..d12344ec91 100644 --- a/packages/backend-common/src/reading/GithubUrlReader.ts +++ b/packages/backend-common/src/reading/GithubUrlReader.ts @@ -16,7 +16,7 @@ import { Config } from '@backstage/config'; import parseGitUri from 'git-url-parse'; -import fetch, { HeadersInit, RequestInit, Response } from 'node-fetch'; +import fetch from 'cross-fetch'; import { NotFoundError } from '../errors'; import { ReaderFactory, UrlReader } from './types'; @@ -219,7 +219,7 @@ export class GithubUrlReader implements UrlReader { } if (response.ok) { - return response.buffer(); + return Buffer.from(await response.text()); } const message = `${url} could not be read as ${ghUrl}, ${response.status} ${response.statusText}`; diff --git a/packages/backend-common/src/reading/GitlabUrlReader.test.ts b/packages/backend-common/src/reading/GitlabUrlReader.test.ts index 09da9c4e0a..4b3aa471c3 100644 --- a/packages/backend-common/src/reading/GitlabUrlReader.test.ts +++ b/packages/backend-common/src/reading/GitlabUrlReader.test.ts @@ -19,14 +19,14 @@ import { setupServer } from 'msw/node'; import { ConfigReader } from '@backstage/config'; import { getVoidLogger } from '../logging'; import { GitlabUrlReader } from './GitlabUrlReader'; +import { msw } from '@backstage/test-utils'; const logger = getVoidLogger(); describe('GitlabUrlReader', () => { const worker = setupServer(); - beforeAll(() => worker.listen({ onUnhandledRequest: 'error' })); - afterAll(() => worker.close()); + msw.setupDefaultHandlers(worker); beforeEach(() => { worker.use( @@ -44,7 +44,6 @@ describe('GitlabUrlReader', () => { ), ); }); - afterEach(() => worker.resetHandlers()); const createConfig = (token?: string) => new ConfigReader( diff --git a/packages/backend-common/src/reading/GitlabUrlReader.ts b/packages/backend-common/src/reading/GitlabUrlReader.ts index 0e430d650c..378e76fb61 100644 --- a/packages/backend-common/src/reading/GitlabUrlReader.ts +++ b/packages/backend-common/src/reading/GitlabUrlReader.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import fetch, { RequestInit, Response } from 'node-fetch'; +import fetch from 'cross-fetch'; import { Config } from '@backstage/config'; import { NotFoundError } from '../errors'; import { ReaderFactory, UrlReader } from './types'; @@ -77,7 +77,7 @@ export class GitlabUrlReader implements UrlReader { } if (response.ok) { - return response.buffer(); + return Buffer.from(await response.text()); } const message = `${url} could not be read as ${builtUrl}, ${response.status} ${response.statusText}`; diff --git a/packages/backend/src/index.ts b/packages/backend/src/index.ts index cbeaf4315c..b74954ecc5 100644 --- a/packages/backend/src/index.ts +++ b/packages/backend/src/index.ts @@ -64,7 +64,10 @@ function makeCreateEnv(config: Config) { } async function main() { - const config = await loadBackendConfig({ logger: getRootLogger() }); + const config = await loadBackendConfig({ + argv: process.argv, + logger: getRootLogger(), + }); const createEnv = makeCreateEnv(config); const healthcheckEnv = useHotMemoize(module, () => createEnv('healthcheck')); diff --git a/packages/catalog-model/src/entity/Entity.ts b/packages/catalog-model/src/entity/Entity.ts index 9e7ece4122..ac43137546 100644 --- a/packages/catalog-model/src/entity/Entity.ts +++ b/packages/catalog-model/src/entity/Entity.ts @@ -143,13 +143,14 @@ export type EntityRelation = { }; /** - * Holds the relationship data for entities + * Holds the relation data for entities. */ export type EntityRelationSpec = { /** * The source entity of this relation. */ source: EntityName; + /** * The type of the relation. */ diff --git a/packages/catalog-model/src/kinds/index.ts b/packages/catalog-model/src/kinds/index.ts index 6df4f1212a..4a3faf977b 100644 --- a/packages/catalog-model/src/kinds/index.ts +++ b/packages/catalog-model/src/kinds/index.ts @@ -44,3 +44,4 @@ export type { UserEntityV1alpha1 as UserEntity, UserEntityV1alpha1, } from './UserEntityV1alpha1'; +export * from './relations'; diff --git a/packages/catalog-model/src/kinds/relations.ts b/packages/catalog-model/src/kinds/relations.ts new file mode 100644 index 0000000000..846313ec8c --- /dev/null +++ b/packages/catalog-model/src/kinds/relations.ts @@ -0,0 +1,55 @@ +/* + * Copyright 2020 Spotify AB + * + * 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. + */ + +/* +Naming rules for relations in priority order: + +1. Use at most two words. One main verb and a specifier, e.g. "ownerOf" +2. Reading out " " should make sense in English. +3. Maintain symmetry between pairs, e.g. "ownedBy" and "ownerOf" rather than "owns". +*/ + +/** + * An ownership relation where the owner is usually an organizational + * entity (user or group), and the other entity can be anything. + */ +export const RELATION_OWNED_BY = 'ownedBy'; +export const RELATION_OWNER_OF = 'ownerOf'; + +/** + * A relation with an API entity, typically from a component or system + */ +export const RELATION_CONSUMES_API = 'consumesApi'; +export const RELATION_PROVIDES_API = 'providesApi'; + +/** + * A relation denoting a dependency on another entity. + */ +export const RELATION_DEPENDS_ON = 'dependsOn'; +export const RELATION_DEPENDENCY_OF = 'dependencyOf'; + +/** + * A parent/child relation to build up a tree, used for example to describe + * the organizational structure between groups. + */ +export const RELATION_PARENT_OF = 'parentOf'; +export const RELATION_CHILD_OF = 'childOf'; + +/** + * A membership relation, typically for users in a group. + */ +export const RELATION_MEMBER_OF = 'memberOf'; +export const RELATION_HAS_MEMBER = 'hasMember'; diff --git a/packages/catalog-model/src/location/types.ts b/packages/catalog-model/src/location/types.ts index 50e6e82a54..33e443e04f 100644 --- a/packages/catalog-model/src/location/types.ts +++ b/packages/catalog-model/src/location/types.ts @@ -17,6 +17,10 @@ export type LocationSpec = { type: string; target: string; + // When using repo importer plugin, location is being created before the component yaml file is merged to the main branch. + // This flag is then set to indicate that the file can be not present. + // default value: 'required'. + presence?: 'optional' | 'required'; }; export type Location = { diff --git a/packages/catalog-model/src/location/validation.ts b/packages/catalog-model/src/location/validation.ts index b1b91edd83..27d74b9a82 100644 --- a/packages/catalog-model/src/location/validation.ts +++ b/packages/catalog-model/src/location/validation.ts @@ -21,6 +21,7 @@ export const locationSpecSchema = yup .object({ type: yup.string().required(), target: yup.string().required(), + presence: yup.string(), }) .noUnknown() .required(); diff --git a/packages/cli/package.json b/packages/cli/package.json index dd9e82c325..2300612d3f 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -114,6 +114,7 @@ "@types/http-proxy": "^1.17.4", "@types/inquirer": "^7.3.1", "@types/mini-css-extract-plugin": "^0.9.1", + "@types/mock-fs": "^4.13.0", "@types/node": "^13.7.2", "@types/ora": "^3.2.0", "@types/react-dev-utils": "^9.0.4", diff --git a/packages/cli/src/commands/app/build.ts b/packages/cli/src/commands/app/build.ts index 5a9d2cdf3a..c5f5f5aa2f 100644 --- a/packages/cli/src/commands/app/build.ts +++ b/packages/cli/src/commands/app/build.ts @@ -15,27 +15,15 @@ */ import { Command } from 'commander'; -import { loadConfig } from '@backstage/config-loader'; -import { ConfigReader } from '@backstage/config'; -import { paths } from '../../lib/paths'; import { buildBundle } from '../../lib/bundler'; import { parseParallel, PARALLEL_ENV_VAR } from '../../lib/parallel'; +import { loadCliConfig } from '../../lib/config'; export default async (cmd: Command) => { - const appConfigs = await loadConfig({ - env: process.env.APP_ENV ?? process.env.NODE_ENV ?? 'production', - rootPaths: [paths.targetRoot, paths.targetDir], - }); - - console.log( - `Loaded config from ${appConfigs.map(c => c.context).join(', ')}`, - ); - await buildBundle({ entry: 'src/index', parallel: parseParallel(process.env[PARALLEL_ENV_VAR]), statsJsonEnabled: cmd.stats, - config: ConfigReader.fromConfigs(appConfigs), - appConfigs, + ...(await loadCliConfig(cmd.config)), }); }; diff --git a/packages/cli/src/commands/app/serve.ts b/packages/cli/src/commands/app/serve.ts index 6524eb3acf..c3f58774b5 100644 --- a/packages/cli/src/commands/app/serve.ts +++ b/packages/cli/src/commands/app/serve.ts @@ -15,26 +15,14 @@ */ import { Command } from 'commander'; -import { loadConfig } from '@backstage/config-loader'; -import { ConfigReader } from '@backstage/config'; -import { paths } from '../../lib/paths'; import { serveBundle } from '../../lib/bundler'; +import { loadCliConfig } from '../../lib/config'; export default async (cmd: Command) => { - const appConfigs = await loadConfig({ - env: process.env.APP_ENV ?? process.env.NODE_ENV ?? 'development', - rootPaths: [paths.targetRoot, paths.targetDir], - }); - - console.log( - `Loaded config from ${appConfigs.map(c => c.context).join(', ')}`, - ); - const waitForExit = await serveBundle({ entry: 'src/index', checksEnabled: cmd.check, - config: ConfigReader.fromConfigs(appConfigs), - appConfigs, + ...(await loadCliConfig(cmd.config)), }); await waitForExit(); diff --git a/packages/cli/src/commands/backend/dev.ts b/packages/cli/src/commands/backend/dev.ts index d7b5ca2924..88c069a92b 100644 --- a/packages/cli/src/commands/backend/dev.ts +++ b/packages/cli/src/commands/backend/dev.ts @@ -14,28 +14,14 @@ * limitations under the License. */ -import { ConfigReader } from '@backstage/config'; -import { loadConfig } from '@backstage/config-loader'; import { Command } from 'commander'; -import { paths } from '../../lib/paths'; import { serveBackend } from '../../lib/bundler/backend'; export default async (cmd: Command) => { - const appConfigs = await loadConfig({ - env: process.env.APP_ENV ?? process.env.NODE_ENV ?? 'development', - rootPaths: [paths.targetRoot, paths.targetDir], - }); - - console.log( - `Loaded config from ${appConfigs.map(c => c.context).join(', ')}`, - ); - const waitForExit = await serveBackend({ entry: 'src/index', checksEnabled: cmd.check, inspectEnabled: cmd.inspect, - config: ConfigReader.fromConfigs(appConfigs), - appConfigs, }); await waitForExit(); diff --git a/packages/cli/src/commands/config/print.ts b/packages/cli/src/commands/config/print.ts index 693724e4d9..eb814a5bee 100644 --- a/packages/cli/src/commands/config/print.ts +++ b/packages/cli/src/commands/config/print.ts @@ -15,24 +15,13 @@ */ import { Command } from 'commander'; -import { loadConfig } from '@backstage/config-loader'; -import { ConfigReader } from '@backstage/config'; -import { paths } from '../../lib/paths'; import { stringify as stringifyYaml } from 'yaml'; +import { loadCliConfig } from '../../lib/config'; export default async (cmd: Command) => { - const appConfigs = await loadConfig({ - env: - cmd.env ?? process.env.APP_ENV ?? process.env.NODE_ENV ?? 'development', - shouldReadSecrets: cmd.withSecrets ?? false, - rootPaths: [paths.targetRoot, paths.targetDir], - }); + const { config } = await loadCliConfig(cmd.config, cmd.withSecrets ?? false); - console.log( - `Loaded config from ${appConfigs.map(c => c.context).join(', ')}`, - ); - - const flatConfig = ConfigReader.fromConfigs(appConfigs).get(); + const flatConfig = config.get(); if (cmd.format === 'json') { process.stdout.write(`${JSON.stringify(flatConfig, null, 2)}\n`); diff --git a/packages/cli/src/commands/create-plugin/createPlugin.test.ts b/packages/cli/src/commands/create-plugin/createPlugin.test.ts index 72b95b072c..356b315380 100644 --- a/packages/cli/src/commands/create-plugin/createPlugin.test.ts +++ b/packages/cli/src/commands/create-plugin/createPlugin.test.ts @@ -16,14 +16,20 @@ import fs from 'fs-extra'; import path from 'path'; +import mockFs from 'mock-fs'; import os from 'os'; import del from 'del'; import { createTemporaryPluginFolder, movePlugin } from './createPlugin'; +const id = 'testPluginMock'; + describe('createPlugin', () => { + afterAll(() => { + mockFs.restore(); + }); + describe('createPluginFolder', () => { it('should create a temporary plugin directory in the correct place', async () => { - const id = 'testPlugin'; const tempDir = path.join(os.tmpdir(), id); try { await createTemporaryPluginFolder(tempDir); @@ -35,35 +41,27 @@ describe('createPlugin', () => { }); it('should not create a temporary plugin directory if it already exists', async () => { - const id = 'testPlugin'; - const tempDir = path.join(os.tmpdir(), id); - try { - await createTemporaryPluginFolder(tempDir); - await expect(fs.pathExists(tempDir)).resolves.toBe(true); - await expect(createTemporaryPluginFolder(tempDir)).rejects.toThrow( - /Failed to create temporary plugin directory/, - ); - } finally { - await del(tempDir, { force: true }); - } + mockFs({ + [id]: {}, + }); + + await expect(createTemporaryPluginFolder(id)).rejects.toThrow( + /Failed to create temporary plugin directory/, + ); }); }); describe('movePlugin', () => { it('should move the temporary plugin directory to its final place', async () => { - const id = 'testPlugin'; - const tempDir = path.join(os.tmpdir(), id); - const rootDir = await fs.mkdtemp(path.join(os.tmpdir(), 'test-')); - const pluginDir = path.join(rootDir, 'plugins', id); - try { - await createTemporaryPluginFolder(tempDir); - await movePlugin(tempDir, pluginDir, id); - await expect(fs.pathExists(pluginDir)).resolves.toBe(true); - expect(pluginDir).toMatch(path.join('', 'plugins', id)); - } finally { - await del(tempDir, { force: true }); - await del(rootDir, { force: true }); - } + mockFs({ + [id]: {}, + }); + const tempDir = id; + const pluginDir = `/test-temp/plugins/${id}`; + + await movePlugin(tempDir, pluginDir, id); + await expect(fs.pathExists(pluginDir)).resolves.toBe(true); + expect(pluginDir).toMatch(path.join('', 'plugins', id)); }); }); }); diff --git a/packages/cli/src/commands/index.ts b/packages/cli/src/commands/index.ts index e5017b829a..7f028919f7 100644 --- a/packages/cli/src/commands/index.ts +++ b/packages/cli/src/commands/index.ts @@ -18,16 +18,25 @@ import { CommanderStatic } from 'commander'; import { exitWithError } from '../lib/errors'; export function registerCommands(program: CommanderStatic) { + const configOption = [ + '--config ', + 'Config files to load instead of app-config.yaml', + (opt: string, opts: string[]) => [...opts, opt], + Array(), + ] as const; + program .command('app:build') .description('Build an app for a production release') .option('--stats', 'Write bundle stats to output directory') + .option(...configOption) .action(lazy(() => import('./app/build').then(m => m.default))); program .command('app:serve') .description('Serve an app for local development') .option('--check', 'Enable type checking and linting') + .option(...configOption) .action(lazy(() => import('./app/serve').then(m => m.default))); program @@ -50,6 +59,8 @@ export function registerCommands(program: CommanderStatic) { .description('Start local development server with HMR for the backend') .option('--check', 'Enable type checking and linting') .option('--inspect', 'Enable debugger') + // We don't actually use the config in the CLI, just pass them on to the NodeJS process + .option(...configOption) .action(lazy(() => import('./backend/dev').then(m => m.default))); program @@ -89,12 +100,14 @@ export function registerCommands(program: CommanderStatic) { .command('plugin:serve') .description('Serves the dev/ folder of a plugin') .option('--check', 'Enable type checking and linting') + .option(...configOption) .action(lazy(() => import('./plugin/serve').then(m => m.default))); program .command('plugin:export') .description('Exports the dev/ folder of a plugin') .option('--stats', 'Write bundle stats to output directory') + .option(...configOption) .action(lazy(() => import('./plugin/export').then(m => m.default))); program @@ -131,14 +144,11 @@ export function registerCommands(program: CommanderStatic) { program .command('config:print') .option('--with-secrets', 'Include secrets in the printed configuration') - .option( - '--env ', - 'The environment to print configuration for [APP_ENV or NODE_ENV or development]', - ) .option( '--format ', 'Format to print the configuration in, either json or yaml [yaml]', ) + .option(...configOption) .description('Print the app configuration for the current package') .action(lazy(() => import('./config/print').then(m => m.default))); diff --git a/packages/cli/src/commands/plugin/export.ts b/packages/cli/src/commands/plugin/export.ts index 39865f3d32..69955a2e5f 100644 --- a/packages/cli/src/commands/plugin/export.ts +++ b/packages/cli/src/commands/plugin/export.ts @@ -15,25 +15,13 @@ */ import { Command } from 'commander'; -import { loadConfig } from '@backstage/config-loader'; -import { ConfigReader } from '@backstage/config'; -import { paths } from '../../lib/paths'; import { buildBundle } from '../../lib/bundler'; +import { loadCliConfig } from '../../lib/config'; export default async (cmd: Command) => { - const appConfigs = await loadConfig({ - env: process.env.APP_ENV ?? process.env.NODE_ENV ?? 'production', - rootPaths: [paths.targetRoot, paths.targetDir], - }); - - console.log( - `Loaded config from ${appConfigs.map(c => c.context).join(', ')}`, - ); - await buildBundle({ entry: 'dev/index', statsJsonEnabled: cmd.stats, - config: ConfigReader.fromConfigs(appConfigs), - appConfigs, + ...(await loadCliConfig(cmd.config)), }); }; diff --git a/packages/cli/src/commands/plugin/serve.ts b/packages/cli/src/commands/plugin/serve.ts index 7c92c8f312..cb9def3158 100644 --- a/packages/cli/src/commands/plugin/serve.ts +++ b/packages/cli/src/commands/plugin/serve.ts @@ -15,26 +15,14 @@ */ import { Command } from 'commander'; -import { loadConfig } from '@backstage/config-loader'; -import { ConfigReader } from '@backstage/config'; -import { paths } from '../../lib/paths'; import { serveBundle } from '../../lib/bundler'; +import { loadCliConfig } from '../../lib/config'; export default async (cmd: Command) => { - const appConfigs = await loadConfig({ - env: process.env.APP_ENV ?? process.env.NODE_ENV ?? 'development', - rootPaths: [paths.targetRoot, paths.targetDir], - }); - - console.log( - `Loaded config from ${appConfigs.map(c => c.context).join(', ')}`, - ); - const waitForExit = await serveBundle({ entry: 'dev/index', checksEnabled: cmd.check, - config: ConfigReader.fromConfigs(appConfigs), - appConfigs, + ...(await loadCliConfig(cmd.config)), }); await waitForExit(); diff --git a/packages/cli/src/commands/remove-plugin/removePlugin.test.ts b/packages/cli/src/commands/remove-plugin/removePlugin.test.ts index 6433cb6116..076f54402b 100644 --- a/packages/cli/src/commands/remove-plugin/removePlugin.test.ts +++ b/packages/cli/src/commands/remove-plugin/removePlugin.test.ts @@ -16,13 +16,9 @@ import fse from 'fs-extra'; import path from 'path'; -import os from 'os'; +import mockFs from 'mock-fs'; import { paths } from '../../lib/paths'; -import { - addExportStatement, - capitalize, - createTemporaryPluginFolder, -} from '../create-plugin/createPlugin'; +import { addExportStatement, capitalize } from '../create-plugin/createPlugin'; import { addCodeownersEntry } from '../../lib/codeowners'; import { removeReferencesFromAppPackage, @@ -35,7 +31,7 @@ import { const BACKSTAGE = `@backstage`; const testPluginName = 'yarn-test-package'; const testPluginPackage = `${BACKSTAGE}/plugin-${testPluginName}`; -const tempDir = path.join(os.tmpdir(), 'remove-plugin-test'); +const tempDir = '/remove-plugin-test'; const removeEmptyLines = (file: string): string => file.split(/\r?\n/).filter(Boolean).join('\n'); @@ -47,13 +43,24 @@ const createTestPackageFile = async ( // Copy contents of package file for test const packageFileContent = JSON.parse(fse.readFileSync(packageFile, 'utf8')); - packageFileContent.dependencies[testPluginPackage] = '0.1.0'; - fse.createFileSync(testFilePath); - fse.writeFileSync( - testFilePath, - `${JSON.stringify(packageFileContent, null, 2)}\n`, - 'utf8', - ); + const testFileContent = { + ...packageFileContent, + dependencies: { + ...packageFileContent.dependencies, + [testPluginPackage]: '0.1.0', + }, + }; + + mockFs({ + '/packages': { + app: { + 'package.json': `${JSON.stringify(packageFileContent, null, 2)}\n`, + }, + }, + [tempDir]: { + [testFilePath]: `${JSON.stringify(testFileContent, null, 2)}\n`, + }, + }); return; }; @@ -62,93 +69,126 @@ const createTestPluginFile = async ( pluginsFilePath: string, ) => { // Copy contents of package file for test - fse.copyFileSync(pluginsFilePath, testFilePath); + const pluginsFileContent = fse.readFileSync(pluginsFilePath); + + mockFs({ + [tempDir]: { + [testFilePath]: `${pluginsFileContent}\n`, + [pluginsFilePath]: `${pluginsFileContent}\n`, + }, + '/packages': { + app: { + src: { + 'plugin.ts': `${pluginsFileContent}\n`, + }, + }, + }, + }); + const pluginNameCapitalized = testPluginName .split('-') .map(name => capitalize(name)) .join(''); - const exportStatement = `export { plugin as ${pluginNameCapitalized}} from @backstage/plugin-${testPluginName}`; - await addExportStatement(testFilePath, exportStatement); + const exportStatement = `export { default as ${pluginNameCapitalized}} from @backstage/plugin-${testPluginName}`; + await addExportStatement(path.join(tempDir, testFilePath), exportStatement); }; const mkTestPluginDir = (testDirPath: string) => { - fse.mkdirSync(testDirPath); - for (let i = 0; i < 50; i++) - fse.createFileSync(path.join(testDirPath, `testFile${i}.ts`)); + const dirPath = `/${testDirPath}`; + + const pluginFiles: { [index: number]: string } = {}; + for (let i = 0; i < 50; i++) { + pluginFiles[i] = ''; + } + + mockFs({ + [dirPath]: pluginFiles, + }); }; describe('removePlugin', () => { - beforeAll(() => { + beforeEach(() => { // Create temporary directory for all tests - createTemporaryPluginFolder(tempDir); + const appPath = paths.resolveTargetRoot('packages', 'app'); + mockFs({ + [tempDir]: { + 'package.json': mockFs.load(path.join(appPath, 'package.json')), + src: { + 'plugin.ts': mockFs.load(path.join(appPath, 'src', 'plugins.ts')), + }, + }, + }); }); afterAll(() => { - // Remove temporary directory - fse.removeSync(tempDir); + mockFs.restore(); }); describe('Remove Plugin Dependencies', () => { - const appPath = paths.resolveTargetRoot('packages', 'app'); const githubDir = paths.resolveTargetRoot('.github'); it('removes plugin references from /packages/app/package.json', async () => { // Set up test - const packageFilePath = path.join(appPath, 'package.json'); - const testFilePath = path.join(tempDir, 'test.json'); + const packageFilePath = path.join(tempDir, 'package.json'); + const testFilePath = 'test.json'; createTestPackageFile(testFilePath, packageFilePath); - try { - await removeReferencesFromAppPackage(testFilePath, testPluginName); - const testFileContent = removeEmptyLines( - fse.readFileSync(testFilePath, 'utf8'), - ); - const packageFileContent = removeEmptyLines( - fse.readFileSync(packageFilePath, 'utf8'), - ); - expect(testFileContent).toBe(packageFileContent); - } finally { - fse.removeSync(testFilePath); - } - }); + await removeReferencesFromAppPackage( + path.join(tempDir, testFilePath), + testPluginName, + ); + const testFileContent = removeEmptyLines( + fse.readFileSync(path.join(tempDir, testFilePath), 'utf8'), + ); - it('removes plugin exports from /packages/app/src/package.json', async () => { - const testFilePath = path.join(tempDir, 'test.ts'); - const pluginsFilePaths = path.join(appPath, 'src', 'plugins.ts'); - await createTestPluginFile(testFilePath, pluginsFilePaths); - try { - await removeReferencesFromPluginsFile(testFilePath, testPluginName); - const testFileContent = removeEmptyLines( - fse.readFileSync(testFilePath, 'utf8'), - ); - const pluginsFileContent = removeEmptyLines( - fse.readFileSync(pluginsFilePaths, 'utf8'), - ); - expect(testFileContent).toBe(pluginsFileContent); - } finally { - fse.removeSync(testFilePath); - } + const packageFileContent = removeEmptyLines( + fse.readFileSync('/packages/app/package.json', 'utf8'), + ); + expect(testFileContent).toBe(packageFileContent); + }); + it('removes plugin exports from /packages/app/src/packacge.json', async () => { + const testFilePath = 'test.ts'; + const pluginsFilePaths = path.join(tempDir, 'src/plugin.ts'); + createTestPluginFile(testFilePath, pluginsFilePaths); + await removeReferencesFromPluginsFile( + path.join(tempDir, testFilePath), + testPluginName, + ); + const testFileContent = removeEmptyLines( + fse.readFileSync(path.join(tempDir, testFilePath), 'utf8'), + ); + const pluginsFileContent = removeEmptyLines( + fse.readFileSync('/packages/app/src/plugin.ts', 'utf8'), + ); + expect(testFileContent).toBe(pluginsFileContent); }); it('removes codeOwners references', async () => { - const testFilePath = path.join(tempDir, 'test'); + const testFileName = 'test'; + const testFilePath = path.join(tempDir, testFileName); const codeownersPath = path.join(githubDir, 'CODEOWNERS'); - try { - fse.copySync(codeownersPath, testFilePath); - const testFileContent = removeEmptyLines( - fse.readFileSync(testFilePath, 'utf8'), - ); - const codeOwnersFileContent = removeEmptyLines( - fse.readFileSync(codeownersPath, 'utf8'), - ); - await addCodeownersEntry(testFilePath!, `/plugins/${testPluginName}`, [ - '@thisIsAtestTeam', - 'test@gmail.com', - ]); - await removePluginFromCodeOwners(testFilePath, testPluginName); - expect(testFileContent).toBe(codeOwnersFileContent); - } finally { - if (fse.existsSync(testFilePath)) fse.removeSync(testFilePath); - } + const mockedCodeownersPath = '/.github/CODEOWNERS'; + + mockFs({ + [tempDir]: { + [testFileName]: '', + }, + '/.github': { + CODEOWNERS: mockFs.load(codeownersPath), + }, + }); + fse.copySync(mockedCodeownersPath, testFilePath); + const testFileContent = removeEmptyLines( + fse.readFileSync(testFilePath, 'utf8'), + ); + const codeOwnersFileContent = removeEmptyLines( + fse.readFileSync(mockedCodeownersPath, 'utf8'), + ); + await addCodeownersEntry(testFilePath!, `/plugins/${testPluginName}`, [ + '@thisIsAtestTeam', + 'test@gmail.com', + ]); + await removePluginFromCodeOwners(testFilePath, testPluginName); + expect(testFileContent).toBe(codeOwnersFileContent); }); }); @@ -161,34 +201,35 @@ describe('removePlugin', () => { describe('Removes Plugin Directory', () => { it('removes plugin directory from /plugins', async () => { - try { - mkTestPluginDir(testDirPath); - expect(fse.existsSync(testDirPath)).toBeTruthy(); - await removePluginDirectory(testDirPath); - expect(fse.existsSync(testDirPath)).toBeFalsy(); - } finally { - if (fse.existsSync(testDirPath)) fse.removeSync(testDirPath); - } + mkTestPluginDir(testDirPath); + expect(fse.existsSync(testDirPath)).toBeTruthy(); + await removePluginDirectory(testDirPath); + expect(fse.existsSync(testDirPath)).toBeFalsy(); }); }); describe('Removes System Link', () => { it('removes system link from @backstage', async () => { - const scopedDir = paths.resolveTargetRoot('node_modules', '@backstage'); - const testSymLinkPath = path.join( - scopedDir, - `plugin-${testPluginName}`, - ); - try { - mkTestPluginDir(testDirPath); - fse.ensureSymlinkSync(testSymLinkPath, testDirPath); + const symLink = `plugin-${testPluginName}`; + const testSymLinkPath = `/node_modules/@backstage/${symLink}`; + const mockedTestDirPath = path.join('/plugins', testPluginName); - await removeSymLink(testSymLinkPath); - expect(fse.existsSync(testSymLinkPath)).toBeFalsy(); - } finally { - if (fse.existsSync(testDirPath)) fse.removeSync(testDirPath); - if (fse.existsSync(testSymLinkPath)) fse.removeSync(testSymLinkPath); - } + mockFs({ + '/plugins': { + [testPluginName]: {}, + }, + '/node_modules': { + '@backstage': { + [symLink]: mockFs.symlink({ + path: mockedTestDirPath, + }), + }, + }, + }); + + expect(fse.existsSync(testSymLinkPath)).toBeTruthy(); + await removeSymLink(testSymLinkPath); + expect(fse.existsSync(testSymLinkPath)).toBeFalsy(); }); }); }); diff --git a/packages/cli/src/lib/bundler/backend.ts b/packages/cli/src/lib/bundler/backend.ts index e3cc8af8d1..9633c1b963 100644 --- a/packages/cli/src/lib/bundler/backend.ts +++ b/packages/cli/src/lib/bundler/backend.ts @@ -17,13 +17,9 @@ import webpack from 'webpack'; import { createBackendConfig } from './config'; import { resolveBundlingPaths } from './paths'; -import { ServeOptions } from './types'; +import { BackendServeOptions } from './types'; -export async function serveBackend( - options: ServeOptions & { - inspectEnabled: boolean; - }, -) { +export async function serveBackend(options: BackendServeOptions) { const paths = resolveBundlingPaths(options); const config = await createBackendConfig(paths, { ...options, diff --git a/packages/cli/src/lib/bundler/types.ts b/packages/cli/src/lib/bundler/types.ts index 827d517fef..00f1895725 100644 --- a/packages/cli/src/lib/bundler/types.ts +++ b/packages/cli/src/lib/bundler/types.ts @@ -27,10 +27,6 @@ export type BundlingOptions = { parallel?: ParallelOption; }; -export type BackendBundlingOptions = Omit & { - inspectEnabled: boolean; -}; - export type ServeOptions = BundlingPathsOptions & { checksEnabled: boolean; config: Config; @@ -43,3 +39,15 @@ export type BuildOptions = BundlingPathsOptions & { config: Config; appConfigs: AppConfig[]; }; + +export type BackendBundlingOptions = { + checksEnabled: boolean; + isDev: boolean; + parallel?: ParallelOption; + inspectEnabled: boolean; +}; + +export type BackendServeOptions = BundlingPathsOptions & { + checksEnabled: boolean; + inspectEnabled: boolean; +}; diff --git a/packages/cli/src/lib/config.ts b/packages/cli/src/lib/config.ts new file mode 100644 index 0000000000..5801632ea5 --- /dev/null +++ b/packages/cli/src/lib/config.ts @@ -0,0 +1,42 @@ +/* + * Copyright 2020 Spotify AB + * + * 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 { loadConfig } from '@backstage/config-loader'; +import { ConfigReader } from '@backstage/config'; +import { paths } from './paths'; + +export async function loadCliConfig( + configArgs: string[], + shouldReadSecrets: boolean = false, +) { + const configPaths = configArgs.map(arg => paths.resolveTarget(arg)); + + const appConfigs = await loadConfig({ + shouldReadSecrets, + env: process.env.APP_ENV ?? process.env.NODE_ENV ?? 'production', + configRoot: paths.targetRoot, + configPaths, + }); + + console.log( + `Loaded config from ${appConfigs.map(c => c.context).join(', ')}`, + ); + + return { + appConfigs, + config: ConfigReader.fromConfigs(appConfigs), + }; +} diff --git a/packages/cli/src/lib/tasks.test.ts b/packages/cli/src/lib/tasks.test.ts index 871d5a4d37..1fb07d06fc 100644 --- a/packages/cli/src/lib/tasks.test.ts +++ b/packages/cli/src/lib/tasks.test.ts @@ -15,39 +15,41 @@ */ import fs from 'fs-extra'; +import mockFs from 'mock-fs'; import { resolve as resolvePath } from 'path'; -import os from 'os'; -import del from 'del'; import { templatingTask } from './tasks'; describe('templatingTask', () => { + afterEach(() => { + mockFs.restore(); + }); + it('should template a directory with mix of regular files and templates', async () => { - // Set up a testing template directory - const tmplDir = await fs.mkdtemp(resolvePath(os.tmpdir(), 'test-')); - await fs.ensureDir(resolvePath(tmplDir, 'sub')); - await fs.writeFile(resolvePath(tmplDir, 'test.txt'), 'testing'); - await fs.writeFile( - resolvePath(tmplDir, 'sub/version.txt.hbs'), - 'version: {{version}}', - ); + // Testing template directory + const tmplDir = 'test-tmpl'; - // Set up a temporary dest dir to write the template to - const destDir = await fs.mkdtemp(resolvePath(os.tmpdir(), 'test-')); + // Temporary dest dir to write the template to + const destDir = 'test-dest'; - try { - await templatingTask(tmplDir, destDir, { - version: '0.0.0', - }); + mockFs({ + [tmplDir]: { + sub: { + 'version.txt.hbs': 'version: {{version}}', + }, + 'test.txt': 'testing', + }, + [destDir]: {}, + }); - await expect( - fs.readFile(resolvePath(destDir, 'test.txt'), 'utf8'), - ).resolves.toBe('testing'); - await expect( - fs.readFile(resolvePath(destDir, 'sub/version.txt'), 'utf8'), - ).resolves.toBe('version: 0.0.0'); - } finally { - await del(tmplDir, { force: true }); - await del(destDir, { force: true }); - } + await templatingTask(tmplDir, destDir, { + version: '0.0.0', + }); + + await expect( + fs.readFile(resolvePath(destDir, 'test.txt'), 'utf8'), + ).resolves.toBe('testing'); + await expect( + fs.readFile(resolvePath(destDir, 'sub/version.txt'), 'utf8'), + ).resolves.toBe('version: 0.0.0'); }); }); diff --git a/packages/cli/templates/default-backend-plugin/package.json.hbs b/packages/cli/templates/default-backend-plugin/package.json.hbs index 822ce584a5..1a73515622 100644 --- a/packages/cli/templates/default-backend-plugin/package.json.hbs +++ b/packages/cli/templates/default-backend-plugin/package.json.hbs @@ -29,14 +29,14 @@ "express": "^4.17.1", "express-promise-router": "^3.0.3", "winston": "^3.2.1", - "node-fetch": "^2.6.1", + "cross-fetch": "^3.0.6", "yn": "^4.0.0" }, "devDependencies": { "@backstage/cli": "^{{backstageVersion}}", "@types/supertest": "^2.0.8", "supertest": "^4.0.2", - "msw": "^0.20.5" + "msw": "^0.21.2" }, "files": [ "dist" diff --git a/packages/cli/templates/default-backend-plugin/src/setupTests.ts b/packages/cli/templates/default-backend-plugin/src/setupTests.ts index a5907fd52f..ba33cf996b 100644 --- a/packages/cli/templates/default-backend-plugin/src/setupTests.ts +++ b/packages/cli/templates/default-backend-plugin/src/setupTests.ts @@ -15,4 +15,3 @@ */ export {}; -global.fetch = require('node-fetch'); diff --git a/packages/cli/templates/default-plugin/package.json.hbs b/packages/cli/templates/default-plugin/package.json.hbs index e024884547..41f942fb79 100644 --- a/packages/cli/templates/default-plugin/package.json.hbs +++ b/packages/cli/templates/default-plugin/package.json.hbs @@ -36,13 +36,14 @@ "devDependencies": { "@backstage/cli": "^{{backstageVersion}}", "@backstage/dev-utils": "^{{backstageVersion}}", + "@backstage/test-utils": "^{{backstageVersion}}", "@testing-library/jest-dom": "^5.10.1", "@testing-library/react": "^10.4.1", "@testing-library/user-event": "^12.0.7", "@types/jest": "^26.0.7", "@types/node": "^12.0.0", - "msw": "^0.20.5", - "node-fetch": "^2.6.1" + "msw": "^0.21.2", + "cross-fetch": "^3.0.6" }, "files": [ "dist" diff --git a/packages/cli/templates/default-plugin/src/components/ExampleComponent/ExampleComponent.test.tsx.hbs b/packages/cli/templates/default-plugin/src/components/ExampleComponent/ExampleComponent.test.tsx.hbs index fdb39444d8..e805900f36 100644 --- a/packages/cli/templates/default-plugin/src/components/ExampleComponent/ExampleComponent.test.tsx.hbs +++ b/packages/cli/templates/default-plugin/src/components/ExampleComponent/ExampleComponent.test.tsx.hbs @@ -5,18 +5,13 @@ import { ThemeProvider } from '@material-ui/core'; import { lightTheme } from '@backstage/theme'; import { rest } from 'msw'; import { setupServer } from 'msw/node'; +import { msw } from '@backstage/test-utils'; describe('ExampleComponent', () => { const server = setupServer(); - // Enable API mocking before tests. - beforeAll(() => server.listen({ onUnhandledRequest: 'error' })) - - // Reset any runtime request handlers we may add during the tests. - afterEach(() => server.resetHandlers()) - - // Disable API mocking after the tests are done. - afterAll(() => server.close()) + // Enable sane handlers for network requests + msw.setupDefaultHandlers(server); // setup mock response beforeEach(() => { diff --git a/packages/cli/templates/default-plugin/src/components/ExampleFetchComponent/ExampleFetchComponent.test.tsx.hbs b/packages/cli/templates/default-plugin/src/components/ExampleFetchComponent/ExampleFetchComponent.test.tsx.hbs index ca1990b4bc..81e1b4be09 100644 --- a/packages/cli/templates/default-plugin/src/components/ExampleFetchComponent/ExampleFetchComponent.test.tsx.hbs +++ b/packages/cli/templates/default-plugin/src/components/ExampleFetchComponent/ExampleFetchComponent.test.tsx.hbs @@ -3,18 +3,13 @@ import { render } from '@testing-library/react'; import ExampleFetchComponent from './ExampleFetchComponent'; import { rest } from 'msw'; import { setupServer } from 'msw/node'; +import { msw } from '@backstage/test-utils'; describe('ExampleFetchComponent', () => { const server = setupServer(); - // Enable API mocking before tests. - beforeAll(() => server.listen({ onUnhandledRequest: 'error' })) - - // Reset any runtime request handlers we may add during the tests. - afterEach(() => server.resetHandlers()) - - // Disable API mocking after the tests are done. - afterAll(() => server.close()) - + // Enable sane handlers for network requests + msw.setupDefaultHandlers(server); + // setup mock response beforeEach(() => { server.use(rest.get('https://randomuser.me/*', (_, res, ctx) => res(ctx.status(200), ctx.delay(2000), ctx.json({})))) diff --git a/packages/cli/templates/default-plugin/src/setupTests.ts b/packages/cli/templates/default-plugin/src/setupTests.ts index cc559f672e..292b0cc471 100644 --- a/packages/cli/templates/default-plugin/src/setupTests.ts +++ b/packages/cli/templates/default-plugin/src/setupTests.ts @@ -1,2 +1,2 @@ import '@testing-library/jest-dom'; -global.fetch = require('node-fetch'); +import 'cross-fetch/polyfill' diff --git a/packages/config-loader/src/lib/index.ts b/packages/config-loader/src/lib/index.ts index 40252b9cec..1b5ad2ef50 100644 --- a/packages/config-loader/src/lib/index.ts +++ b/packages/config-loader/src/lib/index.ts @@ -14,7 +14,6 @@ * limitations under the License. */ -export { resolveStaticConfig } from './resolver'; export { readConfigFile } from './reader'; export { readEnvConfig } from './env'; export { readSecret } from './secrets'; diff --git a/packages/config-loader/src/lib/resolver.test.ts b/packages/config-loader/src/lib/resolver.test.ts deleted file mode 100644 index 9fd71f2486..0000000000 --- a/packages/config-loader/src/lib/resolver.test.ts +++ /dev/null @@ -1,119 +0,0 @@ -/* - * Copyright 2020 Spotify AB - * - * 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 mockFs from 'mock-fs'; -import { resolveStaticConfig } from './resolver'; - -function normalizePaths(paths: string[]) { - return paths.map(p => - p - .replace(/^[a-z]:/i, '') - .split('\\') - .join('/'), - ); -} - -describe('resolveStaticConfig', () => { - afterEach(() => { - mockFs.restore(); - }); - - it('should resolve no files for empty roots', async () => { - mockFs({}); - const resolved = await resolveStaticConfig({ - env: 'development', - rootPaths: [], - }); - - expect(normalizePaths(resolved)).toEqual([]); - }); - - it('should resolve a single app-config', async () => { - mockFs({ '/repo/app-config.yaml': '' }); - const resolved = await resolveStaticConfig({ - env: 'development', - rootPaths: ['/repo'], - }); - - expect(normalizePaths(resolved)).toEqual(['/repo/app-config.yaml']); - }); - - it('should resolve a app-configs in different directories', async () => { - mockFs({ - '/repo/app-config.yaml': '', - '/repo/packages/a/app-config.yaml': '', - }); - const resolved = await resolveStaticConfig({ - env: 'development', - rootPaths: [ - '/repo', - '/other-repo', - '/repo/packages/a', - '/repo/packages/b', - ], - }); - - expect(normalizePaths(resolved)).toEqual([ - '/repo/app-config.yaml', - '/repo/packages/a/app-config.yaml', - ]); - }); - - it('should resolve env and local configs', async () => { - mockFs({ - '/repo/app-config.yaml': '', - '/repo/app-config.local.yaml': '', - '/repo/app-config.production.yaml': '', - '/repo/app-config.production.local.yaml': '', - '/repo/app-config.development.local.yaml': '', - '/repo/packages/a/app-config.development.yaml': '', - '/repo/packages/a/app-config.local.yaml': '', - }); - const resolved = await resolveStaticConfig({ - env: 'development', - rootPaths: ['/repo', '/repo/packages/a'], - }); - - expect(normalizePaths(resolved)).toEqual([ - '/repo/app-config.yaml', - '/repo/app-config.local.yaml', - '/repo/app-config.development.local.yaml', - '/repo/packages/a/app-config.local.yaml', - '/repo/packages/a/app-config.development.yaml', - ]); - }); - - it('resolves suffixed configs in the correct order', async () => { - mockFs({ - '/repo/app-config.yaml': '', - '/repo/app-config.local.yaml': '', - '/repo/app-config.production.yaml': '', - '/repo/app-config.production.local.yaml': '', - }); - - const resolved = await resolveStaticConfig({ - env: 'production', - rootPaths: ['/repo'], - }); - - expect(normalizePaths(resolved)).toEqual([ - '/repo/app-config.yaml', - '/repo/app-config.local.yaml', - '/repo/app-config.production.yaml', - '/repo/app-config.production.local.yaml', - ]); - }); -}); diff --git a/packages/config-loader/src/lib/resolver.ts b/packages/config-loader/src/lib/resolver.ts deleted file mode 100644 index 3856598331..0000000000 --- a/packages/config-loader/src/lib/resolver.ts +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2020 Spotify AB - * - * 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 { resolve as resolvePath } from 'path'; -import { pathExists } from 'fs-extra'; - -type ResolveOptions = { - // Root paths to search for config files. Config from earlier paths has lower priority. - rootPaths: string[]; - // The environment that we're loading config for, e.g. 'development', 'production'. - env: string; -}; - -/** - * Resolves all configuration files that should be loaded in the given environment. - * - * For each root directory, search for the default app-config.yaml, along with suffixed - * APP_ENV and local variants, e.g. app-config.production.yaml or app-config.development.local.yaml - * - * The priority order of config loaded through suffixes is `env > local > none`, meaning that - * for example app-config.development.yaml has higher priority than `app-config.local.yaml`. - * - */ -export async function resolveStaticConfig( - options: ResolveOptions, -): Promise { - const filePaths = [ - `app-config.yaml`, - `app-config.local.yaml`, - `app-config.${options.env}.yaml`, - `app-config.${options.env}.local.yaml`, - ]; - - const resolvedPaths = []; - - for (const rootPath of options.rootPaths) { - for (const filePath of filePaths) { - const path = resolvePath(rootPath, filePath); - if (await pathExists(path)) { - resolvedPaths.push(path); - } - } - } - - return resolvedPaths; -} diff --git a/packages/config-loader/src/loader.test.ts b/packages/config-loader/src/loader.test.ts index 087e2309c0..3b0d8e6e92 100644 --- a/packages/config-loader/src/loader.test.ts +++ b/packages/config-loader/src/loader.test.ts @@ -38,10 +38,31 @@ describe('loadConfig', () => { mockFs.restore(); }); + it('load config from default path', async () => { + await expect( + loadConfig({ + configRoot: '/root', + configPaths: [], + env: 'production', + shouldReadSecrets: false, + }), + ).resolves.toEqual([ + { + context: 'app-config.yaml', + data: { + app: { + title: 'Example App', + }, + }, + }, + ]); + }); + it('loads config without secrets', async () => { await expect( loadConfig({ - rootPaths: ['/root'], + configRoot: '/root', + configPaths: ['/root/app-config.yaml'], env: 'production', shouldReadSecrets: false, }), @@ -60,7 +81,8 @@ describe('loadConfig', () => { it('loads config with secrets', async () => { await expect( loadConfig({ - rootPaths: ['/root'], + configRoot: '/root', + configPaths: ['/root/app-config.yaml'], env: 'production', shouldReadSecrets: true, }), @@ -80,7 +102,11 @@ describe('loadConfig', () => { it('loads development config without secrets', async () => { await expect( loadConfig({ - rootPaths: ['/root'], + configRoot: '/root', + configPaths: [ + '/root/app-config.yaml', + '/root/app-config.development.yaml', + ], env: 'development', shouldReadSecrets: false, }), @@ -105,7 +131,11 @@ describe('loadConfig', () => { it('loads development config with secrets', async () => { await expect( loadConfig({ - rootPaths: ['/root'], + configRoot: '/root', + configPaths: [ + '/root/app-config.yaml', + '/root/app-config.development.yaml', + ], env: 'development', shouldReadSecrets: true, }), diff --git a/packages/config-loader/src/loader.ts b/packages/config-loader/src/loader.ts index cf5edd3ce3..e7eb2a38fa 100644 --- a/packages/config-loader/src/loader.ts +++ b/packages/config-loader/src/loader.ts @@ -15,20 +15,18 @@ */ import fs from 'fs-extra'; -import { resolve as resolvePath, dirname } from 'path'; +import { resolve as resolvePath, dirname, isAbsolute } from 'path'; import { AppConfig, JsonObject } from '@backstage/config'; -import { - resolveStaticConfig, - readConfigFile, - readEnvConfig, - readSecret, -} from './lib'; +import { readConfigFile, readEnvConfig, readSecret } from './lib'; export type LoadConfigOptions = { - // Root paths to search for config files. Config from earlier paths has lower priority. - rootPaths: string[]; + // The root directory of the config loading context. Used to find default configs. + configRoot: string; - // The environment that we're loading config for, e.g. 'development', 'production'. + // Absolute paths to load config files from. Configs from earlier paths have lower priority. + configPaths: string[]; + + // TODO(Rugvip): This will be removed in the future, but for now we use it to warn about possible mistakes. env: string; // Whether to read secrets or omit them, defaults to false. @@ -77,13 +75,37 @@ export async function loadConfig( options: LoadConfigOptions, ): Promise { const configs = []; + const { configRoot } = options; + const configPaths = options.configPaths.slice(); - const configPaths = await resolveStaticConfig(options); + // If no paths are provided, we default to reading + // `app-config.yaml` and, if it exists, `app-config.local.yaml` + if (configPaths.length === 0) { + configPaths.push(resolvePath(configRoot, 'app-config.yaml')); + + const localConfig = resolvePath(configRoot, 'app-config.local.yaml'); + if (await fs.pathExists(localConfig)) { + configPaths.push(localConfig); + } + + const envFile = `app-config.${options.env}.yaml`; + if (await fs.pathExists(resolvePath(configRoot, envFile))) { + console.error( + `Env config file '${envFile}' is not loaded as APP_ENV and NODE_ENV-based config loading has been removed`, + ); + console.error( + `To load the config file, use --config , listing every config file that you want to load`, + ); + } + } try { const secretPaths = new Set(); for (const configPath of configPaths) { + if (!isAbsolute(configPath)) { + throw new Error(`Config load path is not absolute: '${configPath}'`); + } const config = await readConfigFile( configPath, new Context({ diff --git a/packages/core-api/package.json b/packages/core-api/package.json index 5859382c5c..5b1456d1ba 100644 --- a/packages/core-api/package.json +++ b/packages/core-api/package.json @@ -30,6 +30,7 @@ }, "dependencies": { "@backstage/config": "^0.1.1-alpha.25", + "@backstage/test-utils": "^0.1.1-alpha.25", "@backstage/theme": "^0.1.1-alpha.25", "@material-ui/core": "^4.11.0", "@material-ui/icons": "^4.9.1", @@ -49,7 +50,8 @@ "@types/jest": "^26.0.7", "@types/node": "^12.0.0", "@types/zen-observable": "^0.8.0", - "jest-fetch-mock": "^3.0.3" + "cross-fetch": "^3.0.6", + "msw": "^0.21.3" }, "files": [ "dist" diff --git a/packages/core-api/src/lib/AuthConnector/DefaultAuthConnector.test.ts b/packages/core-api/src/lib/AuthConnector/DefaultAuthConnector.test.ts index 5781130799..391b86952d 100644 --- a/packages/core-api/src/lib/AuthConnector/DefaultAuthConnector.test.ts +++ b/packages/core-api/src/lib/AuthConnector/DefaultAuthConnector.test.ts @@ -19,8 +19,9 @@ import { DefaultAuthConnector } from './DefaultAuthConnector'; import MockOAuthApi from '../../apis/implementations/OAuthRequestApi/MockOAuthApi'; import * as loginPopup from '../loginPopup'; import { UrlPatternDiscovery } from '../../apis'; - -const anyFetch = fetch as any; +import { msw } from '@backstage/test-utils'; +import { setupServer } from 'msw/node'; +import { rest } from 'msw'; const defaultOptions = { discoveryApi: UrlPatternDiscovery.compile('http://my-host/api/{{pluginId}}'), @@ -39,19 +40,25 @@ const defaultOptions = { }; describe('DefaultAuthConnector', () => { + const server = setupServer(); + msw.setupDefaultHandlers(server); + afterEach(() => { jest.resetAllMocks(); - anyFetch.resetMocks(); }); it('should refresh a session', async () => { - anyFetch.mockResponseOnce( - JSON.stringify({ - idToken: 'mock-id-token', - accessToken: 'mock-access-token', - scopes: 'a b c', - expiresInSeconds: '60', - }), + server.use( + rest.get('*', (_req, res, ctx) => + res( + ctx.json({ + idToken: 'mock-id-token', + accessToken: 'mock-access-token', + scopes: 'a b c', + expiresInSeconds: '60', + }), + ), + ), ); const helper = new DefaultAuthConnector(defaultOptions); @@ -64,7 +71,11 @@ describe('DefaultAuthConnector', () => { }); it('should handle failure to refresh session', async () => { - anyFetch.mockRejectOnce(new Error('Network NOPE')); + server.use( + rest.get('*', (_req, res, ctx) => + res(ctx.status(500, 'Error: Network NOPE')), + ), + ); const helper = new DefaultAuthConnector(defaultOptions); await expect(helper.refreshSession()).rejects.toThrow( @@ -73,7 +84,7 @@ describe('DefaultAuthConnector', () => { }); it('should handle failure response when refreshing session', async () => { - anyFetch.mockResponseOnce({}, { status: 401, statusText: 'NOPE' }); + server.use(rest.get('*', (_req, res, ctx) => res(ctx.status(401, 'NOPE')))); const helper = new DefaultAuthConnector(defaultOptions); await expect(helper.refreshSession()).rejects.toThrow( diff --git a/packages/core-api/src/setupTests.ts b/packages/core-api/src/setupTests.ts index 8553642152..aea2220869 100644 --- a/packages/core-api/src/setupTests.ts +++ b/packages/core-api/src/setupTests.ts @@ -15,5 +15,4 @@ */ import '@testing-library/jest-dom'; - -require('jest-fetch-mock').enableMocks(); +import 'cross-fetch/polyfill'; diff --git a/packages/core/package.json b/packages/core/package.json index 4d70457f6c..2b0fa58266 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -65,8 +65,7 @@ "@types/jest": "^26.0.7", "@types/node": "^12.0.0", "@types/react-helmet": "^6.1.0", - "@types/zen-observable": "^0.8.0", - "jest-fetch-mock": "^3.0.3" + "@types/zen-observable": "^0.8.0" }, "files": [ "dist" diff --git a/packages/core/src/components/EmptyState/assets/createComponent.svg b/packages/core/src/components/EmptyState/assets/createComponent.svg index 2634bb7f23..b81b9f39aa 100644 --- a/packages/core/src/components/EmptyState/assets/createComponent.svg +++ b/packages/core/src/components/EmptyState/assets/createComponent.svg @@ -1,36 +1 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + \ No newline at end of file diff --git a/packages/core/src/components/EmptyState/assets/missingAnnotation.svg b/packages/core/src/components/EmptyState/assets/missingAnnotation.svg index 4f69605e6a..08f5a513eb 100644 --- a/packages/core/src/components/EmptyState/assets/missingAnnotation.svg +++ b/packages/core/src/components/EmptyState/assets/missingAnnotation.svg @@ -1,42 +1 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + \ No newline at end of file diff --git a/packages/core/src/components/EmptyState/assets/noBuild.svg b/packages/core/src/components/EmptyState/assets/noBuild.svg index d84f36af30..832d98efa7 100644 --- a/packages/core/src/components/EmptyState/assets/noBuild.svg +++ b/packages/core/src/components/EmptyState/assets/noBuild.svg @@ -1,44 +1 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + \ No newline at end of file diff --git a/packages/core/src/components/EmptyState/assets/noInformation.svg b/packages/core/src/components/EmptyState/assets/noInformation.svg index c199cf8b13..9a1c230e97 100644 --- a/packages/core/src/components/EmptyState/assets/noInformation.svg +++ b/packages/core/src/components/EmptyState/assets/noInformation.svg @@ -1 +1 @@ - + \ No newline at end of file diff --git a/packages/core/src/layout/Sidebar/Page.tsx b/packages/core/src/layout/Sidebar/Page.tsx index 2a4cf0fbb2..0c6f4a526d 100644 --- a/packages/core/src/layout/Sidebar/Page.tsx +++ b/packages/core/src/layout/Sidebar/Page.tsx @@ -45,7 +45,9 @@ export const SidebarPinStateContext = createContext( ); export const SidebarPage: FC<{}> = props => { - const [isPinned, setIsPinned] = useState(LocalStorage.getSidebarPinState()); + const [isPinned, setIsPinned] = useState(() => + LocalStorage.getSidebarPinState(), + ); useEffect(() => { LocalStorage.setSidebarPinState(isPinned); diff --git a/packages/core/src/setupTests.ts b/packages/core/src/setupTests.ts index 8553642152..825bcd4115 100644 --- a/packages/core/src/setupTests.ts +++ b/packages/core/src/setupTests.ts @@ -15,5 +15,3 @@ */ import '@testing-library/jest-dom'; - -require('jest-fetch-mock').enableMocks(); diff --git a/packages/create-app/templates/default-app/app-config.development.yaml b/packages/create-app/templates/default-app/app-config.development.yaml deleted file mode 100644 index 817847c6d6..0000000000 --- a/packages/create-app/templates/default-app/app-config.development.yaml +++ /dev/null @@ -1,13 +0,0 @@ -app: - baseUrl: http://localhost:3000 - -backend: - baseUrl: http://localhost:7000 - listen: - port: 7000 - cors: - origin: http://localhost:3000 - methods: [GET, POST, PUT, DELETE] - credentials: true - csp: - connect-src: ["'self'", 'http:', 'https:'] diff --git a/packages/create-app/templates/default-app/app-config.yaml.hbs b/packages/create-app/templates/default-app/app-config.yaml.hbs index b92cdf22bd..a0b9f156ed 100644 --- a/packages/create-app/templates/default-app/app-config.yaml.hbs +++ b/packages/create-app/templates/default-app/app-config.yaml.hbs @@ -1,6 +1,6 @@ app: title: Scaffolded Backstage App - baseUrl: http://localhost:7000 + baseUrl: http://localhost:3000 organization: name: My Company @@ -10,7 +10,11 @@ backend: listen: port: 7000 csp: - connect-src: ["'self'", 'https:'] + connect-src: ["'self'", 'http:', 'https:'] + cors: + origin: http://localhost:3000 + methods: [GET, POST, PUT, DELETE] + credentials: true {{#if dbTypeSqlite}} database: client: sqlite3 diff --git a/packages/create-app/templates/default-app/packages/backend/src/index.ts b/packages/create-app/templates/default-app/packages/backend/src/index.ts index 18b6cfa647..51de1ad6b3 100644 --- a/packages/create-app/templates/default-app/packages/backend/src/index.ts +++ b/packages/create-app/templates/default-app/packages/backend/src/index.ts @@ -42,7 +42,10 @@ function makeCreateEnv(config: Config) { } async function main() { - const config = await loadBackendConfig({logger: getRootLogger()}); + const config = await loadBackendConfig({ + argv: process.argv, + logger: getRootLogger(), + }); const createEnv = makeCreateEnv(config); const catalogEnv = useHotMemoize(module, () => createEnv('catalog')); @@ -52,16 +55,16 @@ async function main() { const techdocsEnv = useHotMemoize(module, () => createEnv('techdocs')); const apiRouter = Router(); - apiRouter.use('/catalog', await catalog(catalogEnv)) - apiRouter.use('/scaffolder', await scaffolder(scaffolderEnv)) - apiRouter.use('/auth', await auth(authEnv)) - apiRouter.use('/techdocs', await techdocs(techdocsEnv)) - apiRouter.use('/proxy', await proxy(proxyEnv)) + apiRouter.use('/catalog', await catalog(catalogEnv)); + apiRouter.use('/scaffolder', await scaffolder(scaffolderEnv)); + apiRouter.use('/auth', await auth(authEnv)); + apiRouter.use('/techdocs', await techdocs(techdocsEnv)); + apiRouter.use('/proxy', await proxy(proxyEnv)); apiRouter.use(notFoundHandler()); const service = createServiceBuilder(module) .loadConfig(config) - .addRouter('/api', apiRouter) + .addRouter('/api', apiRouter); await service.start().catch(err => { console.log(err); diff --git a/packages/e2e-test/package.json b/packages/e2e-test/package.json index 8a0246d4db..764e3c415a 100644 --- a/packages/e2e-test/package.json +++ b/packages/e2e-test/package.json @@ -31,7 +31,7 @@ "commander": "^6.1.0", "fs-extra": "^9.0.0", "handlebars": "^4.7.3", - "node-fetch": "^2.6.0", + "cross-fetch": "^3.0.6", "pgtools": "^0.3.0", "tree-kill": "^1.2.2", "ts-node": "^8.6.2", diff --git a/packages/e2e-test/src/commands/run.ts b/packages/e2e-test/src/commands/run.ts index 5caed14227..905cb20d52 100644 --- a/packages/e2e-test/src/commands/run.ts +++ b/packages/e2e-test/src/commands/run.ts @@ -16,7 +16,7 @@ import os from 'os'; import fs from 'fs-extra'; -import fetch from 'node-fetch'; +import fetch from 'cross-fetch'; import handlebars from 'handlebars'; import killTree from 'tree-kill'; import { resolve as resolvePath, join as joinPath } from 'path'; diff --git a/packages/techdocs-container/Dockerfile b/packages/techdocs-container/Dockerfile index b34fcbd861..49f7940d32 100644 --- a/packages/techdocs-container/Dockerfile +++ b/packages/techdocs-container/Dockerfile @@ -17,7 +17,7 @@ FROM python:3.8-alpine RUN apk update && apk --no-cache add gcc musl-dev openjdk11-jdk curl graphviz ttf-dejavu fontconfig -RUN curl -L http://sourceforge.net/projects/plantuml/files/plantuml.1.2020.16.jar/download > /opt/plantuml.jar +RUN curl -o plantuml.jar -L http://sourceforge.net/projects/plantuml/files/plantuml.1.2020.16.jar/download && echo "c789ace48347c43073232b1458badc5810c01fe8 plantuml.jar" | sha1sum -c - && mv plantuml.jar /opt/plantuml.jar RUN pip install --upgrade pip && pip install mkdocs-techdocs-core==0.0.8 # Create script to call plantuml.jar from a location in path diff --git a/packages/test-utils/package.json b/packages/test-utils/package.json index 1b7d35a197..2a290802d3 100644 --- a/packages/test-utils/package.json +++ b/packages/test-utils/package.json @@ -38,6 +38,7 @@ "@testing-library/react": "^10.4.1", "@testing-library/user-event": "^12.0.7", "@types/react": "^16.9", + "msw": "^0.21.3", "react": "^16.12.0", "react-dom": "^16.12.0", "react-router": "6.0.0-beta.0", diff --git a/packages/test-utils/src/testUtils/index.tsx b/packages/test-utils/src/testUtils/index.tsx index 4c5eb642bd..8206d10834 100644 --- a/packages/test-utils/src/testUtils/index.tsx +++ b/packages/test-utils/src/testUtils/index.tsx @@ -17,3 +17,4 @@ export * from './apis'; export { default as mockBreakpoint } from './mockBreakpoint'; export { wrapInTestApp, renderInTestApp } from './appWrappers'; +export * from './msw'; diff --git a/packages/test-utils/src/testUtils/msw/index.ts b/packages/test-utils/src/testUtils/msw/index.ts new file mode 100644 index 0000000000..0deaac0986 --- /dev/null +++ b/packages/test-utils/src/testUtils/msw/index.ts @@ -0,0 +1,27 @@ +/* + * Copyright 2020 Spotify AB + * + * 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. + */ + +export const msw = { + setupDefaultHandlers: (worker: { + listen: (t: any) => void; + close: () => void; + resetHandlers: () => void; + }) => { + beforeAll(() => worker.listen({ onUnhandledRequest: 'error' })); + afterAll(() => worker.close()); + afterEach(() => worker.resetHandlers()); + }, +}; diff --git a/plugins/api-docs/package.json b/plugins/api-docs/package.json index b6764f6593..6820027c6e 100644 --- a/plugins/api-docs/package.json +++ b/plugins/api-docs/package.json @@ -49,9 +49,8 @@ "@types/node": "^12.0.0", "@types/react": "^16.9", "@types/swagger-ui-react": "^3.23.3", - "jest-fetch-mock": "^3.0.3", - "msw": "^0.20.5", - "node-fetch": "^2.6.1" + "msw": "^0.21.2", + "cross-fetch": "^3.0.6" }, "files": [ "dist" diff --git a/plugins/api-docs/src/setupTests.ts b/plugins/api-docs/src/setupTests.ts index 8553642152..825bcd4115 100644 --- a/plugins/api-docs/src/setupTests.ts +++ b/plugins/api-docs/src/setupTests.ts @@ -15,5 +15,3 @@ */ import '@testing-library/jest-dom'; - -require('jest-fetch-mock').enableMocks(); diff --git a/plugins/auth-backend/package.json b/plugins/auth-backend/package.json index aff536d334..25129f33f7 100644 --- a/plugins/auth-backend/package.json +++ b/plugins/auth-backend/package.json @@ -37,7 +37,6 @@ "knex": "^0.21.1", "moment": "^2.26.0", "morgan": "^1.10.0", - "node-fetch": "^2.6.1", "passport": "^0.4.1", "passport-github2": "^0.1.12", "passport-gitlab2": "^5.0.0", @@ -48,6 +47,7 @@ "passport-saml": "^1.3.3", "uuid": "^8.0.0", "winston": "^3.2.1", + "cross-fetch": "^3.0.6", "yn": "^4.0.0" }, "devDependencies": { @@ -55,14 +55,12 @@ "@types/body-parser": "^1.19.0", "@types/cookie-parser": "^1.4.2", "@types/jwt-decode": "2.2.1", - "@types/node-fetch": "^2.5.7", "@types/passport": "^1.0.3", "@types/passport-github2": "^1.2.4", "@types/passport-google-oauth20": "^2.0.3", "@types/passport-microsoft": "^0.0.0", "@types/passport-saml": "^1.1.2", - "jest-fetch-mock": "^3.0.3", - "msw": "^0.20.5" + "msw": "^0.21.2" }, "files": [ "dist", diff --git a/plugins/auth-backend/src/lib/catalog/CatalogIdentityClient.ts b/plugins/auth-backend/src/lib/catalog/CatalogIdentityClient.ts index 2c1d66e1f8..b6919a98c1 100644 --- a/plugins/auth-backend/src/lib/catalog/CatalogIdentityClient.ts +++ b/plugins/auth-backend/src/lib/catalog/CatalogIdentityClient.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import fetch from 'node-fetch'; +import fetch from 'cross-fetch'; import { UserEntity } from '@backstage/catalog-model'; import { ConflictError, diff --git a/plugins/auth-backend/src/service/standaloneServer.ts b/plugins/auth-backend/src/service/standaloneServer.ts index a3c64c6e8e..a4f2377507 100644 --- a/plugins/auth-backend/src/service/standaloneServer.ts +++ b/plugins/auth-backend/src/service/standaloneServer.ts @@ -33,7 +33,7 @@ export async function startStandaloneServer( options: ServerOptions, ): Promise { const logger = options.logger.child({ service: 'auth-backend' }); - const config = await loadBackendConfig({ logger }); + const config = await loadBackendConfig({ logger, argv: process.argv }); const discovery = SingleHostDiscovery.fromConfig(config); const database = useHotMemoize(module, () => { diff --git a/plugins/auth-backend/src/setupTests.ts b/plugins/auth-backend/src/setupTests.ts index f7b6ca962d..ba33cf996b 100644 --- a/plugins/auth-backend/src/setupTests.ts +++ b/plugins/auth-backend/src/setupTests.ts @@ -14,6 +14,4 @@ * limitations under the License. */ -require('jest-fetch-mock').enableMocks(); - export {}; diff --git a/plugins/catalog-backend/migrations/20201019130742_add_relations_table.js b/plugins/catalog-backend/migrations/20201019130742_add_relations_table.js index accf9035e7..70150ff173 100644 --- a/plugins/catalog-backend/migrations/20201019130742_add_relations_table.js +++ b/plugins/catalog-backend/migrations/20201019130742_add_relations_table.js @@ -28,7 +28,7 @@ exports.up = async function up(knex) { .inTable('entities') .onDelete('CASCADE') .notNullable() - .comment('The originating entity of the relation'); + .comment('The entity that provided the relation'); table .string('source_full_name') .notNullable() diff --git a/plugins/catalog-backend/package.json b/plugins/catalog-backend/package.json index 34f9c7b360..ecc154bc5a 100644 --- a/plugins/catalog-backend/package.json +++ b/plugins/catalog-backend/package.json @@ -25,9 +25,9 @@ "@backstage/config": "^0.1.1-alpha.25", "@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", + "cross-fetch": "^3.0.6", "express": "^4.17.1", "express-promise-router": "^3.0.3", "fs-extra": "^9.0.0", @@ -36,7 +36,6 @@ "ldapjs": "^2.2.0", "lodash": "^4.17.15", "morgan": "^1.10.0", - "node-fetch": "^2.6.0", "p-limit": "^3.0.2", "sqlite3": "^5.0.0", "uuid": "^8.0.0", @@ -47,6 +46,7 @@ }, "devDependencies": { "@backstage/cli": "^0.1.1-alpha.25", + "@backstage/test-utils": "^0.1.1-alpha.25", "@types/core-js": "^2.5.4", "@types/git-url-parse": "^9.0.0", "@types/ldapjs": "^1.0.9", @@ -54,8 +54,7 @@ "@types/supertest": "^2.0.8", "@types/uuid": "^8.0.0", "@types/yup": "^0.29.8", - "jest-fetch-mock": "^3.0.3", - "msw": "^0.20.5", + "msw": "^0.21.2", "supertest": "^4.0.2" }, "files": [ diff --git a/plugins/catalog-backend/src/catalog/DatabaseEntitiesCatalog.test.ts b/plugins/catalog-backend/src/catalog/DatabaseEntitiesCatalog.test.ts index d84a8e071c..a801015915 100644 --- a/plugins/catalog-backend/src/catalog/DatabaseEntitiesCatalog.test.ts +++ b/plugins/catalog-backend/src/catalog/DatabaseEntitiesCatalog.test.ts @@ -18,6 +18,7 @@ import { getVoidLogger } from '@backstage/backend-common'; import type { Entity } from '@backstage/catalog-model'; import { Database, DatabaseManager } from '../database'; import { DatabaseEntitiesCatalog } from './DatabaseEntitiesCatalog'; +import { EntityUpsertRequest } from './types'; describe('DatabaseEntitiesCatalog', () => { let db: jest.Mocked; @@ -46,7 +47,7 @@ describe('DatabaseEntitiesCatalog', () => { db.transaction.mockImplementation(async f => f('tx')); }); - describe('addOrUpdateEntity', () => { + describe('batchAddOrUpdateEntities', () => { it('adds when no given uid and no matching by name', async () => { const entity: Entity = { apiVersion: 'a', @@ -58,19 +59,25 @@ describe('DatabaseEntitiesCatalog', () => { }; db.entities.mockResolvedValue([]); - db.addEntities.mockResolvedValue([{ entity }]); + db.addEntities.mockResolvedValue([ + { entity: { ...entity, metadata: { ...entity.metadata, uid: 'u' } } }, + ]); const catalog = new DatabaseEntitiesCatalog(db, getVoidLogger()); - const result = await catalog.addOrUpdateEntity(entity); + const result = await catalog.batchAddOrUpdateEntities([ + { entity, relations: [] }, + ]); - expect(db.entityByName).toHaveBeenCalledTimes(1); - expect(db.entityByName).toHaveBeenCalledWith(expect.anything(), { + expect(db.entities).toHaveBeenCalledTimes(1); + expect(db.entities).toHaveBeenCalledWith(expect.anything(), { kind: 'b', - namespace: 'd', - name: 'c', + 'metadata.namespace': 'd', + 'metadata.name': ['c'], }); + expect(db.setRelations).toHaveBeenCalledTimes(1); + expect(db.setRelations).toHaveBeenCalledWith(expect.anything(), 'u', []); expect(db.addEntities).toHaveBeenCalledTimes(1); - expect(result).toBe(entity); + expect(result).toEqual([{ entityId: 'u' }]); }); it('updates when given uid', async () => { @@ -82,9 +89,11 @@ describe('DatabaseEntitiesCatalog', () => { name: 'c', namespace: 'd', }, + spec: { + x: 'b', + }, }; - - db.entityByUid.mockResolvedValue({ + const existing = { entity: { apiVersion: 'a', kind: 'b', @@ -95,14 +104,28 @@ describe('DatabaseEntitiesCatalog', () => { name: 'c', namespace: 'd', }, + spec: { + x: 'a', + }, }, - }); + }; + + db.entities.mockResolvedValue([existing]); + db.entityByUid.mockResolvedValue(existing); db.updateEntity.mockResolvedValue({ entity }); const catalog = new DatabaseEntitiesCatalog(db, getVoidLogger()); - const result = await catalog.addOrUpdateEntity(entity); + const result = await catalog.batchAddOrUpdateEntities([ + { entity, relations: [] }, + ]); - expect(db.entities).toHaveBeenCalledTimes(0); + expect(db.entities).toHaveBeenCalledTimes(1); + expect(db.entities).toHaveBeenCalledWith(expect.anything(), { + kind: 'b', + 'metadata.namespace': 'd', + 'metadata.name': ['c'], + }); + expect(db.entityByName).not.toHaveBeenCalled(); expect(db.entityByUid).toHaveBeenCalledTimes(1); expect(db.entityByUid).toHaveBeenCalledWith(expect.anything(), 'u'); expect(db.updateEntity).toHaveBeenCalledTimes(1); @@ -114,17 +137,22 @@ describe('DatabaseEntitiesCatalog', () => { kind: 'b', metadata: { uid: 'u', - etag: 'e', - generation: 1, + etag: expect.any(String), + generation: 2, name: 'c', namespace: 'd', }, + spec: { + x: 'b', + }, }, }, 'e', 1, ); - expect(result).toBe(entity); + expect(db.setRelations).toHaveBeenCalledTimes(1); + expect(db.setRelations).toHaveBeenCalledWith(expect.anything(), 'u', []); + expect(result).toEqual([{ entityId: 'u' }]); }); it('update when no given uid and matching by name', async () => { @@ -135,25 +163,42 @@ describe('DatabaseEntitiesCatalog', () => { name: 'c', namespace: 'd', }, + spec: { + x: 'b', + }, }; - const existing: Entity = { - apiVersion: 'a', - kind: 'b', - metadata: { - uid: 'u', - etag: 'e', - generation: 1, - name: 'c', - namespace: 'd', + const existing = { + entity: { + apiVersion: 'a', + kind: 'b', + metadata: { + uid: 'u', + etag: 'e', + generation: 1, + name: 'c', + namespace: 'd', + }, + spec: { + x: 'a', + }, }, }; - db.entityByName.mockResolvedValue({ entity: existing }); - db.updateEntity.mockResolvedValue({ entity: existing }); + db.entities.mockResolvedValue([existing]); + db.entityByName.mockResolvedValue(existing); + db.updateEntity.mockResolvedValue(existing); const catalog = new DatabaseEntitiesCatalog(db, getVoidLogger()); - const result = await catalog.addOrUpdateEntity(added); + const result = await catalog.batchAddOrUpdateEntities([ + { entity: added, relations: [] }, + ]); + expect(db.entities).toHaveBeenCalledTimes(1); + expect(db.entities).toHaveBeenCalledWith(expect.anything(), { + kind: 'b', + 'metadata.namespace': 'd', + 'metadata.name': ['c'], + }); expect(db.entityByName).toHaveBeenCalledTimes(1); expect(db.entityByName).toHaveBeenCalledWith(expect.anything(), { kind: 'b', @@ -169,51 +214,95 @@ describe('DatabaseEntitiesCatalog', () => { kind: 'b', metadata: { uid: 'u', - etag: 'e', - generation: 1, + etag: expect.any(String), + generation: 2, name: 'c', namespace: 'd', }, + spec: { + x: 'b', + }, }, }, 'e', 1, ); - expect(result).toEqual(existing); + expect(result).toEqual([{ entityId: 'u' }]); + }); + + it('should not update if entity is unchanged', async () => { + const entity: Entity = { + apiVersion: 'a', + kind: 'b', + metadata: { + uid: 'u', + name: 'c', + namespace: 'd', + }, + spec: { + x: 'a', + }, + }; + + db.entities.mockResolvedValue([{ entity }]); + db.entityByUid.mockResolvedValue({ entity }); + db.updateEntity.mockResolvedValue({ entity }); + + const catalog = new DatabaseEntitiesCatalog(db, getVoidLogger()); + const result = await catalog.batchAddOrUpdateEntities([ + { entity, relations: [] }, + ]); + + expect(db.entities).toHaveBeenCalledTimes(1); + expect(db.entities).toHaveBeenCalledWith(expect.anything(), { + kind: 'b', + 'metadata.namespace': 'd', + 'metadata.name': ['c'], + }); + expect(db.entityByName).not.toHaveBeenCalled(); + expect(db.entityByUid).not.toHaveBeenCalled(); + expect(db.updateEntity).not.toHaveBeenCalled(); + expect(db.setRelations).toHaveBeenCalledTimes(1); + expect(db.setRelations).toHaveBeenCalledWith(expect.anything(), 'u', []); + expect(result).toEqual([{ entityId: 'u' }]); }); - }); - describe('batchAddOrUpdateEntities', () => { it('both adds and updates', async () => { const catalog = new DatabaseEntitiesCatalog( await DatabaseManager.createTestDatabase(), getVoidLogger(), ); - const entities: Entity[] = []; - for (let i = 0; i < 500; ++i) { + const entities: EntityUpsertRequest[] = []; + for (let i = 0; i < 300; ++i) { entities.push({ - apiVersion: 'a', - kind: 'k', - metadata: { name: `n${i}` }, + entity: { + apiVersion: 'a', + kind: 'k', + metadata: { name: `n${i}` }, + }, + relations: [], }); } await catalog.batchAddOrUpdateEntities(entities); const afterFirst = await catalog.entities(); - expect(afterFirst.length).toBe(500); + expect(afterFirst.length).toBe(300); - entities[40].metadata.op = 'changed'; + entities[40].entity.metadata.op = 'changed'; entities.push({ - apiVersion: 'a', - kind: 'k', - metadata: { name: `n500`, op: 'added' }, + entity: { + apiVersion: 'a', + kind: 'k', + metadata: { name: `n300`, op: 'added' }, + }, + relations: [], }); await catalog.batchAddOrUpdateEntities(entities); const afterSecond = await catalog.entities(); - expect(afterSecond.length).toBe(501); + expect(afterSecond.length).toBe(301); expect(afterSecond.find(e => e.metadata.op === 'changed')).toBeDefined(); expect(afterSecond.find(e => e.metadata.op === 'added')).toBeDefined(); - }); + }, 10000); }); }); diff --git a/plugins/catalog-backend/src/catalog/DatabaseEntitiesCatalog.ts b/plugins/catalog-backend/src/catalog/DatabaseEntitiesCatalog.ts index efb541727a..6c3065aeaa 100644 --- a/plugins/catalog-backend/src/catalog/DatabaseEntitiesCatalog.ts +++ b/plugins/catalog-backend/src/catalog/DatabaseEntitiesCatalog.ts @@ -29,7 +29,11 @@ import limiterFactory from 'p-limit'; import { Logger } from 'winston'; import type { Database, DbEntityResponse, EntityFilters } from '../database'; import { durationText } from '../util/timing'; -import type { EntitiesCatalog } from './types'; +import type { + EntitiesCatalog, + EntityUpsertRequest, + EntityUpsertResponse, +} from './types'; type BatchContext = { kind: string; @@ -63,7 +67,7 @@ export class DatabaseEntitiesCatalog implements EntitiesCatalog { return items.map(i => i.entity); } - async addOrUpdateEntity( + private async addOrUpdateEntity( entity: Entity, locationId?: string, ): Promise { @@ -96,15 +100,6 @@ export class DatabaseEntitiesCatalog implements EntitiesCatalog { }); } - async addEntities(entities: Entity[], locationId?: string): Promise { - await this.database.transaction(async tx => { - await this.database.addEntities( - tx, - entities.map(entity => ({ locationId, entity })), - ); - }); - } - async removeEntityByUid(uid: string): Promise { return await this.database.transaction(async tx => { const entityResponse = await this.database.entityByUid(tx, uid); @@ -138,27 +133,31 @@ export class DatabaseEntitiesCatalog implements EntitiesCatalog { * @param entities Some entities * @param locationId The location that they all belong to */ - async batchAddOrUpdateEntities(entities: Entity[], locationId?: string) { + async batchAddOrUpdateEntities( + requests: EntityUpsertRequest[], + locationId?: string, + ): Promise { // Group the entities by unique kind+namespace combinations - const entitiesByKindAndNamespace = groupBy(entities, entity => { + const entitiesByKindAndNamespace = groupBy(requests, ({ entity }) => { const name = getEntityName(entity); return `${name.kind}:${name.namespace}`.toLowerCase(); }); const limiter = limiterFactory(BATCH_CONCURRENCY); - const tasks: Promise[] = []; + const tasks: Promise[] = []; - for (const groupEntities of Object.values(entitiesByKindAndNamespace)) { - const { kind, namespace } = getEntityName(groupEntities[0]); + for (const groupRequests of Object.values(entitiesByKindAndNamespace)) { + const { kind, namespace } = getEntityName(groupRequests[0].entity); // Go through the new entities in reasonable chunk sizes (sometimes, // sources produce tens of thousands of entities, and those are too large // batch sizes to reasonably send to the database) - for (const batch of chunk(groupEntities, BATCH_SIZE)) { + for (const batch of chunk(groupRequests, BATCH_SIZE)) { tasks.push( limiter(async () => { - const first = serializeEntityRef(batch[0]); - const last = serializeEntityRef(batch[batch.length - 1]); + const first = serializeEntityRef(batch[0].entity); + const last = serializeEntityRef(batch[batch.length - 1].entity); + const modifiedEntityIds: EntityUpsertResponse[] = []; this.logger.debug( `Considering batch ${first}-${last} (${batch.length} entries)`, ); @@ -167,12 +166,28 @@ export class DatabaseEntitiesCatalog implements EntitiesCatalog { const context = { kind, namespace, locationId }; for (let attempt = 1; attempt <= BATCH_ATTEMPTS; ++attempt) { try { - const { toAdd, toUpdate } = await this.analyzeBatch( + const { toAdd, toUpdate, toIgnore } = await this.analyzeBatch( batch, context, ); - if (toAdd.length) await this.batchAdd(toAdd, context); - if (toUpdate.length) await this.batchUpdate(toUpdate, context); + if (toAdd.length) { + modifiedEntityIds.push( + ...(await this.batchAdd(toAdd, context)), + ); + } + if (toUpdate.length) { + modifiedEntityIds.push( + ...(await this.batchUpdate(toUpdate, context)), + ); + } + // TODO(Rugvip): We currently always update relations, but we + // likely want to figure out a way to avoid that + for (const { entity, relations } of toIgnore) { + const entityId = entity.metadata.uid!; + await this.setRelations(entityId, relations); + modifiedEntityIds.push({ entityId }); + } + break; } catch (e) { if (e instanceof ConflictError && attempt < BATCH_ATTEMPTS) { @@ -184,16 +199,18 @@ export class DatabaseEntitiesCatalog implements EntitiesCatalog { } } } + + return modifiedEntityIds; }), ); } } - await Promise.all(tasks); + return (await Promise.all(tasks)).flat(); } // Set the relations originating from an entity using the DB layer - async setRelations( + private async setRelations( originatingEntityId: string, relations: EntityRelationSpec[], ): Promise { @@ -207,15 +224,16 @@ export class DatabaseEntitiesCatalog implements EntitiesCatalog { // produce the list of entities to be added, and the list of entities to be // updated private async analyzeBatch( - newEntities: Entity[], + requests: EntityUpsertRequest[], { kind, namespace }: BatchContext, ): Promise<{ - toAdd: Entity[]; - toUpdate: Entity[]; + toAdd: EntityUpsertRequest[]; + toUpdate: EntityUpsertRequest[]; + toIgnore: EntityUpsertRequest[]; }> { const markTimestamp = process.hrtime(); - const names = newEntities.map(e => e.metadata.name); + const names = requests.map(({ entity }) => entity.metadata.name); const oldEntities = await this.entities({ kind: kind, 'metadata.namespace': namespace, @@ -226,18 +244,22 @@ export class DatabaseEntitiesCatalog implements EntitiesCatalog { oldEntities.map(e => [e.metadata.name, e]), ); - const toAdd: Entity[] = []; - const toUpdate: Entity[] = []; + const toAdd: EntityUpsertRequest[] = []; + const toUpdate: EntityUpsertRequest[] = []; + const toIgnore: EntityUpsertRequest[] = []; - for (const newEntity of newEntities) { + for (const request of requests) { + const newEntity = request.entity; const oldEntity = oldEntitiesByName.get(newEntity.metadata.name); if (!oldEntity) { - toAdd.push(newEntity); + toAdd.push(request); } else if (entityHasChanges(oldEntity, newEntity)) { // TODO(freben): This currently uses addOrUpdateEntity under the hood, // but should probably calculate the end result entity right here // instead and call a dedicated batch update database method instead - toUpdate.push(newEntity); + toUpdate.push(request); + } else { + toIgnore.push(request); } } @@ -247,33 +269,60 @@ export class DatabaseEntitiesCatalog implements EntitiesCatalog { } entities to update in ${durationText(markTimestamp)}`, ); - return { toAdd, toUpdate }; + return { toAdd, toUpdate, toIgnore }; } // Efficiently adds the given entities to storage, under the assumption that // they do not conflict with any existing entities - private async batchAdd(entities: Entity[], { locationId }: BatchContext) { + private async batchAdd( + requests: EntityUpsertRequest[], + { locationId }: BatchContext, + ): Promise { const markTimestamp = process.hrtime(); - await this.addEntities(entities, locationId); + const res = await this.database.transaction( + async tx => + await this.database.addEntities( + tx, + requests.map(({ entity }) => ({ locationId, entity })), + ), + ); + + const entityIds = res.map(({ entity }) => ({ + entityId: entity.metadata.uid!, + })); + + for (const [index, { entityId }] of entityIds.entries()) { + await this.setRelations(entityId, requests[index].relations); + } this.logger.debug( - `Added ${entities.length} entities in ${durationText(markTimestamp)}`, + `Added ${requests.length} entities in ${durationText(markTimestamp)}`, ); + + return entityIds; } // Efficiently updates the given entities into storage, under the assumption // that there already exist entities with the same names - private async batchUpdate(entities: Entity[], { locationId }: BatchContext) { + private async batchUpdate( + requests: EntityUpsertRequest[], + { locationId }: BatchContext, + ): Promise { const markTimestamp = process.hrtime(); - + const responseIds: EntityUpsertResponse[] = []; // TODO(freben): Still not batched - for (const entity of entities) { - await this.addOrUpdateEntity(entity, locationId); + for (const entity of requests) { + const res = await this.addOrUpdateEntity(entity.entity, locationId); + const entityId = res.metadata.uid!; + responseIds.push({ entityId }); + await this.setRelations(entityId, entity.relations); } this.logger.debug( - `Updated ${entities.length} entities in ${durationText(markTimestamp)}`, + `Updated ${requests.length} entities in ${durationText(markTimestamp)}`, ); + + return responseIds; } } diff --git a/plugins/catalog-backend/src/catalog/types.ts b/plugins/catalog-backend/src/catalog/types.ts index e50b5f15db..4d821c428a 100644 --- a/plugins/catalog-backend/src/catalog/types.ts +++ b/plugins/catalog-backend/src/catalog/types.ts @@ -21,10 +21,17 @@ import type { EntityFilters } from '../database'; // Entities // +export type EntityUpsertRequest = { + entity: Entity; + relations: EntityRelationSpec[]; +}; + +export type EntityUpsertResponse = { + entityId: string; +}; + export type EntitiesCatalog = { entities(filters?: EntityFilters): Promise; - addOrUpdateEntity(entity: Entity, locationId?: string): Promise; - addEntities(entities: Entity[], locationId?: string): Promise; removeEntityByUid(uid: string): Promise; /** @@ -34,15 +41,9 @@ export type EntitiesCatalog = { * @param locationId The location that they all belong to */ batchAddOrUpdateEntities( - entities: Entity[], + entities: EntityUpsertRequest[], locationId?: string, - ): Promise; - - // Same as the DB layer - setRelations( - entityUid: string, - relations: EntityRelationSpec[], - ): Promise; + ): Promise; }; // diff --git a/plugins/catalog-backend/src/ingestion/HigherOrderOperations.test.ts b/plugins/catalog-backend/src/ingestion/HigherOrderOperations.test.ts index 34320fe18f..933433b47e 100644 --- a/plugins/catalog-backend/src/ingestion/HigherOrderOperations.test.ts +++ b/plugins/catalog-backend/src/ingestion/HigherOrderOperations.test.ts @@ -31,10 +31,7 @@ describe('HigherOrderOperations', () => { beforeAll(() => { entitiesCatalog = { entities: jest.fn(), - addOrUpdateEntity: jest.fn(), - addEntities: jest.fn(), removeEntityByUid: jest.fn(), - setRelations: jest.fn(), batchAddOrUpdateEntities: jest.fn(), }; locationsCatalog = { @@ -69,7 +66,10 @@ describe('HigherOrderOperations', () => { }; locationsCatalog.addLocation.mockImplementation(x => Promise.resolve(x)); locationsCatalog.locations.mockResolvedValue([]); - locationReader.read.mockResolvedValue({ entities: [], errors: [] }); + locationReader.read.mockResolvedValue({ + entities: [], + errors: [], + }); const result = await higherOrderOperation.addLocation(spec); @@ -83,7 +83,7 @@ describe('HigherOrderOperations', () => { expect(locationsCatalog.locations).toBeCalledTimes(1); expect(locationReader.read).toBeCalledTimes(1); expect(locationReader.read).toBeCalledWith({ type: 'a', target: 'b' }); - expect(entitiesCatalog.addOrUpdateEntity).not.toBeCalled(); + expect(entitiesCatalog.batchAddOrUpdateEntities).not.toBeCalled(); expect(locationsCatalog.addLocation).toBeCalledTimes(1); expect(locationsCatalog.addLocation).toBeCalledWith( expect.objectContaining({ @@ -109,7 +109,10 @@ describe('HigherOrderOperations', () => { data: location, }, ]); - locationReader.read.mockResolvedValue({ entities: [], errors: [] }); + locationReader.read.mockResolvedValue({ + entities: [], + errors: [], + }); const result = await higherOrderOperation.addLocation(spec); @@ -118,7 +121,7 @@ describe('HigherOrderOperations', () => { expect(locationsCatalog.locations).toBeCalledTimes(1); expect(locationReader.read).toBeCalledTimes(1); expect(locationReader.read).toBeCalledWith({ type: 'a', target: 'b' }); - expect(entitiesCatalog.addOrUpdateEntity).not.toBeCalled(); + expect(entitiesCatalog.batchAddOrUpdateEntities).not.toBeCalled(); expect(locationsCatalog.addLocation).not.toBeCalled(); }); @@ -136,7 +139,7 @@ describe('HigherOrderOperations', () => { locationsCatalog.locations.mockResolvedValue([]); locationReader.read.mockResolvedValue({ - entities: [{ entity, location }], + entities: [{ entity, location, relations: [] }], errors: [{ error: new Error('abcd'), location }], }); @@ -144,7 +147,7 @@ describe('HigherOrderOperations', () => { /abcd/, ); expect(locationsCatalog.locations).toBeCalledTimes(1); - expect(entitiesCatalog.addOrUpdateEntity).not.toBeCalled(); + expect(entitiesCatalog.batchAddOrUpdateEntities).not.toBeCalled(); expect(locationsCatalog.addLocation).not.toBeCalled(); }); }); @@ -159,7 +162,7 @@ describe('HigherOrderOperations', () => { expect(locationsCatalog.locations).toHaveBeenCalledTimes(1); expect(locationReader.read).not.toHaveBeenCalled(); - expect(entitiesCatalog.addOrUpdateEntity).not.toHaveBeenCalled(); + expect(entitiesCatalog.batchAddOrUpdateEntities).not.toHaveBeenCalled(); }); it('can update a single location where a matching entity did not exist', async () => { @@ -179,15 +182,18 @@ describe('HigherOrderOperations', () => { metadata: { name: 'c1' }, spec: { type: 'service' }, }; + const entityId = 'xyz123'; locationsCatalog.locations.mockResolvedValue([ { currentStatus: locationStatus, data: location }, ]); locationReader.read.mockResolvedValue({ - entities: [{ entity: desc, location }], + entities: [{ entity: desc, location, relations: [] }], errors: [], }); - entitiesCatalog.batchAddOrUpdateEntities.mockResolvedValue(undefined); + entitiesCatalog.batchAddOrUpdateEntities.mockResolvedValue([ + { entityId }, + ]); await expect( higherOrderOperation.refreshAllLocations(), @@ -200,9 +206,13 @@ describe('HigherOrderOperations', () => { target: 'thing', }); expect(entitiesCatalog.batchAddOrUpdateEntities).toHaveBeenCalledTimes(1); - expect(entitiesCatalog.batchAddOrUpdateEntities).toHaveBeenNthCalledWith( - 1, - [expect.objectContaining({ metadata: { name: 'c1' } })], + expect(entitiesCatalog.batchAddOrUpdateEntities).toHaveBeenCalledWith( + [ + expect.objectContaining({ + entity: expect.objectContaining({ metadata: { name: 'c1' } }), + relations: [], + }), + ], '123', ); }); @@ -229,11 +239,11 @@ describe('HigherOrderOperations', () => { { currentStatus: locationStatus, data: location }, ]); locationReader.read.mockResolvedValue({ - entities: [{ entity: desc, location }], + entities: [{ entity: desc, location, relations: [] }], errors: [], }); entitiesCatalog.entities.mockResolvedValue([]); - entitiesCatalog.addEntities.mockResolvedValue(undefined); + entitiesCatalog.batchAddOrUpdateEntities.mockResolvedValue([]); await expect( higherOrderOperation.refreshAllLocations(), diff --git a/plugins/catalog-backend/src/ingestion/HigherOrderOperations.ts b/plugins/catalog-backend/src/ingestion/HigherOrderOperations.ts index a644dd48aa..cb161d1ccf 100644 --- a/plugins/catalog-backend/src/ingestion/HigherOrderOperations.ts +++ b/plugins/catalog-backend/src/ingestion/HigherOrderOperations.ts @@ -14,7 +14,7 @@ * limitations under the License. */ -import { Entity, Location, LocationSpec } from '@backstage/catalog-model'; +import { Location, LocationSpec } from '@backstage/catalog-model'; import { v4 as uuidv4 } from 'uuid'; import { Logger } from 'winston'; import { EntitiesCatalog, LocationsCatalog } from '../catalog'; @@ -78,7 +78,7 @@ export class HigherOrderOperations implements HigherOrderOperation { // Read the location fully, bailing on any errors const readerOutput = await this.locationReader.read(spec); - if (readerOutput.errors.length) { + if (!(spec.presence === 'optional') && readerOutput.errors.length) { const item = readerOutput.errors[0]; throw item.error; } @@ -91,16 +91,20 @@ export class HigherOrderOperations implements HigherOrderOperation { if (!previousLocation) { await this.locationsCatalog.addLocation(location); } - const outputEntities: Entity[] = []; - for (const entity of readerOutput.entities) { - const out = await this.entitiesCatalog.addOrUpdateEntity( - entity.entity, - location.id, - ); - outputEntities.push(out); + if (readerOutput.entities.length === 0) { + return { location, entities: [] }; } - return { location, entities: outputEntities }; + const writtenEntities = await this.entitiesCatalog.batchAddOrUpdateEntities( + readerOutput.entities, + location.id, + ); + + const entities = await this.entitiesCatalog.entities({ + 'metadata.uid': writtenEntities.map(e => e.entityId), + }); + + return { location, entities }; } /** @@ -163,7 +167,7 @@ export class HigherOrderOperations implements HigherOrderOperation { try { await this.entitiesCatalog.batchAddOrUpdateEntities( - readerOutput.entities.map(e => e.entity), + readerOutput.entities, location.id, ); } catch (e) { diff --git a/plugins/catalog-backend/src/ingestion/LocationReaders.ts b/plugins/catalog-backend/src/ingestion/LocationReaders.ts index 8fec1541df..11ae45cae9 100644 --- a/plugins/catalog-backend/src/ingestion/LocationReaders.ts +++ b/plugins/catalog-backend/src/ingestion/LocationReaders.ts @@ -18,6 +18,7 @@ import { UrlReader } from '@backstage/backend-common'; import { Entity, EntityPolicy, + EntityRelationSpec, ENTITY_DEFAULT_NAMESPACE, LocationSpec, } from '@backstage/catalog-model'; @@ -61,7 +62,10 @@ export class LocationReaders implements LocationReader { async read(location: LocationSpec): Promise { const { rulesEnforcer, logger } = this.options; - const output: ReadLocationResult = { entities: [], errors: [] }; + const output: ReadLocationResult = { + entities: [], + errors: [], + }; let items: CatalogProcessorResult[] = [result.location(location, false)]; for (let depth = 0; depth < MAX_DEPTH; ++depth) { @@ -75,11 +79,21 @@ export class LocationReaders implements LocationReader { await this.handleData(item, emit); } else if (item.type === 'entity') { if (rulesEnforcer.isAllowed(item.entity, item.location)) { - const entity = await this.handleEntity(item, emit); + const relations = Array(); + + const entity = await this.handleEntity(item, emitResult => { + if (emitResult.type === 'relation') { + relations.push(emitResult.relation); + return; + } + emit(emitResult); + }); + if (entity) { output.entities.push({ entity, location: item.location, + relations, }); } } else { @@ -118,11 +132,23 @@ export class LocationReaders implements LocationReader { ) { const { processors, logger } = this.options; + const validatedEmit: CatalogProcessorEmit = emitResult => { + if (emitResult.type === 'relation') { + throw new Error('readLocation may not emit entity relations'); + } + + emit(emitResult); + }; + for (const processor of processors) { if (processor.readLocation) { try { if ( - await processor.readLocation(item.location, item.optional, emit) + await processor.readLocation( + item.location, + item.optional, + validatedEmit, + ) ) { return; } @@ -145,10 +171,20 @@ export class LocationReaders implements LocationReader { ) { const { processors, logger } = this.options; + const validatedEmit: CatalogProcessorEmit = emitResult => { + if (emitResult.type === 'relation') { + throw new Error('parseData may not emit entity relations'); + } + + emit(emitResult); + }; + for (const processor of processors) { if (processor.parseData) { try { - if (await processor.parseData(item.data, item.location, emit)) { + if ( + await processor.parseData(item.data, item.location, validatedEmit) + ) { return; } } catch (e) { @@ -242,10 +278,18 @@ export class LocationReaders implements LocationReader { `Encountered error at location ${item.location.type} ${item.location.target}, ${item.error}`, ); + const validatedEmit: CatalogProcessorEmit = emitResult => { + if (emitResult.type === 'relation') { + throw new Error('handleError may not emit entity relations'); + } + + emit(emitResult); + }; + for (const processor of processors) { if (processor.handleError) { try { - await processor.handleError(item.error, item.location, emit); + await processor.handleError(item.error, item.location, validatedEmit); } catch (e) { const message = `Processor ${processor.constructor.name} threw an error while handling another error at ${item.location.type} ${item.location.target}, ${e}`; emit(result.generalError(item.location, message)); diff --git a/plugins/catalog-backend/src/ingestion/processors/AzureApiReaderProcessor.ts b/plugins/catalog-backend/src/ingestion/processors/AzureApiReaderProcessor.ts index b6e65a7d7a..9b9ba586ef 100644 --- a/plugins/catalog-backend/src/ingestion/processors/AzureApiReaderProcessor.ts +++ b/plugins/catalog-backend/src/ingestion/processors/AzureApiReaderProcessor.ts @@ -15,8 +15,9 @@ */ import { LocationSpec } from '@backstage/catalog-model'; + import { Config } from '@backstage/config'; -import fetch, { HeadersInit, RequestInit } from 'node-fetch'; +import fetch from 'cross-fetch'; import * as result from './results'; import { CatalogProcessor, CatalogProcessorEmit } from './types'; @@ -44,11 +45,9 @@ export class AzureApiReaderProcessor implements CatalogProcessor { ).toString('base64')}`; } - const requestOptions: RequestInit = { + return { headers, }; - - return requestOptions; } async readLocation( @@ -67,7 +66,7 @@ export class AzureApiReaderProcessor implements CatalogProcessor { // 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 4efcec2884..337053965f 100644 --- a/plugins/catalog-backend/src/ingestion/processors/BitbucketApiReaderProcessor.ts +++ b/plugins/catalog-backend/src/ingestion/processors/BitbucketApiReaderProcessor.ts @@ -15,9 +15,9 @@ */ import { LocationSpec } from '@backstage/catalog-model'; -import { Config } from '@backstage/config'; -import fetch, { HeadersInit, RequestInit } from 'node-fetch'; +import fetch from 'cross-fetch'; import * as result from './results'; +import { Config } from '@backstage/config'; import { CatalogProcessor, CatalogProcessorEmit } from './types'; // *********************************************************************** @@ -70,8 +70,8 @@ export class BitbucketApiReaderProcessor implements CatalogProcessor { 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 2f03900cdb..fed7f2a5c2 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 { CatalogProcessor, CatalogProcessorEmit } from './types'; @@ -252,8 +252,8 @@ export class GithubReaderProcessor implements CatalogProcessor { 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 e47b7d1aad..51a286c7ea 100644 --- a/plugins/catalog-backend/src/ingestion/processors/GitlabApiReaderProcessor.ts +++ b/plugins/catalog-backend/src/ingestion/processors/GitlabApiReaderProcessor.ts @@ -15,8 +15,8 @@ */ import { LocationSpec } from '@backstage/catalog-model'; +import fetch from 'cross-fetch'; import { Config } from '@backstage/config'; -import fetch, { HeadersInit, RequestInit } from 'node-fetch'; import * as result from './results'; import { CatalogProcessor, CatalogProcessorEmit } from './types'; @@ -61,8 +61,8 @@ export class GitlabApiReaderProcessor implements CatalogProcessor { 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 2c58a6d43e..38498b44fa 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 { CatalogProcessor, CatalogProcessorEmit } from './types'; @@ -40,8 +40,8 @@ export class GitlabReaderProcessor implements CatalogProcessor { 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) { diff --git a/plugins/catalog-backend/src/ingestion/processors/OwnerRelationProcessor.ts b/plugins/catalog-backend/src/ingestion/processors/OwnerRelationProcessor.ts new file mode 100644 index 0000000000..4c67589789 --- /dev/null +++ b/plugins/catalog-backend/src/ingestion/processors/OwnerRelationProcessor.ts @@ -0,0 +1,74 @@ +/* + * Copyright 2020 Spotify AB + * + * 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 { + Entity, + ENTITY_DEFAULT_NAMESPACE, + LocationSpec, + parseEntityRef, + ApiEntityV1alpha1, + ComponentEntityV1alpha1, + RELATION_OWNED_BY, + RELATION_OWNER_OF, + getEntityName, +} from '@backstage/catalog-model'; +import { CatalogProcessor, CatalogProcessorEmit } from './types'; +import * as result from './results'; + +const includedKinds = new Set(['api', 'component']); + +export class OwnerRelationProcessor implements CatalogProcessor { + async postProcessEntity( + entity: Entity, + _location: LocationSpec, + emit: CatalogProcessorEmit, + ): Promise { + if (!includedKinds.has(entity.kind.toLowerCase())) { + return entity; + } + const apiOrComponentEntity = entity as + | ApiEntityV1alpha1 + | ComponentEntityV1alpha1; + + const owner = apiOrComponentEntity.spec?.owner; + if (owner) { + const namespace = entity.metadata.namespace ?? ENTITY_DEFAULT_NAMESPACE; + + const selfRef = getEntityName(entity); + const ownerRef = parseEntityRef(owner, { + defaultKind: 'group', + defaultNamespace: namespace, + }); + + emit( + result.relation({ + source: selfRef, + type: RELATION_OWNED_BY, + target: ownerRef, + }), + ); + emit( + result.relation({ + source: ownerRef, + type: RELATION_OWNER_OF, + target: selfRef, + }), + ); + } + + return entity; + } +} diff --git a/plugins/catalog-backend/src/ingestion/processors/UrlReaderProcessor.test.ts b/plugins/catalog-backend/src/ingestion/processors/UrlReaderProcessor.test.ts index a2b9678ba0..73046f0ed1 100644 --- a/plugins/catalog-backend/src/ingestion/processors/UrlReaderProcessor.test.ts +++ b/plugins/catalog-backend/src/ingestion/processors/UrlReaderProcessor.test.ts @@ -14,25 +14,23 @@ * limitations under the License. */ +import { UrlReaderProcessor } from './UrlReaderProcessor'; import { getVoidLogger, UrlReaders } from '@backstage/backend-common'; import { ConfigReader } from '@backstage/config'; import { rest } from 'msw'; import { setupServer } from 'msw/node'; +import { msw } from '@backstage/test-utils'; import { CatalogProcessorDataResult, CatalogProcessorErrorResult, CatalogProcessorResult, } from './types'; -import { UrlReaderProcessor } from './UrlReaderProcessor'; describe('UrlReaderProcessor', () => { const mockApiOrigin = 'http://localhost:23000'; const server = setupServer(); - beforeAll(() => server.listen({ onUnhandledRequest: 'error' })); - afterEach(() => server.resetHandlers()); - afterAll(() => server.close()); - + msw.setupDefaultHandlers(server); it('should load from url', async () => { const logger = getVoidLogger(); const reader = UrlReaders.default({ logger, config: new ConfigReader({}) }); diff --git a/plugins/catalog-backend/src/ingestion/processors/index.ts b/plugins/catalog-backend/src/ingestion/processors/index.ts index 59a87af555..92303f04a3 100644 --- a/plugins/catalog-backend/src/ingestion/processors/index.ts +++ b/plugins/catalog-backend/src/ingestion/processors/index.ts @@ -28,6 +28,7 @@ export { GithubOrgReaderProcessor } from './GithubOrgReaderProcessor'; export { GithubReaderProcessor } from './GithubReaderProcessor'; export { GitlabApiReaderProcessor } from './GitlabApiReaderProcessor'; export { GitlabReaderProcessor } from './GitlabReaderProcessor'; +export { OwnerRelationProcessor } from './OwnerRelationProcessor'; export { LocationRefProcessor } from './LocationEntityProcessor'; export { PlaceholderProcessor } from './PlaceholderProcessor'; export type { PlaceholderResolver } from './PlaceholderProcessor'; diff --git a/plugins/catalog-backend/src/ingestion/processors/results.ts b/plugins/catalog-backend/src/ingestion/processors/results.ts index 2718d871dd..158bc3d950 100644 --- a/plugins/catalog-backend/src/ingestion/processors/results.ts +++ b/plugins/catalog-backend/src/ingestion/processors/results.ts @@ -15,7 +15,11 @@ */ import { InputError, NotFoundError } from '@backstage/backend-common'; -import { Entity, LocationSpec } from '@backstage/catalog-model'; +import { + Entity, + EntityRelationSpec, + LocationSpec, +} from '@backstage/catalog-model'; import { CatalogProcessorResult } from './types'; export function notFoundError( @@ -67,3 +71,7 @@ export function entity( ): CatalogProcessorResult { return { type: 'entity', location: atLocation, entity: newEntity }; } + +export function relation(spec: EntityRelationSpec): CatalogProcessorResult { + return { type: 'relation', relation: spec }; +} diff --git a/plugins/catalog-backend/src/ingestion/processors/types.ts b/plugins/catalog-backend/src/ingestion/processors/types.ts index c9adf07eff..7821a61437 100644 --- a/plugins/catalog-backend/src/ingestion/processors/types.ts +++ b/plugins/catalog-backend/src/ingestion/processors/types.ts @@ -14,7 +14,11 @@ * limitations under the License. */ -import { Entity, LocationSpec } from '@backstage/catalog-model'; +import { + Entity, + EntityRelationSpec, + LocationSpec, +} from '@backstage/catalog-model'; export type CatalogProcessor = { /** @@ -113,6 +117,12 @@ export type CatalogProcessorEntityResult = { location: LocationSpec; }; +export type CatalogProcessorRelationResult = { + type: 'relation'; + relation: EntityRelationSpec; + entityRef?: string; +}; + export type CatalogProcessorErrorResult = { type: 'error'; error: Error; @@ -123,4 +133,5 @@ export type CatalogProcessorResult = | CatalogProcessorLocationResult | CatalogProcessorDataResult | CatalogProcessorEntityResult + | CatalogProcessorRelationResult | CatalogProcessorErrorResult; diff --git a/plugins/catalog-backend/src/ingestion/processors/util/github.test.ts b/plugins/catalog-backend/src/ingestion/processors/util/github.test.ts index 25a37e0546..54afdd1bdc 100644 --- a/plugins/catalog-backend/src/ingestion/processors/util/github.test.ts +++ b/plugins/catalog-backend/src/ingestion/processors/util/github.test.ts @@ -17,6 +17,7 @@ import { graphql } from '@octokit/graphql'; import { graphql as graphqlMsw } from 'msw'; import { setupServer } from 'msw/node'; +import { msw } from '@backstage/test-utils'; import { getOrganizationTeams, getOrganizationUsers, @@ -26,9 +27,7 @@ import { describe('github', () => { const server = setupServer(); - beforeAll(() => server.listen({ onUnhandledRequest: 'error' })); - afterEach(() => server.resetHandlers()); - afterAll(() => server.close()); + msw.setupDefaultHandlers(server); describe('getOrganizationUsers', () => { it('reads members', async () => { diff --git a/plugins/catalog-backend/src/ingestion/types.ts b/plugins/catalog-backend/src/ingestion/types.ts index 5bbec01e7e..c586e2d438 100644 --- a/plugins/catalog-backend/src/ingestion/types.ts +++ b/plugins/catalog-backend/src/ingestion/types.ts @@ -14,7 +14,12 @@ * limitations under the License. */ -import type { Entity, Location, LocationSpec } from '@backstage/catalog-model'; +import type { + Entity, + EntityRelationSpec, + Location, + LocationSpec, +} from '@backstage/catalog-model'; // // HigherOrderOperation @@ -53,6 +58,7 @@ export type ReadLocationResult = { export type ReadLocationEntity = { location: LocationSpec; entity: Entity; + relations: EntityRelationSpec[]; }; export type ReadLocationError = { diff --git a/plugins/catalog-backend/src/service/CatalogBuilder.test.ts b/plugins/catalog-backend/src/service/CatalogBuilder.test.ts index 3f7c613c00..bc71934488 100644 --- a/plugins/catalog-backend/src/service/CatalogBuilder.test.ts +++ b/plugins/catalog-backend/src/service/CatalogBuilder.test.ts @@ -128,12 +128,16 @@ describe('CatalogBuilder', () => { { apiVersion: 'av', kind: 'Component', - metadata: expect.objectContaining({ + metadata: { name: 'n', namespace: 'ns', post: 'p', replaced: 'tt2', - }), + uid: expect.any(String), + etag: expect.any(String), + generation: expect.any(Number), + }, + relations: [], }, ]); }); diff --git a/plugins/catalog-backend/src/service/CatalogBuilder.ts b/plugins/catalog-backend/src/service/CatalogBuilder.ts index e6fb540f16..4ec2a0834d 100644 --- a/plugins/catalog-backend/src/service/CatalogBuilder.ts +++ b/plugins/catalog-backend/src/service/CatalogBuilder.ts @@ -52,6 +52,7 @@ import { GithubReaderProcessor, GitlabApiReaderProcessor, GitlabReaderProcessor, + OwnerRelationProcessor, HigherOrderOperation, HigherOrderOperations, LocationReaders, @@ -334,6 +335,7 @@ export class CatalogBuilder { new YamlProcessor(), new CodeOwnersProcessor({ reader }), new LocationRefProcessor(), + new OwnerRelationProcessor(), new AnnotateLocationEntityProcessor(), ]; diff --git a/plugins/catalog-backend/src/service/router.test.ts b/plugins/catalog-backend/src/service/router.test.ts index a2b17ba360..cf843acf3c 100644 --- a/plugins/catalog-backend/src/service/router.test.ts +++ b/plugins/catalog-backend/src/service/router.test.ts @@ -32,10 +32,7 @@ describe('createRouter', () => { beforeAll(async () => { entitiesCatalog = { entities: jest.fn(), - addOrUpdateEntity: jest.fn(), - addEntities: jest.fn(), removeEntityByUid: jest.fn(), - setRelations: jest.fn(), batchAddOrUpdateEntities: jest.fn(), }; locationsCatalog = { @@ -173,7 +170,7 @@ describe('createRouter', () => { .set('Content-Type', 'application/json') .send(); - expect(entitiesCatalog.addOrUpdateEntity).not.toHaveBeenCalled(); + expect(entitiesCatalog.batchAddOrUpdateEntities).not.toHaveBeenCalled(); expect(response.status).toEqual(400); expect(response.text).toMatch(/body/); }); @@ -188,18 +185,24 @@ describe('createRouter', () => { }, }; - entitiesCatalog.addOrUpdateEntity.mockResolvedValue(entity); + entitiesCatalog.batchAddOrUpdateEntities.mockResolvedValue([ + { entityId: 'u' }, + ]); + entitiesCatalog.entities.mockResolvedValue([entity]); const response = await request(app) .post('/entities') .send(entity) .set('Content-Type', 'application/json'); - expect(entitiesCatalog.addOrUpdateEntity).toHaveBeenCalledTimes(1); - expect(entitiesCatalog.addOrUpdateEntity).toHaveBeenNthCalledWith( - 1, - entity, - ); + expect(entitiesCatalog.batchAddOrUpdateEntities).toHaveBeenCalledTimes(1); + expect(entitiesCatalog.batchAddOrUpdateEntities).toHaveBeenCalledWith([ + { entity, relations: [] }, + ]); + expect(entitiesCatalog.entities).toHaveBeenCalledTimes(1); + expect(entitiesCatalog.entities).toHaveBeenCalledWith({ + 'metadata.uid': 'u', + }); expect(response.status).toEqual(200); expect(response.body).toEqual(entity); }); diff --git a/plugins/catalog-backend/src/service/router.ts b/plugins/catalog-backend/src/service/router.ts index a262e9b5f4..c7d05afb2d 100644 --- a/plugins/catalog-backend/src/service/router.ts +++ b/plugins/catalog-backend/src/service/router.ts @@ -49,8 +49,13 @@ export async function createRouter( }) .post('/entities', async (req, res) => { const body = await requireRequestBody(req); - const result = await entitiesCatalog.addOrUpdateEntity(body as Entity); - res.status(200).send(result); + const [result] = await entitiesCatalog.batchAddOrUpdateEntities([ + { entity: body as Entity, relations: [] }, + ]); + const [entity] = await entitiesCatalog.entities({ + 'metadata.uid': result.entityId, + }); + res.status(200).send(entity); }) .get('/entities/by-uid/:uid', async (req, res) => { const { uid } = req.params; diff --git a/plugins/catalog-backend/src/service/standaloneServer.ts b/plugins/catalog-backend/src/service/standaloneServer.ts index 23f9795036..73a33c8c8e 100644 --- a/plugins/catalog-backend/src/service/standaloneServer.ts +++ b/plugins/catalog-backend/src/service/standaloneServer.ts @@ -36,7 +36,7 @@ export async function startStandaloneServer( options: ServerOptions, ): Promise { const logger = options.logger.child({ service: 'catalog-backend' }); - const config = await loadBackendConfig({ logger }); + const config = await loadBackendConfig({ logger, argv: process.argv }); const reader = UrlReaders.default({ logger, config }); const db = useHotMemoize(module, () => DatabaseManager.createInMemoryDatabaseConnection(), diff --git a/plugins/catalog-graphql/package.json b/plugins/catalog-graphql/package.json index 73832bb8ef..3c8c45d246 100644 --- a/plugins/catalog-graphql/package.json +++ b/plugins/catalog-graphql/package.json @@ -28,19 +28,20 @@ "graphql": "^15.3.0", "graphql-tag": "^2.11.0", "graphql-type-json": "^0.3.2", - "node-fetch": "^2.6.0", + "cross-fetch": "^3.0.6", "winston": "^3.2.1" }, "devDependencies": { "@backstage/cli": "^0.1.1-alpha.25", + "@backstage/test-utils": "^0.1.1-alpha.25", "@graphql-codegen/cli": "^1.17.7", "@graphql-codegen/typescript": "^1.17.7", "@graphql-codegen/typescript-resolvers": "^1.17.7", "@types/express": "^4.17.7", "@types/supertest": "^2.0.8", "eslint-plugin-graphql": "^4.0.0", - "msw": "^0.20.5", "supertest": "^4.0.2", + "msw": "^0.21.2", "ts-node": "^8.10.2" }, "files": [ diff --git a/plugins/catalog-graphql/src/graphql/module.test.ts b/plugins/catalog-graphql/src/graphql/module.test.ts index e3e03a9b4a..b359c4396b 100644 --- a/plugins/catalog-graphql/src/graphql/module.test.ts +++ b/plugins/catalog-graphql/src/graphql/module.test.ts @@ -21,6 +21,7 @@ import { setupServer } from 'msw/node'; import { ConfigReader } from '@backstage/config'; import { ReaderEntity } from '../service/client'; import { createLogger } from 'winston'; +import { msw } from '@backstage/test-utils'; import { gql } from 'apollo-server'; describe('Catalog Module', () => { @@ -37,9 +38,7 @@ describe('Catalog Module', () => { }, ]); - beforeAll(() => worker.listen({ onUnhandledRequest: 'error' })); - afterAll(() => worker.close()); - afterEach(() => worker.resetHandlers()); + msw.setupDefaultHandlers(worker); describe('Default Entity', () => { beforeEach(() => { diff --git a/plugins/catalog-graphql/src/service/client.test.ts b/plugins/catalog-graphql/src/service/client.test.ts index d8cc59d1a8..c9708af2d0 100644 --- a/plugins/catalog-graphql/src/service/client.test.ts +++ b/plugins/catalog-graphql/src/service/client.test.ts @@ -16,13 +16,11 @@ import { CatalogClient } from './client'; import { rest } from 'msw'; import { setupServer } from 'msw/node'; +import { msw } from '@backstage/test-utils'; describe('Catalog GraphQL Module', () => { const worker = setupServer(); - - beforeAll(() => worker.listen({ onUnhandledRequest: 'error' })); - afterAll(() => worker.close()); - afterEach(() => worker.resetHandlers()); + msw.setupDefaultHandlers(worker); const baseUrl = 'http://localhost:1234'; diff --git a/plugins/catalog-graphql/src/service/client.ts b/plugins/catalog-graphql/src/service/client.ts index 8af97a89b1..59f9e90254 100644 --- a/plugins/catalog-graphql/src/service/client.ts +++ b/plugins/catalog-graphql/src/service/client.ts @@ -15,7 +15,7 @@ */ import { Entity, EntityMeta } from '@backstage/catalog-model'; -import fetch from 'node-fetch'; +import fetch from 'cross-fetch'; import { JsonObject } from '@backstage/config'; export interface ReaderEntityMeta extends EntityMeta { diff --git a/plugins/catalog/package.json b/plugins/catalog/package.json index b29a93e69d..3d0c932cdd 100644 --- a/plugins/catalog/package.json +++ b/plugins/catalog/package.json @@ -50,11 +50,9 @@ "@testing-library/user-event": "^12.0.7", "@types/jest": "^26.0.7", "@types/node": "^12.0.0", - "jest-fetch-mock": "^3.0.3", - "msw": "^0.20.5", - "node-fetch": "^2.6.1", + "msw": "^0.21.2", "react-test-renderer": "^16.13.1", - "whatwg-fetch": "^3.4.0" + "cross-fetch": "^3.0.6" }, "files": [ "dist" diff --git a/plugins/catalog/src/api/CatalogClient.test.ts b/plugins/catalog/src/api/CatalogClient.test.ts index 832a114276..7c0123317d 100644 --- a/plugins/catalog/src/api/CatalogClient.test.ts +++ b/plugins/catalog/src/api/CatalogClient.test.ts @@ -19,18 +19,17 @@ import { setupServer } from 'msw/node'; import { CatalogClient } from './CatalogClient'; import { Entity } from '@backstage/catalog-model'; import { UrlPatternDiscovery } from '@backstage/core'; +import { msw } from '@backstage/test-utils'; const server = setupServer(); const mockBaseUrl = 'http://backstage:9191/i-am-a-mock-base'; const discoveryApi = UrlPatternDiscovery.compile(mockBaseUrl); describe('CatalogClient', () => { - beforeAll(() => server.listen({ onUnhandledRequest: 'error' })); - afterEach(() => server.resetHandlers()); - afterAll(() => server.close()); - let client = new CatalogClient({ discoveryApi }); + msw.setupDefaultHandlers(server); + beforeEach(() => { client = new CatalogClient({ discoveryApi }); }); diff --git a/plugins/catalog/src/api/CatalogClient.ts b/plugins/catalog/src/api/CatalogClient.ts index d5ff033caa..6b10541107 100644 --- a/plugins/catalog/src/api/CatalogClient.ts +++ b/plugins/catalog/src/api/CatalogClient.ts @@ -109,9 +109,15 @@ export class CatalogClient implements CatalogApi { const { location, entities } = await response.json(); - if (!location || entities.length === 0) + if (!location) { throw new Error(`Location wasn't added: ${target}`); + } + if (entities.length === 0) { + throw new Error( + `Location was added but has no entities specified yet: ${target}`, + ); + } return { location, entities, diff --git a/plugins/catalog/src/components/AboutCard/AboutCard.tsx b/plugins/catalog/src/components/AboutCard/AboutCard.tsx index 266d5fa843..fbbf5fa11c 100644 --- a/plugins/catalog/src/components/AboutCard/AboutCard.tsx +++ b/plugins/catalog/src/components/AboutCard/AboutCard.tsx @@ -14,7 +14,12 @@ * limitations under the License. */ -import { Entity, ENTITY_DEFAULT_NAMESPACE } from '@backstage/catalog-model'; +import { + Entity, + ENTITY_DEFAULT_NAMESPACE, + RELATION_OWNED_BY, + serializeEntityRef, +} from '@backstage/catalog-model'; import { Card, CardContent, @@ -26,6 +31,7 @@ import { makeStyles, Typography, } from '@material-ui/core'; +import ExtensionIcon from '@material-ui/icons/Extension'; import DocsIcon from '@material-ui/icons/Description'; import EditIcon from '@material-ui/icons/Edit'; import GitHubIcon from '@material-ui/icons/GitHub'; @@ -120,6 +126,12 @@ export function AboutCard({ entity, variant }: AboutCardProps) { entity.metadata.namespace || ENTITY_DEFAULT_NAMESPACE }/${entity.kind}/${entity.metadata.name}`} /> + } + href="api" + /> } /> @@ -139,7 +151,20 @@ export function AboutCard({ entity, variant }: AboutCardProps) { r.type === RELATION_OWNED_BY) + .map(({ target: { kind, name, namespace } }) => + // TODO(Rugvip): we want to provide some utils for this + serializeEntityRef({ + kind, + name, + namespace: + namespace === ENTITY_DEFAULT_NAMESPACE + ? undefined + : namespace, + }), + ) + .join(', ')} gridSizes={{ xs: 12, sm: 6, lg: 4 }} /> + {icon} + {props.label} + + ); + } + return ( - + {icon} {props.label} diff --git a/plugins/catalog/src/components/Router.tsx b/plugins/catalog/src/components/Router.tsx index c6e706c15b..0a002ba907 100644 --- a/plugins/catalog/src/components/Router.tsx +++ b/plugins/catalog/src/components/Router.tsx @@ -31,7 +31,7 @@ const DefaultEntityPage = () => ( title="Overview" element={ - This is default entity page. + This is the default entity page. To override this component with your custom implementation, read docs on{' '} @@ -62,9 +62,9 @@ export const Router = ({ EntityPage?: ComponentType; }) => ( - } /> + } /> diff --git a/plugins/catalog/src/setupTests.ts b/plugins/catalog/src/setupTests.ts index 4016f3f38e..aea2220869 100644 --- a/plugins/catalog/src/setupTests.ts +++ b/plugins/catalog/src/setupTests.ts @@ -15,4 +15,4 @@ */ import '@testing-library/jest-dom'; -import 'whatwg-fetch'; +import 'cross-fetch/polyfill'; diff --git a/plugins/circleci/package.json b/plugins/circleci/package.json index 64684b0f99..464bb41441 100644 --- a/plugins/circleci/package.json +++ b/plugins/circleci/package.json @@ -46,9 +46,9 @@ "@types/jest": "^26.0.7", "@types/node": "^12.0.0", "@types/react-lazylog": "^4.5.0", - "jest-fetch-mock": "^3.0.3", - "msw": "^0.20.5", - "node-fetch": "^2.6.1" + "msw": "^0.21.2", + "cross-fetch": "^3.0.6", + "@backstage/test-utils": "^0.1.1-alpha.25" }, "files": [ "dist" diff --git a/plugins/circleci/src/setupTests.ts b/plugins/circleci/src/setupTests.ts index a838246198..8925258421 100644 --- a/plugins/circleci/src/setupTests.ts +++ b/plugins/circleci/src/setupTests.ts @@ -15,5 +15,3 @@ */ import '@testing-library/jest-dom/extend-expect'; - -require('jest-fetch-mock').enableMocks(); diff --git a/plugins/cloudbuild/package.json b/plugins/cloudbuild/package.json index d36b661664..f8bf338dd1 100644 --- a/plugins/cloudbuild/package.json +++ b/plugins/cloudbuild/package.json @@ -46,9 +46,9 @@ "@testing-library/user-event": "^12.0.7", "@types/jest": "^26.0.7", "@types/node": "^12.0.0", - "jest-fetch-mock": "^3.0.3", - "msw": "^0.20.5", - "node-fetch": "^2.6.1" + "msw": "^0.21.2", + "cross-fetch": "^3.0.6", + "@backstage/test-utils": "^0.1.1-alpha.25" }, "files": [ "dist" diff --git a/plugins/cloudbuild/src/setupTests.ts b/plugins/cloudbuild/src/setupTests.ts index 4b4cdbdaaf..0bfa67b49a 100644 --- a/plugins/cloudbuild/src/setupTests.ts +++ b/plugins/cloudbuild/src/setupTests.ts @@ -14,5 +14,3 @@ * limitations under the License. */ import '@testing-library/jest-dom'; - -require('jest-fetch-mock').enableMocks(); diff --git a/plugins/cost-insights/package.json b/plugins/cost-insights/package.json index f94c467ab3..e79c4ab962 100644 --- a/plugins/cost-insights/package.json +++ b/plugins/cost-insights/package.json @@ -52,9 +52,10 @@ "@types/jest": "^26.0.7", "@types/node": "^12.0.0", "@types/yup": "^0.29.8", - "jest-fetch-mock": "^3.0.3", - "msw": "^0.20.5", - "node-fetch": "^2.6.1" + "@types/recharts": "^1.8.14", + "msw": "^0.21.2", + "cross-fetch": "^3.0.6", + "@backstage/test-utils": "^0.1.1-alpha.25" }, "files": [ "dist" diff --git a/plugins/cost-insights/src/setupTests.ts b/plugins/cost-insights/src/setupTests.ts index 8553642152..825bcd4115 100644 --- a/plugins/cost-insights/src/setupTests.ts +++ b/plugins/cost-insights/src/setupTests.ts @@ -15,5 +15,3 @@ */ import '@testing-library/jest-dom'; - -require('jest-fetch-mock').enableMocks(); diff --git a/plugins/explore/package.json b/plugins/explore/package.json index feeaf31985..c3e08dc9fe 100644 --- a/plugins/explore/package.json +++ b/plugins/explore/package.json @@ -41,9 +41,8 @@ "@testing-library/user-event": "^12.0.7", "@types/jest": "^26.0.7", "@types/node": "^12.0.0", - "jest-fetch-mock": "^3.0.3", - "msw": "^0.20.5", - "node-fetch": "^2.6.1" + "msw": "^0.21.2", + "cross-fetch": "^3.0.6" }, "files": [ "dist" diff --git a/plugins/explore/src/setupTests.ts b/plugins/explore/src/setupTests.ts index 8553642152..825bcd4115 100644 --- a/plugins/explore/src/setupTests.ts +++ b/plugins/explore/src/setupTests.ts @@ -15,5 +15,3 @@ */ import '@testing-library/jest-dom'; - -require('jest-fetch-mock').enableMocks(); diff --git a/plugins/gcp-projects/package.json b/plugins/gcp-projects/package.json index 8de69290e0..783504a2cf 100644 --- a/plugins/gcp-projects/package.json +++ b/plugins/gcp-projects/package.json @@ -38,9 +38,9 @@ "@testing-library/user-event": "^12.0.7", "@types/jest": "^26.0.7", "@types/node": "^12.0.0", - "jest-fetch-mock": "^3.0.3", - "msw": "^0.20.5", - "node-fetch": "^2.6.1" + "msw": "^0.21.2", + "cross-fetch": "^3.0.6", + "@backstage/test-utils": "^0.1.1-alpha.25" }, "files": [ "dist" diff --git a/plugins/gcp-projects/src/setupTests.ts b/plugins/gcp-projects/src/setupTests.ts index 8553642152..825bcd4115 100644 --- a/plugins/gcp-projects/src/setupTests.ts +++ b/plugins/gcp-projects/src/setupTests.ts @@ -15,5 +15,3 @@ */ import '@testing-library/jest-dom'; - -require('jest-fetch-mock').enableMocks(); diff --git a/plugins/github-actions/package.json b/plugins/github-actions/package.json index 0a5a8c4b41..63390fdcc0 100644 --- a/plugins/github-actions/package.json +++ b/plugins/github-actions/package.json @@ -47,9 +47,9 @@ "@testing-library/user-event": "^12.0.7", "@types/jest": "^26.0.7", "@types/node": "^12.0.0", - "jest-fetch-mock": "^3.0.3", - "msw": "^0.20.5", - "node-fetch": "^2.6.1" + "msw": "^0.21.2", + "cross-fetch": "^3.0.6", + "@backstage/test-utils": "^0.1.1-alpha.25" }, "files": [ "dist" diff --git a/plugins/github-actions/src/setupTests.ts b/plugins/github-actions/src/setupTests.ts index 8553642152..825bcd4115 100644 --- a/plugins/github-actions/src/setupTests.ts +++ b/plugins/github-actions/src/setupTests.ts @@ -15,5 +15,3 @@ */ import '@testing-library/jest-dom'; - -require('jest-fetch-mock').enableMocks(); diff --git a/plugins/gitops-profiles/package.json b/plugins/gitops-profiles/package.json index 5cfeb24077..2daa8b103e 100644 --- a/plugins/gitops-profiles/package.json +++ b/plugins/gitops-profiles/package.json @@ -39,9 +39,9 @@ "@testing-library/user-event": "^12.0.7", "@types/jest": "^26.0.7", "@types/node": "^12.0.0", - "jest-fetch-mock": "^3.0.3", - "msw": "^0.20.5", - "node-fetch": "^2.6.1" + "msw": "^0.21.2", + "cross-fetch": "^3.0.6", + "@backstage/test-utils": "^0.1.1-alpha.25" }, "files": [ "dist" diff --git a/plugins/gitops-profiles/src/components/ProfileCatalog/ProfileCatalog.test.tsx b/plugins/gitops-profiles/src/components/ProfileCatalog/ProfileCatalog.test.tsx index 06074ff84d..f01ac70e64 100644 --- a/plugins/gitops-profiles/src/components/ProfileCatalog/ProfileCatalog.test.tsx +++ b/plugins/gitops-profiles/src/components/ProfileCatalog/ProfileCatalog.test.tsx @@ -16,7 +16,6 @@ import React from 'react'; import { render } from '@testing-library/react'; -import mockFetch from 'jest-fetch-mock'; import ProfileCatalog from './ProfileCatalog'; import { ThemeProvider } from '@material-ui/core'; import { lightTheme } from '@backstage/theme'; @@ -45,7 +44,6 @@ describe('ProfileCatalog', () => { }), ], ]); - mockFetch.mockResponse(() => new Promise(() => {})); const rendered = render( diff --git a/plugins/gitops-profiles/src/setupTests.ts b/plugins/gitops-profiles/src/setupTests.ts index 8553642152..825bcd4115 100644 --- a/plugins/gitops-profiles/src/setupTests.ts +++ b/plugins/gitops-profiles/src/setupTests.ts @@ -15,5 +15,3 @@ */ import '@testing-library/jest-dom'; - -require('jest-fetch-mock').enableMocks(); diff --git a/plugins/graphiql/package.json b/plugins/graphiql/package.json index 749be5c96f..bda33bb43e 100644 --- a/plugins/graphiql/package.json +++ b/plugins/graphiql/package.json @@ -52,10 +52,9 @@ "@types/codemirror": "^0.0.97", "@types/jest": "^26.0.7", "@types/node": "^12.0.0", - "jest-fetch-mock": "^3.0.3", - "msw": "^0.20.5", - "node-fetch": "^2.6.1", - "react-router-dom": "6.0.0-beta.0" + "msw": "^0.21.2", + "react-router-dom": "6.0.0-beta.0", + "cross-fetch": "^3.0.6" }, "files": [ "dist" diff --git a/plugins/graphiql/src/setupTests.ts b/plugins/graphiql/src/setupTests.ts index 8553642152..825bcd4115 100644 --- a/plugins/graphiql/src/setupTests.ts +++ b/plugins/graphiql/src/setupTests.ts @@ -15,5 +15,3 @@ */ import '@testing-library/jest-dom'; - -require('jest-fetch-mock').enableMocks(); diff --git a/plugins/graphql/package.json b/plugins/graphql/package.json index 645334f791..63fb2c88db 100644 --- a/plugins/graphql/package.json +++ b/plugins/graphql/package.json @@ -30,7 +30,6 @@ "express-promise-router": "^3.0.3", "graphql": "^15.3.0", "helmet": "^4.0.0", - "node-fetch": "^2.6.0", "reflect-metadata": "^0.1.13", "winston": "^3.2.1", "yn": "^4.0.0" diff --git a/plugins/jenkins/package.json b/plugins/jenkins/package.json index d14b9ff297..b3f19b2b16 100644 --- a/plugins/jenkins/package.json +++ b/plugins/jenkins/package.json @@ -44,9 +44,9 @@ "@types/jest": "^26.0.7", "@types/node": "^12.0.0", "@types/testing-library__jest-dom": "^5.9.1", - "jest-fetch-mock": "^3.0.3", - "msw": "^0.20.5", - "node-fetch": "^2.6.1" + "msw": "^0.21.2", + "cross-fetch": "^3.0.6", + "@backstage/test-utils": "^0.1.1-alpha.25" }, "files": [ "dist" diff --git a/plugins/jenkins/src/setupTests.ts b/plugins/jenkins/src/setupTests.ts index 8553642152..825bcd4115 100644 --- a/plugins/jenkins/src/setupTests.ts +++ b/plugins/jenkins/src/setupTests.ts @@ -15,5 +15,3 @@ */ import '@testing-library/jest-dom'; - -require('jest-fetch-mock').enableMocks(); diff --git a/plugins/kubernetes-backend/examples/dice-roller/README.md b/plugins/kubernetes-backend/examples/dice-roller/README.md index 774867de04..bf3e7d261a 100644 --- a/plugins/kubernetes-backend/examples/dice-roller/README.md +++ b/plugins/kubernetes-backend/examples/dice-roller/README.md @@ -23,7 +23,7 @@ This can be used to run the kubernetes plugin locally against a mock service. 6. Register existing component in Backstage - https://github.com/mclarke47/dice-roller/blob/master/catalog-info.yaml -Update `app-config.development.yaml` as follows. +Add or update `app-config.local.yaml` with the following: ```yaml kubernetes: @@ -45,4 +45,4 @@ Mac copy to clipboard: kubectl get secret $(kubectl get sa dice-roller -o=json | jq -r .secrets[0].name) -o=json | jq -r '.data["token"]' | base64 --decode | pbcopy ``` -Paste into `app-config.development.yaml` `kubernetes.clusters[0].serviceAccountToken` +Paste into `app-config.local.yaml` `kubernetes.clusters[0].serviceAccountToken` diff --git a/plugins/kubernetes-backend/package.json b/plugins/kubernetes-backend/package.json index 1be8b7509b..fc068621ee 100644 --- a/plugins/kubernetes-backend/package.json +++ b/plugins/kubernetes-backend/package.json @@ -38,7 +38,6 @@ }, "devDependencies": { "@backstage/cli": "^0.1.1-alpha.25", - "jest-fetch-mock": "^3.0.3", "supertest": "^4.0.2" }, "files": [ diff --git a/plugins/kubernetes-backend/src/setupTests.ts b/plugins/kubernetes-backend/src/setupTests.ts index f7b6ca962d..ba33cf996b 100644 --- a/plugins/kubernetes-backend/src/setupTests.ts +++ b/plugins/kubernetes-backend/src/setupTests.ts @@ -14,6 +14,4 @@ * limitations under the License. */ -require('jest-fetch-mock').enableMocks(); - export {}; diff --git a/plugins/kubernetes/package.json b/plugins/kubernetes/package.json index 9a0e41689d..1ab4e4a2fb 100644 --- a/plugins/kubernetes/package.json +++ b/plugins/kubernetes/package.json @@ -43,9 +43,8 @@ "@testing-library/user-event": "^12.0.7", "@types/jest": "^26.0.7", "@types/node": "^12.0.0", - "jest-fetch-mock": "^3.0.3", - "msw": "^0.20.5", - "node-fetch": "^2.6.1" + "msw": "^0.21.2", + "cross-fetch": "^3.0.6" }, "files": [ "dist" diff --git a/plugins/kubernetes/src/setupTests.ts b/plugins/kubernetes/src/setupTests.ts index 4b4cdbdaaf..0bfa67b49a 100644 --- a/plugins/kubernetes/src/setupTests.ts +++ b/plugins/kubernetes/src/setupTests.ts @@ -14,5 +14,3 @@ * limitations under the License. */ import '@testing-library/jest-dom'; - -require('jest-fetch-mock').enableMocks(); diff --git a/plugins/lighthouse/package.json b/plugins/lighthouse/package.json index 1cc177cba7..eb2fdde5bc 100644 --- a/plugins/lighthouse/package.json +++ b/plugins/lighthouse/package.json @@ -47,9 +47,8 @@ "@testing-library/user-event": "^12.0.7", "@types/jest": "^26.0.7", "@types/node": "^12.0.0", - "jest-fetch-mock": "^3.0.3", - "msw": "^0.20.5", - "node-fetch": "^2.6.1" + "msw": "^0.21.2", + "cross-fetch": "^3.0.6" }, "files": [ "dist" diff --git a/plugins/lighthouse/src/components/AuditList/AuditListForEntity.test.tsx b/plugins/lighthouse/src/components/AuditList/AuditListForEntity.test.tsx index 7f10914d9c..15a4ba3255 100644 --- a/plugins/lighthouse/src/components/AuditList/AuditListForEntity.test.tsx +++ b/plugins/lighthouse/src/components/AuditList/AuditListForEntity.test.tsx @@ -22,7 +22,6 @@ import { LighthouseRestApi, WebsiteListResponse, } from '../../api'; -import mockFetch from 'jest-fetch-mock'; import * as data from '../../__fixtures__/website-list-response.json'; import { EntityContext } from '@backstage/plugin-catalog'; @@ -53,7 +52,7 @@ describe('', () => { [lighthouseApiRef, new LighthouseRestApi('http://lighthouse')], [errorApiRef, mockErrorApi], ]); - mockFetch.mockResponse(JSON.stringify(entityWebsite)); + (useWebsiteForEntity as jest.Mock).mockReturnValue({ value: entityWebsite, loading: false, diff --git a/plugins/lighthouse/src/components/AuditList/AuditListTable.test.tsx b/plugins/lighthouse/src/components/AuditList/AuditListTable.test.tsx index e67f939ba1..5c67fec10a 100644 --- a/plugins/lighthouse/src/components/AuditList/AuditListTable.test.tsx +++ b/plugins/lighthouse/src/components/AuditList/AuditListTable.test.tsx @@ -16,7 +16,7 @@ import React from 'react'; import { render } from '@testing-library/react'; -import { wrapInTestApp } from '@backstage/test-utils'; +import { wrapInTestApp, msw } from '@backstage/test-utils'; import { ApiRegistry, ApiProvider } from '@backstage/core'; import AuditListTable from './AuditListTable'; @@ -26,7 +26,7 @@ import { LighthouseRestApi, } from '../../api'; import { formatTime } from '../../utils'; -import mockFetch from 'jest-fetch-mock'; +import { setupServer } from 'msw/node'; import * as data from '../../__fixtures__/website-list-response.json'; @@ -35,11 +35,13 @@ const websiteListResponse = data as WebsiteListResponse; describe('AuditListTable', () => { let apis: ApiRegistry; + const server = setupServer(); + msw.setupDefaultHandlers(server); + beforeEach(() => { apis = ApiRegistry.from([ [lighthouseApiRef, new LighthouseRestApi('http://lighthouse')], ]); - mockFetch.mockResponse(JSON.stringify(websiteListResponse)); }); const auditList = (websiteList: WebsiteListResponse) => { diff --git a/plugins/lighthouse/src/components/AuditList/index.test.tsx b/plugins/lighthouse/src/components/AuditList/index.test.tsx index 34fd760801..5fdc4781ce 100644 --- a/plugins/lighthouse/src/components/AuditList/index.test.tsx +++ b/plugins/lighthouse/src/components/AuditList/index.test.tsx @@ -24,10 +24,9 @@ jest.mock('react-router-dom', () => { }); import React from 'react'; -import mockFetch from 'jest-fetch-mock'; import { render, fireEvent } from '@testing-library/react'; import { ApiRegistry, ApiProvider } from '@backstage/core'; -import { wrapInTestApp } from '@backstage/test-utils'; +import { wrapInTestApp, msw } from '@backstage/test-utils'; import { lighthouseApiRef, @@ -40,18 +39,23 @@ import * as data from '../../__fixtures__/website-list-response.json'; const { useNavigate } = jest.requireMock('react-router-dom'); const websiteListResponse = data as WebsiteListResponse; +import { rest } from 'msw'; +import { setupServer } from 'msw/node'; describe('AuditList', () => { let apis: ApiRegistry; + const server = setupServer(); + msw.setupDefaultHandlers(server); + beforeEach(() => { apis = ApiRegistry.from([ [lighthouseApiRef, new LighthouseRestApi('http://lighthouse')], ]); - mockFetch.mockResponse(JSON.stringify(websiteListResponse)); }); it('should render the table', async () => { + server.use(rest.get('*', (_req, res, ctx) => res(ctx.json(data)))); const rendered = render( wrapInTestApp( @@ -76,22 +80,6 @@ describe('AuditList', () => { }); describe('pagination', () => { - it('requests the correct limit and offset from the api based on the query', () => { - mockFetch.mockClear(); - render( - wrapInTestApp( - - - , - { routeEntries: ['?page=2'] }, - ), - ); - expect(mockFetch).toHaveBeenLastCalledWith( - 'http://lighthouse/v1/websites?limit=10&offset=10', - undefined, - ); - }); - describe('when only one page is needed', () => { it('hides pagination elements', () => { const rendered = render( @@ -111,7 +99,8 @@ describe('AuditList', () => { response.limit = 5; response.offset = 5; response.total = 7; - mockFetch.mockResponseOnce(JSON.stringify(response)); + server.use(rest.get('*', (_req, res, ctx) => res(ctx.json(response)))); + server.use(rest.post('*', (_req, res, ctx) => res(ctx.json(response)))); }); it('shows pagination elements', async () => { @@ -146,7 +135,7 @@ describe('AuditList', () => { describe('when waiting on the request', () => { it('should render the loader', async () => { - mockFetch.mockResponseOnce(() => new Promise(() => {})); + server.use(rest.get('*', (_req, res, ctx) => res(ctx.delay(20000)))); const rendered = render( wrapInTestApp( @@ -161,7 +150,11 @@ describe('AuditList', () => { describe('when the audits fail', () => { it('should render an error', async () => { - mockFetch.mockRejectOnce(new Error('failed to fetch')); + server.use( + rest.get('*', (_req, res, ctx) => + res(ctx.status(500, 'something broke')), + ), + ); const rendered = render( wrapInTestApp( diff --git a/plugins/lighthouse/src/components/AuditView/index.test.tsx b/plugins/lighthouse/src/components/AuditView/index.test.tsx index ca292a133d..df223150bc 100644 --- a/plugins/lighthouse/src/components/AuditView/index.test.tsx +++ b/plugins/lighthouse/src/components/AuditView/index.test.tsx @@ -27,29 +27,36 @@ jest.mock('react-router-dom', () => { }); import React from 'react'; -import mockFetch from 'jest-fetch-mock'; -import { render, fireEvent } from '@testing-library/react'; -import { wrapInTestApp } from '@backstage/test-utils'; +import { render } from '@testing-library/react'; +import { wrapInTestApp, msw } from '@backstage/test-utils'; import { ApiRegistry, ApiProvider } from '@backstage/core'; - import AuditView from '.'; import { lighthouseApiRef, LighthouseRestApi, Audit, Website } from '../../api'; import { formatTime } from '../../utils'; import * as data from '../../__fixtures__/website-response.json'; -import { act } from 'react-dom/test-utils'; + +import { setupServer } from 'msw/node'; +import { rest } from 'msw'; const { useParams }: { useParams: jest.Mock } = jest.requireMock( 'react-router-dom', ); const websiteResponse = data as Website; -const { useNavigate } = jest.requireMock('react-router-dom'); describe('AuditView', () => { let apis: ApiRegistry; let id: string; + const server = setupServer(); + msw.setupDefaultHandlers(server); + beforeEach(() => { - mockFetch.mockResponse(JSON.stringify(websiteResponse)); + server.use( + rest.get('https://lighthouse/*', async (_req, res, ctx) => + res(ctx.json(websiteResponse)), + ), + ); + apis = ApiRegistry.from([ [lighthouseApiRef, new LighthouseRestApi('https://lighthouse')], ]); @@ -74,27 +81,6 @@ describe('AuditView', () => { expect(iframe).toHaveAttribute('src', `https://lighthouse/v1/audits/${id}`); }); - it('renders a button to click to create a new audit for this website', async () => { - const rendered = render( - wrapInTestApp( - - - , - ), - ); - - const button = await rendered.findByText('Create New Audit'); - expect(button).toBeInTheDocument(); - - act(() => { - fireEvent.click(button); - }); - - expect(useNavigate()).toHaveBeenCalledWith( - `../../create-audit?url=${encodeURIComponent('https://spotify.com')}`, - ); - }); - describe('sidebar', () => { it('renders a list of all audits for the website', async () => { const rendered = render( @@ -164,7 +150,7 @@ describe('AuditView', () => { describe('when the request for the website by id is pending', () => { it('shows the loading', async () => { - mockFetch.mockImplementationOnce(() => new Promise(() => {})); + server.use(rest.get('*', (_req, res, ctx) => res(ctx.delay(20000)))); const rendered = render( wrapInTestApp( @@ -178,7 +164,11 @@ describe('AuditView', () => { describe('when the request for the website by id fails', () => { it('shows an error', async () => { - mockFetch.mockRejectOnce(new Error('failed to fetch')); + server.use( + rest.get('*', (_req, res, ctx) => + res(ctx.status(500), ctx.body('failed to fetch')), + ), + ); const rendered = render( wrapInTestApp( @@ -186,7 +176,7 @@ describe('AuditView', () => { , ), ); - expect(await rendered.findByText('failed to fetch')).toBeInTheDocument(); + expect(await rendered.findByText(/failed to fetch/)).toBeInTheDocument(); }); }); diff --git a/plugins/lighthouse/src/components/CreateAudit/index.test.tsx b/plugins/lighthouse/src/components/CreateAudit/index.test.tsx index b2348bf5ff..96829926b5 100644 --- a/plugins/lighthouse/src/components/CreateAudit/index.test.tsx +++ b/plugins/lighthouse/src/components/CreateAudit/index.test.tsx @@ -24,20 +24,22 @@ jest.mock('react-router-dom', () => { }); import React from 'react'; -import mockFetch from 'jest-fetch-mock'; -import { wait, render, fireEvent } from '@testing-library/react'; +import { waitFor, render, fireEvent } from '@testing-library/react'; import { ApiRegistry, ApiProvider, ErrorApi, errorApiRef, } from '@backstage/core'; -import { wrapInTestApp } from '@backstage/test-utils'; +import { wrapInTestApp, msw } from '@backstage/test-utils'; import { lighthouseApiRef, LighthouseRestApi, Audit } from '../../api'; import CreateAudit from '.'; import * as data from '../../__fixtures__/create-audit-response.json'; +import { setupServer } from 'msw/node'; +import { rest } from 'msw'; + const { useNavigate }: { useNavigate: jest.Mock } = jest.requireMock( 'react-router-dom', ); @@ -47,6 +49,8 @@ const createAuditResponse = data as Audit; describe('CreateAudit', () => { let apis: ApiRegistry; let errorApi: ErrorApi; + const server = setupServer(); + msw.setupDefaultHandlers(server); beforeEach(() => { errorApi = { post: jest.fn(), error$: jest.fn() }; @@ -88,7 +92,7 @@ describe('CreateAudit', () => { describe('when waiting on the request', () => { it('disables the form fields', () => { - mockFetch.mockResponseOnce(() => new Promise(() => {})); + server.use(rest.get('*', (_req, res, ctx) => res(ctx.delay(20000)))); const rendered = render( wrapInTestApp( @@ -111,7 +115,11 @@ describe('CreateAudit', () => { describe('when the audit is successfully created', () => { it('triggers a location change to the table', async () => { useNavigate.mockClear(); - mockFetch.mockResponseOnce(JSON.stringify(createAuditResponse)); + server.use( + rest.post('http://lighthouse/v1/audits', (_req, res, ctx) => + res(ctx.json(createAuditResponse)), + ), + ); const rendered = render( wrapInTestApp( @@ -126,14 +134,7 @@ describe('CreateAudit', () => { }); fireEvent.click(rendered.getByText(/Create Audit/)); - expect(mockFetch).toHaveBeenCalledWith( - 'http://lighthouse/v1/audits', - expect.objectContaining({ - method: 'POST', - }), - ); - - await wait(() => expect(rendered.getByLabelText(/URL/)).toBeEnabled()); + await waitFor(() => expect(rendered.getByLabelText(/URL/)).toBeEnabled()); expect(useNavigate()).toHaveBeenCalledWith('..'); }); @@ -141,9 +142,11 @@ describe('CreateAudit', () => { describe('when the audits fail', () => { it('should render an error', async () => { - (errorApi.post as jest.Mock).mockClear(); - mockFetch.mockRejectOnce(new Error('failed to post')); - + server.use( + rest.post('http://lighthouse/v1/audits', (_req, res, ctx) => + res(ctx.status(500, 'failed to post')), + ), + ); const rendered = render( wrapInTestApp( @@ -157,8 +160,7 @@ describe('CreateAudit', () => { }); fireEvent.click(rendered.getByText(/Create Audit/)); - await wait(() => expect(rendered.getByLabelText(/URL/)).toBeEnabled()); - await new Promise(r => setTimeout(r, 0)); + await waitFor(() => expect(rendered.getByLabelText(/URL/)).toBeEnabled()); expect(errorApi.post).toHaveBeenCalledWith(expect.any(Error)); }); diff --git a/plugins/lighthouse/src/setupTests.ts b/plugins/lighthouse/src/setupTests.ts index 8553642152..aea2220869 100644 --- a/plugins/lighthouse/src/setupTests.ts +++ b/plugins/lighthouse/src/setupTests.ts @@ -15,5 +15,4 @@ */ import '@testing-library/jest-dom'; - -require('jest-fetch-mock').enableMocks(); +import 'cross-fetch/polyfill'; diff --git a/plugins/newrelic/package.json b/plugins/newrelic/package.json index 9ca33c8ad1..3372562ca2 100644 --- a/plugins/newrelic/package.json +++ b/plugins/newrelic/package.json @@ -38,9 +38,9 @@ "@testing-library/user-event": "^12.0.7", "@types/jest": "^26.0.7", "@types/node": "^12.0.0", - "jest-fetch-mock": "^3.0.3", - "msw": "^0.20.5", - "node-fetch": "^2.6.1" + "msw": "^0.21.2", + "cross-fetch": "^3.0.6", + "@backstage/test-utils": "^0.1.1-alpha.25" }, "files": [ "dist" diff --git a/plugins/newrelic/src/setupTests.ts b/plugins/newrelic/src/setupTests.ts index 8553642152..825bcd4115 100644 --- a/plugins/newrelic/src/setupTests.ts +++ b/plugins/newrelic/src/setupTests.ts @@ -15,5 +15,3 @@ */ import '@testing-library/jest-dom'; - -require('jest-fetch-mock').enableMocks(); diff --git a/plugins/proxy-backend/package.json b/plugins/proxy-backend/package.json index c7125d8b18..0d7cf00509 100644 --- a/plugins/proxy-backend/package.json +++ b/plugins/proxy-backend/package.json @@ -26,7 +26,6 @@ "express-promise-router": "^3.0.3", "http-proxy-middleware": "^0.19.1", "morgan": "^1.10.0", - "node-fetch": "^2.6.0", "uuid": "^8.0.0", "winston": "^3.2.1", "yaml": "^1.9.2", @@ -36,11 +35,9 @@ "devDependencies": { "@backstage/cli": "^0.1.1-alpha.25", "@types/http-proxy-middleware": "^0.19.3", - "@types/node-fetch": "^2.5.7", "@types/supertest": "^2.0.8", "@types/uuid": "^8.0.0", "@types/yup": "^0.29.8", - "jest-fetch-mock": "^3.0.3", "supertest": "^4.0.2" }, "files": [ diff --git a/plugins/proxy-backend/src/service/router.test.ts b/plugins/proxy-backend/src/service/router.test.ts index 42e723e54a..77db35c917 100644 --- a/plugins/proxy-backend/src/service/router.test.ts +++ b/plugins/proxy-backend/src/service/router.test.ts @@ -41,7 +41,7 @@ const mockCreateProxyMiddleware = createProxyMiddleware as jest.MockedFunction< describe('createRouter', () => { it('works', async () => { const logger = winston.createLogger(); - const config = await loadBackendConfig({ logger }); + const config = await loadBackendConfig({ logger, argv: [] }); const discovery = SingleHostDiscovery.fromConfig(config); const router = await createRouter({ config, diff --git a/plugins/proxy-backend/src/service/standaloneServer.ts b/plugins/proxy-backend/src/service/standaloneServer.ts index 160ed1db86..c64d69e2a4 100644 --- a/plugins/proxy-backend/src/service/standaloneServer.ts +++ b/plugins/proxy-backend/src/service/standaloneServer.ts @@ -36,7 +36,7 @@ export async function startStandaloneServer( logger.debug('Creating application...'); - const config = await loadBackendConfig({ logger }); + const config = await loadBackendConfig({ logger, argv: process.argv }); const discovery = SingleHostDiscovery.fromConfig(config); const router = await createRouter({ config, diff --git a/plugins/proxy-backend/src/setupTests.ts b/plugins/proxy-backend/src/setupTests.ts index f7b6ca962d..ba33cf996b 100644 --- a/plugins/proxy-backend/src/setupTests.ts +++ b/plugins/proxy-backend/src/setupTests.ts @@ -14,6 +14,4 @@ * limitations under the License. */ -require('jest-fetch-mock').enableMocks(); - export {}; diff --git a/plugins/register-component/package.json b/plugins/register-component/package.json index 464dd60a98..9729a78d80 100644 --- a/plugins/register-component/package.json +++ b/plugins/register-component/package.json @@ -43,9 +43,9 @@ "@testing-library/user-event": "^12.0.7", "@types/jest": "^26.0.7", "@types/node": "^12.0.0", - "jest-fetch-mock": "^3.0.3", - "msw": "^0.20.5", - "node-fetch": "^2.6.1" + "msw": "^0.21.2", + "cross-fetch": "^3.0.6", + "@backstage/test-utils": "^0.1.1-alpha.25" }, "files": [ "dist" diff --git a/plugins/register-component/src/setupTests.ts b/plugins/register-component/src/setupTests.ts index 8553642152..825bcd4115 100644 --- a/plugins/register-component/src/setupTests.ts +++ b/plugins/register-component/src/setupTests.ts @@ -15,5 +15,3 @@ */ import '@testing-library/jest-dom'; - -require('jest-fetch-mock').enableMocks(); diff --git a/plugins/rollbar-backend/package.json b/plugins/rollbar-backend/package.json index 2e559a698f..e00f046901 100644 --- a/plugins/rollbar-backend/package.json +++ b/plugins/rollbar-backend/package.json @@ -39,7 +39,6 @@ "devDependencies": { "@backstage/cli": "^0.1.1-alpha.25", "@types/supertest": "^2.0.8", - "jest-fetch-mock": "^3.0.3", "supertest": "^4.0.2" }, "files": [ diff --git a/plugins/rollbar-backend/src/service/standaloneServer.ts b/plugins/rollbar-backend/src/service/standaloneServer.ts index 1be2599724..b30bf6fc6b 100644 --- a/plugins/rollbar-backend/src/service/standaloneServer.ts +++ b/plugins/rollbar-backend/src/service/standaloneServer.ts @@ -32,7 +32,7 @@ export async function startStandaloneServer( options: ServerOptions, ): Promise { const logger = options.logger.child({ service: 'rollbar-backend' }); - const config = await loadBackendConfig({ logger }); + const config = await loadBackendConfig({ logger, argv: process.argv }); logger.debug('Creating application...'); diff --git a/plugins/rollbar-backend/src/setupTests.ts b/plugins/rollbar-backend/src/setupTests.ts index f7b6ca962d..ba33cf996b 100644 --- a/plugins/rollbar-backend/src/setupTests.ts +++ b/plugins/rollbar-backend/src/setupTests.ts @@ -14,6 +14,4 @@ * limitations under the License. */ -require('jest-fetch-mock').enableMocks(); - export {}; diff --git a/plugins/rollbar/package.json b/plugins/rollbar/package.json index 2289096ce7..4b44d17c62 100644 --- a/plugins/rollbar/package.json +++ b/plugins/rollbar/package.json @@ -47,9 +47,8 @@ "@types/jest": "^26.0.7", "@types/node": "^12.0.0", "@types/react": "^16.9", - "jest-fetch-mock": "^3.0.3", - "msw": "^0.20.5", - "node-fetch": "^2.6.1" + "msw": "^0.21.2", + "cross-fetch": "^3.0.6" }, "files": [ "dist" diff --git a/plugins/rollbar/src/setupTests.ts b/plugins/rollbar/src/setupTests.ts index 8553642152..825bcd4115 100644 --- a/plugins/rollbar/src/setupTests.ts +++ b/plugins/rollbar/src/setupTests.ts @@ -15,5 +15,3 @@ */ import '@testing-library/jest-dom'; - -require('jest-fetch-mock').enableMocks(); diff --git a/plugins/scaffolder/package.json b/plugins/scaffolder/package.json index 97f5cd891c..bda3ff52aa 100644 --- a/plugins/scaffolder/package.json +++ b/plugins/scaffolder/package.json @@ -49,9 +49,8 @@ "@testing-library/user-event": "^12.0.7", "@types/jest": "^26.0.7", "@types/node": "^12.0.0", - "jest-fetch-mock": "^3.0.3", - "msw": "^0.20.5", - "node-fetch": "^2.6.1" + "msw": "^0.21.2", + "cross-fetch": "^3.0.6" }, "files": [ "dist" diff --git a/plugins/scaffolder/src/setupTests.ts b/plugins/scaffolder/src/setupTests.ts index 8553642152..825bcd4115 100644 --- a/plugins/scaffolder/src/setupTests.ts +++ b/plugins/scaffolder/src/setupTests.ts @@ -15,5 +15,3 @@ */ import '@testing-library/jest-dom'; - -require('jest-fetch-mock').enableMocks(); diff --git a/plugins/sentry-backend/package.json b/plugins/sentry-backend/package.json index 9e69e58507..4702242b2c 100644 --- a/plugins/sentry-backend/package.json +++ b/plugins/sentry-backend/package.json @@ -34,8 +34,7 @@ "yn": "^4.0.0" }, "devDependencies": { - "@backstage/cli": "^0.1.1-alpha.25", - "jest-fetch-mock": "^3.0.3" + "@backstage/cli": "^0.1.1-alpha.25" }, "files": [ "dist" diff --git a/plugins/sentry/package.json b/plugins/sentry/package.json index a926c6b4db..0060777491 100644 --- a/plugins/sentry/package.json +++ b/plugins/sentry/package.json @@ -38,14 +38,14 @@ "devDependencies": { "@backstage/cli": "^0.1.1-alpha.25", "@backstage/dev-utils": "^0.1.1-alpha.25", + "@backstage/test-utils": "^0.1.1-alpha.25", "@testing-library/jest-dom": "^5.10.1", "@testing-library/react": "^10.4.1", "@testing-library/user-event": "^12.0.7", "@types/jest": "^26.0.7", "@types/node": "^12.0.0", - "jest-fetch-mock": "^3.0.3", - "msw": "^0.20.5", - "node-fetch": "^2.6.1" + "cross-fetch": "^3.0.6", + "msw": "^0.21.2" }, "files": [ "dist" diff --git a/plugins/sentry/src/components/SentryPluginPage/SentryPluginPage.test.tsx b/plugins/sentry/src/components/SentryPluginPage/SentryPluginPage.test.tsx index d508ddf7c2..21c21ce385 100644 --- a/plugins/sentry/src/components/SentryPluginPage/SentryPluginPage.test.tsx +++ b/plugins/sentry/src/components/SentryPluginPage/SentryPluginPage.test.tsx @@ -16,10 +16,13 @@ import React from 'react'; import { render } from '@testing-library/react'; -import mockFetch from 'jest-fetch-mock'; import SentryPluginPage from './SentryPluginPage'; import { ThemeProvider } from '@material-ui/core'; import { lightTheme } from '@backstage/theme'; +import { msw } from '@backstage/test-utils'; +import { setupServer } from 'msw/node'; +import { rest } from 'msw'; + import { ApiProvider, ApiRegistry, @@ -31,8 +34,11 @@ const errorApi = { post: () => {} }; const ConfigApi = { getString: () => 'test' }; describe('SentryPluginPage', () => { + const server = setupServer(); + msw.setupDefaultHandlers(server); + it('should render header and time switched', () => { - mockFetch.mockResponse('{}'); + server.use(rest.get('/', (_req, res, ctx) => res(ctx.json({})))); const rendered = render( => { // TODO(Rugvip): Config should not be loaded here, pass it in instead - const config = await loadBackendConfig({ logger: getRootLogger() }); + const config = await loadBackendConfig({ + logger: getRootLogger(), + argv: process.argv, + }); const type = getGitRepoType(repositoryUrl); try { diff --git a/plugins/techdocs-backend/src/git-auth.ts b/plugins/techdocs-backend/src/git-auth.ts index d77596929f..b580a6f05b 100644 --- a/plugins/techdocs-backend/src/git-auth.ts +++ b/plugins/techdocs-backend/src/git-auth.ts @@ -93,7 +93,11 @@ export function getAzureHostToken( export const getTokenForGitRepo = async ( repositoryUrl: string, ): Promise => { - const config = await loadBackendConfig({ logger: getRootLogger() }); + // TODO(Rugvip): Config should not be loaded here, pass it in instead + const config = await loadBackendConfig({ + logger: getRootLogger(), + argv: process.argv, + }); const host = getGitHost(repositoryUrl); const type = getGitRepoType(repositoryUrl); diff --git a/plugins/techdocs-backend/src/helpers.ts b/plugins/techdocs-backend/src/helpers.ts index 4a21800b07..4cbf46516f 100644 --- a/plugins/techdocs-backend/src/helpers.ts +++ b/plugins/techdocs-backend/src/helpers.ts @@ -20,7 +20,7 @@ import parseGitUrl from 'git-url-parse'; import NodeGit, { Clone, Repository } from 'nodegit'; import fs from 'fs-extra'; import { getDefaultBranch } from './default-branch'; -import { getTokenForGitRepo } from './git-auth'; +import { getGitRepoType, getTokenForGitRepo } from './git-auth'; import { Entity } from '@backstage/catalog-model'; import { InputError } from '@backstage/backend-common'; import { RemoteProtocol } from './techdocs/stages/prepare/types'; @@ -121,14 +121,6 @@ export const checkoutGitRepository = async ( ): Promise => { const parsedGitLocation = parseGitUrl(repoUrl); const repositoryTmpPath = await getGitRepositoryTempFolder(repoUrl); - - // TODO: Should propably not be hardcoded names of env variables, but seems too hard to access config down here - const user = - process.env.GITHUB_PRIVATE_TOKEN_USER || - process.env.GITLAB_PRIVATE_TOKEN_USER || - process.env.AZURE_PRIVATE_TOKEN_USER || - ''; - const token = await getTokenForGitRepo(repoUrl); if (fs.existsSync(repositoryTmpPath)) { @@ -152,7 +144,9 @@ export const checkoutGitRepository = async ( } if (token) { - parsedGitLocation.token = `${user}:${token}`; + const type = getGitRepoType(repoUrl); + const auth = type === 'github' ? `${token}:x-oauth-basic` : `:${token}`; + parsedGitLocation.token = auth; } const repositoryCheckoutUrl = parsedGitLocation.toString('https'); diff --git a/plugins/techdocs-backend/src/service/metadata.ts b/plugins/techdocs-backend/src/service/metadata.ts index 671fcdda17..760180f8d2 100644 --- a/plugins/techdocs-backend/src/service/metadata.ts +++ b/plugins/techdocs-backend/src/service/metadata.ts @@ -1,4 +1,3 @@ -import fetch from 'node-fetch'; /* * Copyright 2020 Spotify AB * @@ -15,6 +14,8 @@ import fetch from 'node-fetch'; * limitations under the License. */ +import fetch from 'cross-fetch'; + export class TechDocsMetadata { private async getMetadataFile(docsUrl: String) { const metadataURL = `${docsUrl}/techdocs_metadata.json`; diff --git a/plugins/techdocs-backend/src/service/router.ts b/plugins/techdocs-backend/src/service/router.ts index f2317f749e..b6f8865a57 100644 --- a/plugins/techdocs-backend/src/service/router.ts +++ b/plugins/techdocs-backend/src/service/router.ts @@ -17,7 +17,7 @@ import { Logger } from 'winston'; import Router from 'express-promise-router'; import express from 'express'; import Knex from 'knex'; -import fetch from 'node-fetch'; +import fetch from 'cross-fetch'; import { Config } from '@backstage/config'; import Docker from 'dockerode'; import { @@ -111,7 +111,9 @@ export async function createRouter({ const catalogRes = await fetch(`${catalogUrl}/entities/by-name/${triple}`); if (!catalogRes.ok) { - catalogRes.body.pipe(res.status(catalogRes.status)); + const catalogResText = await catalogRes.text(); + res.status(catalogRes.status); + res.send(catalogResText); return; } diff --git a/plugins/techdocs/package.json b/plugins/techdocs/package.json index 99742d2661..eb6bb597d9 100644 --- a/plugins/techdocs/package.json +++ b/plugins/techdocs/package.json @@ -48,9 +48,9 @@ "@types/jest": "^26.0.7", "@types/node": "^12.0.0", "canvas": "^2.6.1", - "jest-fetch-mock": "^3.0.3", - "msw": "^0.20.5", - "node-fetch": "^2.6.1" + "msw": "^0.21.2", + "cross-fetch": "^3.0.6", + "@backstage/test-utils": "^0.1.1-alpha.25" }, "files": [ "dist" diff --git a/plugins/techdocs/src/setupTests.ts b/plugins/techdocs/src/setupTests.ts index 8553642152..825bcd4115 100644 --- a/plugins/techdocs/src/setupTests.ts +++ b/plugins/techdocs/src/setupTests.ts @@ -15,5 +15,3 @@ */ import '@testing-library/jest-dom'; - -require('jest-fetch-mock').enableMocks(); diff --git a/plugins/user-settings/package.json b/plugins/user-settings/package.json index 029ff99222..9748bb1a9e 100644 --- a/plugins/user-settings/package.json +++ b/plugins/user-settings/package.json @@ -40,8 +40,8 @@ "@testing-library/user-event": "^12.0.7", "@types/jest": "^26.0.7", "@types/node": "^12.0.0", - "msw": "^0.20.5", - "node-fetch": "^2.6.1" + "msw": "^0.21.2", + "cross-fetch": "^3.0.6" }, "files": [ "dist" diff --git a/plugins/welcome/package.json b/plugins/welcome/package.json index 4a23e5ee86..653678f1f3 100644 --- a/plugins/welcome/package.json +++ b/plugins/welcome/package.json @@ -38,10 +38,10 @@ "@testing-library/react": "^10.4.1", "@testing-library/user-event": "^12.0.7", "@types/jest": "^26.0.7", - "@types/node": "^12.0.0", - "jest-fetch-mock": "^3.0.3", - "msw": "^0.20.5", - "node-fetch": "^2.6.1" + "msw": "^0.21.2", + "cross-fetch": "^3.0.6", + "@backstage/test-utils": "^0.1.1-alpha.25", + "@types/node": "^12.0.0" }, "files": [ "dist" diff --git a/yarn.lock b/yarn.lock index ad9b65f276..aeba84153a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3830,16 +3830,16 @@ integrity sha512-so8w32ZV42CHWxOEXcBtbNO/hLXFrQNXVmhfzhUI6dVB9cq2xjRaiqu8GjFj8LvKbWpPj+S+KwTIS4aDVWqrFQ== "@storybook/addon-actions@^6.0.21": - version "6.0.21" - resolved "https://registry.npmjs.org/@storybook/addon-actions/-/addon-actions-6.0.21.tgz#0de1d109d4b1eb99f644bbe84e74c25cfd2b1b6b" - integrity sha512-9y3ve+3GK1TsxQ5pxDjhB7E/XJXY+WqcSNlOX8Mb+XbS6AAgpFbkZCw1q8CGzyEUclHsQ6UK2+lo+IRGs4TLpA== + version "6.0.26" + resolved "https://registry.npmjs.org/@storybook/addon-actions/-/addon-actions-6.0.26.tgz#d0de9e4d78a8f8f5bf8730c04d0b6d1741c29273" + integrity sha512-9tWbAqSwzWWVz8zwAndZFusZYjIcRYgZUC0LqC8QlH79DgF3ASjw9y97+w1VTTAzdb6LYnsMuSpX6+8m5hrR4g== dependencies: - "@storybook/addons" "6.0.21" - "@storybook/api" "6.0.21" - "@storybook/client-api" "6.0.21" - "@storybook/components" "6.0.21" - "@storybook/core-events" "6.0.21" - "@storybook/theming" "6.0.21" + "@storybook/addons" "6.0.26" + "@storybook/api" "6.0.26" + "@storybook/client-api" "6.0.26" + "@storybook/components" "6.0.26" + "@storybook/core-events" "6.0.26" + "@storybook/theming" "6.0.26" core-js "^3.0.1" fast-deep-equal "^3.1.1" global "^4.3.2" @@ -3892,7 +3892,7 @@ react-syntax-highlighter "^12.2.1" regenerator-runtime "^0.13.3" -"@storybook/addons@6.0.21", "@storybook/addons@^6.0.21": +"@storybook/addons@6.0.21": version "6.0.21" resolved "https://registry.npmjs.org/@storybook/addons/-/addons-6.0.21.tgz#bd5229652102c3aed59b78ef6920ff6b482b4d78" integrity sha512-yDttNLc3vXqBxwK795ykgzTC6MpvuXDQuF4LHSlHZQe6wsMu1m3fljnbYdafJWdx6cNZwUblU3KYcR11PqhkPg== @@ -3907,6 +3907,21 @@ global "^4.3.2" regenerator-runtime "^0.13.3" +"@storybook/addons@6.0.26", "@storybook/addons@^6.0.21": + version "6.0.26" + resolved "https://registry.npmjs.org/@storybook/addons/-/addons-6.0.26.tgz#343cbea3eee2d39413b80bc2d66535a7f61488fc" + integrity sha512-OhAApFKgsj9an7FLYfHI4cJQuZ4Zm6yoGOpaxhOvKQMw7dXUPsLvbCyw/6dZOLvaFhjJjQiXtbxtZG+UjR8nvA== + dependencies: + "@storybook/api" "6.0.26" + "@storybook/channels" "6.0.26" + "@storybook/client-logger" "6.0.26" + "@storybook/core-events" "6.0.26" + "@storybook/router" "6.0.26" + "@storybook/theming" "6.0.26" + core-js "^3.0.1" + global "^4.3.2" + regenerator-runtime "^0.13.3" + "@storybook/api@6.0.21": version "6.0.21" resolved "https://registry.npmjs.org/@storybook/api/-/api-6.0.21.tgz#a25a1eb4d07dc43500e03c856db43baba46726f1" @@ -3933,6 +3948,32 @@ ts-dedent "^1.1.1" util-deprecate "^1.0.2" +"@storybook/api@6.0.26": + version "6.0.26" + resolved "https://registry.npmjs.org/@storybook/api/-/api-6.0.26.tgz#c45222c132eb8bc2e383536adfebbeb7a89867d0" + integrity sha512-aszDoz1c6T+eRtTUwWvySoyd3gRXmQxsingD084NnEp4VfFLA5H7VS/0sre0ZvU5GWh8d9COxY0DS2Ry/QSKvw== + dependencies: + "@reach/router" "^1.3.3" + "@storybook/channels" "6.0.26" + "@storybook/client-logger" "6.0.26" + "@storybook/core-events" "6.0.26" + "@storybook/csf" "0.0.1" + "@storybook/router" "6.0.26" + "@storybook/semver" "^7.3.2" + "@storybook/theming" "6.0.26" + "@types/reach__router" "^1.3.5" + core-js "^3.0.1" + fast-deep-equal "^3.1.1" + global "^4.3.2" + lodash "^4.17.15" + memoizerific "^1.11.3" + react "^16.8.3" + regenerator-runtime "^0.13.3" + store2 "^2.7.1" + telejson "^5.0.2" + ts-dedent "^1.1.1" + util-deprecate "^1.0.2" + "@storybook/channel-postmessage@6.0.21": version "6.0.21" resolved "https://registry.npmjs.org/@storybook/channel-postmessage/-/channel-postmessage-6.0.21.tgz#97e8f43c1b66f84c7b8271e447d45d4f66d355d1" @@ -3946,6 +3987,19 @@ qs "^6.6.0" telejson "^5.0.2" +"@storybook/channel-postmessage@6.0.26": + version "6.0.26" + resolved "https://registry.npmjs.org/@storybook/channel-postmessage/-/channel-postmessage-6.0.26.tgz#a98a0132d6bdf06741afac2607e9feabe34ab98b" + integrity sha512-FT6lC8M5JlNBxPT0rYfmF1yl9mBv04nfYs82TZpp1CzpLxf7wxdCBZ8SSRmvWIVBoNwGZPDhIk5+6JWyDEISBg== + dependencies: + "@storybook/channels" "6.0.26" + "@storybook/client-logger" "6.0.26" + "@storybook/core-events" "6.0.26" + core-js "^3.0.1" + global "^4.3.2" + qs "^6.6.0" + telejson "^5.0.2" + "@storybook/channels@6.0.21": version "6.0.21" resolved "https://registry.npmjs.org/@storybook/channels/-/channels-6.0.21.tgz#bc0951efacbaa5f8827693fba4fe7c2290b5772c" @@ -3955,6 +4009,15 @@ ts-dedent "^1.1.1" util-deprecate "^1.0.2" +"@storybook/channels@6.0.26": + version "6.0.26" + resolved "https://registry.npmjs.org/@storybook/channels/-/channels-6.0.26.tgz#3e8678b4b40085081257a39b9e85fab13a19943c" + integrity sha512-H0iUorayYqS+zfhbjd+cYRzAdRLGLWUeWFu2Aa+oJ4/zeAQNL+DafWboHc567RQ4Vb5KqE5QZoCFskWUUYqJYA== + dependencies: + core-js "^3.0.1" + ts-dedent "^1.1.1" + util-deprecate "^1.0.2" + "@storybook/client-api@6.0.21": version "6.0.21" resolved "https://registry.npmjs.org/@storybook/client-api/-/client-api-6.0.21.tgz#6a652dea67d219a31d18af0e05b9f17ba6c7c316" @@ -3978,6 +4041,29 @@ ts-dedent "^1.1.1" util-deprecate "^1.0.2" +"@storybook/client-api@6.0.26": + version "6.0.26" + resolved "https://registry.npmjs.org/@storybook/client-api/-/client-api-6.0.26.tgz#ac9334ba86834e5cb23fc4fb577de60bda66164d" + integrity sha512-Qd5wR5b5lio/EchuJMhAmmJAE1pfvnEyu+JnyFGwMZLV9mN9NSspz+YsqbSCCDZsYcP5ewvPEnumIWqmj/wagQ== + dependencies: + "@storybook/addons" "6.0.26" + "@storybook/channel-postmessage" "6.0.26" + "@storybook/channels" "6.0.26" + "@storybook/client-logger" "6.0.26" + "@storybook/core-events" "6.0.26" + "@storybook/csf" "0.0.1" + "@types/qs" "^6.9.0" + "@types/webpack-env" "^1.15.2" + core-js "^3.0.1" + global "^4.3.2" + lodash "^4.17.15" + memoizerific "^1.11.3" + qs "^6.6.0" + stable "^0.1.8" + store2 "^2.7.1" + ts-dedent "^1.1.1" + util-deprecate "^1.0.2" + "@storybook/client-logger@6.0.21": version "6.0.21" resolved "https://registry.npmjs.org/@storybook/client-logger/-/client-logger-6.0.21.tgz#20369addf9eb79fc0c85a2e0dcb48f5a1a544532" @@ -3986,6 +4072,14 @@ core-js "^3.0.1" global "^4.3.2" +"@storybook/client-logger@6.0.26": + version "6.0.26" + resolved "https://registry.npmjs.org/@storybook/client-logger/-/client-logger-6.0.26.tgz#e3d28bd8dc02ec2c53a9d69773a68189590b746f" + integrity sha512-VNoL6/oehVhn3hZi9vrTNT+C/3oAZKV+smfZFnPtsCR/Fq7CKbmsBd0pGPL57f81RU8e8WygwrIlAGJTDSNIjw== + dependencies: + core-js "^3.0.1" + global "^4.3.2" + "@storybook/components@6.0.21": version "6.0.21" resolved "https://registry.npmjs.org/@storybook/components/-/components-6.0.21.tgz#2f355370f993e0b7b9062094a03dffc2cdda91db" @@ -4014,6 +4108,34 @@ react-textarea-autosize "^8.1.1" ts-dedent "^1.1.1" +"@storybook/components@6.0.26": + version "6.0.26" + resolved "https://registry.npmjs.org/@storybook/components/-/components-6.0.26.tgz#e1f6e16aae850a71c9ac7bdd1d44a068ec9cfdc1" + integrity sha512-8wigI1pDFJO1m1IQWPguOK+nOsaAVRWkVdu+2te/rDcIR9QNvMzzou0+Lhfp3zKSVT4E6mEoGB/TWXXF5Iq0sQ== + dependencies: + "@storybook/client-logger" "6.0.26" + "@storybook/csf" "0.0.1" + "@storybook/theming" "6.0.26" + "@types/overlayscrollbars" "^1.9.0" + "@types/react-color" "^3.0.1" + "@types/react-syntax-highlighter" "11.0.4" + core-js "^3.0.1" + fast-deep-equal "^3.1.1" + global "^4.3.2" + lodash "^4.17.15" + markdown-to-jsx "^6.11.4" + memoizerific "^1.11.3" + overlayscrollbars "^1.10.2" + polished "^3.4.4" + popper.js "^1.14.7" + react "^16.8.3" + react-color "^2.17.0" + react-dom "^16.8.3" + react-popper-tooltip "^2.11.0" + react-syntax-highlighter "^12.2.1" + react-textarea-autosize "^8.1.1" + ts-dedent "^1.1.1" + "@storybook/core-events@6.0.21": version "6.0.21" resolved "https://registry.npmjs.org/@storybook/core-events/-/core-events-6.0.21.tgz#2ce51e6d7524e7543dbb29571beac1dbeb4e5f40" @@ -4021,6 +4143,13 @@ dependencies: core-js "^3.0.1" +"@storybook/core-events@6.0.26": + version "6.0.26" + resolved "https://registry.npmjs.org/@storybook/core-events/-/core-events-6.0.26.tgz#61181c9a8610d26cc85d47f133a563879044ca2d" + integrity sha512-nWjS/+kMiw31OPgeJQaiFsJk9ZJJo3/d4c+kc6GOl2iC1H3Q4/5cm3NvJBn/7bUtKHmSFwfbDouj+XjUk5rZbQ== + dependencies: + core-js "^3.0.1" + "@storybook/core@6.0.21": version "6.0.21" resolved "https://registry.npmjs.org/@storybook/core/-/core-6.0.21.tgz#105c2b90ab27e7b478cb1b7d10e9fe5aba5e0708" @@ -4180,6 +4309,18 @@ memoizerific "^1.11.3" qs "^6.6.0" +"@storybook/router@6.0.26": + version "6.0.26" + resolved "https://registry.npmjs.org/@storybook/router/-/router-6.0.26.tgz#5b991001afa7d7eb5e40c53cd4c58266b6f9edfd" + integrity sha512-kQ1LF/2gX3IkjS1wX7CsoeBc9ptHQzOsyax16rUyJa769DT5vMNtFtQxjNXMqSiSapPg2yrXJFKQNaoWvKgQEQ== + dependencies: + "@reach/router" "^1.3.3" + "@types/reach__router" "^1.3.5" + core-js "^3.0.1" + global "^4.3.2" + memoizerific "^1.11.3" + qs "^6.6.0" + "@storybook/semver@^7.3.2": version "7.3.2" resolved "https://registry.npmjs.org/@storybook/semver/-/semver-7.3.2.tgz#f3b9c44a1c9a0b933c04e66d0048fcf2fa10dac0" @@ -4222,6 +4363,24 @@ resolve-from "^5.0.0" ts-dedent "^1.1.1" +"@storybook/theming@6.0.26": + version "6.0.26" + resolved "https://registry.npmjs.org/@storybook/theming/-/theming-6.0.26.tgz#e5b545fb2653dfd1b043b567197d490b1c3c0da3" + integrity sha512-9yon2ofb9a+RT1pdvn8Njydy7XRw0qXcIsMqGsJRKoZecmRRozqB6DxH9Gbdf1vRSbM9gYUUDjbiMDFz7+4RiQ== + dependencies: + "@emotion/core" "^10.0.20" + "@emotion/is-prop-valid" "^0.8.6" + "@emotion/styled" "^10.0.17" + "@storybook/client-logger" "6.0.26" + core-js "^3.0.1" + deep-object-diff "^1.1.0" + emotion-theming "^10.0.19" + global "^4.3.2" + memoizerific "^1.11.3" + polished "^3.4.4" + resolve-from "^5.0.0" + ts-dedent "^1.1.1" + "@storybook/ui@6.0.21": version "6.0.21" resolved "https://registry.npmjs.org/@storybook/ui/-/ui-6.0.21.tgz#5dac2b68a30f5dba5457e0315f58977e07138968" @@ -5192,6 +5351,11 @@ resolved "https://registry.npmjs.org/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d" integrity sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA== +"@types/minimist@^1.2.0": + version "1.2.0" + resolved "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.0.tgz#69a23a3ad29caf0097f06eda59b361ee2f0639f6" + integrity sha1-aaI6OtKcrwCX8G7aWbNh7i8GOfY= + "@types/minipass@*": version "2.2.0" resolved "https://registry.npmjs.org/@types/minipass/-/minipass-2.2.0.tgz#51ad404e8eb1fa961f75ec61205796807b6f9651" @@ -5206,6 +5370,13 @@ dependencies: "@types/node" "*" +"@types/mock-fs@^4.13.0": + version "4.13.0" + resolved "https://registry.npmjs.org/@types/mock-fs/-/mock-fs-4.13.0.tgz#b8b01cd2db588668b2532ecd21b1babd3fffb2c0" + integrity sha512-FUqxhURwqFtFBCuUj3uQMp7rPSQs//b3O9XecAVxhqS9y4/W8SIJEZFq2mmpnFVZBXwR/2OyPLE97CpyYiB8Mw== + dependencies: + "@types/node" "*" + "@types/morgan@^1.9.0": version "1.9.1" resolved "https://registry.npmjs.org/@types/morgan/-/morgan-1.9.1.tgz#6457872df95647c1dbc6b3741e8146b71ece74bf" @@ -5213,7 +5384,7 @@ dependencies: "@types/node" "*" -"@types/node-fetch@2.5.7", "@types/node-fetch@^2.5.4", "@types/node-fetch@^2.5.7": +"@types/node-fetch@2.5.7", "@types/node-fetch@^2.5.4": version "2.5.7" resolved "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.5.7.tgz#20a2afffa882ab04d44ca786449a276f9f6bbf3c" integrity sha512-o2WVNf5UhWRkxlf6eq+jMZDu7kjgpgJfl4xVNlvryc95O/6F2ld8ztKX+qu+Rjyet93WAWm5LjeX9H5FGkODvw== @@ -9061,7 +9232,7 @@ cross-env@^7.0.0: dependencies: cross-spawn "^7.0.1" -cross-fetch@3.0.6, cross-fetch@^3.0.4, cross-fetch@^3.0.5: +cross-fetch@3.0.6, cross-fetch@^3.0.5, cross-fetch@^3.0.6: version "3.0.6" resolved "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.0.6.tgz#3a4040bc8941e653e0e9cf17f29ebcd177d3365c" integrity sha512-KBPUbqgFjzWlVcURG+Svp9TlhA5uliYtiNx/0r8nv0pdypeQCRJ9IaSIc3q/x3q8t3F75cHuwxVql1HFGHCNJQ== @@ -10656,9 +10827,9 @@ escodegen@^1.14.1, escodegen@^1.9.1: source-map "~0.6.1" eslint-config-prettier@^6.0.0: - version "6.10.0" - resolved "https://registry.npmjs.org/eslint-config-prettier/-/eslint-config-prettier-6.10.0.tgz#7b15e303bf9c956875c948f6b21500e48ded6a7f" - integrity sha512-AtndijGte1rPILInUdHjvKEGbIV06NuvPrqlIEaEaWtbtvJh464mDeyGMdZEQMsGvC0ZVkiex1fSNcC4HAbRGg== + version "6.14.0" + resolved "https://registry.npmjs.org/eslint-config-prettier/-/eslint-config-prettier-6.14.0.tgz#390e7863a8ae99970981933826476169285b3a27" + integrity sha512-DbVwh0qZhAC7CNDWcq8cBdK6FcVHiMTKmCypOPWeZkp9hJ8xYwTaWSa6bb6cjfi8KOeJy0e9a8Izxyx+O4+gCQ== dependencies: get-stdin "^6.0.0" @@ -14224,14 +14395,6 @@ jest-esm-transformer@^1.0.0: "@babel/core" "^7.4.4" "@babel/plugin-transform-modules-commonjs" "^7.4.4" -jest-fetch-mock@^3.0.3: - version "3.0.3" - resolved "https://registry.npmjs.org/jest-fetch-mock/-/jest-fetch-mock-3.0.3.tgz#31749c456ae27b8919d69824f1c2bd85fe0a1f3b" - integrity sha512-Ux1nWprtLrdrH4XwE7O7InRY6psIi3GOsqNESJgMJ+M5cv4A8Lh7SN9d2V2kKRZ8ebAfcd1LNyZguAOb6JiDqw== - dependencies: - cross-fetch "^3.0.4" - promise-polyfill "^8.1.3" - jest-get-type@^25.2.6: version "25.2.6" resolved "https://registry.npmjs.org/jest-get-type/-/jest-get-type-25.2.6.tgz#0b0a32fab8908b44d508be81681487dbabb8d877" @@ -16376,6 +16539,42 @@ msw@^0.20.5: statuses "^2.0.0" yargs "^15.4.1" +msw@^0.21.2: + version "0.21.2" + resolved "https://registry.npmjs.org/msw/-/msw-0.21.2.tgz#74ed10b8eb224325652a3c3812b5460dac297bd8" + integrity sha512-XOJehxtJThNFdMJdVjxDAbZ8KuC3UltOlO5nQDks0Q1yzSUqqKcVUjbKrH7T+K2hckBr0KEY2fwJHv21R4BV2A== + dependencies: + "@open-draft/until" "^1.0.3" + "@types/cookie" "^0.4.0" + chalk "^4.1.0" + chokidar "^3.4.2" + cookie "^0.4.1" + graphql "^15.3.0" + headers-utils "^1.2.0" + node-fetch "^2.6.1" + node-match-path "^0.4.4" + node-request-interceptor "^0.5.1" + statuses "^2.0.0" + yargs "^16.0.3" + +msw@^0.21.3: + version "0.21.3" + resolved "https://registry.npmjs.org/msw/-/msw-0.21.3.tgz#d073842f9570a08f4041806a2c7303a9b8494602" + integrity sha512-voPc/EJsjarvi454vSEuozZQQqLG4AUHT6qQL5Ah47lq7sGCpc7icByeUlfvEj5+MvaugN0c7JwXyCa2rxu8cA== + dependencies: + "@open-draft/until" "^1.0.3" + "@types/cookie" "^0.4.0" + chalk "^4.1.0" + chokidar "^3.4.2" + cookie "^0.4.1" + graphql "^15.3.0" + headers-utils "^1.2.0" + node-fetch "^2.6.1" + node-match-path "^0.4.4" + node-request-interceptor "^0.5.1" + statuses "^2.0.0" + yargs "^16.0.3" + multicast-dns-service-types@^1.1.0: version "1.1.0" resolved "https://registry.npmjs.org/multicast-dns-service-types/-/multicast-dns-service-types-1.1.0.tgz#899f11d9686e5e05cb91b35d5f0e63b773cfc901" @@ -16704,6 +16903,15 @@ node-request-interceptor@^0.3.5: debug "^4.1.1" headers-utils "^1.2.0" +node-request-interceptor@^0.5.1: + version "0.5.1" + resolved "https://registry.npmjs.org/node-request-interceptor/-/node-request-interceptor-0.5.1.tgz#b4757a033bde4412d9ffc4503804abb28ed962d2" + integrity sha512-ex5mlI5nGokxocomS2Rj2r1aspmt7qZoI8OvKLt24ylp1bYCzGQ+0XD911guCNDb/kKLMIGC67HHyeFrJCz7jA== + dependencies: + "@open-draft/until" "^1.0.3" + debug "^4.1.1" + headers-utils "^1.2.0" + nodegit@0.27.0, nodegit@^0.27.0: version "0.27.0" resolved "https://registry.npmjs.org/nodegit/-/nodegit-0.27.0.tgz#4e8cc236f60e1c97324a5acff99056fe116a6ebe" @@ -18660,11 +18868,6 @@ promise-inflight@^1.0.1: resolved "https://registry.npmjs.org/promise-inflight/-/promise-inflight-1.0.1.tgz#98472870bf228132fcbdd868129bad12c3c029e3" integrity sha1-mEcocL8igTL8vdhoEputEsPAKeM= -promise-polyfill@^8.1.3: - version "8.1.3" - resolved "https://registry.npmjs.org/promise-polyfill/-/promise-polyfill-8.1.3.tgz#8c99b3cf53f3a91c68226ffde7bde81d7f904116" - integrity sha512-MG5r82wBzh7pSKDRa9y+vllNHz3e3d4CNj1PQE4BQYxLme0gKYYBm9YENq+UkEikyZ0XbiGWxYlVw3Rl9O/U8g== - promise-retry@^1.1.1: version "1.1.1" resolved "https://registry.npmjs.org/promise-retry/-/promise-retry-1.1.1.tgz#6739e968e3051da20ce6497fb2b50f6911df3d6d" @@ -23570,7 +23773,7 @@ whatwg-fetch@^2.0.4: resolved "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-2.0.4.tgz#dde6a5df315f9d39991aa17621853d720b85566f" integrity sha512-dcQ1GWpOD/eEQ97k66aiEVpNnapVj90/+R+SXTPYGHpYBBypfKJEQjLrvMZ7YXbKm21gXd4NcuxUTjiv1YtLng== -whatwg-fetch@^3.0.0, whatwg-fetch@^3.4.0, whatwg-fetch@^3.4.1: +whatwg-fetch@^3.0.0, whatwg-fetch@^3.4.1: version "3.4.1" resolved "https://registry.npmjs.org/whatwg-fetch/-/whatwg-fetch-3.4.1.tgz#e5f871572d6879663fa5674c8f833f15a8425ab3" integrity sha512-sofZVzE1wKwO+EYPbWfiwzaKovWiZXf4coEzjGP9b2GBVgQRLQUZ2QcuPpQExGDAW5GItpEm6Tl4OU5mywnAoQ==