diff --git a/.changeset/nice-pants-shave.md b/.changeset/nice-pants-shave.md new file mode 100644 index 0000000000..500888a817 --- /dev/null +++ b/.changeset/nice-pants-shave.md @@ -0,0 +1,8 @@ +--- +'@backstage/plugin-auth-backend-module-cloudflare-access-provider': patch +'@backstage/plugin-scaffolder-backend-module-sentry': patch +'@backstage/plugin-scaffolder-backend-module-gitea': patch +'@backstage/plugin-notifications-node': patch +--- + +Use `node-fetch` instead of native fetch, as per https://backstage.io/docs/architecture-decisions/adrs-adr013 diff --git a/plugins/auth-backend-module-cloudflare-access-provider/src/helpers.ts b/plugins/auth-backend-module-cloudflare-access-provider/src/helpers.ts index 60411e53d5..c1a14ae6b4 100644 --- a/plugins/auth-backend-module-cloudflare-access-provider/src/helpers.ts +++ b/plugins/auth-backend-module-cloudflare-access-provider/src/helpers.ts @@ -23,6 +23,7 @@ import { } from '@backstage/errors'; import express from 'express'; import { createRemoteJWKSet, jwtVerify } from 'jose'; +import fetch, { Headers } from 'node-fetch'; import { CACHE_PREFIX, CF_JWT_HEADER, diff --git a/plugins/notifications-node/package.json b/plugins/notifications-node/package.json index 1f3760f5c6..2500026f3b 100644 --- a/plugins/notifications-node/package.json +++ b/plugins/notifications-node/package.json @@ -37,6 +37,7 @@ "@backstage/plugin-notifications-common": "workspace:^", "@backstage/plugin-signals-node": "workspace:^", "knex": "^3.0.0", + "node-fetch": "^2.6.7", "uuid": "^9.0.0" }, "devDependencies": { diff --git a/plugins/notifications-node/src/service/DefaultNotificationService.test.ts b/plugins/notifications-node/src/service/DefaultNotificationService.test.ts index 4711f3de18..d5c327e788 100644 --- a/plugins/notifications-node/src/service/DefaultNotificationService.test.ts +++ b/plugins/notifications-node/src/service/DefaultNotificationService.test.ts @@ -24,8 +24,6 @@ import { } from './DefaultNotificationService'; import { mockCredentials, mockServices } from '@backstage/backend-test-utils'; -const server = setupServer(); - const testNotification: NotificationPayload = { title: 'Notification 1', link: '/catalog', @@ -33,8 +31,12 @@ const testNotification: NotificationPayload = { }; describe('DefaultNotificationService', () => { + const server = setupServer(); setupRequestMockHandlers(server); - const discovery = mockServices.discovery(); + + const discovery = mockServices.discovery.mock({ + getBaseUrl: jest.fn().mockResolvedValue('http://example.com'), + }); const auth = mockServices.auth(); let service: DefaultNotificationService; @@ -53,20 +55,17 @@ describe('DefaultNotificationService', () => { }; server.use( - rest.post( - `${await discovery.getBaseUrl('notifications')}/`, - async (req, res, ctx) => { - const json = await req.json(); - expect(json).toEqual(body); - expect(req.headers.get('Authorization')).toBe( - mockCredentials.service.header({ - onBehalfOf: await auth.getOwnServiceCredentials(), - targetPluginId: 'notifications', - }), - ); - return res(ctx.status(200)); - }, - ), + rest.post('http://example.com', async (req, res, ctx) => { + const json = await req.json(); + expect(json).toEqual(body); + expect(req.headers.get('Authorization')).toBe( + mockCredentials.service.header({ + onBehalfOf: await auth.getOwnServiceCredentials(), + targetPluginId: 'notifications', + }), + ); + return res(ctx.status(200)); + }), ); await expect(service.send(body)).resolves.toBeUndefined(); }); @@ -78,20 +77,17 @@ describe('DefaultNotificationService', () => { }; server.use( - rest.post( - `${await discovery.getBaseUrl('notifications')}/`, - async (req, res, ctx) => { - const json = await req.json(); - expect(json).toEqual(body); - expect(req.headers.get('Authorization')).toBe( - mockCredentials.service.header({ - onBehalfOf: await auth.getOwnServiceCredentials(), - targetPluginId: 'notifications', - }), - ); - return res(ctx.status(400)); - }, - ), + rest.post('http://example.com', async (req, res, ctx) => { + const json = await req.json(); + expect(json).toEqual(body); + expect(req.headers.get('Authorization')).toBe( + mockCredentials.service.header({ + onBehalfOf: await auth.getOwnServiceCredentials(), + targetPluginId: 'notifications', + }), + ); + return res(ctx.status(400)); + }), ); await expect(service.send(body)).rejects.toThrow( 'Request failed with status 400', diff --git a/plugins/notifications-node/src/service/DefaultNotificationService.ts b/plugins/notifications-node/src/service/DefaultNotificationService.ts index 950f42ae83..7a46e01689 100644 --- a/plugins/notifications-node/src/service/DefaultNotificationService.ts +++ b/plugins/notifications-node/src/service/DefaultNotificationService.ts @@ -17,6 +17,7 @@ import { NotificationService } from './NotificationService'; import { AuthService, DiscoveryService } from '@backstage/backend-plugin-api'; import { NotificationPayload } from '@backstage/plugin-notifications-common'; +import fetch from 'node-fetch'; /** @public */ export type NotificationServiceOptions = { @@ -68,7 +69,7 @@ export class DefaultNotificationService implements NotificationService { targetPluginId: 'notifications', }); - const response = await fetch(`${baseUrl}/`, { + const response = await fetch(baseUrl, { method: 'POST', body: JSON.stringify(notification), headers: { diff --git a/plugins/scaffolder-backend-module-gitea/src/actions/gitea.ts b/plugins/scaffolder-backend-module-gitea/src/actions/gitea.ts index 6b29ff2a4c..601a670237 100644 --- a/plugins/scaffolder-backend-module-gitea/src/actions/gitea.ts +++ b/plugins/scaffolder-backend-module-gitea/src/actions/gitea.ts @@ -30,6 +30,7 @@ import { } from '@backstage/plugin-scaffolder-node'; import { examples } from './gitea.examples'; import crypto from 'crypto'; +import fetch, { Response, RequestInit } from 'node-fetch'; const checkGiteaContentUrl = async ( config: GiteaIntegrationConfig, diff --git a/plugins/scaffolder-backend-module-sentry/package.json b/plugins/scaffolder-backend-module-sentry/package.json index a44de6705a..861e78b565 100644 --- a/plugins/scaffolder-backend-module-sentry/package.json +++ b/plugins/scaffolder-backend-module-sentry/package.json @@ -44,6 +44,7 @@ "@backstage/config": "workspace:^", "@backstage/errors": "workspace:^", "@backstage/plugin-scaffolder-node": "workspace:^", + "node-fetch": "^2.6.7", "yaml": "^2.3.3" }, "devDependencies": { diff --git a/plugins/scaffolder-backend-module-sentry/src/actions/createProject.ts b/plugins/scaffolder-backend-module-sentry/src/actions/createProject.ts index 259b670f9e..686053b178 100644 --- a/plugins/scaffolder-backend-module-sentry/src/actions/createProject.ts +++ b/plugins/scaffolder-backend-module-sentry/src/actions/createProject.ts @@ -17,6 +17,7 @@ import { createTemplateAction } from '@backstage/plugin-scaffolder-node'; import { InputError } from '@backstage/errors'; import { Config } from '@backstage/config'; +import fetch from 'node-fetch'; /** * Creates the `sentry:project:create` Scaffolder action. diff --git a/yarn.lock b/yarn.lock index fde9aa7d1f..214d43ecfe 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6259,6 +6259,7 @@ __metadata: "@backstage/test-utils": "workspace:^" knex: ^3.0.0 msw: ^1.0.0 + node-fetch: ^2.6.7 uuid: ^9.0.0 languageName: unknown linkType: soft @@ -6751,6 +6752,7 @@ __metadata: "@backstage/plugin-scaffolder-node-test-utils": "workspace:^" "@backstage/types": "workspace:^" msw: ^2.0.0 + node-fetch: ^2.6.7 yaml: ^2.3.3 languageName: unknown linkType: soft