Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

ci: 新增 PR 设计依据轻校验(UI 改动需引用设计规范)

Lizie76f8623fe02
diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md
index 2dd9c177b5..22f6fd73ee 100644
--- a/.github/PULL_REQUEST_TEMPLATE.md
+++ b/.github/PULL_REQUEST_TEMPLATE.md
@@ -32,6 +32,16 @@ type:feat / fix / refactor / perf / chore / docs / test / revert / build / ci
<!-- 涉及 UI 时附截图或录屏,并注明平台;不涉及则写“不涉及”。 -->
+- 引用的设计规范:
+
+<!-- 涉及 UI 时必填:列出本次改动遵循的设计规范章节与约束,并说明如何满足。
+规范正本为 docs/design-rules/DESIGN.md(相关文档索引见
+docs/design-rules/cindy-design-system.md)。写到具体章节/条款,
+如“DESIGN.md §间距与栅格:卡片内边距 16px”。
+改动命中 UI 代码路径但确无视觉/交互/文案变化时,写“不涉及:<理由>”;
+完全不涉及 UI 则写“不涉及”。CI(pr-design-basis)会在变更命中 UI 路径时
+校验本字段,判定逻辑见 scripts/check-pr-design-basis.mjs。 -->
+
## 怎么验证的
### 自动验证
@@ -76,6 +86,7 @@ branch/worktree、Metro 归属与 __DEV__ build label 证据。 -->
### 提交前检查
- [ ] 已 review 完整 diff
+- [ ] UI 改动已在「UI 变化」注明引用的设计规范章节(不涉及 UI 则跳过)
- [ ] 未提交凭证、令牌或授权文件
- [ ] 已补充必要文档
- [ ] 已确认测试结果或说明未执行原因
diff --git a/.github/workflows/pr-design-basis.yml b/.github/workflows/pr-design-basis.yml
new file mode 100644
index 0000000000..759fd24ea5
--- /dev/null
+++ b/.github/workflows/pr-design-basis.yml
@@ -0,0 +1,37 @@
+name: pr-design-basis
+
+# 轻校验:PR 变更命中 UI 路径时,正文「引用的设计规范」必须已按模板填写。
+# 判定逻辑与豁免写法见 scripts/check-pr-design-basis.mjs 头部注释。
+
+on:
+ pull_request:
+ types: [opened, edited, reopened, synchronize, ready_for_review]
+
+permissions:
+ contents: read
+ pull-requests: read
+
+concurrency:
+ group: pr-design-basis-${{ github.event.pull_request.number }}
+ cancel-in-progress: true
+
+jobs:
+ check:
+ name: check:pr-design-basis
+ runs-on: ubuntu-latest
+ timeout-minutes: 5
+ if: ${{ !github.event.pull_request.draft }}
+
+ steps:
+ - name: Checkout
+ uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+
+ - name: Setup Node.js
+ uses: actions/setup-node@820762786026740c76f36085b0efc47a31fe5020 # v7.0.0
+ with:
+ node-version: 22
+
+ - name: Verify PR design basis
+ env:
+ GITHUB_TOKEN: ${{ github.token }}
+ run: node scripts/check-pr-design-basis.mjs
diff --git a/docs/dev-rules/development-workflow.md b/docs/dev-rules/development-workflow.md
index 5aec034729..401197a9f8 100644
--- a/docs/dev-rules/development-workflow.md
+++ b/docs/dev-rules/development-workflow.md
@@ -38,8 +38,12 @@ worktree 会话契约、直推 `main` 的额外门禁与 review 严重度口径
具备 bypass 权限的维护者明确选择,并执行本节的额外门禁。
- PR 的 Title/Description 以 [`../../.github/PULL_REQUEST_TEMPLATE.md`](../../.github/PULL_REQUEST_TEMPLATE.md)
为准(这次改了什么/怎么验证的/风险);涉及 SQLite migration、system prompt、协议、
- 原生层或跨平台差异时必须在「风险」里说明。Reviewer 只看 Title + Description 决定要不要
- review,写不清直接退回。
+ 原生层或跨平台差异时必须在「风险」里说明;涉及 UI 时必须在「UI 变化」注明引用的
+ 设计规范章节与约束(正本为 `docs/design-rules/DESIGN.md`)。CI 的
+ `pr-design-basis` 会在 PR 变更命中 UI 路径时轻校验该字段(非空、引用了
+ design-rules 文档,或「不涉及:<理由>」豁免;判定逻辑见
+ `scripts/check-pr-design-basis.mjs`),但通过 CI 不代表内容合格,质量仍由
+ review 把关。Reviewer 只看 Title + Description 决定要不要 review,写不清直接退回。
- **提交前测试门禁(硬性要求)**:无论是提 PR 还是直接 commit,提交前都必须在本地跑完
仓库根 `pnpm test:unit`(全部单元测试),并对本次改动涉及的每个 package 跑
`pnpm --filter <包名> run --if-present typecheck`(`<包名>` 用该 package 在
diff --git a/scripts/check-pr-design-basis.mjs b/scripts/check-pr-design-basis.mjs
new file mode 100644
index 0000000000..76eba49731
--- /dev/null
+++ b/scripts/check-pr-design-basis.mjs
@@ -0,0 +1,187 @@
+#!/usr/bin/env node
+// PR 正文「设计依据」轻校验:当 PR 的变更命中 UI 路径时,要求正文「UI 变化」里的
+// 「引用的设计规范」字段已经填写——要么引用 docs/design-rules/ 下的具体规范
+// (如 DESIGN.md §某章节),要么写明「不涉及」并附理由(改动命中 UI 路径但确无
+// 视觉/交互/文案变化时)。只做锚点、非空与引用格式检查,内容质量仍由人工 review
+// 与内部 review-pr Skill 把关;本脚本不读取任何私有配置。
+//
+// CI 用法(由 .github/workflows/pr-design-basis.yml 调用):
+// GITHUB_EVENT_PATH pull_request 事件 payload(取 PR body、仓库与编号)
+// GITHUB_TOKEN 只读调用 GitHub API 拉取 PR 变更文件列表
+// 本地调试(不访问网络):
+// node scripts/check-pr-design-basis.mjs --body <body.md> --files <files.txt>
+// files.txt 为每行一个变更文件路径的文本文件。
+
+import { readFileSync } from 'node:fs';
+import { pathToFileURL } from 'node:url';
+
+// UI 路径启发式白名单:命中即视为「涉及 UI」。这是轻校验的判定边界,
+// 有误报时作者可用「不涉及:<理由>」豁免;调整边界直接改这两个列表。
+const UI_PATH_PREFIXES = [
+ 'apps/desktop/src/renderer/',
+ 'apps/mobile/app/',
+ 'apps/mobile/src/auth/',
+ 'apps/mobile/src/components/',
+ 'apps/mobile/src/i18n/',
+ 'apps/mobile/src/notifications/',
+ 'apps/mobile/src/session/',
+ 'apps/mobile/src/settings/',
+ 'apps/mobile/src/theme/',
+];
+const UI_FILE_SUFFIXES = ['.css', '.scss', '.less'];
+
+const FIELD_LABEL = '引用的设计规范';
+// 设计规范文档引用特征:docs/design-rules/ 目录或其下具体文档名。
+const DESIGN_DOC_PATTERN =
+ /(docs\/design-rules\/|DESIGN\.md|cindy-design-system|figma-component-spec|token-decision-table)/i;
+// 「不涉及」豁免在去掉该词与标点后至少要剩这么多字符,才算附了理由
+// (放到 4 是为了让「纯逻辑重构」「无 UI 变化」这类最短合理理由能过)。
+const MIN_EXEMPT_REASON_CHARS = 4;
+// 任务清单行(如模板「提交前检查」的勾选项)不算字段锚点:其文案里同时含有
+// 字段标签与「不涉及」,若被当成字段内容会造成假通过。
+const CHECKLIST_LINE_PATTERN = /^\s*[-*+]\s*\[[ xX]\]/;
+
+export function isUiPath(path) {
+ return (
+ UI_PATH_PREFIXES.some((prefix) => path.startsWith(prefix)) ||
+ UI_FILE_SUFFIXES.some((suffix) => path.endsWith(suffix))
+ );
+}
+
+/** 提取「引用的设计规范」字段正文:从标签行到下一个 Markdown 标题为止。 */
+export function extractDesignBasis(body) {
+ // 先剥掉 HTML 注释,避免模板自带的填写说明(含规范文件名)造成假通过。
+ const stripped = body.replace(/<!--[\s\S]*?-->/g, '');
+ const lines = stripped.split(/\r?\n/);
+ const labelIndex = lines.findIndex(
+ (line) => line.includes(FIELD_LABEL) && !CHECKLIST_LINE_PATTERN.test(line)
+ );
+ if (labelIndex === -1) return null;
+
+ const labelLine = lines[labelIndex];
+ const afterLabel = labelLine.slice(labelLine.indexOf(FIELD_LABEL) + FIELD_LABEL.length);
+ const parts = [afterLabel.replace(/^[\s::]+/, '')];
+ for (let i = labelIndex + 1; i < lines.length; i += 1) {
+ if (/^#{1,6}\s/.test(lines[i])) break;
+ parts.push(lines[i]);
+ }
+ return parts.join('\n').trim();
+}
+
+/** 返回错误列表;空列表即通过。uiFiles 为命中 UI 路径的变更文件。 */
+export function validateDesignBasis(body, uiFiles) {
+ if (uiFiles.length === 0) return [];
+
+ const errors = [];
+ const content = typeof body === 'string' ? extractDesignBasis(body) : null;
+
+ if (content === null) {
+ errors.push(
+ `PR 正文缺少「${FIELD_LABEL}」字段:请按 .github/PULL_REQUEST_TEMPLATE.md` +
+ '「UI 变化」小节补齐(改动命中了 UI 路径)。'
+ );
+ return errors;
+ }
+ if (content === '') {
+ errors.push(`「${FIELD_LABEL}」未填写:改动命中 UI 路径时该字段必填。`);
+ return errors;
+ }
+
+ if (DESIGN_DOC_PATTERN.test(content)) return errors;
+
+ if (content.includes('不涉及')) {
+ const reason = content.replace(/不涉及/g, '').replace(/[\s、,,。.::;;!!??()()-]/g, '');
+ if (reason.length < MIN_EXEMPT_REASON_CHARS) {
+ errors.push(
+ '改动命中 UI 路径却填「不涉及」时必须附理由,' +
+ '如「不涉及:纯逻辑重构,无视觉/交互/文案变化」。'
+ );
+ }
+ return errors;
+ }
+
+ errors.push(
+ `「${FIELD_LABEL}」需引用 docs/design-rules/ 下的具体规范章节` +
+ '(如「DESIGN.md §间距与栅格:卡片内边距 16px」),' +
+ '或在确无 UI 变化时写「不涉及:<理由>」。'
+ );
+ return errors;
+}
+
+async function fetchChangedFiles(repo, prNumber, token) {
+ const files = [];
+ // GitHub 列表 API 上限 3000 个文件,per_page=100 时最多 30 页。
+ for (let page = 1; page <= 30; page += 1) {
+ const url = `https://api.github.com/repos/${repo}/pulls/${prNumber}/files?per_page=100&page=${page}`;
+ const res = await fetch(url, {
+ headers: {
+ accept: 'application/vnd.github+json',
+ authorization: `Bearer ${token}`,
+ 'x-github-api-version': '2022-11-28',
+ },
+ });
+ if (!res.ok) throw new Error(`拉取 PR 变更文件列表失败:HTTP ${res.status}`);
+ const batch = await res.json();
+ for (const file of batch) files.push(file.filename);
+ if (batch.length < 100) break;
+ }
+ return files;
+}
+
+function readArg(name) {
+ const index = process.argv.indexOf(name);
+ return index !== -1 ? process.argv[index + 1] : undefined;
+}
+
+async function main() {
+ let body;
+ let changedFiles;
+
+ const bodyPath = readArg('--body');
+ const filesPath = readArg('--files');
+ if (bodyPath || filesPath) {
+ if (!bodyPath || !filesPath) throw new Error('本地调试需同时提供 --body 与 --files。');
+ body = readFileSync(bodyPath, 'utf8');
+ changedFiles = readFileSync(filesPath, 'utf8')
+ .split(/\r?\n/)
+ .map((line) => line.trim())
+ .filter(Boolean);
+ } else {
+ const eventPath = process.env.GITHUB_EVENT_PATH;
+ if (!eventPath) throw new Error('缺少 GITHUB_EVENT_PATH(本脚本需在 pull_request 事件下运行)。');
+ const event = JSON.parse(readFileSync(eventPath, 'utf8'));
+ const pr = event.pull_request;
+ if (!pr) throw new Error('事件 payload 中没有 pull_request,请检查 workflow 触发配置。');
+ const token = process.env.GITHUB_TOKEN;
+ if (!token) throw new Error('缺少 GITHUB_TOKEN,无法拉取 PR 变更文件列表。');
+ body = pr.body ?? '';
+ changedFiles = await fetchChangedFiles(event.repository.full_name, pr.number, token);
+ }
+
+ const uiFiles = changedFiles.filter(isUiPath);
+ if (uiFiles.length === 0) {
+ console.log('变更未命中 UI 路径,跳过设计依据校验。');
+ return;
+ }
+
+ const errors = validateDesignBasis(body, uiFiles);
+ if (errors.length > 0) {
+ console.error('PR 设计依据校验未通过:');
+ for (const error of errors) console.error(`- ${error}`);
+ console.error('\n命中 UI 路径的变更文件(最多列 20 个):');
+ for (const file of uiFiles.slice(0, 20)) console.error(`- ${file}`);
+ if (uiFiles.length > 20) console.error(`- …另有 ${uiFiles.length - 20} 个`);
+ process.exit(1);
+ }
+
+ console.log(`设计依据校验通过(命中 UI 路径的变更文件 ${uiFiles.length} 个)。`);
+}
+
+// 仅作为入口执行时运行;被 import 时只导出纯函数,便于本地测试。
+const isDirectRun = process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href;
+if (isDirectRun) {
+ main().catch((error) => {
+ console.error(String(error?.message ?? error));
+ process.exit(1);
+ });
+}