feat(workflow): support webhook sample records, params preview and expand trigger catalog
This commit is contained in:
1 parent
8531530ccd
commit
c56460716f
6 files changed
+175
-15
No files matched your search
@@ -3,6 +3,7 @@ import {
|
||||
classifyWorkflowTrigger,
|
||||
getWorkflowDebugSource,
|
||||
getWorkflowDetail,
|
||||
getWorkflowSampleData,
|
||||
runWorkflowDebugTest,
|
||||
type WorkflowTriggerCategory,
|
||||
} from "../../../domains/platform/services.js";
|
||||
@@ -47,6 +48,7 @@ export async function handleWorkflowTest(
|
||||
const rawBody = stringOption(parsed.options, ["body", "data", "payload"]);
|
||||
const filePath = stringOption(parsed.options, ["file", "path"]);
|
||||
const isEmpty = booleanOption(parsed.options, ["empty", "none"]);
|
||||
const sampleOption = stringOption(parsed.options, ["sample", "sample-id", "sampleId"]);
|
||||
|
||||
const warnings = ["调试会在真实环境执行工作流节点,可能改变业务数据、发送通知并生成快照;auto-publish 还可能发布流程。"];
|
||||
const confirmExecution = () => requireMutationConfirmation(parsed, runtime, "执行工作流调试", `hose-use workflow test ${pipelineId} --yes`);
|
||||
@@ -79,7 +81,8 @@ export async function handleWorkflowTest(
|
||||
}
|
||||
|
||||
case "WEBHOOK": {
|
||||
// 分支 B: Webhook / 外部事件触发器 ── 接收模拟请求体 (Payload)
|
||||
// 分支 B: Webhook / 外部事件触发器 ── 支持 sample / body / file / empty
|
||||
targetId = "";
|
||||
if (rawBody) {
|
||||
try {
|
||||
customPayload = JSON.parse(rawBody);
|
||||
@@ -107,21 +110,92 @@ export async function handleWorkflowTest(
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
} else if (sampleOption) {
|
||||
// 从服务器获取历史接收样本发起测试
|
||||
try {
|
||||
const sampleResult = await getWorkflowSampleData(client, pipelineId);
|
||||
const records = sampleResult.records || [];
|
||||
let matched = records.find((r) => r.id === sampleOption);
|
||||
if (!matched) {
|
||||
const num = parseInt(sampleOption, 10);
|
||||
if (!isNaN(num) && num >= 1 && num <= records.length) {
|
||||
matched = records[num - 1];
|
||||
}
|
||||
}
|
||||
if (!matched) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `❌ 未找到指定的数据样本 [${sampleOption}]。当前服务器保存了 ${records.length} 条样本。\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
customPayload = JSON.parse(matched.dataJson);
|
||||
} catch (e: any) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `❌ 获取或解析数据样本失败: ${e.message}\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
} else if (isEmpty) {
|
||||
customPayload = {};
|
||||
} else {
|
||||
// 未提供 Payload 时给出交互式指引
|
||||
// 未提供 Payload 时,展示参数定义与服务器最近接收的数据样本
|
||||
let sampleLines: string[] = [];
|
||||
try {
|
||||
const sampleResult = await getWorkflowSampleData(client, pipelineId);
|
||||
const records = sampleResult.records || [];
|
||||
if (records.length > 0) {
|
||||
const rows = records.map((r, idx) => {
|
||||
const timeStr = r.createTime
|
||||
? new Date(r.createTime).toLocaleString("zh-CN", { hour12: false })
|
||||
: "-";
|
||||
let preview = r.dataJson || "";
|
||||
if (preview.length > 55) preview = preview.slice(0, 52) + "...";
|
||||
return [idx + 1, r.id, timeStr, preview];
|
||||
});
|
||||
sampleLines = [
|
||||
"",
|
||||
`── 服务器最近接收的数据样本 (最近 ${records.length} 条) ──`,
|
||||
printTable(["#", "样本 ID", "接收时间", "数据摘要 (Payload Preview)"], rows),
|
||||
];
|
||||
}
|
||||
} catch {}
|
||||
|
||||
// 解析触发器定义的请求参数与中文昵称映射
|
||||
const triggerParams = Array.isArray((triggerNode as any)?.params)
|
||||
? ((triggerNode as any).params as Array<{ key: string; value: string }>)
|
||||
: [];
|
||||
let paramLines: string[] = [];
|
||||
if (triggerParams.length > 0) {
|
||||
paramLines = [
|
||||
"",
|
||||
"── 接收的请求参数定义 (Params) ──",
|
||||
printTable(
|
||||
["参数名 (Key)", "中文昵称 (Label)"],
|
||||
triggerParams.map((p) => [p.key, p.value])
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
return {
|
||||
stdout: [
|
||||
`📡 [外部事件触发器: ${triggerLabel} (${triggerType})]`,
|
||||
`该工作流由 Webhook / 外部系统事件驱动,需提供模拟请求体 (Payload) 进行测试。`,
|
||||
...paramLines,
|
||||
...sampleLines,
|
||||
"",
|
||||
"调试传参选项:",
|
||||
` --body='{"key": "value"}' 直接传入 JSON 请求体`,
|
||||
...(sampleLines.length > 0
|
||||
? [` --sample=<1|2|3> 直接选用上述服务器最近接收的数据样本发起测试`]
|
||||
: []),
|
||||
` --body='{"key": "value"}' 直接传入自定义 JSON 请求体`,
|
||||
` --file=<payload.json> 从本地 JSON 文件读取请求体`,
|
||||
` --empty 使用空对象 {} 直接发起测试`,
|
||||
"",
|
||||
`示例: hose-use workflow test ${pipelineId} --body='{"code": "TEST-001"}'`,
|
||||
`示例: hose-use workflow test ${pipelineId} ${
|
||||
sampleLines.length > 0 ? "--sample=1" : `--body='{"code": "TEST-001"}'`
|
||||
}`,
|
||||
].join("\n") + "\n",
|
||||
stderr: "",
|
||||
exitCode: 1,
|
||||
|
||||
Reference in new issue
Block a user