@backstage/plugin-notifications-backend-module-slack 0.4.1 → 0.4.2-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @backstage/plugin-notifications-backend-module-slack
2
2
 
3
+ ## 0.4.2-next.0
4
+
5
+ ### Patch Changes
6
+
7
+ - 744fa1f: Removed duplicated entries that appeared in both `dependencies` and `devDependencies`.
8
+ - f399a7a: Added scope-based message update support. When a notification is re-sent with the same `scope` and `notification.updated` is set, the processor now calls `chat.update()` on the existing Slack message instead of sending a duplicate via `chat.postMessage()`. Message timestamps are persisted in a new `slack_message_timestamps` database table with automatic daily cleanup. The processor gracefully degrades to the previous behavior when no database is provided.
9
+ - Updated dependencies
10
+ - @backstage/errors@1.3.1-next.0
11
+ - @backstage/plugin-notifications-common@0.2.3-next.0
12
+ - @backstage/plugin-notifications-node@0.2.26-next.0
13
+ - @backstage/backend-plugin-api@1.9.1-next.0
14
+ - @backstage/catalog-model@1.8.1-next.0
15
+ - @backstage/config@1.3.8-next.0
16
+ - @backstage/plugin-catalog-node@2.2.1-next.0
17
+ - @backstage/types@1.2.2
18
+
3
19
  ## 0.4.1
4
20
 
5
21
  ### Patch Changes
@@ -23,6 +23,8 @@ class SlackNotificationProcessor {
23
23
  sendNotifications;
24
24
  messagesSent;
25
25
  messagesFailed;
26
+ messagesUpdated;
27
+ db;
26
28
  broadcastChannels;
27
29
  broadcastRoutes;
28
30
  entityLoader;
@@ -115,22 +117,34 @@ class SlackNotificationProcessor {
115
117
  unit: "{message}"
116
118
  }
117
119
  );
120
+ this.messagesUpdated = metrics.createCounter(
121
+ "notifications.processors.slack.update.count",
122
+ {
123
+ description: "Number of existing Slack messages updated via scope matching",
124
+ unit: "{message}"
125
+ }
126
+ );
118
127
  const throttle = pThrottle__default.default({
119
128
  limit: this.concurrencyLimit,
120
129
  interval: this.throttleInterval
121
130
  });
122
131
  const throttled = throttle(
123
- (opts) => this.sendNotification(opts)
132
+ (opts, ctx) => this.sendNotification(opts, ctx)
124
133
  );
