Skip to content

Commit 03cfeb3

Browse files
committed
feat(python-cli-parity): send scan notifications to Slack webhooks
1 parent 549c635 commit 03cfeb3

13 files changed

Lines changed: 806 additions & 3 deletions

‎.gitignore‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,10 @@
891891
!/src/commands/ci/publish-gitlab-commit-status.test.mts
892892
!/src/commands/scan/diff-scan-verdict.mts
893893
!/src/commands/scan/diff-scan-verdict.test.mts
894+
!/src/commands/scan/notifications/
895+
!/src/commands/scan/notifications/report.mts
896+
!/src/commands/scan/notifications/slack-webhook.mts
897+
!/src/commands/scan/notifications/slack-webhook.test.mts
894898
!/src/commands/ci/publish-gitlab-mr-notes.mts
895899
!/src/commands/ci/publish-gitlab-mr-notes.test.mts
896900
!/src/commands/ci/publish-pr-comments.mts

‎docs/commands/scan/report.md‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,37 @@ socket-dependency-scan:
3232
The report uses GitLab Dependency Scanning schema version `15.0.0`. Findings
3333
are generated from the scan's package and alert data before CLI report folding
3434
or policy-level filtering is applied.
35+
36+
## Slack CI notifications
37+
38+
The scan creation report flow can send an opt-in Slack summary after it evaluates
39+
the report. Store the webhook URL in your CI secret store and expose the JSON
40+
configuration only to trusted jobs:
41+
42+
```sh
43+
export SOCKET_SLACK_CONFIG_JSON='{"url":"https://hooks.slack.com/services/..."}'
44+
export SOCKET_SLACK_LEVELS='block,warn'
45+
socket scan create --report
46+
```
47+
48+
For multiple destinations, use named URLs and configure each destination's
49+
filters:
50+
51+
```json
52+
{
53+
"url": [
54+
{"name":"security","url":"https://hooks.slack.com/services/..."}
55+
],
56+
"url_configs": {
57+
"security": {
58+
"repos": ["api"],
59+
"alert_types": ["cve"],
60+
"severities": ["critical", "high"]
61+
}
62+
}
63+
}
64+
```
65+
66+
`alert_types` takes precedence over `severities`, matching the Python CLI.
67+
Delivery errors are reported as warnings and do not change the scan decision.
68+
Existing report viewing commands do not send notifications.

