|
| 1 | +const fs = require('fs') |
| 2 | +const path = require('path') |
| 3 | + |
| 4 | +const DEFAULT_MANIFEST_PATH = 'release/manifest.json' |
| 5 | +const SEMVER_PATTERN = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/ |
| 6 | +const APPROVAL_BEAD_PATTERN = /^haxe\.elixir(?:\.codex)?-[0-9A-Za-z.]+$/ |
| 7 | +const APPROVAL_DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/ |
| 8 | +const EVIDENCE_KEYS = [ |
| 9 | + 'platformToolchain', |
| 10 | + 'compatibility', |
| 11 | + 'applicationRuntime', |
| 12 | + 'independentReview', |
| 13 | +] |
| 14 | + |
| 15 | +function fail(message) { |
| 16 | + throw new Error(`Invalid release manifest: ${message}`) |
| 17 | +} |
| 18 | + |
| 19 | +function isObject(value) { |
| 20 | + return value !== null && typeof value === 'object' && !Array.isArray(value) |
| 21 | +} |
| 22 | + |
| 23 | +function parseSemver(version, label = 'version') { |
| 24 | + const match = typeof version === 'string' ? SEMVER_PATTERN.exec(version) : null |
| 25 | + if (!match) fail(`${label} must be a valid semantic version`) |
| 26 | + return { |
| 27 | + major: Number(match[1]), |
| 28 | + minor: Number(match[2]), |
| 29 | + patch: Number(match[3]), |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +function validateReleaseManifest(manifest) { |
| 34 | + if (!isObject(manifest)) fail('root must be an object') |
| 35 | + if (manifest.schemaVersion !== 1) fail('schemaVersion must be 1') |
| 36 | + |
| 37 | + if (!isObject(manifest.package)) fail('package must be an object') |
| 38 | + if (manifest.package.name !== 'reflaxe.elixir') { |
| 39 | + fail('package.name must be reflaxe.elixir') |
| 40 | + } |
| 41 | + parseSemver(manifest.package.version, 'package.version') |
| 42 | + |
| 43 | + const policy = manifest.releasePolicy |
| 44 | + if (!isObject(policy)) fail('releasePolicy must be an object') |
| 45 | + if (policy.currentLine !== 'pre1' && policy.currentLine !== 'stable') { |
| 46 | + fail('releasePolicy.currentLine must be pre1 or stable') |
| 47 | + } |
| 48 | + |
| 49 | + if (!isObject(policy.lines)) fail('releasePolicy.lines must be an object') |
| 50 | + const pre1 = policy.lines.pre1 |
| 51 | + const stable = policy.lines.stable |
| 52 | + if (!isObject(pre1) || pre1.major !== 0 || pre1.breakingRelease !== 'minor') { |
| 53 | + fail('pre1 line must use major 0 and minor breaking releases') |
| 54 | + } |
| 55 | + if (!isObject(stable) || stable.minimumMajor !== 1 || stable.breakingRelease !== 'major') { |
| 56 | + fail('stable line must start at major 1 and use major breaking releases') |
| 57 | + } |
| 58 | + |
| 59 | + const graduation = policy.graduation |
| 60 | + if (!isObject(graduation) || typeof graduation.approved !== 'boolean') { |
| 61 | + fail('releasePolicy.graduation must contain an approved boolean') |
| 62 | + } |
| 63 | + if (!isObject(graduation.evidence)) { |
| 64 | + fail('releasePolicy.graduation.evidence must be an object') |
| 65 | + } |
| 66 | + for (const key of EVIDENCE_KEYS) { |
| 67 | + if (!(key in graduation.evidence)) { |
| 68 | + fail(`releasePolicy.graduation.evidence.${key} is required`) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return manifest |
| 73 | +} |
| 74 | + |
| 75 | +function loadReleaseManifest(manifestPath = DEFAULT_MANIFEST_PATH, cwd = process.cwd()) { |
| 76 | + const absolutePath = path.resolve(cwd, manifestPath) |
| 77 | + let parsed |
| 78 | + try { |
| 79 | + parsed = JSON.parse(fs.readFileSync(absolutePath, 'utf8')) |
| 80 | + } catch (error) { |
| 81 | + throw new Error(`Unable to read release manifest ${absolutePath}: ${error.message}`) |
| 82 | + } |
| 83 | + return validateReleaseManifest(parsed) |
| 84 | +} |
| 85 | + |
| 86 | +function assertGraduationApproved(manifest) { |
| 87 | + validateReleaseManifest(manifest) |
| 88 | + const graduation = manifest.releasePolicy.graduation |
| 89 | + const missing = [] |
| 90 | + |
| 91 | + if (graduation.approved !== true) missing.push('approved=true') |
| 92 | + if (!APPROVAL_BEAD_PATTERN.test(graduation.approvalBead || '')) { |
| 93 | + missing.push('a reviewed approvalBead') |
| 94 | + } |
| 95 | + if (!APPROVAL_DATE_PATTERN.test(graduation.approvedAt || '')) { |
| 96 | + missing.push('approvedAt in YYYY-MM-DD form') |
| 97 | + } |
| 98 | + for (const key of EVIDENCE_KEYS) { |
| 99 | + const value = graduation.evidence[key] |
| 100 | + if (typeof value !== 'string' || value.trim() === '') { |
| 101 | + missing.push(`${key} evidence`) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if (missing.length > 0) { |
| 106 | + throw new Error(`Stable graduation is not approved: missing ${missing.join(', ')}`) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +function assertVersionAllowed(manifest, version) { |
| 111 | + validateReleaseManifest(manifest) |
| 112 | + const { major } = parseSemver(version, 'requested version') |
| 113 | + const line = manifest.releasePolicy.currentLine |
| 114 | + |
| 115 | + if (major === 0 && line !== 'pre1') { |
| 116 | + throw new Error('Stable release policy cannot generate a 0.x version') |
| 117 | + } |
| 118 | + if (major >= 1) { |
| 119 | + if (line !== 'stable') { |
| 120 | + throw new Error('A 1.x release requires releasePolicy.currentLine=stable') |
| 121 | + } |
| 122 | + assertGraduationApproved(manifest) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +function releaseRulesForManifest(manifest) { |
| 127 | + validateReleaseManifest(manifest) |
| 128 | + const line = manifest.releasePolicy.currentLine |
| 129 | + if (line === 'stable') assertGraduationApproved(manifest) |
| 130 | + |
| 131 | + return [ |
| 132 | + { |
| 133 | + breaking: true, |
| 134 | + release: manifest.releasePolicy.lines[line].breakingRelease, |
| 135 | + }, |
| 136 | + { type: 'feat', release: 'minor' }, |
| 137 | + { type: 'fix', release: 'patch' }, |
| 138 | + { type: 'perf', release: 'patch' }, |
| 139 | + { type: 'revert', release: 'patch' }, |
| 140 | + ] |
| 141 | +} |
| 142 | + |
| 143 | +module.exports = { |
| 144 | + DEFAULT_MANIFEST_PATH, |
| 145 | + assertGraduationApproved, |
| 146 | + assertVersionAllowed, |
| 147 | + loadReleaseManifest, |
| 148 | + parseSemver, |
| 149 | + releaseRulesForManifest, |
| 150 | + validateReleaseManifest, |
| 151 | +} |
0 commit comments