125
- this.sendNotifications = async (opts) => {
134
+ this.sendNotifications = async (opts, scopeContext) => {
126
135
  const results = await Promise.allSettled(
127
- opts.map((message) => throttled(message))
136
+ opts.map((message) => throttled(message, scopeContext))
128
137
  );
129
- let successCount = 0;
138
+ let sentCount = 0;
139
+ let updateCount = 0;
130
140
  let failureCount = 0;
131
141
  results.forEach((result, index) => {
132
142
  if (result.status === "fulfilled") {
133
- successCount++;
143
+ if (result.value === "updated") {
144
+ updateCount++;
145
+ } else {
146
+ sentCount++;
147
+ }
134
148
  } else {
135
149
  this.logger.error(
136
150
  `Failed to send Slack channel notification to ${opts[index].channel}: ${result.reason.message}`
@@ -138,10 +152,14 @@ class SlackNotificationProcessor {
138
152
  failureCount++;
139
153
  }
140
154
  });
141
- this.messagesSent.add(successCount);
155
+ this.messagesSent.add(sentCount);
156
+ this.messagesUpdated.add(updateCount);
142
157
  this.messagesFailed.add(failureCount);
143
158
  };
144
159
  }
160
+ setDatabase(db) {
161
+ this.db = db;
162
+ }
145
163
  getName() {
146
164
  return "SlackNotificationProcessor";
147
165
  }
@@ -232,7 +250,11 @@ class SlackNotificationProcessor {
232
250
  outbound.forEach((payload) => {
233
251
  this.logger.debug(`Sending notification: ${JSON.stringify(payload)}`);
234
252
  });
235
- await this.sendNotifications(outbound);
253
+ await this.sendNotifications(outbound, {
254
+ origin: notification.origin,
255
+ scope: notification.payload.scope,
256
+ isUpdate: !!notification.updated
257
+ });
236
258
  }
237
259
  async formatPayloadDescriptionForSlack(payload) {
238
260
  return {
@@ -301,11 +323,64 @@ class SlackNotificationProcessor {
301
323
  return void 0;
302
324
  }
303
325
  }
304
- async sendNotification(args) {
326
+ async sendNotification(args, scopeContext) {
327
+ const channel = args.channel;
328
+ const scope = scopeContext?.scope;
329
+ const origin = scopeContext?.origin;
330
+ if (scopeContext?.isUpdate && origin && scope && this.db) {
331
+ const storedTs = await this.getStoredTimestamp(origin, scope, channel);
332
+ if (storedTs) {
333
+ const updateArgs = {
334
+ channel,
335
+ ts: storedTs,
336
+ ..."text" in args ? { text: args.text } : {},
337
+ ..."blocks" in args ? { blocks: args.blocks } : {},
338
+ ..."attachments" in args ? { attachments: args.attachments } : {}
339
+ };
340
+ const updateResponse = await this.slack.chat.update(updateArgs);
341
+ if (!updateResponse.ok) {
342
+ throw new Error(
343
+ `Failed to update notification: ${updateResponse.error}`
344
+ );
345
+ }
346
+ return "updated";
347
+ }
348
+ }
305
349
  const response = await this.slack.chat.postMessage(args);
306
350
  if (!response.ok) {
307
351
  throw new Error(`Failed to send notification: ${response.error}`);
308
352
  }
353
+ if (origin && scope && response.ts && this.db) {
354
+ await this.saveTimestamp(origin, scope, channel, response.ts);
355
+ }
356
+ return "sent";
357
+ }
358
+ async getStoredTimestamp(origin, scope, channel) {
359
+ try {
360
+ const row = await this.db("slack_message_timestamps").where({ origin, scope, channel }).first();
361
+ return row?.ts;
362
+ } catch (error) {
363
+ this.logger.warn("Failed to look up stored Slack message timestamp", {
364
+ origin,
365
+ scope,
366
+ channel,
367
+ error
368
+ });
369
+ return void 0;
370
+ }
371
+ }
372
+ async saveTimestamp(origin, scope, channel, ts) {
373
+ try {
374
+ const now = this.db.fn.now();
375
+ await this.db("slack_message_timestamps").insert({ origin, scope, channel, ts, created_at: now }).onConflict(["origin", "scope", "channel"]).merge({ ts, created_at: now });
376
+ } catch (error) {
377
+ this.logger.warn("Failed to persist Slack message timestamp", {
378
+ origin,
379
+ scope,
380
+ channel,
381
+ error
382
+ });
383
+ }
309
384
  }
310
385
  static parseBroadcastRoute(route) {
311
386
  const channelValue = route.getOptional("channel");
@@ -1 +1 @@
1
- {"version":3,"file":"SlackNotificationProcessor.cjs.js","sources":["../../src/lib/SlackNotificationProcessor.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { AuthService, LoggerService } from '@backstage/backend-plugin-api';\nimport {\n MetricsService,\n MetricsServiceCounter,\n} from '@backstage/backend-plugin-api/alpha';\nimport {\n Entity,\n isUserEntity,\n parseEntityRef,\n stringifyEntityRef,\n UserEntity,\n} from '@backstage/catalog-model';\nimport { Config, readDurationFromConfig } from '@backstage/config';\nimport { NotFoundError, toError } from '@backstage/errors';\nimport { Notification } from '@backstage/plugin-notifications-common';\nimport {\n NotificationProcessor,\n NotificationSendOptions,\n} from '@backstage/plugin-notifications-node';\nimport { durationToMilliseconds } from '@backstage/types';\nimport { ChatPostMessageArguments, WebClient } from '@slack/web-api';\nimport DataLoader from 'dataloader';\nimport pThrottle from 'p-throttle';\nimport { ANNOTATION_SLACK_BOT_NOTIFY } from './constants';\nimport { BroadcastRoute } from './types';\nimport { ExpiryMap, toChatPostMessageArgs } from './util';\nimport { CatalogService } from '@backstage/plugin-catalog-node';\nimport { SlackBlockKitRenderer } from '../extensions';\n\nexport class SlackNotificationProcessor implements NotificationProcessor {\n private readonly logger: LoggerService;\n private readonly catalog: CatalogService;\n private readonly auth: AuthService;\n private readonly slack: WebClient;\n private readonly sendNotifications: (\n opts: ChatPostMessageArguments[],\n ) => Promise<void>;\n private readonly messagesSent: MetricsServiceCounter;\n private readonly messagesFailed: MetricsServiceCounter;\n private readonly broadcastChannels?: string[];\n private readonly broadcastRoutes?: BroadcastRoute[];\n private readonly entityLoader: DataLoader<string, Entity | undefined>;\n private readonly username?: string;\n private readonly concurrencyLimit: number;\n private readonly throttleInterval: number;\n private readonly blockKitRenderer?: SlackBlockKitRenderer;\n\n static fromConfig(\n config: Config,\n options: {\n auth: AuthService;\n logger: LoggerService;\n catalog: CatalogService;\n metrics: MetricsService;\n slack?: WebClient;\n broadcastChannels?: string[];\n blockKitRenderer?: SlackBlockKitRenderer;\n },\n ): SlackNotificationProcessor[] {\n const slackConfig =\n config.getOptionalConfigArray('notifications.processors.slack') ?? [];\n return slackConfig.map(c => {\n const token = c.getString('token');\n const slack = options.slack ?? new WebClient(token);\n const broadcastChannels = c.getOptionalStringArray('broadcastChannels');\n const username = c.getOptionalString('username');\n const broadcastRoutesConfig = c.getOptionalConfigArray('broadcastRoutes');\n const broadcastRoutes = broadcastRoutesConfig?.map(route =>\n this.parseBroadcastRoute(route),\n );\n const concurrencyLimit = c.getOptionalNumber('concurrencyLimit') ?? 10;\n const throttleInterval = c.has('throttleInterval')\n ? durationToMilliseconds(\n readDurationFromConfig(c, { key: 'throttleInterval' }),\n )\n : durationToMilliseconds({ minutes: 1 });\n return new SlackNotificationProcessor({\n slack,\n broadcastChannels,\n broadcastRoutes,\n username,\n concurrencyLimit,\n throttleInterval,\n ...options,\n });\n });\n }\n\n private constructor(options: {\n slack: WebClient;\n auth: AuthService;\n logger: LoggerService;\n catalog: CatalogService;\n metrics: MetricsService;\n broadcastChannels?: string[];\n broadcastRoutes?: BroadcastRoute[];\n username?: string;\n concurrencyLimit?: number;\n throttleInterval?: number;\n blockKitRenderer?: SlackBlockKitRenderer;\n }) {\n const {\n auth,\n catalog,\n logger,\n metrics,\n slack,\n broadcastChannels,\n broadcastRoutes,\n username,\n concurrencyLimit,\n throttleInterval,\n blockKitRenderer,\n } = options;\n this.logger = logger;\n this.catalog = catalog;\n this.auth = auth;\n this.slack = slack;\n this.broadcastChannels = broadcastChannels;\n this.broadcastRoutes = broadcastRoutes;\n this.username = username;\n this.concurrencyLimit = concurrencyLimit ?? 10;\n this.throttleInterval =\n throttleInterval ?? durationToMilliseconds({ minutes: 1 });\n this.blockKitRenderer = blockKitRenderer;\n\n this.entityLoader = new DataLoader<string, Entity | undefined>(\n async entityRefs => {\n return await this.catalog\n .getEntitiesByRefs(\n {\n entityRefs: entityRefs.slice(),\n fields: [\n `kind`,\n `spec.profile.email`,\n `metadata.annotations.${ANNOTATION_SLACK_BOT_NOTIFY}`,\n ],\n },\n { credentials: await this.auth.getOwnServiceCredentials() },\n )\n .then(r => r.items);\n },\n {\n name: 'SlackNotificationProcessor.entityLoader',\n cacheMap: new ExpiryMap(durationToMilliseconds({ minutes: 10 })),\n maxBatchSize: 100,\n batchScheduleFn: cb =>\n setTimeout(cb, durationToMilliseconds({ milliseconds: 10 })),\n },\n );\n\n this.messagesSent = metrics.createCounter(\n 'notifications.processors.slack.sent.count',\n {\n description: 'Number of messages sent to Slack successfully',\n unit: '{message}',\n },\n );\n this.messagesFailed = metrics.createCounter(\n 'notifications.processors.slack.error.count',\n {\n description: 'Number of messages that failed to send to Slack',\n unit: '{message}',\n },\n );\n\n const throttle = pThrottle({\n limit: this.concurrencyLimit,\n interval: this.throttleInterval,\n });\n const throttled = throttle((opts: ChatPostMessageArguments) =>\n this.sendNotification(opts),\n );\n this.sendNotifications = async (opts: ChatPostMessageArguments[]) => {\n const results = await Promise.allSettled(\n opts.map(message => throttled(message)),\n );\n\n let successCount = 0;\n let failureCount = 0;\n\n results.forEach((result, index) => {\n if (result.status === 'fulfilled') {\n successCount++;\n } else {\n this.logger.error(\n `Failed to send Slack channel notification to ${opts[index].channel}: ${result.reason.message}`,\n );\n failureCount++;\n }\n });\n\n this.messagesSent.add(successCount);\n this.messagesFailed.add(failureCount);\n };\n }\n\n getName(): string {\n return 'SlackNotificationProcessor';\n }\n\n async processOptions(\n options: NotificationSendOptions,\n ): Promise<NotificationSendOptions> {\n if (options.recipients.type !== 'entity') {\n return options;\n }\n\n const entityRefs = [options.recipients.entityRef].flat();\n\n const outbound: ChatPostMessageArguments[] = [];\n await Promise.all(\n entityRefs.map(async entityRef => {\n const compoundEntityRef = parseEntityRef(entityRef);\n // skip users as they are sent direct messages\n if (compoundEntityRef.kind === 'user') {\n return;\n }\n\n let channel;\n try {\n channel = await this.getSlackNotificationTarget(entityRef);\n } catch (error) {\n this.logger.error(\n `Failed to get Slack channel for entity: ${toError(error).message}`,\n );\n return;\n }\n\n if (!channel) {\n this.logger.debug(`No Slack channel found for entity: ${entityRef}`);\n return;\n }\n\n this.logger.debug(\n `Sending notification with payload: ${JSON.stringify(\n options.payload,\n )}`,\n );\n\n const payload = toChatPostMessageArgs({\n channel,\n payload: options.payload,\n username: this.username,\n blockKitRenderer: this.blockKitRenderer,\n });\n\n this.logger.debug(\n `Sending Slack channel notification: ${JSON.stringify(payload)}`,\n );\n outbound.push(payload);\n }),\n );\n\n await this.sendNotifications(outbound);\n\n return options;\n }\n\n async postProcess(\n notification: Notification,\n options: NotificationSendOptions,\n ): Promise<void> {\n const destinations: string[] = [];\n\n // Handle broadcast case\n if (notification.user === null) {\n const routedChannels = this.getBroadcastDestinations(notification);\n destinations.push(...routedChannels);\n } else if (options.recipients.type === 'entity') {\n // Handle user-specific notification\n const entityRefs = [options.recipients.entityRef].flat();\n const explicitUserEntityRefs = entityRefs\n .filter(entityRef => parseEntityRef(entityRef).kind === 'user')\n .map(entityRef => stringifyEntityRef(parseEntityRef(entityRef)));\n const normalizedUserRef = stringifyEntityRef(\n parseEntityRef(notification.user),\n );\n\n if (!explicitUserEntityRefs.includes(normalizedUserRef)) {\n // This user was resolved from a non-user entity. Skip sending a DM.\n return;\n }\n\n const destination = await this.getSlackNotificationTarget(\n notification.user,\n );\n\n if (!destination) {\n this.logger.error(\n `No slack.com/bot-notify annotation found for user: ${notification.user}`,\n );\n return;\n }\n\n destinations.push(destination);\n }\n\n // If no destinations, nothing to do\n if (destinations.length === 0) {\n return;\n }\n\n // Prepare outbound messages\n const formattedPayload = await this.formatPayloadDescriptionForSlack(\n options.payload,\n );\n const outbound = destinations.map(channel =>\n toChatPostMessageArgs({\n channel,\n payload: formattedPayload,\n username: this.username,\n blockKitRenderer: this.blockKitRenderer,\n }),\n );\n\n // Log debug info\n outbound.forEach(payload => {\n this.logger.debug(`Sending notification: ${JSON.stringify(payload)}`);\n });\n\n // Send notifications\n await this.sendNotifications(outbound);\n }\n\n private async formatPayloadDescriptionForSlack(\n payload: Notification['payload'],\n ) {\n return {\n ...payload,\n description: await this.replaceUserRefsWithSlackIds(payload.description),\n };\n }\n\n async replaceUserRefsWithSlackIds(\n text?: string,\n ): Promise<string | undefined> {\n if (!text) return undefined;\n\n // Match user entity refs like \"<@user:default/billy>\"\n const userRefRegex = /<@(user:[^>]+)>/gi;\n const matches = [...text.matchAll(userRefRegex)];\n\n if (matches.length === 0) return text;\n\n const uniqueUserRefs = new Set(\n matches.map(match => match[1].toLowerCase()),\n );\n\n const slackIdMap = new Map<string, string>();\n\n await Promise.all(\n [...uniqueUserRefs].map(async userRef => {\n try {\n const slackId = await this.getSlackNotificationTarget(userRef);\n if (slackId) {\n slackIdMap.set(userRef, `<@${slackId}>`);\n }\n } catch (error) {\n this.logger.warn(\n `Failed to resolve Slack ID for user ref \"${userRef}\": ${error}`,\n );\n }\n }),\n );\n\n return text.replace(userRefRegex, (match, userRef) => {\n const slackId = slackIdMap.get(userRef.toLowerCase());\n return slackId ?? match;\n });\n }\n\n async getSlackNotificationTarget(\n entityRef: string,\n ): Promise<string | undefined> {\n const entity = await this.entityLoader.load(entityRef);\n if (!entity) {\n throw new NotFoundError(`Entity not found: ${entityRef}`);\n }\n\n const slackId = await this.resolveSlackId(entity);\n return slackId;\n }\n\n private async resolveSlackId(entity: Entity): Promise<string | undefined> {\n // First try to get Slack ID from annotations\n const slackId = entity.metadata?.annotations?.[ANNOTATION_SLACK_BOT_NOTIFY];\n if (slackId) {\n return slackId;\n }\n\n // If no Slack ID in annotations and entity is a User, try to find by email\n if (isUserEntity(entity)) {\n return this.findSlackIdByEmail(entity);\n }\n\n return undefined;\n }\n\n private async findSlackIdByEmail(\n entity: UserEntity,\n ): Promise<string | undefined> {\n const email = entity.spec?.profile?.email;\n if (!email) {\n return undefined;\n }\n\n try {\n const user = await this.slack.users.lookupByEmail({ email });\n return user.user?.id;\n } catch (error) {\n this.logger.warn(\n `Failed to lookup Slack user by email ${email}: ${error}`,\n );\n return undefined;\n }\n }\n\n async sendNotification(args: ChatPostMessageArguments): Promise<void> {\n const response = await this.slack.chat.postMessage(args);\n\n if (!response.ok) {\n throw new Error(`Failed to send notification: ${response.error}`);\n }\n }\n\n private static parseBroadcastRoute(route: Config): BroadcastRoute {\n const channelValue = route.getOptional('channel');\n let channels: string[];\n\n if (typeof channelValue === 'string') {\n channels = [channelValue];\n } else if (Array.isArray(channelValue)) {\n channels = channelValue as string[];\n } else {\n throw new Error(\n 'broadcastRoutes entry must have a channel property (string or string[])',\n );\n }\n\n return {\n origin: route.getOptionalString('origin'),\n topic: route.getOptionalString('topic'),\n channels,\n };\n }\n\n /**\n * Gets the destination channels for a broadcast notification based on\n * configured routes. Routes are matched by origin and/or topic.\n *\n * Matching precedence:\n * 1. Routes with both origin AND topic matching (most specific)\n * 2. Routes with only origin matching\n * 3. Routes with only topic matching\n * 4. Default broadcastChannels (least specific fallback)\n *\n * The first matching route wins within each precedence level.\n */\n private getBroadcastDestinations(notification: Notification): string[] {\n const { origin } = notification;\n const { topic } = notification.payload;\n\n if (!this.broadcastRoutes || this.broadcastRoutes.length === 0) {\n // Fall back to legacy broadcastChannels config\n return this.broadcastChannels ?? [];\n }\n\n // Find most specific match\n // Priority 1: origin AND topic match\n const originAndTopicMatch = this.broadcastRoutes.find(\n route =>\n route.origin !== undefined &&\n route.topic !== undefined &&\n route.origin === origin &&\n route.topic === topic,\n );\n\n if (originAndTopicMatch) {\n return originAndTopicMatch.channels;\n }\n\n // Priority 2: origin-only match (no topic specified in route)\n const originOnlyMatch = this.broadcastRoutes.find(\n route =>\n route.origin !== undefined &&\n route.topic === undefined &&\n route.origin === origin,\n );\n\n if (originOnlyMatch) {\n return originOnlyMatch.channels;\n }\n\n // Priority 3: topic-only match (no origin specified in route)\n const topicOnlyMatch = this.broadcastRoutes.find(\n route =>\n route.topic !== undefined &&\n route.origin === undefined &&\n route.topic === topic,\n );\n\n if (topicOnlyMatch) {\n return topicOnlyMatch.channels;\n }\n\n // No match found, fall back to legacy broadcastChannels\n return this.broadcastChannels ?? [];\n }\n}\n"],"names":["config","WebClient","durationToMilliseconds","readDurationFromConfig","DataLoader","ANNOTATION_SLACK_BOT_NOTIFY","ExpiryMap","pThrottle","parseEntityRef","toError","toChatPostMessageArgs","stringifyEntityRef","NotFoundError","isUserEntity"],"mappings":";;;;;;;;;;;;;;;;;AA6CO,MAAM,0BAAA,CAA4D;AAAA,EACtD,MAAA;AAAA,EACA,OAAA;AAAA,EACA,IAAA;AAAA,EACA,KAAA;AAAA,EACA,iBAAA;AAAA,EAGA,YAAA;AAAA,EACA,cAAA;AAAA,EACA,iBAAA;AAAA,EACA,eAAA;AAAA,EACA,YAAA;AAAA,EACA,QAAA;AAAA,EACA,gBAAA;AAAA,EACA,gBAAA;AAAA,EACA,gBAAA;AAAA,EAEjB,OAAO,UAAA,CACLA,QAAA,EACA,OAAA,EAS8B;AAC9B,IAAA,MAAM,WAAA,GACJA,QAAA,CAAO,sBAAA,CAAuB,gCAAgC,KAAK,EAAC;AACtE,IAAA,OAAO,WAAA,CAAY,IAAI,CAAA,CAAA,KAAK;AAC1B,MAAA,MAAM,KAAA,GAAQ,CAAA,CAAE,SAAA,CAAU,OAAO,CAAA;AACjC,MAAA,MAAM,KAAA,GAAQ,OAAA,CAAQ,KAAA,IAAS,IAAIC,iBAAU,KAAK,CAAA;AAClD,MAAA,MAAM,iBAAA,GAAoB,CAAA,CAAE,sBAAA,CAAuB,mBAAmB,CAAA;AACtE,MAAA,MAAM,QAAA,GAAW,CAAA,CAAE,iBAAA,CAAkB,UAAU,CAAA;AAC/C,MAAA,MAAM,qBAAA,GAAwB,CAAA,CAAE,sBAAA,CAAuB,iBAAiB,CAAA;AACxE,MAAA,MAAM,kBAAkB,qBAAA,EAAuB,GAAA;AAAA,QAAI,CAAA,KAAA,KACjD,IAAA,CAAK,mBAAA,CAA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GA,IAAA,MAAM,cAAA,GAAiB,KAAK,eAAA,CAAgB,IAAA;AAAA,MAC1C,CAAA,KAAA,KACE,MAAM,KAAA,KAAU,MAAA,IAChB,MAAM,MAAA,KAAW,MAAA,IACjB,MAAM,KAAA,KAAU;AAAA,KACpB;AAEA,IAAA,IAAI,cAAA,EAAgB;AAClB,MAAA,OAAO,cAAA,CAAe,QAAA;AAAA,IACxB;AAGA,IAAA,OAAO,IAAA,CAAK,qBAAqB,EAAC;AAAA,EACpC;AACF;;;;"}
1
+ {"version":3,"file":"SlackNotificationProcessor.cjs.js","sources":["../../src/lib/SlackNotificationProcessor.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { AuthService, LoggerService } from '@backstage/backend-plugin-api';\nimport {\n MetricsService,\n MetricsServiceCounter,\n} from '@backstage/backend-plugin-api/alpha';\nimport {\n Entity,\n isUserEntity,\n parseEntityRef,\n stringifyEntityRef,\n UserEntity,\n} from '@backstage/catalog-model';\nimport { Config, readDurationFromConfig } from '@backstage/config';\nimport { NotFoundError, toError } from '@backstage/errors';\nimport { Notification } from '@backstage/plugin-notifications-common';\nimport {\n NotificationProcessor,\n NotificationSendOptions,\n} from '@backstage/plugin-notifications-node';\nimport { durationToMilliseconds } from '@backstage/types';\nimport {\n ChatPostMessageArguments,\n ChatUpdateArguments,\n WebClient,\n} from '@slack/web-api';\nimport DataLoader from 'dataloader';\nimport { Knex } from 'knex';\nimport pThrottle from 'p-throttle';\nimport { ANNOTATION_SLACK_BOT_NOTIFY } from './constants';\nimport { BroadcastRoute } from './types';\nimport { ExpiryMap, toChatPostMessageArgs } from './util';\nimport { CatalogService } from '@backstage/plugin-catalog-node';\nimport { SlackBlockKitRenderer } from '../extensions';\n\ninterface ScopeContext {\n origin: string;\n scope?: string;\n isUpdate?: boolean;\n}\n\nexport class SlackNotificationProcessor implements NotificationProcessor {\n private readonly logger: LoggerService;\n private readonly catalog: CatalogService;\n private readonly auth: AuthService;\n private readonly slack: WebClient;\n private readonly sendNotifications: (\n opts: ChatPostMessageArguments[],\n scopeContext?: ScopeContext,\n ) => Promise<void>;\n private readonly messagesSent: MetricsServiceCounter;\n private readonly messagesFailed: MetricsServiceCounter;\n private readonly messagesUpdated: MetricsServiceCounter;\n private db?: Knex;\n private readonly broadcastChannels?: string[];\n private readonly broadcastRoutes?: BroadcastRoute[];\n private readonly entityLoader: DataLoader<string, Entity | undefined>;\n private readonly username?: string;\n private readonly concurrencyLimit: number;\n private readonly throttleInterval: number;\n private readonly blockKitRenderer?: SlackBlockKitRenderer;\n\n static fromConfig(\n config: Config,\n options: {\n auth: AuthService;\n logger: LoggerService;\n catalog: CatalogService;\n metrics: MetricsService;\n slack?: WebClient;\n broadcastChannels?: string[];\n blockKitRenderer?: SlackBlockKitRenderer;\n },\n ): SlackNotificationProcessor[] {\n const slackConfig =\n config.getOptionalConfigArray('notifications.processors.slack') ?? [];\n return slackConfig.map(c => {\n const token = c.getString('token');\n const slack = options.slack ?? new WebClient(token);\n const broadcastChannels = c.getOptionalStringArray('broadcastChannels');\n const username = c.getOptionalString('username');\n const broadcastRoutesConfig = c.getOptionalConfigArray('broadcastRoutes');\n const broadcastRoutes = broadcastRoutesConfig?.map(route =>\n this.parseBroadcastRoute(route),\n );\n const concurrencyLimit = c.getOptionalNumber('concurrencyLimit') ?? 10;\n const throttleInterval = c.has('throttleInterval')\n ? durationToMilliseconds(\n readDurationFromConfig(c, { key: 'throttleInterval' }),\n )\n : durationToMilliseconds({ minutes: 1 });\n return new SlackNotificationProcessor({\n slack,\n broadcastChannels,\n broadcastRoutes,\n username,\n concurrencyLimit,\n throttleInterval,\n ...options,\n });\n });\n }\n\n private constructor(options: {\n slack: WebClient;\n auth: AuthService;\n logger: LoggerService;\n catalog: CatalogService;\n metrics: MetricsService;\n broadcastChannels?: string[];\n broadcastRoutes?: BroadcastRoute[];\n username?: string;\n concurrencyLimit?: number;\n throttleInterval?: number;\n blockKitRenderer?: SlackBlockKitRenderer;\n }) {\n const {\n auth,\n catalog,\n logger,\n metrics,\n slack,\n broadcastChannels,\n broadcastRoutes,\n username,\n concurrencyLimit,\n throttleInterval,\n blockKitRenderer,\n } = options;\n this.logger = logger;\n this.catalog = catalog;\n this.auth = auth;\n this.slack = slack;\n this.broadcastChannels = broadcastChannels;\n this.broadcastRoutes = broadcastRoutes;\n this.username = username;\n this.concurrencyLimit = concurrencyLimit ?? 10;\n this.throttleInterval =\n throttleInterval ?? durationToMilliseconds({ minutes: 1 });\n this.blockKitRenderer = blockKitRenderer;\n\n this.entityLoader = new DataLoader<string, Entity | undefined>(\n async entityRefs => {\n return await this.catalog\n .getEntitiesByRefs(\n {\n entityRefs: entityRefs.slice(),\n fields: [\n `kind`,\n `spec.profile.email`,\n `metadata.annotations.${ANNOTATION_SLACK_BOT_NOTIFY}`,\n ],\n },\n { credentials: await this.auth.getOwnServiceCredentials() },\n )\n .then(r => r.items);\n },\n {\n name: 'SlackNotificationProcessor.entityLoader',\n cacheMap: new ExpiryMap(durationToMilliseconds({ minutes: 10 })),\n maxBatchSize: 100,\n batchScheduleFn: cb =>\n setTimeout(cb, durationToMilliseconds({ milliseconds: 10 })),\n },\n );\n\n this.messagesSent = metrics.createCounter(\n 'notifications.processors.slack.sent.count',\n {\n description: 'Number of messages sent to Slack successfully',\n unit: '{message}',\n },\n );\n this.messagesFailed = metrics.createCounter(\n 'notifications.processors.slack.error.count',\n {\n description: 'Number of messages that failed to send to Slack',\n unit: '{message}',\n },\n );\n this.messagesUpdated = metrics.createCounter(\n 'notifications.processors.slack.update.count',\n {\n description:\n 'Number of existing Slack messages updated via scope matching',\n unit: '{message}',\n },\n );\n\n const throttle = pThrottle({\n limit: this.concurrencyLimit,\n interval: this.throttleInterval,\n });\n const throttled = throttle(\n (opts: ChatPostMessageArguments, ctx?: ScopeContext) =>\n this.sendNotification(opts, ctx),\n );\n this.sendNotifications = async (\n opts: ChatPostMessageArguments[],\n scopeContext?: ScopeContext,\n ) => {\n const results = await Promise.allSettled(\n opts.map(message => throttled(message, scopeContext)),\n );\n\n let sentCount = 0;\n let updateCount = 0;\n let failureCount = 0;\n\n results.forEach((result, index) => {\n if (result.status === 'fulfilled') {\n if (result.value === 'updated') {\n updateCount++;\n } else {\n sentCount++;\n }\n } else {\n this.logger.error(\n `Failed to send Slack channel notification to ${opts[index].channel}: ${result.reason.message}`,\n );\n failureCount++;\n }\n });\n\n this.messagesSent.add(sentCount);\n this.messagesUpdated.add(updateCount);\n this.messagesFailed.add(failureCount);\n };\n }\n\n setDatabase(db: Knex): void {\n this.db = db;\n }\n\n getName(): string {\n return 'SlackNotificationProcessor';\n }\n\n async processOptions(\n options: NotificationSendOptions,\n ): Promise<NotificationSendOptions> {\n if (options.recipients.type !== 'entity') {\n return options;\n }\n\n const entityRefs = [options.recipients.entityRef].flat();\n\n const outbound: ChatPostMessageArguments[] = [];\n await Promise.all(\n entityRefs.map(async entityRef => {\n const compoundEntityRef = parseEntityRef(entityRef);\n // skip users as they are sent direct messages\n if (compoundEntityRef.kind === 'user') {\n return;\n }\n\n let channel;\n try {\n channel = await this.getSlackNotificationTarget(entityRef);\n } catch (error) {\n this.logger.error(\n `Failed to get Slack channel for entity: ${toError(error).message}`,\n );\n return;\n }\n\n if (!channel) {\n this.logger.debug(`No Slack channel found for entity: ${entityRef}`);\n return;\n }\n\n this.logger.debug(\n `Sending notification with payload: ${JSON.stringify(\n options.payload,\n )}`,\n );\n\n const payload = toChatPostMessageArgs({\n channel,\n payload: options.payload,\n username: this.username,\n blockKitRenderer: this.blockKitRenderer,\n });\n\n this.logger.debug(\n `Sending Slack channel notification: ${JSON.stringify(payload)}`,\n );\n outbound.push(payload);\n }),\n );\n\n await this.sendNotifications(outbound);\n\n return options;\n }\n\n async postProcess(\n notification: Notification,\n options: NotificationSendOptions,\n ): Promise<void> {\n const destinations: string[] = [];\n\n // Handle broadcast case\n if (notification.user === null) {\n const routedChannels = this.getBroadcastDestinations(notification);\n destinations.push(...routedChannels);\n } else if (options.recipients.type === 'entity') {\n // Handle user-specific notification\n const entityRefs = [options.recipients.entityRef].flat();\n const explicitUserEntityRefs = entityRefs\n .filter(entityRef => parseEntityRef(entityRef).kind === 'user')\n .map(entityRef => stringifyEntityRef(parseEntityRef(entityRef)));\n const normalizedUserRef = stringifyEntityRef(\n parseEntityRef(notification.user),\n );\n\n if (!explicitUserEntityRefs.includes(normalizedUserRef)) {\n // This user was resolved from a non-user entity. Skip sending a DM.\n return;\n }\n\n const destination = await this.getSlackNotificationTarget(\n notification.user,\n );\n\n if (!destination) {\n this.logger.error(\n `No slack.com/bot-notify annotation found for user: ${notification.user}`,\n );\n return;\n }\n\n destinations.push(destination);\n }\n\n // If no destinations, nothing to do\n if (destinations.length === 0) {\n return;\n }\n\n // Prepare outbound messages\n const formattedPayload = await this.formatPayloadDescriptionForSlack(\n options.payload,\n );\n const outbound = destinations.map(channel =>\n toChatPostMessageArgs({\n channel,\n payload: formattedPayload,\n username: this.username,\n blockKitRenderer: this.blockKitRenderer,\n }),\n );\n\n // Log debug info\n outbound.forEach(payload => {\n this.logger.debug(`Sending notification: ${JSON.stringify(payload)}`);\n });\n\n await this.sendNotifications(outbound, {\n origin: notification.origin,\n scope: notification.payload.scope,\n isUpdate: !!notification.updated,\n });\n }\n\n private async formatPayloadDescriptionForSlack(\n payload: Notification['payload'],\n ) {\n return {\n ...payload,\n description: await this.replaceUserRefsWithSlackIds(payload.description),\n };\n }\n\n async replaceUserRefsWithSlackIds(\n text?: string,\n ): Promise<string | undefined> {\n if (!text) return undefined;\n\n // Match user entity refs like \"<@user:default/billy>\"\n const userRefRegex = /<@(user:[^>]+)>/gi;\n const matches = [...text.matchAll(userRefRegex)];\n\n if (matches.length === 0) return text;\n\n const uniqueUserRefs = new Set(\n matches.map(match => match[1].toLowerCase()),\n );\n\n const slackIdMap = new Map<string, string>();\n\n await Promise.all(\n [...uniqueUserRefs].map(async userRef => {\n try {\n const slackId = await this.getSlackNotificationTarget(userRef);\n if (slackId) {\n slackIdMap.set(userRef, `<@${slackId}>`);\n }\n } catch (error) {\n this.logger.warn(\n `Failed to resolve Slack ID for user ref \"${userRef}\": ${error}`,\n );\n }\n }),\n );\n\n return text.replace(userRefRegex, (match, userRef) => {\n const slackId = slackIdMap.get(userRef.toLowerCase());\n return slackId ?? match;\n });\n }\n\n async getSlackNotificationTarget(\n entityRef: string,\n ): Promise<string | undefined> {\n const entity = await this.entityLoader.load(entityRef);\n if (!entity) {\n throw new NotFoundError(`Entity not found: ${entityRef}`);\n }\n\n const slackId = await this.resolveSlackId(entity);\n return slackId;\n }\n\n private async resolveSlackId(entity: Entity): Promise<string | undefined> {\n // First try to get Slack ID from annotations\n const slackId = entity.metadata?.annotations?.[ANNOTATION_SLACK_BOT_NOTIFY];\n if (slackId) {\n return slackId;\n }\n\n // If no Slack ID in annotations and entity is a User, try to find by email\n if (isUserEntity(entity)) {\n return this.findSlackIdByEmail(entity);\n }\n\n return undefined;\n }\n\n private async findSlackIdByEmail(\n entity: UserEntity,\n ): Promise<string | undefined> {\n const email = entity.spec?.profile?.email;\n if (!email) {\n return undefined;\n }\n\n try {\n const user = await this.slack.users.lookupByEmail({ email });\n return user.user?.id;\n } catch (error) {\n this.logger.warn(\n `Failed to lookup Slack user by email ${email}: ${error}`,\n );\n return undefined;\n }\n }\n\n async sendNotification(\n args: ChatPostMessageArguments,\n scopeContext?: ScopeContext,\n ): Promise<'sent' | 'updated'> {\n const channel = args.channel as string;\n const scope = scopeContext?.scope;\n\n // If this is a scoped update, try to update the existing Slack message.\n const origin = scopeContext?.origin;\n if (scopeContext?.isUpdate && origin && scope && this.db) {\n const storedTs = await this.getStoredTimestamp(origin, scope, channel);\n if (storedTs) {\n const updateArgs = {\n channel,\n ts: storedTs,\n ...('text' in args ? { text: args.text } : {}),\n ...('blocks' in args ? { blocks: args.blocks } : {}),\n ...('attachments' in args ? { attachments: args.attachments } : {}),\n } as ChatUpdateArguments;\n const updateResponse = await this.slack.chat.update(updateArgs);\n\n if (!updateResponse.ok) {\n throw new Error(\n `Failed to update notification: ${updateResponse.error}`,\n );\n }\n\n return 'updated';\n }\n }\n\n // Send a new message.\n const response = await this.slack.chat.postMessage(args);\n\n if (!response.ok) {\n throw new Error(`Failed to send notification: ${response.error}`);\n }\n\n // Persist the message timestamp for future scope-based updates.\n if (origin && scope && response.ts && this.db) {\n await this.saveTimestamp(origin, scope, channel, response.ts);\n }\n\n return 'sent';\n }\n\n private async getStoredTimestamp(\n origin: string,\n scope: string,\n channel: string,\n ): Promise<string | undefined> {\n try {\n const row = await this.db!('slack_message_timestamps')\n .where({ origin, scope, channel })\n .first();\n return row?.ts;\n } catch (error) {\n this.logger.warn('Failed to look up stored Slack message timestamp', {\n origin,\n scope,\n channel,\n error,\n });\n return undefined;\n }\n }\n\n private async saveTimestamp(\n origin: string,\n scope: string,\n channel: string,\n ts: string,\n ): Promise<void> {\n try {\n const now = this.db!.fn.now();\n await this.db!('slack_message_timestamps')\n .insert({ origin, scope, channel, ts, created_at: now })\n .onConflict(['origin', 'scope', 'channel'])\n .merge({ ts, created_at: now });\n } catch (error) {\n this.logger.warn('Failed to persist Slack message timestamp', {\n origin,\n scope,\n channel,\n error,\n });\n }\n }\n\n private static parseBroadcastRoute(route: Config): BroadcastRoute {\n const channelValue = route.getOptional('channel');\n let channels: string[];\n\n if (typeof channelValue === 'string') {\n channels = [channelValue];\n } else if (Array.isArray(channelValue)) {\n channels = channelValue as string[];\n } else {\n throw new Error(\n 'broadcastRoutes entry must have a channel property (string or string[])',\n );\n }\n\n return {\n origin: route.getOptionalString('origin'),\n topic: route.getOptionalString('topic'),\n channels,\n };\n }\n\n /**\n * Gets the destination channels for a broadcast notification based on\n * configured routes. Routes are matched by origin and/or topic.\n *\n * Matching precedence:\n * 1. Routes with both origin AND topic matching (most specific)\n * 2. Routes with only origin matching\n * 3. Routes with only topic matching\n * 4. Default broadcastChannels (least specific fallback)\n *\n * The first matching route wins within each precedence level.\n */\n private getBroadcastDestinations(notification: Notification): string[] {\n const { origin } = notification;\n const { topic } = notification.payload;\n\n if (!this.broadcastRoutes || this.broadcastRoutes.length === 0) {\n // Fall back to legacy broadcastChannels config\n return this.broadcastChannels ?? [];\n }\n\n // Find most specific match\n // Priority 1: origin AND topic match\n const originAndTopicMatch = this.broadcastRoutes.find(\n route =>\n route.origin !== undefined &&\n route.topic !== undefined &&\n route.origin === origin &&\n route.topic === topic,\n );\n\n if (originAndTopicMatch) {\n return originAndTopicMatch.channels;\n }\n\n // Priority 2: origin-only match (no topic specified in route)\n const originOnlyMatch = this.broadcastRoutes.find(\n route =>\n route.origin !== undefined &&\n route.topic === undefined &&\n route.origin === origin,\n );\n\n if (originOnlyMatch) {\n return originOnlyMatch.channels;\n }\n\n // Priority 3: topic-only match (no origin specified in route)\n const topicOnlyMatch = this.broadcastRoutes.find(\n route =>\n route.topic !== undefined &&\n route.origin === undefined &&\n route.topic === topic,\n );\n\n if (topicOnlyMatch) {\n return topicOnlyMatch.channels;\n }\n\n // No match found, fall back to legacy broadcastChannels\n return this.broadcastChannels ?? [];\n }\n}\n"],"names":["config","WebClient","durationToMilliseconds","readDurationFromConfig","DataLoader","ANNOTATION_SLACK_BOT_NOTIFY","ExpiryMap","pThrottle","parseEntityRef","toError","toChatPostMessageArgs","stringifyEntityRef","NotFoundError","isUserEntity"],"mappings":";;;;;;;;;;;;;;;;;AAwDO,MAAM,0BAAA,CAA4D;AAAA,EACtD,MAAA;AAAA,EACA,OAAA;AAAA,EACA,IAAA;AAAA,EACA,KAAA;AAAA,EACA,iBAAA;AAAA,EAIA,YAAA;AAAA,EACA,cAAA;AAAA,E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ACzB;AAGA,IAAA,MAAM,cAAA,GAAiB,KAAK,eAAA,CAAgB,IAAA;AAAA,MAC1C,CAAA,KAAA,KACE,MAAM,KAAA,KAAU,MAAA,IAChB,MAAM,MAAA,KAAW,MAAA,IACjB,MAAM,KAAA,KAAU;AAAA,KACpB;AAEA,IAAA,IAAI,cAAA,EAAgB;AAClB,MAAA,OAAO,cAAA,CAAe,QAAA;AAAA,IACxB;AAGA,IAAA,OAAO,IAAA,CAAK,qBAAqB,EAAC;AAAA,EACpC;AACF;;;;"}
@@ -7,6 +7,20 @@ var SlackNotificationProcessor = require('./lib/SlackNotificationProcessor.cjs.j
7
7
  var pluginCatalogNode = require('@backstage/plugin-catalog-node');
8
8
  var extensions = require('./extensions.cjs.js');
9
9
 
10
+ const MIGRATIONS_DIR = backendPluginApi.resolvePackagePath(
11
+ "@backstage/plugin-notifications-backend-module-slack",
12
+ "migrations"
13
+ );
14
+ const DB_MIGRATIONS_TABLE = "notifications_module_slack__knex_migrations";
15
+ const CLEANUP_RETENTION_SECONDS = 24 * 60 * 60;
16
+ function nowMinus(knex, seconds) {
17
+ if (knex.client.config.client.includes("sqlite3")) {
18
+ return knex.raw(`datetime('now', ?)`, [`-${seconds} seconds`]);
19
+ } else if (knex.client.config.client.includes("mysql")) {
20
+ return knex.raw(`now() - interval ${seconds} second`);
21
+ }
22
+ return knex.raw(`now() - interval '${seconds} seconds'`);
23
+ }
10
24
  const notificationsModuleSlack = backendPluginApi.createBackendModule({
11
25
  pluginId: "notifications",
12
26
  moduleId: "slack",
@@ -27,18 +41,58 @@ const notificationsModuleSlack = backendPluginApi.createBackendModule({
27
41
  logger: backendPluginApi.coreServices.logger,
28
42
  catalog: pluginCatalogNode.catalogServiceRef,
29
43
  notifications: pluginNotificationsNode.notificationsProcessingExtensionPoint,
30
- metrics: alpha.metricsServiceRef
44
+ metrics: alpha.metricsServiceRef,
45
+ database: backendPluginApi.coreServices.database,
46
+ scheduler: backendPluginApi.coreServices.scheduler
31
47
  },
32
- async init({ auth, config, logger, catalog, notifications, metrics }) {
33
- notifications.addProcessor(
34
- SlackNotificationProcessor.SlackNotificationProcessor.fromConfig(config, {
35
- auth,
36
- logger,
37
- catalog,
38
- metrics,
39
- blockKitRenderer
40
- })
41
- );
48
+ async init({
49
+ auth,
50
+ config,
51
+ logger,
52
+ catalog,
53
+ notifications,
54
+ metrics,
55
+ database,
56
+ scheduler
57
+ }) {
58
+ const processors = SlackNotificationProcessor.SlackNotificationProcessor.fromConfig(config, {
59
+ auth,
60
+ logger,
61
+ catalog,
62
+ metrics,
63
+ blockKitRenderer
64
+ });
65
+ if (processors.length === 0) {
66
+ return;
67
+ }
68
+ const db = await database.getClient();
69
+ if (!database.migrations?.skip) {
70
+ await db.migrate.latest({
71
+ directory: MIGRATIONS_DIR,
72
+ tableName: DB_MIGRATIONS_TABLE
73
+ });
74
+ }
75
+ for (const processor of processors) {
76
+ processor.setDatabase(db);
77
+ }
78
+ notifications.addProcessor(processors);
79
+ await scheduler.scheduleTask({
80
+ id: "slack-message-timestamps-cleanup",
81
+ frequency: { hours: 24 },
82
+ timeout: { minutes: 5 },
83
+ initialDelay: { hours: 2 },
84
+ scope: "global",
85
+ fn: async () => {
86
+ const deleted = await db("slack_message_timestamps").where(
87
+ "created_at",
88
+ "<=",
89
+ nowMinus(db, CLEANUP_RETENTION_SECONDS)
90
+ ).delete();
91
+ logger.info("Cleaned up old Slack message timestamps", {
92
+ deleted
93
+ });
94
+ }
95
+ });
42
96
  }
43
97
  });
44
98
  }
@@ -1 +1 @@
1
- {"version":3,"file":"module.cjs.js","sources":["../src/module.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport {\n coreServices,\n createBackendModule,\n} from '@backstage/backend-plugin-api';\nimport { metricsServiceRef } from '@backstage/backend-plugin-api/alpha';\nimport { notificationsProcessingExtensionPoint } from '@backstage/plugin-notifications-node';\nimport { SlackNotificationProcessor } from './lib/SlackNotificationProcessor';\nimport { catalogServiceRef } from '@backstage/plugin-catalog-node';\nimport {\n notificationsSlackBlockKitExtensionPoint,\n SlackBlockKitRenderer,\n} from './extensions';\n\n/**\n * The Slack notification processor for use with the notifications plugin.\n * This allows sending of notifications via Slack DMs or to channels.\n *\n * @public\n */\nexport const notificationsModuleSlack = createBackendModule({\n pluginId: 'notifications',\n moduleId: 'slack',\n register(reg) {\n let blockKitRenderer: SlackBlockKitRenderer | undefined;\n reg.registerExtensionPoint(notificationsSlackBlockKitExtensionPoint, {\n setBlockKitRenderer(renderer) {\n if (blockKitRenderer) {\n throw new Error(`Slack block kit renderer was already registered`);\n }\n blockKitRenderer = renderer;\n },\n });\n\n reg.registerInit({\n deps: {\n auth: coreServices.auth,\n config: coreServices.rootConfig,\n logger: coreServices.logger,\n catalog: catalogServiceRef,\n notifications: notificationsProcessingExtensionPoint,\n metrics: metricsServiceRef,\n },\n async init({ auth, config, logger, catalog, notifications, metrics }) {\n notifications.addProcessor(\n SlackNotificationProcessor.fromConfig(config, {\n auth,\n logger,\n catalog,\n metrics,\n blockKitRenderer,\n }),\n );\n },\n });\n },\n});\n"],"names":["createBackendModule","notificationsSlackBlockKitExtensionPoint","coreServices","catalogServiceRef","notificationsProcessingExtensionPoint","metricsServiceRef","SlackNotificationProcessor"],"mappings":";;;;;;;;;AAkCO,MAAM,2BAA2BA,oCAAA,CAAoB;AAAA,EAC1D,QAAA,EAAU,eAAA;AAAA,EACV,QAAA,EAAU,OAAA;AAAA,EACV,SAAS,GAAA,EAAK;AACZ,IAAA,IAAI,gBAAA;AACJ,IAAA,GAAA,CAAI,uBAAuBC,mDAAA,EAA0C;AAAA,MACnE,oBAAoB,QAAA,EAAU;AAC5B,QAAA,IAAI,gBAAA,EAAkB;AACpB,UAAA,MAAM,IAAI,MAAM,CAAA,+CAAA,CAAiD,CAAA;AAAA,QACnE;AACA,QAAA,gBAAA,GAAmB,QAAA;AAAA,MACrB;AAAA,KACD,CAAA;AAED,IAAA,GAAA,CAAI,YAAA,CAAa;AAAA,MACf,IAAA,EAAM;AAAA,QACJ,MAAMC,6BAAA,CAAa,IAAA;AAAA,QACnB,QAAQA,6BAAA,CAAa,UAAA;AAAA,QACrB,QAAQA,6BAAA,CAAa,MAAA;AAAA,QACrB,OAAA,EAASC,mCAAA;AAAA,QACT,aAAA,EAAeC,6DAAA;AAAA,QACf,OAAA,EAASC;AAAA,OACX;AAAA,MACA,MAAM,KAAK,EAAE,IAAA,EAAM,QAAQ,MAAA,EAAQ,OAAA,EAAS,aAAA,EAAe,OAAA,EAAQ,EAAG;AACpE,QAAA,aAAA,CAAc,YAAA;AAAA,UACZC,qDAAA,CAA2B,WAAW,MAAA,EAAQ;AAAA,YAC5C,IAAA;AAAA,YACA,MAAA;AAAA,YACA,OAAA;AAAA,YACA,OAAA;AAAA,YACA;AAAA,WACD;AAAA,SACH;AAAA,MACF;AAAA,KACD,CAAA;AAAA,EACH;AACF,CAAC;;;;"}
1
+ {"version":3,"file":"module.cjs.js","sources":["../src/module.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport {\n coreServices,\n createBackendModule,\n resolvePackagePath,\n} from '@backstage/backend-plugin-api';\nimport { Knex } from 'knex';\nimport { metricsServiceRef } from '@backstage/backend-plugin-api/alpha';\nimport { notificationsProcessingExtensionPoint } from '@backstage/plugin-notifications-node';\nimport { SlackNotificationProcessor } from './lib/SlackNotificationProcessor';\nimport { catalogServiceRef } from '@backstage/plugin-catalog-node';\nimport {\n notificationsSlackBlockKitExtensionPoint,\n SlackBlockKitRenderer,\n} from './extensions';\n\nconst MIGRATIONS_DIR = resolvePackagePath(\n '@backstage/plugin-notifications-backend-module-slack',\n 'migrations',\n);\n\nconst DB_MIGRATIONS_TABLE = 'notifications_module_slack__knex_migrations';\nconst CLEANUP_RETENTION_SECONDS = 24 * 60 * 60; // 24 hours\n\nfunction nowMinus(knex: Knex, seconds: number): Knex.Raw {\n if (knex.client.config.client.includes('sqlite3')) {\n return knex.raw(`datetime('now', ?)`, [`-${seconds} seconds`]);\n } else if (knex.client.config.client.includes('mysql')) {\n return knex.raw(`now() - interval ${seconds} second`);\n }\n return knex.raw(`now() - interval '${seconds} seconds'`);\n}\n\n/**\n * The Slack notification processor for use with the notifications plugin.\n * This allows sending of notifications via Slack DMs or to channels.\n *\n * @public\n */\nexport const notificationsModuleSlack = createBackendModule({\n pluginId: 'notifications',\n moduleId: 'slack',\n register(reg) {\n let blockKitRenderer: SlackBlockKitRenderer | undefined;\n reg.registerExtensionPoint(notificationsSlackBlockKitExtensionPoint, {\n setBlockKitRenderer(renderer) {\n if (blockKitRenderer) {\n throw new Error(`Slack block kit renderer was already registered`);\n }\n blockKitRenderer = renderer;\n },\n });\n\n reg.registerInit({\n deps: {\n auth: coreServices.auth,\n config: coreServices.rootConfig,\n logger: coreServices.logger,\n catalog: catalogServiceRef,\n notifications: notificationsProcessingExtensionPoint,\n metrics: metricsServiceRef,\n database: coreServices.database,\n scheduler: coreServices.scheduler,\n },\n async init({\n auth,\n config,\n logger,\n catalog,\n notifications,\n metrics,\n database,\n scheduler,\n }) {\n const processors = SlackNotificationProcessor.fromConfig(config, {\n auth,\n logger,\n catalog,\n metrics,\n blockKitRenderer,\n });\n\n if (processors.length === 0) {\n return;\n }\n\n const db = await database.getClient();\n\n if (!database.migrations?.skip) {\n await db.migrate.latest({\n directory: MIGRATIONS_DIR,\n tableName: DB_MIGRATIONS_TABLE,\n });\n }\n\n // Attach the DB to each processor now that migrations have run.\n for (const processor of processors) {\n processor.setDatabase(db);\n }\n\n notifications.addProcessor(processors);\n\n // Clean up old message timestamp records daily. These records are only\n // needed for the short window between initial send and scope-based\n // update (typically minutes), so a 24-hour retention is sufficient.\n await scheduler.scheduleTask({\n id: 'slack-message-timestamps-cleanup',\n frequency: { hours: 24 },\n timeout: { minutes: 5 },\n initialDelay: { hours: 2 },\n scope: 'global',\n fn: async () => {\n const deleted = await db('slack_message_timestamps')\n .where(\n 'created_at',\n '<=',\n nowMinus(db, CLEANUP_RETENTION_SECONDS),\n )\n .delete();\n logger.info('Cleaned up old Slack message timestamps', {\n deleted,\n });\n },\n });\n },\n });\n },\n});\n"],"names":["resolvePackagePath","createBackendModule","notificationsSlackBlockKitExtensionPoint","coreServices","catalogServiceRef","notificationsProcessingExtensionPoint","metricsServiceRef","SlackNotificationProcessor"],"mappings":";;;;;;;;;AA8BA,MAAM,cAAA,GAAiBA,mCAAA;AAAA,EACrB,sDAAA;AAAA,EACA;AACF,CAAA;AAEA,MAAM,mBAAA,GAAsB,6CAAA;AAC5B,MAAM,yBAAA,GAA4B,KAAK,EAAA,GAAK,EAAA;AAE5C,SAAS,QAAA,CAAS,MAAY,OAAA,EAA2B;AACvD,EAAA,IAAI,KAAK,MAAA,CAAO,MAAA,CAAO,MAAA,CAAO,QAAA,CAAS,SAAS,CAAA,EAAG;AACjD,IAAA,OAAO,KAAK,GAAA,CAAI,CAAA,kBAAA,CAAA,EAAsB,CAAC,CAAA,CAAA,EAAI,OAAO,UAAU,CAAC,CAAA;AAAA,EAC/D,WAAW,IAAA,CAAK,MAAA,CAAO,OAAO,MAAA,CAAO,QAAA,CAAS,OAAO,CAAA,EAAG;AACtD,IAAA,OAAO,IAAA,C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;AAAA,UACA;AAAA,SACD,CAAA;AAED,QAAA,IAAI,UAAA,CAAW,WAAW,CAAA,EAAG;AAC3B,UAAA;AAAA,QACF;AAEA,QAAA,MAAM,EAAA,GAAK,MAAM,QAAA,CAAS,SAAA,EAAU;AAEpC,QAAA,IAAI,CAAC,QAAA,CAAS,UAAA,EAAY,IAAA,EAAM;AAC9B,UAAA,MAAM,EAAA,CAAG,QAAQ,MAAA,CAAO;AAAA,YACtB,SAAA,EAAW,cAAA;AAAA,YACX,SAAA,EAAW;AAAA,WACZ,CAAA;AAAA,QACH;AAGA,QAAA,KAAA,MAAW,aAAa,UAAA,EAAY;AAClC,UAAA,SAAA,CAAU,YAAY,EAAE,CAAA;AAAA,QAC1B;AAEA,QAAA,aAAA,CAAc,aAAa,UAAU,CAAA;AAKrC,QAAA,MAAM,UAAU,YAAA,CAAa;AAAA,UAC3B,EAAA,EAAI,kCAAA;AAAA,UACJ,SAAA,EAAW,EAAE,KAAA,EAAO,EAAA,EAAG;AAAA,UACvB,OAAA,EAAS,EAAE,OAAA,EAAS,CAAA,EAAE;AAAA,UACtB,YAAA,EAAc,EAAE,KAAA,EAAO,CAAA,EAAE;AAAA,UACzB,KAAA,EAAO,QAAA;AAAA,UACP,IAAI,YAAY;AACd,YAAA,MAAM,OAAA,GAAU,MAAM,EAAA,CAAG,0BAA0B,CAAA,CAChD,KAAA;AAAA,cACC,YAAA;AAAA,cACA,IAAA;AAAA,cACA,QAAA,CAAS,IAAI,yBAAyB;AAAA,cAEvC,MAAA,EAAO;AACV,YAAA,MAAA,CAAO,KAAK,yCAAA,EAA2C;AAAA,cACrD;AAAA,aACD,CAAA;AAAA,UACH;AAAA,SACD,CAAA;AAAA,MACH;AAAA,KACD,CAAA;AAAA,EACH;AACF,CAAC;;;;"}
@@ -0,0 +1,44 @@
1
+ // @ts-check
2
+ /*
3
+ * Copyright 2025 The Backstage Authors
4
+ *
5
+ * Licensed under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License.
7
+ * You may obtain a copy of the License at
8
+ *
9
+ * http://www.apache.org/licenses/LICENSE-2.0
10
+ *
11
+ * Unless required by applicable law or agreed to in writing, software
12
+ * distributed under the License is distributed on an "AS IS" BASIS,
13
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ * See the License for the specific language governing permissions and
15
+ * limitations under the License.
16
+ */
17
+
18
+ /**
19
+ * @param {import('knex').Knex} knex
20
+ */
21
+ exports.up = async function up(knex) {
22
+ await knex.schema.createTable(
23
+ 'slack_message_timestamps',
24
+ function tableSetup(table) {
25
+ table.string('origin', 256).notNullable();
26
+ table.string('scope', 512).notNullable();
27
+ table.string('channel', 64).notNullable();
28
+ table.string('ts', 64).notNullable();
29
+ table
30
+ .timestamp('created_at', { useTz: true })
31
+ .defaultTo(knex.fn.now())
32
+ .notNullable();
33
+ table.primary(['origin', 'scope', 'channel']);
34
+ table.index('created_at', 'idx_slack_message_timestamps_created_at');
35
+ },
36
+ );
37
+ };
38
+
39
+ /**
40
+ * @param {import('knex').Knex} knex
41
+ */
42
+ exports.down = async function down(knex) {
43
+ await knex.schema.dropTable('slack_message_timestamps');
44
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backstage/plugin-notifications-backend-module-slack",
3
- "version": "0.4.1",
3
+ "version": "0.4.2-next.0",
4
4
  "description": "The slack backend module for the notifications plugin.",
5
5
  "backstage": {
6
6
  "role": "backend-plugin-module",
@@ -25,7 +25,8 @@
25
25
  "types": "dist/index.d.ts",
26
26
  "files": [
27
27
  "dist",
28
- "config.d.ts"
28
+ "config.d.ts",
29
+ "migrations"
29
30
  ],
30
31
  "scripts": {
31
32
  "build": "backstage-cli package build",
@@ -37,25 +38,25 @@
37
38
  "test": "backstage-cli package test"
38
39
  },
39
40
  "dependencies": {
40
- "@backstage/backend-plugin-api": "^1.9.0",
41
- "@backstage/catalog-model": "^1.8.0",
42
- "@backstage/config": "^1.3.7",
43
- "@backstage/errors": "^1.3.0",
44
- "@backstage/plugin-catalog-node": "^2.2.0",
45
- "@backstage/plugin-notifications-common": "^0.2.2",
46
- "@backstage/plugin-notifications-node": "^0.2.25",
47
- "@backstage/types": "^1.2.2",
41
+ "@backstage/backend-plugin-api": "1.9.1-next.0",
42
+ "@backstage/catalog-model": "1.8.1-next.0",
43
+ "@backstage/config": "1.3.8-next.0",
44
+ "@backstage/errors": "1.3.1-next.0",
45
+ "@backstage/plugin-catalog-node": "2.2.1-next.0",
46
+ "@backstage/plugin-notifications-common": "0.2.3-next.0",
47
+ "@backstage/plugin-notifications-node": "0.2.26-next.0",
48
+ "@backstage/types": "1.2.2",
48
49
  "@slack/bolt": "^3.21.4",
49
50
  "@slack/types": "^2.14.0",
50
51
  "@slack/web-api": "^7.5.0",
51
52
  "dataloader": "^2.0.0",
53
+ "knex": "^3.0.0",
52
54
  "p-throttle": "^4.1.1"
53
55
  },
54
56
  "devDependencies": {
55
- "@backstage/backend-test-utils": "^1.11.2",
56
- "@backstage/cli": "^0.36.1",
57
- "@backstage/plugin-catalog-node": "^2.2.0",
58
- "@backstage/test-utils": "^1.7.17",
57
+ "@backstage/backend-test-utils": "1.11.3-next.0",
58
+ "@backstage/cli": "0.36.2-next.0",
59
+ "@backstage/test-utils": "1.7.18-next.0",
59
60
  "@faker-js/faker": "^10.0.0",
60
61
  "msw": "^2.0.0"
61
62
  },