‎src/commands/scan/handle-create-new-scan.mts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ import {
4343
import { buildToolsFor } from '../manifest/scripts/build-tool.mts'
4444

4545
import type { ReachabilityFallback } from './output-create-new-scan.mts'
46-
import type { PythonDiffJsonContext } from './python-diff-json.mts'
4746
import type { ReachabilityOptions } from './perform-reachability-analysis.mts'
47+
import type { PythonDiffJsonContext } from './python-diff-json.mts'
4848
import type { REPORT_LEVEL } from './types.mts'
4949
import type { OutputKind } from '../../types.mts'
5050
import type { FactsOnlyTypes } from '../../utils/coana.mts'
@@ -604,6 +604,7 @@ async function createNewScan(
604604
outputKind,
605605
pythonJsonFormat,
606606
pythonJsonContext,
607+
notificationContext: { repository: repoName, branch: branchName },
607608
})
608609
} else {
609610
await handleScanReport({
@@ -615,6 +616,7 @@ async function createNewScan(
615616
reportLevel,
616617
scanId,
617618
short: false,
619+
notificationContext: { repository: repoName, branch: branchName },
618620
})
619621
}
620622
} else {

‎src/commands/scan/handle-diff-scan.mts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ export async function handleDiffScan({
4545
export async function handlePrDiffScan({
4646
id1,
4747
id2,
48+
notificationContext,
4849
onDiffReport,
4950
orgSlug,
5051
outputKind,
@@ -67,6 +68,9 @@ export async function handlePrDiffScan({
6768
outputKind: OutputKind
6869
pythonJsonFormat?: boolean | undefined
6970
pythonJsonContext?: PythonDiffJsonContext | undefined
71+
notificationContext?:
72+
| { repository?: string | undefined; branch?: string | undefined }
73+
| undefined
7074
}): Promise<void> {
7175
const result = await fetchDiffScan({ id1, id2, orgSlug })
7276
await outputDiffScan(result, {
@@ -76,6 +80,7 @@ export async function handlePrDiffScan({
7680
strictBlocking: false,
7781
pythonJsonFormat,
7882
pythonJsonContext,
83+
notificationContext: result.ok ? notificationContext : undefined,
7984
})
8085
if (result.ok) {
8186
await onDiffReport?.(

‎src/commands/scan/handle-scan-report.mts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { fetchScanData } from './fetch-report-data.mts'
22
import { outputScanReport } from './output-scan-report.mts'
33

4+
import type { NotificationMetadata } from './notifications/report.mts'
45
import type { FOLD_SETTING, REPORT_LEVEL } from './types.mts'
56
import type { OutputKind } from '../../types.mts'
67

78
export type HandleScanReportConfig = {
89
orgSlug: string
10+
notificationContext?: NotificationMetadata | undefined
911
scanId: string
1012
includeLicensePolicy: boolean
1113
outputKind: OutputKind
@@ -23,6 +25,7 @@ export async function handleScanReport({
2325
fold,
2426
gitlabSecurityFile,
2527
includeLicensePolicy,
28+
notificationContext,
2629
orgSlug,
2730
outputKind,
2831
reportLevel,
@@ -42,6 +45,7 @@ export async function handleScanReport({
4245
outputKind,
4346
reportLevel,
4447
short,
48+
notificationContext,
4549
enableGitlabSecurity: enableGitlabSecurity ?? false,
4650
gitlabSecurityFile:
4751
gitlabSecurityFile ?? 'gl-dependency-scanning-report.json',
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import type { SocketArtifact } from '../../../utils/alert/artifact.mts'
2+
import type { PythonDiffJson } from '../python-diff-json.mts'
3+
4+
export type NotificationFinding = {
5+
packageName: string
6+
ecosystem: string
7+
version: string
8+
alertType: string
9+
severity: string
10+
action: string
11+
url?: string | undefined
12+
advisoryId?: string | undefined
13+
purl?: string | undefined
14+
introducedBy?: Array<[string, string]> | undefined
15+
manifests?: string | undefined
16+
}
17+
18+
export type NotificationReport = {
19+
kind: 'diff' | 'scan'
20+
scanId?: string | undefined
21+
diffScanId?: string | undefined
22+
repository?: string | undefined
23+
branch?: string | undefined
24+
scanUrl?: string | undefined
25+
decision: 'pass' | 'fail' | 'unknown' | 'not-evaluated'
26+
summary: string
27+
findings: NotificationFinding[]
28+
}
29+
30+
export type NotificationMetadata = {
31+
repository?: string | undefined
32+
branch?: string | undefined
33+
}
34+
35+
export type NotificationContext = NotificationMetadata & {
36+
decision: NotificationReport['decision']
37+
}
38+
39+
function advisoryId(props: Record<string, unknown>): string | undefined {
40+
for (const value of [props['ghsaId'], props['cveId']]) {
41+
const ids = (Array.isArray(value) ? value : [value]).filter(
42+
(id): id is string => typeof id === 'string' && Boolean(id.trim()),
43+
)
44+
if (ids.length) {
45+
return ids.join(', ')
46+
}
47+
}
48+
return undefined
49+
}
50+
51+
function findingsFromArtifacts(
52+
artifacts: SocketArtifact[],
53+
): NotificationFinding[] {
54+
const findings: NotificationFinding[] = []
55+
for (const artifact of artifacts) {
56+
for (const alert of artifact.alerts ?? []) {
57+
const props = alert.props ?? {}
58+
findings.push({
59+
packageName: artifact.name ?? 'unknown',
60+
ecosystem: artifact.type ?? 'unknown',
61+
version: artifact.version ?? 'unknown',
62+
alertType: alert.type ?? 'unknown',
63+
severity: alert.severity ?? 'unknown',
64+
action: alert.action ?? 'unknown',
65+
url: typeof props['url'] === 'string' ? props['url'] : undefined,
66+
advisoryId: advisoryId(props),
67+
})
68+
}
69+
}
70+
return findings
71+
}
72+
73+
export function summarize(findings: NotificationFinding[]): string {
74+
const blocking = findings.filter(finding => finding.action === 'error').length
75+
const warning = findings.filter(finding => finding.action === 'warn').length
76+
return `${findings.length} finding${findings.length === 1 ? '' : 's'} (${blocking} blocking, ${warning} warning)`
77+
}
78+
79+
export function createScanNotificationReport(
80+
scan: SocketArtifact[],
81+
context: NotificationContext & {
82+
scanId?: string | undefined
83+
scanUrl?: string | undefined
84+
},
85+
): NotificationReport {
86+
const findings = findingsFromArtifacts(scan)
87+
return {
88+
kind: 'scan',
89+
scanId: context.scanId,
90+
repository: context.repository,
91+
branch: context.branch,
92+
scanUrl: context.scanUrl,
93+
decision: context.decision,
94+
summary: summarize(findings),
95+
findings,
96+
}
97+
}
98+
99+
export function createDiffNotificationReport(
100+
result: PythonDiffJson,
101+
context: NotificationContext,
102+
): NotificationReport {
103+
const findings: NotificationFinding[] = result.new_alerts.map(alert => ({
104+
packageName: alert.pkg_name ?? 'unknown',
105+
ecosystem: alert.pkg_type ?? 'unknown',
106+
version: alert.pkg_version ?? 'unknown',
107+
alertType: alert.type,
108+
severity: alert.severity ?? 'unknown',
109+
action: alert.action ?? 'unknown',
110+
url: alert.url ?? undefined,
111+
advisoryId: advisoryId(alert.props),
112+
purl: alert.purl ?? undefined,
113+
introducedBy: alert.introduced_by,
114+
manifests: alert.manifests,
115+
}))
116+
return {
117+
kind: 'diff',
118+
scanId: result.head_scan_id ?? undefined,
119+
diffScanId: result.diff_scan_id ?? undefined,
120+
repository: result.repo ?? context.repository,
121+
branch: result.branch ?? context.branch,
122+
scanUrl: result.diff_url ?? result.report_url ?? undefined,
123+
decision: result.decision,
124+
summary: summarize(findings),
125+
findings,
126+
}
127+
}

0 commit comments

Comments
 (0)