Public Access
generated from Philipp/template-git
Philipp/Global-Workflows: Pre-commit Lint (Scoped) / pre-commit (pull_request) Successful in 1m53s
Extract ~450 lines of complex bash from action.yaml into modular, unit-tested Node.js scripts in lib/. All logic is covered by 26 native node:test unit tests (node --test, no external dependencies). Modules added: - lib/platforms.js - platform parsing and OCI manifest auto-discovery - lib/distro.js - SPDX PURL distro detection (os_name/os_version) - lib/verifier.js - Trivy arch mapping and SHA256 verification - lib/diagnostics.js - CI terminal diagnostic formatters - lib/install-trivy.js - Trivy install/verify entrypoint (replaces bash step) - lib/extract-sboms.js - SBOM extraction entrypoint (replaces bash step) - lib/scan-sboms.js - SBOM scan + diff entrypoint (replaces bash step) Tests added (all pass, auto-discovered by ode --test): - test/platforms.test.js - test/distro.test.js - test/verifier.test.js - test/diagnostics.test.js - test/orchestration.test.js action.yaml shrinks from 554 to 107 lines. Inputs are bridged to Node scripts via INPUT_* env vars so modules are fully decoupled from the Actions runner and testable in isolation.
77 lines
2.6 KiB
JavaScript
77 lines
2.6 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const {
|
|
formatSbomOverview,
|
|
formatScanOverview,
|
|
countVulnerabilities,
|
|
} = require('../lib/diagnostics.js');
|
|
|
|
test('formatSbomOverview formats successful extraction banner', () => {
|
|
const overview = formatSbomOverview({
|
|
headImage: 'my-head:latest',
|
|
targetImage: 'my-target:latest',
|
|
headFiles: [{ name: 'sbom-amd64.spdx.json', size: 1024 }],
|
|
targetFiles: [{ name: 'sbom-amd64.spdx.json', size: 2048 }],
|
|
headError: null,
|
|
targetError: null,
|
|
});
|
|
|
|
assert.ok(overview.includes('SBOM EXTRACTION DIAGNOSTIC OVERVIEW'));
|
|
assert.ok(overview.includes('HEAD Image (my-head:latest): ✅ SUCCESS'));
|
|
assert.ok(overview.includes('TARGET Baseline Image (my-target:latest): ✅ SUCCESS'));
|
|
assert.ok(overview.includes('sbom-amd64.spdx.json (1024 bytes)'));
|
|
});
|
|
|
|
test('formatSbomOverview formats failure banner when error present', () => {
|
|
const overview = formatSbomOverview({
|
|
headImage: 'my-head:latest',
|
|
targetImage: 'my-target:latest',
|
|
headFiles: [],
|
|
targetFiles: [],
|
|
headError: 'Image not found',
|
|
targetError: null,
|
|
});
|
|
|
|
assert.ok(overview.includes('HEAD Image (my-head:latest): ❌ FAILED - Image not found'));
|
|
});
|
|
|
|
test('formatScanOverview formats scan summary with strategy', () => {
|
|
const overview = formatScanOverview({
|
|
headImage: 'debian:bookworm-slim',
|
|
targetImage: 'debian:bullseye-slim',
|
|
targetBranch: 'main',
|
|
strategy: 'Direct Trivy Image Scan',
|
|
headSummary: [' - amd64: ✅ OK (104 vulnerability findings)'],
|
|
targetSummary: [' - amd64: ✅ OK (130 vulnerability findings)'],
|
|
});
|
|
|
|
assert.ok(overview.includes('CVE VULNERABILITY SCAN DIAGNOSTIC OVERVIEW'));
|
|
assert.ok(overview.includes('Scan Strategy: Direct Trivy Image Scan'));
|
|
assert.ok(overview.includes('HEAD Image: debian:bookworm-slim'));
|
|
assert.ok(overview.includes('TARGET Baseline Image: debian:bullseye-slim (branch: \'main\')'));
|
|
assert.ok(overview.includes('104 vulnerability findings'));
|
|
});
|
|
|
|
test('countVulnerabilities counts all vulnerabilities in a Trivy JSON report', () => {
|
|
const report = {
|
|
Results: [
|
|
{
|
|
Vulnerabilities: [
|
|
{ VulnerabilityID: 'CVE-2023-0001' },
|
|
{ VulnerabilityID: 'CVE-2023-0002' },
|
|
],
|
|
},
|
|
{
|
|
Vulnerabilities: [
|
|
{ VulnerabilityID: 'CVE-2023-0003' },
|
|
],
|
|
},
|
|
],
|
|
};
|
|
|
|
assert.equal(countVulnerabilities(report), 3);
|
|
assert.equal(countVulnerabilities(JSON.stringify(report)), 3);
|
|
assert.equal(countVulnerabilities({}), 0);
|
|
assert.equal(countVulnerabilities(null), 0);
|
|
});
|