From b3d70e2a4f66a5540c758faa91c6c53c1bf882f0 Mon Sep 17 00:00:00 2001 From: Heikki Hellgren Date: Fri, 15 Dec 2023 14:47:22 +0200 Subject: [PATCH] feat: add support for new backend to notifications Signed-off-by: Heikki Hellgren --- plugins/notifications-backend/api-report.md | 4 ++ plugins/notifications-backend/package.json | 1 + plugins/notifications-backend/src/index.ts | 1 + plugins/notifications-backend/src/plugin.ts | 50 +++++++++++++++++++++ plugins/notifications-node/api-report.md | 4 ++ plugins/notifications-node/package.json | 1 + plugins/notifications-node/src/index.ts | 1 + plugins/notifications-node/src/lib.ts | 39 ++++++++++++++++ yarn.lock | 2 + 9 files changed, 103 insertions(+) create mode 100644 plugins/notifications-backend/src/plugin.ts create mode 100644 plugins/notifications-node/src/lib.ts diff --git a/plugins/notifications-backend/api-report.md b/plugins/notifications-backend/api-report.md index e571fd521a..02e86293f9 100644 --- a/plugins/notifications-backend/api-report.md +++ b/plugins/notifications-backend/api-report.md @@ -3,6 +3,7 @@ > Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). ```ts +import { BackendFeature } from '@backstage/backend-plugin-api'; import express from 'express'; import { IdentityApi } from '@backstage/plugin-auth-node'; import { Logger } from 'winston'; @@ -11,6 +12,9 @@ import { NotificationService } from '@backstage/plugin-notifications-node'; // @public (undocumented) export function createRouter(options: RouterOptions): Promise; +// @public +export const notificationsPlugin: () => BackendFeature; + // @public (undocumented) export interface RouterOptions { // (undocumented) diff --git a/plugins/notifications-backend/package.json b/plugins/notifications-backend/package.json index ce67513669..1ee25ec127 100644 --- a/plugins/notifications-backend/package.json +++ b/plugins/notifications-backend/package.json @@ -23,6 +23,7 @@ }, "dependencies": { "@backstage/backend-common": "workspace:^", + "@backstage/backend-plugin-api": "workspace:^", "@backstage/config": "workspace:^", "@backstage/plugin-auth-node": "workspace:^", "@backstage/plugin-notifications-node": "workspace:^", diff --git a/plugins/notifications-backend/src/index.ts b/plugins/notifications-backend/src/index.ts index d2e8d61bad..e3a02aca06 100644 --- a/plugins/notifications-backend/src/index.ts +++ b/plugins/notifications-backend/src/index.ts @@ -14,3 +14,4 @@ * limitations under the License. */ export * from './service/router'; +export * from './plugin'; diff --git a/plugins/notifications-backend/src/plugin.ts b/plugins/notifications-backend/src/plugin.ts new file mode 100644 index 0000000000..d125332c4c --- /dev/null +++ b/plugins/notifications-backend/src/plugin.ts @@ -0,0 +1,50 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { loggerToWinstonLogger } from '@backstage/backend-common'; +import { + coreServices, + createBackendPlugin, +} from '@backstage/backend-plugin-api'; +import { createRouter } from './service/router'; +import { notificationService } from '@backstage/plugin-notifications-node'; + +/** + * Notifications backend plugin + * + * @public + */ +export const notificationsPlugin = createBackendPlugin({ + pluginId: 'notifications', + register(env) { + env.registerInit({ + deps: { + httpRouter: coreServices.httpRouter, + logger: coreServices.logger, + identity: coreServices.identity, + service: notificationService, + }, + async init({ httpRouter, logger, identity, service }) { + httpRouter.use( + await createRouter({ + logger: loggerToWinstonLogger(logger), + identity, + notificationService: service, + }), + ); + }, + }); + }, +}); diff --git a/plugins/notifications-node/api-report.md b/plugins/notifications-node/api-report.md index 575c6a9b09..c7b6383c6f 100644 --- a/plugins/notifications-node/api-report.md +++ b/plugins/notifications-node/api-report.md @@ -8,6 +8,7 @@ import { NotificationStatus } from '@backstage/plugin-notifications-common'; import { NotificationType } from '@backstage/plugin-notifications-common'; import { PluginDatabaseManager } from '@backstage/backend-common'; import { PluginEndpointDiscovery } from '@backstage/backend-common'; +import { ServiceRef } from '@backstage/backend-plugin-api'; // @public (undocumented) export class DatabaseNotificationsStore implements NotificationsStore { @@ -54,6 +55,9 @@ export class NotificationService { ): Promise; } +// @public (undocumented) +export const notificationService: ServiceRef; + // @public (undocumented) export type NotificationServiceOptions = { database: PluginDatabaseManager; diff --git a/plugins/notifications-node/package.json b/plugins/notifications-node/package.json index 4a9e9a2319..d5cb66ba3d 100644 --- a/plugins/notifications-node/package.json +++ b/plugins/notifications-node/package.json @@ -29,6 +29,7 @@ ], "dependencies": { "@backstage/backend-common": "workspace:^", + "@backstage/backend-plugin-api": "workspace:^", "@backstage/catalog-client": "workspace:^", "@backstage/catalog-model": "workspace:^", "@backstage/plugin-notifications-common": "workspace:^", diff --git a/plugins/notifications-node/src/index.ts b/plugins/notifications-node/src/index.ts index c686e95d3c..f820946cc0 100644 --- a/plugins/notifications-node/src/index.ts +++ b/plugins/notifications-node/src/index.ts @@ -22,3 +22,4 @@ export * from './database'; export * from './service'; +export * from './lib'; diff --git a/plugins/notifications-node/src/lib.ts b/plugins/notifications-node/src/lib.ts new file mode 100644 index 0000000000..05a1321bc1 --- /dev/null +++ b/plugins/notifications-node/src/lib.ts @@ -0,0 +1,39 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { + coreServices, + createServiceFactory, + createServiceRef, +} from '@backstage/backend-plugin-api'; +import { NotificationService } from './service'; + +/** @public */ +export const notificationService = createServiceRef({ + id: 'notifications.service', + scope: 'plugin', + defaultFactory: async service => + createServiceFactory({ + service, + deps: { + discovery: coreServices.discovery, + database: coreServices.database, + // TODO: Signals + }, + factory({ discovery, database }) { + return NotificationService.create({ discovery, database }); + }, + }), +}); diff --git a/yarn.lock b/yarn.lock index 23efe10941..593901c68e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7784,6 +7784,7 @@ __metadata: resolution: "@backstage/plugin-notifications-backend@workspace:plugins/notifications-backend" dependencies: "@backstage/backend-common": "workspace:^" + "@backstage/backend-plugin-api": "workspace:^" "@backstage/cli": "workspace:^" "@backstage/config": "workspace:^" "@backstage/plugin-auth-node": "workspace:^" @@ -7814,6 +7815,7 @@ __metadata: resolution: "@backstage/plugin-notifications-node@workspace:plugins/notifications-node" dependencies: "@backstage/backend-common": "workspace:^" + "@backstage/backend-plugin-api": "workspace:^" "@backstage/catalog-client": "workspace:^" "@backstage/catalog-model": "workspace:^" "@backstage/cli": "workspace:^"