diff --git a/docs/commands/scan/report.md b/docs/commands/scan/report.md index e52471ced..87aee6ca8 100644 --- a/docs/commands/scan/report.md +++ b/docs/commands/scan/report.md @@ -35,9 +35,9 @@ or policy-level filtering is applied. ## Slack CI notifications -The scan creation report flow can send an opt-in Slack summary after it evaluates -the report. Store the webhook URL in your CI secret store and expose the JSON -configuration only to trusted jobs: +The scan creation and diff report flows can send an opt-in Slack summary after +they evaluate the report. Store the webhook URL in your CI secret store and +expose the JSON configuration only to trusted jobs: ```sh export SOCKET_SLACK_CONFIG_JSON='{"url":"https://hooks.slack.com/services/..."}' @@ -90,16 +90,16 @@ printing the token or changing the scan decision. ## Jira CI tasks -The scan creation report flow can create one Jira Task per report with findings. -Reruns for the same repository, branch, pull request, and commit update the -existing task. If CI cannot provide a commit SHA, reruns use the stable -repository, branch, and pull request identity instead. Store the API token in -your CI secret store and enable Jira only for trusted jobs: +The scan creation and diff report flows can create one Jira Task per report with +findings. Reruns for the same repository, branch, pull request, and commit +update the existing task. If CI cannot provide a commit SHA, reruns use the +stable repository, branch, and pull request identity instead. Store the API +token in your CI secret store and enable Jira only for trusted jobs: ```sh export SOCKET_JIRA_ENABLED=true # Set SOCKET_JIRA_CONFIG_JSON from your CI secret store. -socket ci +socket scan create --report ``` Store a JSON value like this in `SOCKET_JIRA_CONFIG_JSON`: @@ -112,3 +112,19 @@ Store a JSON value like this in `SOCKET_JIRA_CONFIG_JSON`: email, an API token, and a Jira project key. Notifications include the Socket report link, package, alert, source, and manifest details. Jira lookup or delivery failures are reported as warnings and do not change the scan result. + +## Managed integrations and per-run notifications + +Socket managed organization notifications track alert lifecycle events such as +alert creation, changes, and clearing. Managed Slack webhooks are sent +immediately, managed email is batched, and managed Jira creates tickets for +alert lifecycle changes. The CI notifications above instead summarize findings +from an individual CLI scan report: Slack can send that run to incoming webhooks +or bot channels, and Jira creates or updates one task for the repository, +branch, pull request, and commit. + +These delivery paths can overlap. A default-branch CI scan may report a finding +that also causes a managed alert notification. Enable the per-run settings only +for CI jobs that need scan-level summaries, and configure the Jira and Slack +credentials in trusted CI secret storage. Managed notification settings remain +configured in Socket organization integrations. diff --git a/src/commands/scan/notifications/dispatch.mts b/src/commands/scan/notifications/dispatch.mts index 6a8501f80..d469132aa 100644 --- a/src/commands/scan/notifications/dispatch.mts +++ b/src/commands/scan/notifications/dispatch.mts @@ -1,13 +1,12 @@ +import { logger } from '../../../utils/logger.mts' import { isJiraEnabled, sendJiraTaskNotification } from './jira.mts' +import type { NotificationReport } from './report.mts' import { sendSlackBotNotifications } from './slack-bot.mts' import { getSlackNotificationConfig, - isSlackNotificationConfigured, + isSlackWebhookConfigured, sendSlackWebhookNotifications, } from './slack-webhook.mts' -import { logger } from '../../../utils/logger.mts' - -import type { NotificationReport } from './report.mts' type NotificationProvider = { name: string @@ -15,27 +14,30 @@ type NotificationProvider = { send: (report: NotificationReport) => Promise } -const notificationProviders: NotificationProvider[] = [ - { - name: 'Jira', - isEnabled: isJiraEnabled, - send: sendJiraTaskNotification, - }, - { - name: 'Slack', - isEnabled: isSlackNotificationConfigured, - send: async report => { - const config = getSlackNotificationConfig() ?? null - await sendSlackWebhookNotifications(report, config) - await sendSlackBotNotifications(report, config) - }, - }, -] - /** Deliver reports to each independently configured notification provider. */ export async function dispatchScanNotifications( report: NotificationReport, ): Promise { + const config = getSlackNotificationConfig() ?? null + const notificationProviders: NotificationProvider[] = [ + { + name: 'Jira', + isEnabled: isJiraEnabled, + send: sendJiraTaskNotification, + }, + { + name: 'Slack webhook', + isEnabled: isSlackWebhookConfigured, + send: report => sendSlackWebhookNotifications(report, config), + }, + { + name: 'Slack bot', + // biome-ignore lint/complexity/useLiteralKeys: process.env uses an index signature. + isEnabled: () => Boolean(process.env['SOCKET_SLACK_BOT_TOKEN']), + send: report => sendSlackBotNotifications(report, config), + }, + ] + for (const provider of notificationProviders) { if (!provider.isEnabled()) { continue diff --git a/src/commands/scan/notifications/dispatch.test.mts b/src/commands/scan/notifications/dispatch.test.mts new file mode 100644 index 000000000..5b7f237dc --- /dev/null +++ b/src/commands/scan/notifications/dispatch.test.mts @@ -0,0 +1,82 @@ +import { afterEach, describe, expect, it, vi } from 'vitest' + +import type { NotificationReport } from './report.mts' + +const { + sendJiraTaskNotification, + sendSlackWebhookNotifications, + sendSlackBotNotifications, +} = vi.hoisted(() => ({ + sendJiraTaskNotification: vi.fn(), + sendSlackWebhookNotifications: vi.fn(), + sendSlackBotNotifications: vi.fn(), +})) + +vi.mock('./jira.mts', () => ({ + isJiraEnabled: () => true, + sendJiraTaskNotification, +})) +vi.mock('./slack-webhook.mts', () => ({ + getSlackNotificationConfig: () => ({}), + isSlackWebhookConfigured: () => true, + sendSlackWebhookNotifications, +})) +vi.mock('./slack-bot.mts', () => ({ sendSlackBotNotifications })) +vi.mock('../../../utils/logger.mts', () => ({ + logger: { warn: vi.fn() }, +})) + +import { dispatchScanNotifications } from './dispatch.mts' + +const report: NotificationReport = { + kind: 'diff', + diffScanId: 'diff-42', + scanId: 'scan-42', + repository: 'example/service', + branch: 'feature/fix', + pullRequest: 42, + commitSha: '1234567890abcdef', + scanUrl: 'https://socket.dev/dashboard/org/example/diff/base/scan-42', + decision: 'fail', + summary: '1 finding (1 blocking, 0 warning)', + reachabilityEnabled: true, + reachabilitySummary: '1 reachable', + findings: [], + reachabilityFindings: [ + { + packageName: 'left-pad', + ecosystem: 'npm', + version: '1.0.0', + alertType: 'cve', + severity: 'high', + action: 'error', + advisoryId: 'GHSA-1234', + purl: 'pkg:npm/left-pad@1.0.0', + url: 'https://socket.dev/npm/package/left-pad/overview/1.0.0', + reachability: 'reachable', + }, + ], +} + +afterEach(() => { + vi.unstubAllEnvs() + vi.clearAllMocks() +}) + +describe('scan notification dispatch', () => { + it('sends the same report to Jira and both Slack transports despite a provider failure', async () => { + vi.stubEnv('SOCKET_SLACK_BOT_TOKEN', 'xoxb-test-token') + sendJiraTaskNotification.mockRejectedValueOnce( + new Error('Jira unavailable'), + ) + sendSlackWebhookNotifications.mockRejectedValueOnce( + new Error('Slack webhook unavailable'), + ) + + await expect(dispatchScanNotifications(report)).resolves.toBeUndefined() + + expect(sendJiraTaskNotification).toHaveBeenCalledWith(report) + expect(sendSlackWebhookNotifications).toHaveBeenCalledWith(report, {}) + expect(sendSlackBotNotifications).toHaveBeenCalledWith(report, {}) + }) +})