37 lines
2.8 KiB
JavaScript
37 lines
2.8 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import YAML from 'yaml';
|
|
import { fileDigest, writeNewJson } from './ci-utils.mjs';
|
|
|
|
export function verifyCiWorkflow(root) {
|
|
const file = path.join(root,'.gitea/workflows/ci.yml'), lock = JSON.parse(fs.readFileSync(path.join(root,'scripts/ci-environment.json')));
|
|
const document = YAML.parseDocument(fs.readFileSync(file,'utf8'),{uniqueKeys:true});
|
|
assert.deepEqual(document.errors,[], 'Invalid workflow YAML'); const workflow=document.toJSON();
|
|
assert.ok(workflow.on.push && Object.hasOwn(workflow.on,'pull_request') && Object.hasOwn(workflow.on,'workflow_dispatch'));
|
|
assert.equal(Object.keys(workflow.jobs).length,1);
|
|
const job=workflow.jobs['windows-source']; assert.equal(job['runs-on'],lock.runnerLabel);
|
|
assert.equal(job['continue-on-error'],undefined); assert.equal(job['timeout-minutes'],undefined);
|
|
const uses=job.steps.filter(step=>step.uses).map(step=>step.uses);
|
|
assert.deepEqual(uses,[lock.actions.checkout,lock.actions.setupNode,lock.actions.setupPython,lock.actions.uploadArtifact]);
|
|
for(const reference of uses) assert.match(reference,/^https:\/\/(?:github\.com|gitea\.com)\/[a-z-]+\/[a-z-]+@[a-f0-9]{40}$/);
|
|
assert.equal(job.steps[0].with['persist-credentials'],false); assert.equal(job.steps[0].with['fetch-depth'],1);
|
|
assert.equal(job.steps[1].with['node-version'],lock.node); assert.equal(job.steps[2].with['python-version'],lock.python);
|
|
const execution=job.steps.find(step=>step.run); assert.equal(execution.shell,'pwsh');
|
|
assert.match(execution.run,/scripts\/ci\.mjs --profile windows --python \$qaPython --output-root \.qa\/ci/);
|
|
assert.match(execution.run,/exit \$LASTEXITCODE/);
|
|
const artifact=job.steps.at(-1); assert.equal(artifact.if,'always()'); assert.equal(artifact.with.path,'.qa/ci/**/evidence/**'); assert.equal(artifact.with['if-no-files-found'],'error'); assert.equal(artifact.with['include-hidden-files'],true, 'The explicit .qa evidence directory must be uploaded');
|
|
return {passed:true,workflow:'.gitea/workflows/ci.yml',sha256:fileDigest(file),runnerLabel:lock.runnerLabel,actions:uses,
|
|
scope:'Actual YAML parse, pinned action/Node/Python refs, error propagation and evidence upload contract. Does not assert runner availability or remote execution.'};
|
|
}
|
|
|
|
if(process.argv[1] && path.resolve(process.argv[1])===fileURLToPath(import.meta.url)) {
|
|
try {
|
|
if(process.argv.length>3) throw new Error('node scripts/verify-ci-workflow.mjs [NEW_REPORT]');
|
|
const result=verifyCiWorkflow(path.resolve(path.dirname(fileURLToPath(import.meta.url)),'..'));
|
|
if(process.argv[2]) writeNewJson(path.resolve(process.argv[2]),result);
|
|
console.log(JSON.stringify(result));
|
|
} catch(error) {console.error(error.stack);process.exitCode=1;}
|
|
}
|