Files
codex-security/github-action/scripts/build.mjs
T
Michael D'Angelo 5b4a553e24 feat(action): support GitHub Enterprise Cloud on ghe.com (#1713)
Support GitHub Enterprise Cloud checkout origins and host-aware routing, share Git remote parsing between the Action and SDK, and preserve packaged modules and GitLab IPv6 authorities.

Integrate main while retaining its Action dependency patches, publication changes, and validation fixes.
2026-10-20 06:59:58 +08:00

59 lines
3.9 KiB
JavaScript

import { build } from 'esbuild';
import './apply-patches.mjs';
import { readFile, readdir, mkdir, writeFile } from 'node:fs/promises';
import { createHash } from 'node:crypto';
import { resolve, relative } from 'node:path';
const check = process.argv.includes('--check');
const root = resolve(import.meta.dirname, '..');
const patches = (await readdir(resolve(root, 'patches'))).filter(name => name.endsWith('.patch')).sort();
const hash = value => createHash('sha256').update(value).digest('hex');
const bundle = await build({
absWorkingDir: root, entryPoints: {index: 'src/entry.ts', post: 'src/post.ts'}, outdir: 'dist',
outExtension: {'.js': '.cjs'}, platform: 'node', target: 'node24', format: 'cjs',
bundle: true, write: false, sourcemap: false, legalComments: 'eof', metafile: true,
});
const generated = new Map(bundle.outputFiles.map(file => [relative(root, file.path), Buffer.from(file.contents)]));
const actionVersion = JSON.parse(await readFile(resolve(root, 'package.json'), 'utf8')).version;
const lock = JSON.parse(await readFile(resolve(root, 'package-lock.json'), 'utf8'));
const runtimeManifest = JSON.parse(await readFile(resolve(root, 'runtime/package.json'), 'utf8'));
const cliVersion = runtimeManifest.dependencies['@openai/codex-security'];
const cliLockBytes = await readFile(resolve(root, 'runtime/package-lock.json'));
const cliLock = JSON.parse(cliLockBytes);
function components(packages, namespace) {
return Object.entries(packages).filter(([path, info]) => path && !info.dev).map(([path, info]) => ({
type: 'library', 'bom-ref': `${namespace}:${path}`,
name: info.name || path.slice(path.lastIndexOf('node_modules/') + 13), version: info.version,
...(info.license && /^[A-Za-z0-9-.+]+$/.test(info.license) ? {licenses: [{license: {id: info.license}}]} : {}),
...(info.resolved ? {externalReferences: [{type: 'distribution', url: info.resolved}]} : {}),
}));
}
const sourceFiles = Object.keys(bundle.metafile.inputs).filter(path => !path.includes('node_modules/')).sort();
const sourceHash = createHash('sha256');
for (const file of sourceFiles) sourceHash.update(file).update('\0').update(await readFile(resolve(root, file))).update('\0');
const manifest = {
actionVersion, status: 'unreleased', nodeRuntime: 'node24', platform: 'linux-x64', cliVersion,
sourceSha256: sourceHash.digest('hex'), actionLockSha256: hash(await readFile(resolve(root, 'package-lock.json'))),
runtimeLockSha256: hash(cliLockBytes),
buildInputs: Object.fromEntries(await Promise.all(['../action.yml', 'package.json', 'runtime/package.json', 'scripts/build.mjs', 'scripts/apply-patches.mjs', ...patches.map(name => 'patches/' + name)].map(async path => [path, {sha256: hash(await readFile(resolve(root, path)))}]))),
files: Object.fromEntries([...generated].map(([path, bytes]) => [path, {sha256: hash(bytes), bytes: bytes.length}])),
};
const sbom = {
bomFormat: 'CycloneDX', specVersion: '1.6', version: 1,
metadata: {component: {type: 'application', name: 'codex-security-action', version: actionVersion}},
components: [...components(lock.packages, 'action'), ...components(cliLock.packages, 'cli')],
};
if (!check) await mkdir(resolve(root, 'dist'), {recursive: true});
for (const [path, bytes] of generated) {
if (check) {
const existing = await readFile(resolve(root, path)).catch(() => Buffer.alloc(0));
if (!bytes.equals(existing)) throw new Error(`${path} differs from a clean build. Run npm run build and review the generated changes.`);
} else await writeFile(resolve(root, path), bytes);
}
// Release metadata is generated for CI artifacts, not needed by the runner.
await mkdir(resolve(root, 'build'), {recursive: true});
for (const [name, value] of [['release-manifest.json', manifest], ['sbom.cdx.json', sbom]]) {
await writeFile(resolve(root, 'build', name), JSON.stringify(value, null, 2) + '\n');
}
console.log(check ? 'Distribution matches source; generated release metadata.' : 'Built action, cleanup entrypoint, and release metadata.');