|
| 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