73 lines
3.7 KiB
JavaScript
73 lines
3.7 KiB
JavaScript
'use strict'
|
||
// 版本记录一致性守卫(2026-11-10)
|
||
//
|
||
// 动机:v3.24.56 / v3.24.59 这种"发了但没留痕 / 没打 tag"的情况此前只能靠人肉发现。
|
||
// 复核结论是这两例**都有交代**(56 并入 57 发布、59 是测试站试色中间版),
|
||
// 但**没有任何机械规则拦住下一次**。本守卫补上双向校验:
|
||
//
|
||
// A. CHANGELOG 里标注「状态:已发布」的版本 ⇒ 必须存在同名 git tag
|
||
// B. 存在 git tag vX.Y.Z ⇒ CHANGELOG 必须提到这个版本号
|
||
// C. package.json 当前版本 ⇒ 必须有 CHANGELOG 条目
|
||
//
|
||
// 设计取舍:
|
||
// - CI 若拿不到 tag(浅克隆/未取 tag),**跳过 A/B 并显式说明**,不制造假失败;
|
||
// 本地与 CI(fetch-depth: 0)都会真跑。
|
||
// - 只校验 `v3.24.*` 及之后的当前版本线,历史线(3.8x)按规范不改写、不校验。
|
||
const assert = require('assert')
|
||
const fs = require('fs')
|
||
const path = require('path')
|
||
const { execSync } = require('child_process')
|
||
|
||
const root = path.resolve(__dirname, '..')
|
||
const changelog = fs.readFileSync(path.join(root, 'CHANGELOG.md'), 'utf8')
|
||
const pkg = JSON.parse(fs.readFileSync(path.join(root, 'package.json'), 'utf8'))
|
||
|
||
// ── 解析 CHANGELOG:每个 `## vX.Y.Z` 段落及其是否标注已发布 ──
|
||
// 按 `## ` 标题切段(比带 lookahead 的单正则稳:后者会被 `\s*$` 立刻匹配而截断成空 body)
|
||
const sections = []
|
||
{
|
||
const lines = changelog.split('\n')
|
||
let cur = null
|
||
for (const line of lines) {
|
||
const h = /^## v(\d+\.\d+\.\d+)/.exec(line)
|
||
if (h) { cur = { v: h[1], body: [] }; sections.push(cur); continue }
|
||
if (cur) cur.body.push(line)
|
||
}
|
||
for (const s2 of sections) s2.body = s2.body.join('\n')
|
||
}
|
||
assert.ok(sections.length >= 10, 'CHANGELOG 解析出的版本段落过少(' + sections.length + '),正则可能失效')
|
||
|
||
const released = sections.filter((s) => /状态:已发布/.test(s.body)).map((s) => s.v)
|
||
assert.ok(released.length >= 5, '标注「已发布」的版本过少(' + released.length + '),解析可能失效')
|
||
|
||
// ── C. 当前 package 版本必须有 CHANGELOG 条目(无条件校验)──
|
||
assert.ok(sections.some((s) => s.v === pkg.version),
|
||
'package.json 版本 v' + pkg.version + ' 在 CHANGELOG 中没有条目 —— 发版必须写记录')
|
||
|
||
// ── A/B. tag 双向校验(拿不到 tag 信息时跳过)──
|
||
let tags = []
|
||
let tagAvailable = false
|
||
try {
|
||
const out = execSync('git tag --list "v3.24.*"', { cwd: root, encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] })
|
||
tags = out.split('\n').map((x) => x.trim()).filter(Boolean).map((x) => x.replace(/^v/, ''))
|
||
tagAvailable = tags.length > 0
|
||
} catch (e) { tagAvailable = false }
|
||
|
||
if (!tagAvailable) {
|
||
console.log('release-records: ⚠ 未取到 git tag(浅克隆/未 fetch tags),跳过 tag 双向校验;CHANGELOG 与版本号校验已通过')
|
||
} else {
|
||
// A. 已发布 ⇒ 有 tag
|
||
const missingTag = released.filter((v) => !tags.includes(v))
|
||
assert.deepStrictEqual(missingTag, [],
|
||
'CHANGELOG 标注「已发布」但无同名 git tag:' + missingTag.map((v) => 'v' + v).join(', ') +
|
||
'(要么补 tag,要么把「状态:已发布」改成实际状态并说明)')
|
||
|
||
// B. 有 tag ⇒ CHANGELOG 必须提到
|
||
const tagSet = new Set(sections.map((s) => s.v))
|
||
const noRecord = tags.filter((v) => !tagSet.has(v) && changelog.indexOf(v) === -1)
|
||
assert.deepStrictEqual(noRecord, [],
|
||
'存在 git tag 但 CHANGELOG 完全没有提这个版本:' + noRecord.map((v) => 'v' + v).join(', '))
|
||
|
||
console.log('release-records: ' + released.length + ' 个已发布版本均有 tag;' + tags.length + ' 个 tag 均有 CHANGELOG 记录')
|
||
}
|