/** * Diagnostic formatting and reporting utilities. */ function formatSbomOverview({ headImage, targetImage, headFiles = [], targetFiles = [], headError = null, targetError = null, }) { const lines = [ '', '================================================================================', ' SBOM EXTRACTION DIAGNOSTIC OVERVIEW ', '================================================================================', ]; if (headError) { lines.push(`HEAD Image (${headImage}): ❌ FAILED - ${headError}`); } else { lines.push(`HEAD Image (${headImage}): ✅ SUCCESS`); for (const f of headFiles) { lines.push(` - ${f.name} (${f.size} bytes)`); } } if (targetError) { lines.push(`TARGET Baseline Image (${targetImage}): ❌ FAILED - ${targetError}`); } else { lines.push(`TARGET Baseline Image (${targetImage}): ✅ SUCCESS`); for (const f of targetFiles) { lines.push(` - ${f.name} (${f.size} bytes)`); } } lines.push('================================================================================'); lines.push(''); return lines.join('\n'); } function formatScanOverview({ headImage, targetImage, targetBranch = 'main', strategy = null, headSummary = [], targetSummary = [], }) { const lines = [ '', '================================================================================', ' CVE VULNERABILITY SCAN DIAGNOSTIC OVERVIEW ', '================================================================================', ]; if (strategy) { lines.push(`Scan Strategy: ${strategy}`); lines.push('--------------------------------------------------------------------------------'); } lines.push(`HEAD Image: ${headImage}`); for (const line of headSummary) { lines.push(line); } lines.push(''); lines.push(`TARGET Baseline Image: ${targetImage} (branch: '${targetBranch}')`); for (const line of targetSummary) { lines.push(line); } lines.push('================================================================================'); lines.push(''); return lines.join('\n'); } function countVulnerabilities(report) { if (!report) return 0; let data = report; if (typeof report === 'string' || Buffer.isBuffer(report)) { try { data = JSON.parse(report.toString('utf8')); } catch { return 0; } } if (!data || typeof data !== 'object') return 0; let count = 0; if (Array.isArray(data.Results)) { for (const res of data.Results) { if (res && Array.isArray(res.Vulnerabilities)) { count += res.Vulnerabilities.length; } } } return count; } module.exports = { formatSbomOverview, formatScanOverview, countVulnerabilities, };