|
| 1 | +import { isJiraEnabled, sendJiraTaskNotification } from './jira.mts' |
| 2 | +import { sendSlackBotNotifications } from './slack-bot.mts' |
| 3 | +import { |
| 4 | + getSlackNotificationConfig, |
| 5 | + isSlackNotificationConfigured, |
| 6 | + sendSlackWebhookNotifications, |
| 7 | +} from './slack-webhook.mts' |
| 8 | +import { logger } from '../../../utils/logger.mts' |
| 9 | + |
| 10 | +import type { NotificationReport } from './report.mts' |
| 11 | + |
| 12 | +type NotificationProvider = { |
| 13 | + name: string |
| 14 | + isEnabled: () => boolean |
| 15 | + send: (report: NotificationReport) => Promise<void> |
| 16 | +} |
| 17 | + |
| 18 | +const notificationProviders: NotificationProvider[] = [ |
| 19 | + { |
| 20 | + name: 'Jira', |
| 21 | + isEnabled: isJiraEnabled, |
| 22 | + send: sendJiraTaskNotification, |
| 23 | + }, |
| 24 | + { |
| 25 | + name: 'Slack', |
| 26 | + isEnabled: isSlackNotificationConfigured, |
| 27 | + send: async report => { |
| 28 | + const config = getSlackNotificationConfig() ?? null |
| 29 | + await sendSlackWebhookNotifications(report, config) |
| 30 | + await sendSlackBotNotifications(report, config) |
| 31 | + }, |
| 32 | + }, |
| 33 | +] |
| 34 | + |
| 35 | +/** Deliver reports to each independently configured notification provider. */ |
| 36 | +export async function dispatchScanNotifications( |
| 37 | + report: NotificationReport, |
| 38 | +): Promise<void> { |
| 39 | + for (const provider of notificationProviders) { |
| 40 | + if (!provider.isEnabled()) { |
| 41 | + continue |
| 42 | + } |
| 43 | + try { |
| 44 | + // eslint-disable-next-line no-await-in-loop -- Providers are dispatched independently in order. |
| 45 | + await provider.send(report) |
| 46 | + } catch { |
| 47 | + logger.warn( |
| 48 | + `${provider.name} notification delivery failed; scan policy is unchanged.`, |
| 49 | + ) |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments