Files
2026-11-16 17:26:44 -08:00

90 lines
4.6 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 改动验证:本地分支改完、推上去之后,让 Streamline 从这个提交编译出包、在真机上按用例执行、交付报告。
// 这里做两件事:解析用例文件;确认被测提交已经在远端(平台只能拿到推上去的代码)。
import { execFileSync } from "node:child_process";
import { CliError } from "./http.js";
function git(args) {
try {
return execFileSync("git", args, { encoding: "utf8", timeout: 5000, stdio: ["ignore", "pipe", "ignore"] }).trim();
} catch { return null; }
}
/**
* 被测提交:当前目录的 HEAD。要求工作区干净、且这个提交已经推到远端 —— 否则平台验的不是用户以为的代码。
* 用本地记录的远端引用判断(推送时会更新),不额外联网;节点检出时还会再从远端核对一次。
*/
export function changeUnderTest({ allowDirty = false } = {}) {
if (git(["rev-parse", "--is-inside-work-tree"]) !== "true") throw new CliError("当前目录不是 git 仓库", "在被测代码所在的仓库目录里执行");
const commit = git(["rev-parse", "HEAD"]);
if (!commit) throw new CliError("读不到当前提交");
let branch = git(["rev-parse", "--abbrev-ref", "HEAD"]);
if (branch === "HEAD") branch = null;
const dirty = git(["status", "--porcelain", "--untracked-files=no"]);
if (dirty && !allowDirty) {
throw new CliError("有还没提交的改动:平台只能验证已经推送的提交,这些改动不会被验证",
"先 commit 并 push;确实只想验证已推送的部分,加 --allow-dirty");
}
// 以「有没有远端分支包含这个提交」为准,而不是和上游比:从 develop 拉出的分支上游常常就是
// origin/develop,比它领先是正常的,提交其实早就推到了自己的远端分支上。
const onRemote = !!git(["branch", "-r", "--contains", "HEAD"]);
if (!onRemote) {
throw new CliError("当前提交还不在任何远端分支上(以本地记录的远端分支为准)",
branch ? `先推送:git push -u origin ${branch}(推过了的话先 git fetch 再试)` : "先把这个提交推到远端分支");
}
const remote = git(["remote", "get-url", "origin"]);
return { commit, branch, repo: remote ? remote.replace(/\/\/[^@/]+@/, "//") : null };
}
/**
* 用例文件(模板在 skill/SKILL.md):
* ## 背景 改了什么、原本要解决什么
* ## 用例
* ### <用例标题>
* 前置:… (可多行)
* 步骤:… (可多行)
* 检查点:
* - 预期 1
* - 预期 2
*/
export function parseVerifyCases(markdown) {
let background = [];
const cases = [];
let section = null, cur = null, field = null;
for (const raw of markdown.split(/\r?\n/)) {
const line = raw.replace(/\s+$/, "");
const h2 = line.match(/^##\s+(.+)$/);
if (h2) {
const name = h2[1].trim();
section = /背景|background/i.test(name) ? "bg" : /用例|cases?/i.test(name) ? "cases" : null;
cur = null; field = null;
continue;
}
if (section === "bg") { background.push(line); continue; }
if (section !== "cases") continue;
const h3 = line.match(/^###\s+(.+)$/);
if (h3) { cur = { title: h3[1].trim(), pre: [], step: [], checkpoints: [] }; cases.push(cur); field = null; continue; }
if (!cur) continue;
const label = line.match(/^\s*(前置(?:条件)?|步骤|操作步骤|检查点|预期)\s*[::]\s*(.*)$/);
if (label) {
field = /前置/.test(label[1]) ? "pre" : /步骤/.test(label[1]) ? "step" : "checkpoints";
if (label[2]) (field === "checkpoints" ? cur.checkpoints : cur[field]).push(label[2]);
continue;
}
if (!field || !line.trim()) continue;
if (field === "checkpoints") {
const bullet = line.match(/^\s*(?:[-*]|\d+[.)、])\s+(.*)$/);
if (bullet) cur.checkpoints.push(bullet[1]);
else if (cur.checkpoints.length) cur.checkpoints[cur.checkpoints.length - 1] += ` ${line.trim()}`;
} else {
cur[field].push(line);
}
}
if (!cases.length) throw new CliError("用例文件里没有用例", "在「## 用例」下面用「### 用例标题」开始每一条,按 Skill 里的模板写");
const empty = cases.find((c) => !c.checkpoints.length);
if (empty) throw new CliError(`用例「${empty.title}」没有检查点`, "每条用例在「检查点:」下面用「- 」列出要看到的结果");
return {
background: background.join("\n").trim(),
cases: cases.map((c) => ({ title: c.title, pre: c.pre.join("\n").trim() || null, step: c.step.join("\n").trim() || null, checkpoints: c.checkpoints.map((x) => x.trim()) })),
};
}