feat: follow-up changes on BEP #22641

Signed-off-by: Heikki Hellgren <heikki.hellgren@op.fi>
This commit is contained in:
Heikki Hellgren
2024-02-02 13:30:34 +02:00
parent b3c3672b64
commit acbe630b9d
16 changed files with 236 additions and 251 deletions
+5 -2
View File
@@ -15,15 +15,18 @@ import { NotificationService } from '@backstage/plugin-notifications-node';
function makeCreateEnv(config: Config) {
// ...
const notificationService = DefaultNotificationService.create({
const defaultNotificationService = DefaultNotificationService.create({
logger: root.child({ type: 'plugin' }),
discovery,
tokenManager,
signalService,
});
// ...
return (plugin: string): PluginEnvironment => {
// ...
const notificationService = defaultNotificationService.forPlugin(plugin);
return {
// ...
notificationService,
@@ -55,4 +58,4 @@ a user, the notification will be sent to only that user. If it's a group, the no
members of the group. If it's some other entity, the notification will be sent to the owner of that entity.
If the notification has `topic` set and user already has notification with that topic, the existing notification
will be updated with the new notification values and moved to inbox as unread.
will be updated with the new notification values and moved to inbox as unread.
+10 -9
View File
@@ -5,7 +5,7 @@
```ts
import { DiscoveryService } from '@backstage/backend-plugin-api';
import { LoggerService } from '@backstage/backend-plugin-api';
import { Notification as Notification_2 } from '@backstage/plugin-notifications-common';
import { NotificationPayload } from '@backstage/plugin-notifications-common';
import { ServiceRef } from '@backstage/backend-plugin-api';
import { SignalService } from '@backstage/plugin-signals-node';
import { TokenManager } from '@backstage/backend-common';
@@ -19,28 +19,29 @@ export class DefaultNotificationService implements NotificationService {
discovery,
}: NotificationServiceOptions): DefaultNotificationService;
// (undocumented)
send(options: NotificationSendOptions): Promise<Notification_2[]>;
forPlugin(pluginId: string): NotificationService;
// (undocumented)
send(notification: NotificationSendOptions): Promise<void>;
}
// @public (undocumented)
export type NotificationReceivers = {
export type NotificationRecipients = {
type: 'entity';
entityRef: string | string[];
};
// @public (undocumented)
export type NotificationSendOptions = {
receivers: NotificationReceivers;
title: string;
description: string;
link: string;
topic?: string;
recipients: NotificationRecipients;
payload: NotificationPayload;
};
// @public (undocumented)
export interface NotificationService {
// (undocumented)
send(options: NotificationSendOptions): Promise<Notification_2[]>;
forPlugin(pluginId: string): NotificationService;
// (undocumented)
send(options: NotificationSendOptions): Promise<void>;
}
// @public (undocumented)
+5 -3
View File
@@ -30,18 +30,20 @@ export const notificationService = createServiceRef<NotificationService>({
createServiceFactory({
service,
deps: {
logger: coreServices.logger,
logger: coreServices.rootLogger,
discovery: coreServices.discovery,
tokenManager: coreServices.tokenManager,
pluginMetadata: coreServices.pluginMetadata,
signals: signalService,
},
factory({ logger, discovery, tokenManager, signals }) {
factory({ logger, discovery, tokenManager, signals, pluginMetadata }) {
// TODO: Convert to use createRootContext
return DefaultNotificationService.create({
logger,
discovery,
tokenManager,
signalService: signals,
});
}).forPlugin(pluginMetadata.getId());
},
}),
});
@@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Notification } from '@backstage/plugin-notifications-common';
import { TokenManager } from '@backstage/backend-common';
import { NotificationService } from './NotificationService';
import { DiscoveryService, LoggerService } from '@backstage/backend-plugin-api';
import { SignalService } from '@backstage/plugin-signals-node';
import { NotificationPayload } from '@backstage/plugin-notifications-common';
/** @public */
export type NotificationServiceOptions = {
@@ -28,7 +28,7 @@ export type NotificationServiceOptions = {
};
/** @public */
export type NotificationReceivers = {
export type NotificationRecipients = {
type: 'entity';
entityRef: string | string[];
};
@@ -38,11 +38,8 @@ export type NotificationReceivers = {
/** @public */
export type NotificationSendOptions = {
receivers: NotificationReceivers;
title: string;
description: string;
link: string;
topic?: string;
recipients: NotificationRecipients;
payload: NotificationPayload;
};
/** @public */
@@ -51,6 +48,7 @@ export class DefaultNotificationService implements NotificationService {
private readonly logger: LoggerService,
private readonly discovery: DiscoveryService,
private readonly tokenManager: TokenManager,
private readonly pluginId?: string,
) {}
static create({
@@ -61,22 +59,36 @@ export class DefaultNotificationService implements NotificationService {
return new DefaultNotificationService(logger, discovery, tokenManager);
}
async send(options: NotificationSendOptions): Promise<Notification[]> {
forPlugin(pluginId: string): NotificationService {
return new DefaultNotificationService(
this.logger,
this.discovery,
this.tokenManager,
pluginId,
);
}
async send(notification: NotificationSendOptions): Promise<void> {
if (!this.pluginId) {
throw new Error('Invalid initialization of the NotificationService');
}
try {
const baseUrl = await this.discovery.getBaseUrl('notifications');
const { token } = await this.tokenManager.getToken();
const response = await fetch(`${baseUrl}/notifications`, {
await fetch(`${baseUrl}/notifications`, {
method: 'POST',
body: JSON.stringify(options),
body: JSON.stringify({
...notification,
origin: `plugin-${this.pluginId}`,
}),
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
});
return await response.json();
} catch (error) {
this.logger.error(`Failed to send notifications: ${error}`);
return [];
}
}
}
@@ -15,9 +15,10 @@
*/
import { NotificationSendOptions } from './DefaultNotificationService';
import { Notification } from '@backstage/plugin-notifications-common';
/** @public */
export interface NotificationService {
send(options: NotificationSendOptions): Promise<Notification[]>;
forPlugin(pluginId: string): NotificationService;
send(options: NotificationSendOptions): Promise<void>;
}