import { DefaultArtifactClient } from '@actions/artifact'; import { mkdtemp, chmod, writeFile, rm } from 'node:fs/promises'; import { join } from 'node:path'; import { readReportFile, type ScanResults } from './results.ts'; import type { Inputs } from './inputs.ts'; export async function collectReports(result: ScanResults): Promise> { const reports = new Map(); for (const [name, path] of [ ['scan-manifest.json', result.paths.manifestPath], ['findings.json', result.paths.jsonPath], ['coverage.json', result.paths.coveragePath], ['exports/results.sarif', result.paths.sarifPath], ]) { if (!path) continue; const data = await readReportFile(result.paths.resultsDirectory, name); reports.set(name, data); } return reports; } export async function uploadReports(reports: Map, inputs: Inputs, tempRoot: string): Promise { if (!reports.size) throw new Error('Artifact upload requested, but no validated reports are available.'); const staging = await mkdtemp(join(tempRoot, 'codex-security-upload-')); try { await chmod(staging, 0o700); const files: string[] = []; for (const [name, bytes] of reports) { const dest = join(staging, name === 'exports/results.sarif' ? 'results.sarif' : name); await writeFile(dest, bytes, {mode: 0o600, flag: 'wx'}); files.push(dest); } const artifact = await new DefaultArtifactClient().uploadArtifact(inputs.artifactName, files, staging, {retentionDays: inputs.retentionDays, compressionLevel: 0}); return artifact.id; } finally { await rm(staging, {recursive: true, force: true}); } }