| 1 | |
| 2 | import assert from 'node:assert/strict'; |
| 3 | import { createHash } from 'node:crypto'; |
| 4 | import { execFileSync } from 'node:child_process'; |
| 5 | import { appendFileSync, copyFileSync, lstatSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'; |
| 6 | import { join } from 'node:path'; |
| 7 | import { pathToFileURL } from 'node:url'; |
| 8 | |
| 9 | const repo = 'Lee-xeo/Claude-Cage'; |
| 10 | const gh = args => execFileSync('gh', args, { encoding: 'utf8', maxBuffer: 8 * 1024 * 1024 }); |
| 11 | const api = (path, args = []) => JSON.parse(gh(['api', `repos/${repo}/${path}`, ...args])); |
| 12 | const digest = data => createHash('sha256').update(data).digest('hex'); |
| 13 | |
| 14 | export function stagePreview(windows, linux, destination, version, dependencies) { |
| 15 | assert.match(version, /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/); |
| 16 | const expected = [ |
| 17 | [windows, [`Cage-${version}-windows-amd64-setup-unsigned.exe`, dependencies.mihomoSource.file, `${dependencies.qemuSource.file}.gz`]], |
| 18 | [linux, [`Cage-${version}-linux-amd64.deb`, dependencies.mihomoSource.file]], |
| 19 | ]; |
| 20 | const verified = new Map(); |
| 21 | for (const [directory, names] of expected) { |
| 22 | const manifest = readFileSync(join(directory, 'SHA256SUMS'), 'utf8').replace(/^\uFEFF/, ''); |
| 23 | assert.ok(Buffer.byteLength(manifest) < 65536); |
| 24 | const entries = new Map(); |
| 25 | for (const line of manifest.split(/\r?\n/).filter(Boolean)) { |
| 26 | const match = /^([a-f0-9]{64}) [ *]([A-Za-z0-9][A-Za-z0-9._-]*)$/i.exec(line); |
| 27 | assert.ok(match && !entries.has(match[2]), 'Invalid or duplicate platform checksum'); |
| 28 | entries.set(match[2], match[1].toLowerCase()); |
| 29 | } |
| 30 | assert.deepEqual([...entries.keys()].sort(), [...names].sort(), 'Unexpected platform assets'); |
| 31 | for (const name of names) { |
| 32 | const path = join(directory, name), stat = lstatSync(path); |
| 33 | assert.ok(stat.isFile() && stat.size > 0 && stat.size <= 512 * 1024 * 1024); |
| 34 | const actual = digest(readFileSync(path)); |
| 35 | assert.equal(actual, entries.get(name), `Corrupt artifact: ${name}`); |
| 36 | if (verified.has(name)) assert.equal(verified.get(name).digest, actual, 'Conflicting source archives'); |
| 37 | else verified.set(name, { path, digest: actual, size: stat.size }); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | mkdirSync(destination); |
| 42 | for (const [name, file] of verified) copyFileSync(file.path, join(destination, name)); |
| 43 | const sums = [...verified].map(([name, file]) => `${file.digest} ${name}\n`).join(''); |
| 44 | writeFileSync(join(destination, 'SHA256SUMS'), sums); |
| 45 | verified.set('SHA256SUMS', { digest: digest(sums), size: Buffer.byteLength(sums) }); |
| 46 | return verified; |
| 47 | } |
| 48 | |
| 49 | export function mergeReleaseChecksums(release, text, files, version) { |
| 50 | assert.equal(release.tag_name, `v${version}`); |
| 51 | assert.equal(release.draft, false); |
| 52 | assert.equal(release.prerelease, true, 'Unsigned packages require a prerelease'); |
| 53 | const entries = new Map(); |
| 54 | for (const line of text.trim().split(/\r?\n/)) { |
| 55 | const match = /^([a-f0-9]{64}) [ *]([A-Za-z0-9][A-Za-z0-9._-]*)$/i.exec(line); |
| 56 | assert.ok(match && !entries.has(match[2]), 'Invalid or duplicate existing checksum'); |
| 57 | entries.set(match[2], match[1].toLowerCase()); |
| 58 | } |
| 59 | const assets = release.assets.filter(asset => asset.name !== 'SHA256SUMS'); |
| 60 | for (const name of entries.keys()) assert.ok(assets.some(asset => asset.name === name), 'Existing release is incomplete'); |
| 61 | assert.ok(entries.has(`Cage-${version}-macos-arm64.dmg`)); |
| 62 | for (const asset of assets) { |
| 63 | assert.equal(asset.state, 'uploaded'); |
| 64 | |
| 65 | const expected = entries.get(asset.name) || files.get(asset.name)?.digest; |
| 66 | assert.ok(expected, `Unlisted existing asset: ${asset.name}`); |
| 67 | assert.equal(asset.digest, `sha256:${expected}`, `Existing checksum differs: ${asset.name}`); |
| 68 | } |
| 69 | for (const [name, file] of files) { |
| 70 | if (name === 'SHA256SUMS') continue; |
| 71 | if (entries.has(name)) assert.equal(entries.get(name), file.digest, `Conflicting release asset: ${name}`); |
| 72 | entries.set(name, file.digest); |
| 73 | } |
| 74 | return [...entries].map(([name, hash]) => `${hash} ${name}\n`).join(''); |
| 75 | } |
| 76 | |
| 77 | async function main() { |
| 78 | assert.equal(process.env.GITHUB_EVENT_NAME, 'workflow_dispatch'); |
| 79 | assert.equal(process.env.GITHUB_REPOSITORY, repo); |
| 80 | assert.equal(process.env.GITHUB_REF, 'refs/heads/main'); |
| 81 | const runID = process.env.CAGE_PREVIEW_RUN; |
| 82 | assert.match(runID || '', /^\d+$/); |
| 83 | const run = api(`actions/runs/${runID}`); |
| 84 | assert.equal(run.conclusion, 'success'); |
| 85 | assert.equal(run.path, '.github/workflows/ci.yml'); |
| 86 | assert.equal(run.head_repository.full_name, repo); |
| 87 | assert.match(run.head_sha, /^[a-f0-9]{40}$/); |
| 88 | const sourceFile = path => Buffer.from(api(`contents/${path}?ref=${run.head_sha}`).content, 'base64').toString('utf8'); |
| 89 | const version = sourceFile('VERSION').trim(); |
| 90 | assert.match(version, /^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/); |
| 91 | const dependencies = JSON.parse(sourceFile('packaging/windows/dependencies.json')); |
| 92 | const root = '_output/platform-publication'; |
| 93 | const destination = join(root, 'public'); |
| 94 | const files = stagePreview(join(root, 'windows', version, 'windows'), join(root, 'linux', version, 'linux'), destination, version, dependencies); |
| 95 | const tag = `v${version}`; |
| 96 | let release = api(`releases/tags/${tag}`); |
| 97 | const originalChecksums = release.assets.find(asset => asset.name === 'SHA256SUMS'); |
| 98 | assert.ok(originalChecksums && originalChecksums.size <= 65536); |
| 99 | const response = await fetch(originalChecksums.browser_download_url); |
| 100 | assert.ok(response.ok, 'Cannot read public checksum manifest'); |
| 101 | const originalText = await response.text(); |
| 102 | assert.equal(digest(originalText), originalChecksums.digest.replace('sha256:', '')); |
| 103 | const merged = mergeReleaseChecksums(release, originalText, files, version); |
| 104 | writeFileSync(join(destination, 'SHA256SUMS'), merged); |
| 105 | files.set('SHA256SUMS', { digest: digest(merged), size: Buffer.byteLength(merged) }); |
| 106 | for (const [name, file] of files) { |
| 107 | if (name === 'SHA256SUMS') continue; |
| 108 | const existing = release.assets.find(asset => asset.name === name); |
| 109 | if (existing) { |
| 110 | assert.equal(existing.digest, `sha256:${file.digest}`, `Existing asset differs: ${name}`); |
| 111 | assert.equal(existing.size, file.size); |
| 112 | } else gh(['release', 'upload', tag, join(destination, name), '--repo', repo]); |
| 113 | } |
| 114 | |
| 115 | const uploaded = api(`releases/${release.id}`); |
| 116 | for (const [name, file] of files) { |
| 117 | if (name === 'SHA256SUMS') continue; |
| 118 | const asset = uploaded.assets.find(asset => asset.name === name); |
| 119 | assert.ok(asset && asset.state === 'uploaded'); |
| 120 | assert.equal(asset.digest, `sha256:${file.digest}`); |
| 121 | assert.equal(asset.size, file.size); |
| 122 | } |
| 123 | assert.equal(uploaded.assets.find(asset => asset.name === 'SHA256SUMS').digest, originalChecksums.digest, 'Checksum manifest changed concurrently'); |
| 124 | if (originalChecksums.digest !== `sha256:${digest(merged)}`) { |
| 125 | gh(['release', 'upload', tag, join(destination, 'SHA256SUMS'), '--repo', repo, '--clobber']); |
| 126 | } |
| 127 | release = api(`releases/${release.id}`); |
| 128 | assert.equal(release.assets.find(asset => asset.name === 'SHA256SUMS').digest, `sha256:${digest(merged)}`); |
| 129 | mergeReleaseChecksums(release, merged, files, version); |
| 130 | const marker = `<!-- cage-platform-packages source=${run.head_sha} ci=${runID} -->`; |
| 131 | if (!release.body.includes(marker)) { |
| 132 | const notes = release.body + `\n\n## Windows 与 Linux 下载\n\n${marker}\n\n本版本同时提供三平台安装包,共用 SHA256SUMS。macOS 安装包保持原样;Windows/Linux 包来自完成平台构建修复的源码 [${run.head_sha.slice(0, 7)}](https://github.com/${repo}/commit/${run.head_sha}),与 macOS 标签的构建提交不同。\n\n- **Windows:未签名 x64 预览版**,文件名含 -setup-unsigned.exe,需手动安装和更新,系统可能提示未知发布者;不进入签名自动更新通道。\n- **Linux:Debian 12 / Ubuntu 24.04 x86_64**,下载 .deb 并通过系统包管理器安装、更新。\n- [三平台 CI](https://github.com/${repo}/actions/runs/${runID}) 已通过 Windows 2022/2025 安装器及 Debian 12 容器验收。托管 CI 不代替所有真实设备和硬件虚拟化验收。\n- 随附 Mihomo 与 QEMU 主程序对应源码;升级前保存文件、停止环境并退出 Cage。\n\n[官网三平台下载](https://lee-xeo.github.io/Claude-Cage/#download)\n`; |
| 133 | const notesPath = join(root, 'notes.md'); writeFileSync(notesPath, notes); |
| 134 | gh(['release', 'edit', tag, '--repo', repo, '--notes-file', notesPath]); |
| 135 | } |
| 136 | appendFileSync(process.env.GITHUB_OUTPUT, `tag=${tag}\nurl=${release.html_url}\n`); |
| 137 | console.log(`Verified platform packages in version release: ${release.html_url}`); |
| 138 | } |
| 139 | if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) main().catch(error => { console.error(error.message); process.exitCode = 1; }); |