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.
57 lines
1.4 KiB
JavaScript
57 lines
1.4 KiB
JavaScript
/**
|
|
* Architecture mapping and cryptographic checksum verification.
|
|
*/
|
|
|
|
const fs = require('node:fs');
|
|
const crypto = require('node:crypto');
|
|
|
|
function mapArch(unameArch) {
|
|
const normalized = (unameArch || '').trim().toLowerCase();
|
|
switch (normalized) {
|
|
case 'x86_64':
|
|
case 'amd64':
|
|
return '64bit';
|
|
case 'aarch64':
|
|
case 'arm64':
|
|
return 'ARM64';
|
|
default:
|
|
throw new Error(`Unsupported architecture: ${unameArch}`);
|
|
}
|
|
}
|
|
|
|
function parseChecksums(checksumsContent) {
|
|
const map = new Map();
|
|
if (!checksumsContent) return map;
|
|
|
|
const lines = checksumsContent.split(/\r?\n/);
|
|
for (const line of lines) {
|
|
const trimmed = line.trim();
|
|
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
// Format: <hash> <filename>
|
|
const match = trimmed.match(/^([a-fA-F0-9]{64})\s+(.+)$/);
|
|
if (match) {
|
|
const hash = match[1].toLowerCase();
|
|
const filename = match[2].trim();
|
|
map.set(filename, hash);
|
|
}
|
|
}
|
|
return map;
|
|
}
|
|
|
|
function verifyFileSha256(filePath, expectedHash) {
|
|
if (!fs.existsSync(filePath) || !expectedHash) return false;
|
|
try {
|
|
const fileBuffer = fs.readFileSync(filePath);
|
|
const actualHash = crypto.createHash('sha256').update(fileBuffer).digest('hex');
|
|
return actualHash.toLowerCase() === expectedHash.toLowerCase();
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
mapArch,
|
|
parseChecksums,
|
|
verifyFileSha256,
|
|
};
|