@rowansenne/workflow-run 0.35.0 → 0.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +23 -1
- package/README.md +2 -0
- package/bin/lib/artifact-context.mjs +1 -1
- package/bin/lib/docsrepo.mjs +4 -6
- package/bin/lib/doctor.mjs +0 -3
- package/bin/lib/install.mjs +15 -15
- package/create-release-md-doc/SKILL.md.tpl +1 -1
- package/create-tech-md-doc/SKILL.md.tpl +1 -1
- package/dev/references/delivery.md +3 -3
- package/docs/architecture.html +3 -2
- package/docs/development.md +10 -4
- package/docs/full-install.html +90 -125
- package/docs/installation.md +1 -1
- package/docs/loan-service-install.html +90 -0
- package/docs/team-guide.md +3 -2
- package/docs/workflow-control-roadmap.md +144 -0
- package/docs/workflow-core-phase-one-report.md +41 -0
- package/docs/workflow-core-phase-one.md +317 -0
- package/docs/workflows.md +2 -2
- package/examples/workflow-core.mjs +26 -0
- package/package.json +2 -1
- package/project-review/SKILL.md.tpl +1 -1
- package/project-review/references/checklist.md.tpl +1 -1
- package/project-review/references/reporting.md +2 -2
- package/workflow/core/errors.mjs +61 -0
- package/workflow/core/index.mjs +286 -0
- package/workflow/core/review-handoff.mjs +101 -0
- package/workflow/integrations/skill-document-policy.mjs +28 -0
- package/workflow/lib/artifact-finalize.mjs +49 -10
- package/workflow/lib/code-snapshot.mjs +5 -0
- package/workflow/lib/meta-store.mjs +12 -1
- package/workflow/lib/project.mjs +165 -14
- package/workflow/lib/requirements.mjs +57 -100
- package/workflow/lib/review-scope.mjs +262 -0
- package/workflow/lib/runs.mjs +11 -1
- package/workflow/lib/workspace-read.mjs +13 -4
- package/workflow/references/architecture.md +10 -5
- package/workflow/references/core-api.md +196 -0
- package/workflow/references/execution.md +4 -4
- package/workflow/references/profile.md +20 -0
- package/workflow/references/requirements.md +12 -12
- package/workflow/scripts/artifact.mjs +36 -14
- package/workflow/scripts/requirement.mjs +36 -40
- package/workflow/scripts/resolve.mjs +2 -2
- package/workflow/scripts/review-handoff.mjs +49 -73
- package/workflow/scripts/review-scope.mjs +78 -294
- package/workflow/scripts/run.mjs +21 -9
|
@@ -1,339 +1,123 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
//
|
|
3
|
-
// project-review 第 1 步与 dev 自审(F7)使用;报告里每个文件都要有交代(已审 / 跳过的原因 / 没审完)。
|
|
4
|
-
// - 范围:分支对比 `--diff <base>...<head>`(读 <head>),或 `--base <分支>`:相对合并基点的已提交 + 未提交 + 未跟踪改动(读工作区)
|
|
5
|
-
// - 跳过:删除、二进制、敏感文件(不读内容)、依赖目录、生成文件与锁文件、文档、本工具的产物;画像 review.include 可以把后三类加回来
|
|
6
|
-
// - 风险 P0 / P1 / P2 与分批:规则同 project-review/references/diff-strategy.md
|
|
7
|
-
// - 清单路由:按文件类型和改动内容给出审查清单的小节号;画像 review.rules 里按路径匹配的项目规则一起给出
|
|
8
|
-
// `rules <文件…>` 打印这些文件要看的清单小节原文和项目规则:清单用 resolveTemplate 取 project-review 的 checklist.md(团队在 DocNexus 改过的版本也适用)。
|
|
9
|
-
// 只有 project-review 和 dev 自审会调用 rules,project-review 没装时它报错退出(resolveTemplate 提示没有安装)。
|
|
10
|
-
//
|
|
11
|
-
// 用法(在项目根执行;--root 可指定项目根):
|
|
12
|
-
// node .agents/skills/workflow/scripts/review-scope.mjs --diff <base>...<head> [--exclude a,b]
|
|
13
|
-
// node .agents/skills/workflow/scripts/review-scope.mjs [--base <分支>] [--exclude a,b] # 缺省基线取画像 main_branch(再缺省 main)
|
|
14
|
-
// node .agents/skills/workflow/scripts/review-scope.mjs rules <文件> [<文件>…]
|
|
15
|
-
// 输出 JSON(rules 输出 Markdown)。退出码:0 正常;1 参数错误或 git 失败
|
|
16
|
-
|
|
2
|
+
// 审查范围命令入口;范围、分类和分批规则位于 workflow/lib/review-scope.mjs。
|
|
17
3
|
import fs from 'node:fs';
|
|
18
4
|
import path from 'node:path';
|
|
19
|
-
import { spawnSync } from 'node:child_process';
|
|
20
5
|
import { fileURLToPath } from 'node:url';
|
|
21
|
-
import { readProfile } from '../lib/project.mjs';
|
|
22
|
-
import { resolveTemplate } from './resolve.mjs';
|
|
23
|
-
|
|
24
|
-
const git = (root, args) => spawnSync('git', ['-c', 'core.quotepath=false', ...args], { cwd: root, encoding: 'utf8', maxBuffer: 256 * 1024 * 1024 });
|
|
25
|
-
const toPosix = (p) => p.split(path.sep).join('/');
|
|
26
6
|
|
|
27
7
|
import { matchGlob } from '../lib/path-match.mjs';
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
secret: '敏感文件,不读内容;它出现在改动里就提醒用户确认是否误提交了凭据',
|
|
42
|
-
dependency: '依赖目录',
|
|
43
|
-
generated: '生成文件或锁文件;依赖变化看 pom.xml / package.json / go.mod 等清单文件',
|
|
44
|
-
docs: '文档',
|
|
45
|
-
tooling: 'workflow-run 的产物或已安装的 skills',
|
|
46
|
-
user_exclude: '用户指定不审(--exclude 或画像 review.exclude)',
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
export function skipReason(file, { include = [], exclude = [] } = {}) {
|
|
50
|
-
if (file.status === 'D') return 'deleted';
|
|
51
|
-
if (file.binary) return 'binary';
|
|
52
|
-
const p = file.path;
|
|
53
|
-
if (matchAny(p, SECRET) && !matchAny(p, SECRET_OK)) return 'secret';
|
|
54
|
-
if (matchAny(p, exclude)) return 'user_exclude';
|
|
55
|
-
if (matchAny(p, include)) return null;
|
|
56
|
-
if (matchAny(p, DEPENDENCY)) return 'dependency';
|
|
57
|
-
if (matchAny(p, GENERATED)) return 'generated';
|
|
58
|
-
if (matchAny(p, DOCS)) return 'docs';
|
|
59
|
-
if (matchAny(p, TOOLING)) return 'tooling';
|
|
60
|
-
return null;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// ---------------------------------------------------------------- 分类、风险、清单小节
|
|
64
|
-
const LANG = [
|
|
65
|
-
[/\.(java|kt|kts|scala|groovy)$/i, 'Java / Kotlin'], [/\.go$/i, 'Go'], [/\.(ts|tsx|js|jsx|mjs|cjs|vue)$/i, 'TypeScript / JavaScript'],
|
|
66
|
-
[/\.(py|pyi)$/i, 'Python'], [/\.sql$/i, 'SQL / MyBatis'],
|
|
67
|
-
];
|
|
68
|
-
const TEST = /(^|\/)(src\/test\/|tests?\/|__tests__\/)|(Test|Tests|Spec)\.(java|kt|scala|groovy|swift)$|[._-](test|spec)\.[jt]sx?$|_test\.(go|py|rs)$|(^|\/)test_[^/]*\.py$/i;
|
|
69
|
-
const CONFIG = /(^|\/)(application|bootstrap)[^/]*\.(ya?ml|properties)$|\.(properties|conf|ini|toml)$|(^|\/)config\/[^/]*\.(ya?ml|json)$|(^|\/)\.env\.(example|sample|template)$/i;
|
|
70
|
-
const BUILD = /(^|\/)(pom\.xml|build\.gradle(\.kts)?|settings\.gradle(\.kts)?|package\.json|go\.mod|requirements[^/]*\.txt|pyproject\.toml|Cargo\.toml|composer\.json|Gemfile)$/i;
|
|
71
|
-
// 迁移目录只认 SQL 和 Flyway / Liquibase 的变更集格式;db/ 下的代码文件照常按语言判断
|
|
72
|
-
const SQLFILE = /\.sql$|(^|\/)(db|migration|migrations|flyway|liquibase)\/(?:[^/]+\/)*[^/]+\.(xml|ya?ml|json)$/i;
|
|
73
|
-
const MAPPER = /mapper[^/]*\.xml$|dao[^/]*\.xml$/i;
|
|
74
|
-
const DATA_OBJ = /(^|\/)(entity|entities|dto|vo|po|bo|domain|model|models|pojo|converter|convert|assembler)(\/|$)|(Entity|DTO|Dto|VO|Vo|PO|BO|Converter|Assembler|Mapping)\.(java|kt|ts|go|py)$/;
|
|
75
|
-
const ENTRY = /(^|\/)(controller|controllers|api|apis|endpoint|resource|resources|handler|handlers|router|routes)\/|Controller\.(java|kt)$/i;
|
|
76
|
-
const P1_PATH = /(^|\/)(service|services|impl|biz|manager|job|jobs|task|tasks|listener|consumer|consumers|handler|handlers|domain|entity|dto|vo|converter|repository|dao)\//i;
|
|
77
|
-
|
|
78
|
-
// 只看增删的行。MyBatis 的 ${} 不放这里(JS 模板字符串也是 ${}),Mapper XML 和 .sql 按文件类型已经是 P0
|
|
79
|
-
const KW = {
|
|
80
|
-
p0: /@Transactional|synchronized|\.(?:try)?lock\(|ReentrantLock|@Version|BigDecimal|amount|balance|@KafkaListener|KafkaTemplate|RabbitTemplate|RocketMQ|@PreAuthorize|@Secured|ALTER TABLE|CREATE TABLE|DELETE FROM|UPDATE\s+\S+\s+SET/i,
|
|
81
|
-
concurrency: /synchronized|\.(?:try)?lock\(|ReentrantLock|Atomic[A-Z]|ConcurrentHashMap|volatile|@Async|ThreadPool|Executor|CompletableFuture|go func|sync\.(Mutex|RWMutex|WaitGroup)|@Version|setnx|redisson/i,
|
|
82
|
-
mq: /@KafkaListener|@RabbitListener|@RocketMQMessageListener|@JmsListener|KafkaTemplate|RabbitTemplate|RocketMQTemplate|\.send\(|callback|notify|@Transactional/i,
|
|
83
|
-
sql: /\b(SELECT|INSERT|UPDATE|DELETE)\b.+\b(FROM|INTO|SET|WHERE)\b|@Select|@Update|@Insert|@Delete|createQuery|nativeQuery|jdbcTemplate/i,
|
|
84
|
-
config: /@Value\s*\(|@ConfigurationProperties|@NacosValue|@ApolloConfig|getProperty\(|os\.Getenv|process\.env/,
|
|
85
|
-
security: /@PreAuthorize|@Secured|permission|auth|token|password|secret|role/i,
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
// 赋值、参数、对象值等位置后面的 /…/flags 换成占位,避免模式里的关键字被当成改动内容
|
|
89
|
-
// 行注释(// 前要有空白或在行首,避免把 http:// 当成注释)和以 * 开头的块注释行
|
|
90
|
-
export const stripComments = (text) => text.split('\n').map((l) => (/^\s*(\*|\/\*)/.test(l) ? '' : l.replace(/(^|\s)\/\/.*$/, '$1'))).join('\n');
|
|
91
|
-
export const stripRegexLiterals = (text) => text.replace(/([=(:,[!&|?{};]\s*|return\s+)\/(?![*/])(?:\\.|\[(?:\\.|[^\]\\\n])*\]|[^/\\\n[])+\/[a-z]*/g, '$1/re/');
|
|
92
|
-
|
|
93
|
-
export function classify(file, profileRules = []) {
|
|
94
|
-
const p = file.path;
|
|
95
|
-
// 注释不算;JS / TS 里的正则字面量也不算(规则脚本自己写着匹配关键字的模式,不是业务代码)
|
|
96
|
-
let changed = file.changed_text ?? '';
|
|
97
|
-
if (/\.(java|kt|kts|scala|groovy|go|m?[jt]sx?|cjs|vue)$/i.test(p)) changed = stripComments(changed);
|
|
98
|
-
if (/\.(m?[jt]sx?|cjs|vue)$/i.test(p)) changed = stripRegexLiterals(changed);
|
|
99
|
-
const hit = (k) => KW[k].test(changed);
|
|
100
|
-
const lang = LANG.find(([re]) => re.test(p))?.[1] ?? (MAPPER.test(p) ? 'SQL / MyBatis' : null);
|
|
101
|
-
// 构建文件先于配置判断:pyproject.toml / Cargo.toml 也是 .toml
|
|
102
|
-
const kind = TEST.test(p) ? 'test' : MAPPER.test(p) || SQLFILE.test(p) ? 'sql' : BUILD.test(p) ? 'build' : CONFIG.test(p) ? 'config' : lang ? 'code' : 'other';
|
|
103
|
-
const s = new Set();
|
|
104
|
-
if (kind === 'test') { s.add(1); s.add(13); }
|
|
105
|
-
else if (kind === 'sql') { [1, 6, 7, 12].forEach((n) => s.add(n)); }
|
|
106
|
-
else if (kind === 'config') { [6, 10].forEach((n) => s.add(n)); }
|
|
107
|
-
else if (kind === 'build') { s.add(6); }
|
|
108
|
-
else { [1, 2, 5, 6, 7].forEach((n) => s.add(n)); }
|
|
109
|
-
if (kind === 'code' || kind === 'other') {
|
|
110
|
-
if (DATA_OBJ.test(p)) s.add(3);
|
|
111
|
-
if (hit('concurrency')) s.add(4);
|
|
112
|
-
if (hit('mq')) s.add(11);
|
|
113
|
-
if (hit('sql')) s.add(12);
|
|
114
|
-
if (hit('config')) s.add(10);
|
|
115
|
-
}
|
|
116
|
-
if (lang && kind !== 'config' && kind !== 'build') s.add(14);
|
|
117
|
-
const risk = kind === 'test' ? 'P2'
|
|
118
|
-
: kind === 'sql' || kind === 'config' || ENTRY.test(p) || (kind === 'code' && hit('p0')) ? 'P0'
|
|
119
|
-
: P1_PATH.test(p) || DATA_OBJ.test(p) || kind === 'build' ? 'P1' : 'P2';
|
|
120
|
-
const project_rules = profileRules.filter((r) => r?.path && r?.rule && matchGlob(p, r.path)).map((r) => ({ path: r.path, rule: r.rule }));
|
|
121
|
-
return { kind, lang, risk, sections: [...s].sort((a, b) => a - b), ...(project_rules.length ? { project_rules } : {}) };
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
// ---------------------------------------------------------------- diff
|
|
125
|
-
// git diff -U0 的输出 → [{ path, old_path?, status, binary, added, deleted, hunks: [[start, end]](新文件里新增 / 改动的行), changed_text }]
|
|
126
|
-
export function parseDiff(text) {
|
|
127
|
-
const files = [];
|
|
128
|
-
let f = null;
|
|
129
|
-
let newLine = 0;
|
|
130
|
-
const strip = (s) => s.replace(/^"|"$/g, '').replace(/^[ab]\//, '');
|
|
131
|
-
for (const line of text.split('\n')) {
|
|
132
|
-
if (line.startsWith('diff --git ')) {
|
|
133
|
-
const m = /^diff --git (?:"?a\/(.+?)"?) (?:"?b\/(.+?)"?)$/.exec(line);
|
|
134
|
-
f = { path: m?.[2] ?? '', old: m?.[1] ?? '', status: 'M', binary: false, added: 0, deleted: 0, hunks: [], changed: [], inHunk: false };
|
|
135
|
-
files.push(f);
|
|
136
|
-
continue;
|
|
137
|
-
}
|
|
138
|
-
if (!f) continue;
|
|
139
|
-
// 进入 hunk 之后以 +/- 开头的都是内容(被删的 SQL 注释「-- x」在 diff 里就是「--- x」)
|
|
140
|
-
if (f.inHunk) {
|
|
141
|
-
if (line.startsWith('@@')) { /* 下面统一处理 */ }
|
|
142
|
-
else if (line.startsWith('+')) { f.added++; f.changed.push(line.slice(1)); continue; }
|
|
143
|
-
else if (line.startsWith('-')) { f.deleted++; f.changed.push(line.slice(1)); continue; }
|
|
144
|
-
else continue;
|
|
145
|
-
}
|
|
146
|
-
if (line.startsWith('new file mode')) f.status = 'A';
|
|
147
|
-
else if (line.startsWith('deleted file mode')) f.status = 'D';
|
|
148
|
-
else if (line.startsWith('rename from ')) { f.old = line.slice(12); f.status = 'R'; }
|
|
149
|
-
else if (line.startsWith('rename to ')) f.path = line.slice(10);
|
|
150
|
-
else if (line.startsWith('Binary files ')) f.binary = true;
|
|
151
|
-
else if (line.startsWith('--- ')) { if (line !== '--- /dev/null') f.old = strip(line.slice(4)); }
|
|
152
|
-
else if (line.startsWith('+++ ')) { if (line !== '+++ /dev/null') f.path = strip(line.slice(4)); }
|
|
153
|
-
else if (line.startsWith('@@')) {
|
|
154
|
-
const m = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/.exec(line);
|
|
155
|
-
newLine = Number(m?.[1] ?? 0);
|
|
156
|
-
const count = m?.[2] === undefined ? 1 : Number(m[2]);
|
|
157
|
-
if (count > 0) f.hunks.push([newLine, newLine + count - 1]);
|
|
158
|
-
f.inHunk = true;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
return files.map(({ old, changed, inHunk, ...x }) => ({ ...x, ...(x.status === 'R' && old !== x.path ? { old_path: old } : {}), changed_text: changed.join('\n') }));
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// 范围 → { ok, mode, range, ref('WORKTREE' 或读文件用的提交), files }
|
|
165
|
-
export function loadDiff(root, { diff = null, base = null } = {}) {
|
|
166
|
-
if (diff) {
|
|
167
|
-
if (!/\.{2,3}/.test(diff)) return { ok: false, error: '--diff 写成 <base>...<head>' };
|
|
168
|
-
const head = diff.split(/\.{2,3}/)[1] || 'HEAD';
|
|
169
|
-
const r = git(root, ['diff', '-U0', '-M', '--no-color', '--no-ext-diff', diff]);
|
|
170
|
-
if (r.status !== 0) return { ok: false, error: r.stderr.trim() || `git diff ${diff} 失败` };
|
|
171
|
-
return { ok: true, mode: 'diff', range: diff, ref: head, files: parseDiff(r.stdout) };
|
|
172
|
-
}
|
|
173
|
-
const b = base || readProfile(root).profile?.main_branch || 'main';
|
|
174
|
-
const mb = git(root, ['merge-base', b, 'HEAD']);
|
|
175
|
-
if (mb.status !== 0) return { ok: false, error: mb.stderr.trim() || `找不到 ${b} 与 HEAD 的合并基点` };
|
|
176
|
-
const from = mb.stdout.trim();
|
|
177
|
-
const r = git(root, ['diff', '-U0', '-M', '--no-color', '--no-ext-diff', from]);
|
|
178
|
-
if (r.status !== 0) return { ok: false, error: r.stderr.trim() || `git diff ${b} 失败` };
|
|
179
|
-
const files = parseDiff(r.stdout);
|
|
180
|
-
// 未跟踪的新文件:git diff 看不到,按整文件新增处理
|
|
181
|
-
for (const p of git(root, ['ls-files', '--others', '--exclude-standard', '-z']).stdout.split('\0').filter(Boolean)) {
|
|
182
|
-
const abs = path.join(root, p);
|
|
183
|
-
let buf;
|
|
184
|
-
try { buf = fs.readFileSync(abs); } catch { continue; }
|
|
185
|
-
const binary = buf.subarray(0, 8000).includes(0);
|
|
186
|
-
const text = binary ? '' : buf.toString('utf8');
|
|
187
|
-
const n = binary || !text ? 0 : text.split('\n').length - (text.endsWith('\n') ? 1 : 0);
|
|
188
|
-
files.push({ path: toPosix(p), status: 'A', untracked: true, binary, added: n, deleted: 0, hunks: n ? [[1, n]] : [], changed_text: text });
|
|
189
|
-
}
|
|
190
|
-
return { ok: true, mode: 'base', range: `${b}...(含未提交与未跟踪)`, base: b, ref: 'WORKTREE', files };
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
// 文件在 ref 上的内容(WORKTREE 读磁盘);读不到返回 null
|
|
194
|
-
export function readAt(root, ref, file) {
|
|
195
|
-
if (ref === 'WORKTREE') { try { return fs.readFileSync(path.join(root, file), 'utf8'); } catch { return null; } }
|
|
196
|
-
const r = git(root, ['show', `${ref}:${file}`]);
|
|
197
|
-
return r.status === 0 ? r.stdout : null;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
// ---------------------------------------------------------------- 分批(同 diff-strategy.md)
|
|
201
|
-
const FULL_FILES = 30, FULL_LINES = 1500, BATCH_FILES = 15, BATCH_LINES = 800, TOO_MANY = 500;
|
|
202
|
-
export function plan(review) {
|
|
203
|
-
const lines = review.reduce((a, f) => a + f.added + f.deleted, 0);
|
|
204
|
-
if (review.length > TOO_MANY) return { size: '过大', batches: [], hint: `排除后还有 ${review.length} 个文件:停下,请用户按模块或提交区间缩小范围` };
|
|
205
|
-
const order = { P0: 0, P1: 1, P2: 2 };
|
|
206
|
-
const sorted = [...review].sort((a, b) => order[a.risk] - order[b.risk] || a.path.localeCompare(b.path));
|
|
207
|
-
if (review.length <= FULL_FILES && lines <= FULL_LINES) return { size: '全量精读', batches: [sorted.map((f) => f.path)] };
|
|
208
|
-
const batches = [];
|
|
209
|
-
let cur = [], n = 0;
|
|
210
|
-
for (const f of sorted) {
|
|
211
|
-
const l = f.added + f.deleted;
|
|
212
|
-
if (cur.length && (cur.length >= BATCH_FILES || n + l > BATCH_LINES || cur.at(-1).risk !== f.risk)) { batches.push(cur); cur = []; n = 0; }
|
|
213
|
-
cur.push(f); n += l;
|
|
214
|
-
}
|
|
215
|
-
if (cur.length) batches.push(cur);
|
|
216
|
-
return { size: '分批精读', batches: batches.map((b) => b.map((f) => f.path)), hint: '按批次顺序精读(P0 → P1 → P2),P0 不能跳过;没读完的批次写进报告「未覆盖与待确认」' };
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
export function reviewConfig(root) {
|
|
220
|
-
const r = readProfile(root).profile?.review ?? {};
|
|
221
|
-
const list = (v) => (Array.isArray(v) ? v.filter((x) => typeof x === 'string' && x.trim()) : []);
|
|
222
|
-
return { include: list(r.include), exclude: list(r.exclude), rules: Array.isArray(r.rules) ? r.rules : [] };
|
|
223
|
-
}
|
|
8
|
+
import {
|
|
9
|
+
classify,
|
|
10
|
+
loadDiff,
|
|
11
|
+
parseDiff,
|
|
12
|
+
plan,
|
|
13
|
+
readAt,
|
|
14
|
+
reviewConfig,
|
|
15
|
+
scope,
|
|
16
|
+
skipReason,
|
|
17
|
+
stripComments,
|
|
18
|
+
stripRegexLiterals,
|
|
19
|
+
} from '../lib/review-scope.mjs';
|
|
20
|
+
import { resolveTemplate } from './resolve.mjs';
|
|
224
21
|
|
|
225
|
-
export
|
|
226
|
-
|
|
227
|
-
if (!d.ok) return d;
|
|
228
|
-
const cfg = opts.config ?? reviewConfig(root);
|
|
229
|
-
const exclude = [...cfg.exclude, ...(opts.exclude ?? [])];
|
|
230
|
-
const review = [], skipped = [];
|
|
231
|
-
for (const f of d.files) {
|
|
232
|
-
const reason = skipReason(f, { include: cfg.include, exclude });
|
|
233
|
-
const base = { path: f.path, ...(f.old_path ? { old_path: f.old_path } : {}), status: f.status, ...(f.untracked ? { untracked: true } : {}), added: f.added, deleted: f.deleted };
|
|
234
|
-
if (reason) { skipped.push({ ...base, reason, note: NOTE[reason] }); continue; }
|
|
235
|
-
review.push({ ...base, ...classify(f, cfg.rules), ...(f.added + f.deleted > BATCH_LINES ? { large: true } : {}) });
|
|
236
|
-
}
|
|
237
|
-
const p = plan(review);
|
|
238
|
-
const count = (xs, k) => xs.reduce((m, x) => ((m[x[k]] = (m[x[k]] ?? 0) + 1), m), {});
|
|
239
|
-
return {
|
|
240
|
-
ok: true, mode: d.mode, range: d.range, ref: d.ref,
|
|
241
|
-
summary: {
|
|
242
|
-
files: d.files.length, review: review.length, skipped: skipped.length,
|
|
243
|
-
lines: review.reduce((a, f) => a + f.added + f.deleted, 0), risk: count(review, 'risk'), skipped_by_reason: count(skipped, 'reason'), size: p.size,
|
|
244
|
-
},
|
|
245
|
-
overall_sections: review.length ? [9, 13] : [],
|
|
246
|
-
review, skipped, batches: p.batches,
|
|
247
|
-
...(p.hint ? { hint: p.hint } : {}),
|
|
248
|
-
next: '每个文件按 sections 过清单(rules <文件…> 打印对应小节原文和项目规则);整个改动再过一次 overall_sections(9 影响面、13 测试)。报告「未覆盖与待确认」按 reason 汇总 skipped,没读完的文件逐个列出',
|
|
249
|
-
};
|
|
250
|
-
}
|
|
22
|
+
export { matchGlob, globToRegExp } from '../lib/path-match.mjs';
|
|
23
|
+
export { classify, loadDiff, parseDiff, plan, readAt, reviewConfig, scope, skipReason, stripComments, stripRegexLiterals };
|
|
251
24
|
|
|
252
|
-
// ---------------------------------------------------------------- rules:打印清单小节
|
|
253
25
|
export function checklistSections(text) {
|
|
254
26
|
const out = {};
|
|
255
27
|
const parts = text.replace(/\r/g, '').split(/^(?=## )/m);
|
|
256
28
|
for (const part of parts) {
|
|
257
|
-
const
|
|
258
|
-
if (
|
|
29
|
+
const match = /^## (\d+)[.、.]/.exec(part);
|
|
30
|
+
if (match) out[Number(match[1])] = part.trimEnd();
|
|
259
31
|
}
|
|
260
32
|
return out;
|
|
261
33
|
}
|
|
262
34
|
|
|
263
|
-
// 语言专项(14)只取对应语言那一条
|
|
264
35
|
function languageBullet(section, lang) {
|
|
265
36
|
if (!section || !lang) return null;
|
|
266
37
|
const head = section.split('\n')[0];
|
|
267
|
-
const bullet = section.split('\n').find((
|
|
38
|
+
const bullet = section.split('\n').find((line) => line.includes(`**${lang}**`));
|
|
268
39
|
return bullet ? `${head}\n${bullet}` : null;
|
|
269
40
|
}
|
|
270
41
|
|
|
271
42
|
function readChecklist(root) {
|
|
272
|
-
const
|
|
273
|
-
if (!
|
|
274
|
-
const abs = path.resolve(root,
|
|
275
|
-
for (const
|
|
276
|
-
|
|
43
|
+
const template = resolveTemplate(root, 'project-review', 'checklist.md');
|
|
44
|
+
if (!template.ok) return { ok: false, error: template.hint ?? template.message ?? '取不到审查清单' };
|
|
45
|
+
const abs = path.resolve(root, template.path);
|
|
46
|
+
for (const file of [abs, `${abs}.tpl`]) {
|
|
47
|
+
try { return { ok: true, path: template.path, text: fs.readFileSync(file, 'utf8') }; } catch { /* 下一个 */ }
|
|
48
|
+
}
|
|
49
|
+
return { ok: false, error: `读不到审查清单 ${template.path}` };
|
|
277
50
|
}
|
|
278
51
|
|
|
279
|
-
// 文件在本次范围里时沿用 scope 的分类(按改动内容判断的小节一致);不在范围里的按整个文件内容判断
|
|
280
52
|
export function rulesFor(root, files, { config = reviewConfig(root), checklist = readChecklist(root), inScope = [] } = {}) {
|
|
281
53
|
if (!checklist.ok) return checklist;
|
|
282
|
-
const
|
|
283
|
-
const parsed = Object.keys(
|
|
284
|
-
const known = new Map(inScope.map((
|
|
285
|
-
const
|
|
286
|
-
const key =
|
|
54
|
+
const sections = checklistSections(checklist.text);
|
|
55
|
+
const parsed = Object.keys(sections).length > 0;
|
|
56
|
+
const known = new Map(inScope.map((entry) => [entry.path, entry]));
|
|
57
|
+
const perFile = files.map((filePath) => {
|
|
58
|
+
const key = filePath.split(path.sep).join('/').replace(/^\.\//, '');
|
|
287
59
|
if (known.has(key)) {
|
|
288
|
-
const { project_rules, ...
|
|
289
|
-
const
|
|
290
|
-
|
|
60
|
+
const { project_rules, ...entry } = known.get(key);
|
|
61
|
+
const projectRules = config.rules
|
|
62
|
+
.filter((rule) => rule?.path && rule?.rule && matchGlob(key, rule.path))
|
|
63
|
+
.map((rule) => ({ path: rule.path, rule: rule.rule }));
|
|
64
|
+
return { ...entry, ...(projectRules.length ? { project_rules: projectRules } : {}) };
|
|
291
65
|
}
|
|
292
|
-
|
|
293
|
-
|
|
66
|
+
let text = '';
|
|
67
|
+
try { text = fs.readFileSync(path.join(root, key), 'utf8'); } catch { /* 缺失文件只做路径分类 */ }
|
|
68
|
+
return { path: key, ...classify({ path: key, changed_text: text }, config.rules) };
|
|
294
69
|
});
|
|
295
|
-
const
|
|
296
|
-
const
|
|
297
|
-
const
|
|
298
|
-
for (const
|
|
299
|
-
|
|
300
|
-
if (!parsed)
|
|
301
|
-
for (const
|
|
302
|
-
if (!
|
|
303
|
-
if (
|
|
304
|
-
|
|
70
|
+
const numbers = [...new Set(perFile.flatMap((entry) => entry.sections))].sort((a, b) => a - b);
|
|
71
|
+
const languages = [...new Set(perFile.map((entry) => entry.lang).filter(Boolean))];
|
|
72
|
+
const output = [`# 审查要点:${perFile.map((entry) => entry.path).join('、')}`, '', `清单:${checklist.path}`, ''];
|
|
73
|
+
for (const entry of perFile) output.push(`- ${entry.path}:${entry.kind},风险 ${entry.risk},清单 ${entry.sections.join(' / ')}`);
|
|
74
|
+
output.push('');
|
|
75
|
+
if (!parsed) output.push('(清单没有「## <编号>.」格式的小节,按整份清单检查)', '', checklist.text.trim());
|
|
76
|
+
for (const number of numbers) {
|
|
77
|
+
if (!sections[number]) { output.push(`(清单里没有第 ${number} 节)`, ''); continue; }
|
|
78
|
+
if (number === 14) {
|
|
79
|
+
for (const language of languages) {
|
|
80
|
+
const bullet = languageBullet(sections[14], language);
|
|
81
|
+
if (bullet) output.push(bullet, '');
|
|
82
|
+
}
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
output.push(sections[number], '');
|
|
305
86
|
}
|
|
306
|
-
const
|
|
307
|
-
if (
|
|
308
|
-
|
|
309
|
-
for (const
|
|
87
|
+
const projectRules = perFile.flatMap((entry) => (entry.project_rules ?? []).map((rule) => ({ ...rule, file: entry.path })));
|
|
88
|
+
if (projectRules.length) {
|
|
89
|
+
output.push('## 项目规则(workflow/profile.json 的 review.rules,和上面的清单一起检查)', '');
|
|
90
|
+
for (const rule of projectRules) output.push(`- \`${rule.file}\`(匹配 \`${rule.path}\`):${rule.rule}`);
|
|
310
91
|
}
|
|
311
|
-
return { ok: true, text:
|
|
92
|
+
return { ok: true, text: output.join('\n').trimEnd() + '\n' };
|
|
312
93
|
}
|
|
313
94
|
|
|
314
95
|
function main(argv) {
|
|
315
96
|
let root = process.cwd();
|
|
316
|
-
const
|
|
97
|
+
const options = { exclude: [] };
|
|
317
98
|
const rest = [];
|
|
318
|
-
for (let
|
|
319
|
-
if (argv[
|
|
320
|
-
else if (argv[
|
|
321
|
-
else if (argv[
|
|
322
|
-
else if (argv[
|
|
323
|
-
else rest.push(argv[
|
|
99
|
+
for (let index = 0; index < argv.length; index++) {
|
|
100
|
+
if (argv[index] === '--root') root = path.resolve(argv[++index] ?? '.');
|
|
101
|
+
else if (argv[index] === '--diff') options.diff = argv[++index] ?? null;
|
|
102
|
+
else if (argv[index] === '--base') options.base = argv[++index] ?? null;
|
|
103
|
+
else if (argv[index] === '--exclude') options.exclude.push(...(argv[++index] ?? '').split(',').map((value) => value.trim()).filter(Boolean));
|
|
104
|
+
else rest.push(argv[index]);
|
|
324
105
|
}
|
|
325
106
|
if (rest[0] === 'rules') {
|
|
326
107
|
if (rest.length < 2) { console.error('用法: review-scope.mjs rules <文件> [<文件>…]'); return 1; }
|
|
327
|
-
const
|
|
328
|
-
const
|
|
329
|
-
if (!
|
|
330
|
-
process.stdout.write(
|
|
108
|
+
const scan = scope(root, options);
|
|
109
|
+
const result = rulesFor(root, rest.slice(1), { inScope: scan.ok ? scan.review : [] });
|
|
110
|
+
if (!result.ok) { console.error(result.error); return 1; }
|
|
111
|
+
process.stdout.write(result.text);
|
|
331
112
|
return 0;
|
|
332
113
|
}
|
|
333
|
-
if (rest.length) {
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
114
|
+
if (rest.length) {
|
|
115
|
+
console.error('用法: review-scope.mjs [--diff <base>...<head> | --base <分支>] [--exclude a,b] | rules <文件…>');
|
|
116
|
+
return 1;
|
|
117
|
+
}
|
|
118
|
+
const result = scope(root, options);
|
|
119
|
+
console.log(JSON.stringify(result, null, 2));
|
|
120
|
+
return result.ok ? 0 : 1;
|
|
337
121
|
}
|
|
338
122
|
|
|
339
123
|
export { main };
|
package/workflow/scripts/run.mjs
CHANGED
|
@@ -5,9 +5,7 @@ import path from 'node:path';
|
|
|
5
5
|
import { spawnSync } from 'node:child_process';
|
|
6
6
|
import { fileURLToPath } from 'node:url';
|
|
7
7
|
|
|
8
|
-
import {
|
|
9
|
-
import { codeSnapshot } from '../lib/code-snapshot.mjs';
|
|
10
|
-
export { FLOWS, STATUS_LABELS, allRuns, branchRuns, runState, startRun, changeRun } from '../lib/runs.mjs';
|
|
8
|
+
import { createWorkflow } from '../core/index.mjs';
|
|
11
9
|
const branchOf = (root) => spawnSync('git', ['symbolic-ref', '--short', 'HEAD'], { cwd: root, encoding: 'utf8' }).stdout?.trim() || null;
|
|
12
10
|
|
|
13
11
|
function main(argv) {
|
|
@@ -21,15 +19,29 @@ function main(argv) {
|
|
|
21
19
|
const root = path.resolve(opts.root ?? '.'); const branch = opts.branch ?? branchOf(root);
|
|
22
20
|
if (!branch) throw new Error('无法确定当前分支,请用 --branch 指定');
|
|
23
21
|
const input = opts.input ? JSON.parse(fs.readFileSync(opts.input === '-' ? 0 : path.resolve(root, opts.input), 'utf8')) : {};
|
|
22
|
+
const workflow = createWorkflow({ root });
|
|
24
23
|
let out;
|
|
25
24
|
if (action === 'show') {
|
|
26
|
-
out =
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
else
|
|
30
|
-
|
|
25
|
+
out = workflow.runs.status({ branch, id: opts.id ?? null });
|
|
26
|
+
} else if (action === 'snapshot') out = workflow.snapshots.capture({ branch, id: opts.id });
|
|
27
|
+
else if (action === 'start') out = workflow.runs.start({ branch, kind: opts.kind, size: opts.size, input, dir: opts.dir });
|
|
28
|
+
else {
|
|
29
|
+
const expectedRevision = opts['expect-revision'] === undefined ? null : Number(opts['expect-revision']);
|
|
30
|
+
if (action === 'save') out = workflow.runs.save({ branch, id: opts.id, expectedRevision, input });
|
|
31
|
+
else if (action === 'complete') out = workflow.runs.complete({ branch, id: opts.id, expectedRevision, input });
|
|
32
|
+
else if (action === 'cancel') out = workflow.runs.cancel({ branch, id: opts.id, expectedRevision, reason: input.reason });
|
|
33
|
+
else throw new Error(`未知动作:${action}`);
|
|
34
|
+
}
|
|
31
35
|
console.log(JSON.stringify(out, null, 2)); return 0;
|
|
32
|
-
} catch (
|
|
36
|
+
} catch (error) {
|
|
37
|
+
console.error(JSON.stringify({
|
|
38
|
+
ok: false,
|
|
39
|
+
...(error.code ? { code: error.code } : {}),
|
|
40
|
+
message: error.message,
|
|
41
|
+
...(error.details && Object.keys(error.details).length ? { details: error.details } : {}),
|
|
42
|
+
}, null, 2));
|
|
43
|
+
return 1;
|
|
44
|
+
}
|
|
33
45
|
}
|
|
34
46
|
const invoked = process.argv[1] && fs.realpathSync(process.argv[1]) === fs.realpathSync(fileURLToPath(import.meta.url));
|
|
35
47
|
if (invoked) process.exit(main(process.argv.slice(2)));
|