feat(workflow): implement workflow create command from scratch with draft state defense
This commit is contained in:
1 parent
aa58dd7f6f
commit
02b197c119
7 files changed
+390
-7
No files matched your search
@@ -1297,4 +1297,167 @@ describe("CLI - workflow job 执行详情与异常精准透视", () => {
|
||||
expect(parsedJson.success).toBe(true);
|
||||
expect(parsedJson.pipelineId).toBe("pipe_del_999");
|
||||
});
|
||||
|
||||
it("handles workflow create: requires confirmation, dry-run, execution, triggers, and json mode", async () => {
|
||||
// 1. Missing mutation confirmation
|
||||
await expect(
|
||||
handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "create",
|
||||
args: ["新风险拦截流程"],
|
||||
options: {},
|
||||
raw: ["workflow", "create", "新风险拦截流程"],
|
||||
},
|
||||
mockRuntime
|
||||
)
|
||||
).rejects.toThrow("请先使用 --dry-run");
|
||||
|
||||
// 2. Dry-run preview
|
||||
const dryRunRes = await handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "create",
|
||||
args: ["新风险拦截流程"],
|
||||
options: { trigger: "webhook", "dry-run": true },
|
||||
raw: ["workflow", "create", "新风险拦截流程", "--trigger=webhook", "--dry-run"],
|
||||
},
|
||||
mockRuntime
|
||||
);
|
||||
expect(dryRunRes.exitCode).toBe(0);
|
||||
expect(dryRunRes.stdout).toContain("[DRY-RUN]");
|
||||
expect(dryRunRes.stdout).toContain("新风险拦截流程");
|
||||
expect(dryRunRes.stdout).toContain("webhook");
|
||||
|
||||
let createdPayload: any = null;
|
||||
const mockClient = {
|
||||
post: (url: string, opts: any) => {
|
||||
if (url.includes("api/easo/pipelines")) {
|
||||
createdPayload = opts.json;
|
||||
return {
|
||||
json: async () => ({
|
||||
code: 200,
|
||||
data: {
|
||||
id: "pipe_new_777",
|
||||
label: opts.json.label,
|
||||
platformId: opts.json.platformId || "app_default",
|
||||
state: opts.json.state ?? 0,
|
||||
version: "26.10.1",
|
||||
tasks: opts.json.tasks || [],
|
||||
},
|
||||
}),
|
||||
};
|
||||
}
|
||||
return { json: async () => ({ code: 200, data: null }) };
|
||||
},
|
||||
get: () => ({ json: async () => ({ code: 200, data: null }) }),
|
||||
};
|
||||
|
||||
spies.push(
|
||||
spyOn(helpers, "createClientsForCorp").mockResolvedValue({
|
||||
ekbClient: mockClient as any,
|
||||
easoClient: mockClient as any,
|
||||
corp: mockCorp,
|
||||
} as any)
|
||||
);
|
||||
spies.push(
|
||||
spyOn(helpers, "createPiperClientForCorp").mockResolvedValue(mockClient as any)
|
||||
);
|
||||
|
||||
// 3. Confirmed execution with webhook trigger
|
||||
const createRes = await handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "create",
|
||||
args: ["外部发票接入"],
|
||||
options: { trigger: "webhook", yes: true },
|
||||
raw: ["workflow", "create", "外部发票接入", "--trigger=webhook", "--yes"],
|
||||
},
|
||||
mockRuntime
|
||||
);
|
||||
expect(createRes.exitCode).toBe(0);
|
||||
expect(createRes.stdout).toContain("工作流创建成功");
|
||||
expect(createRes.stdout).toContain("pipe_new_777");
|
||||
expect(createRes.stdout).toContain("外部发票接入");
|
||||
expect(createRes.stdout).toContain("https://piper.ekuaibao.com/api/webhook/");
|
||||
expect(createRes.stdout).toContain("草稿未发布");
|
||||
|
||||
expect(createdPayload).not.toBeNull();
|
||||
expect(createdPayload.label).toBe("外部发票接入");
|
||||
expect(createdPayload.state).toBe(0);
|
||||
expect(createdPayload.tasks).toHaveLength(1);
|
||||
expect(createdPayload.tasks[0].type).toBe("webhook");
|
||||
expect(createdPayload.tasks[0].urlPath).toBeDefined();
|
||||
|
||||
// 4. Create with clock trigger via alias "new"
|
||||
const clockRes = await handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "new",
|
||||
args: ["每日定时同步"],
|
||||
options: { trigger: "clock", yes: true },
|
||||
raw: ["workflow", "new", "每日定时同步", "--trigger=clock", "--yes"],
|
||||
},
|
||||
mockRuntime
|
||||
);
|
||||
expect(clockRes.exitCode).toBe(0);
|
||||
expect(createdPayload.tasks[0].type).toBe("clock");
|
||||
expect(createdPayload.tasks[0].cron).toBe("0 0 * * *");
|
||||
|
||||
// 5. JSON mode via alias "add"
|
||||
const jsonRes = await handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "add",
|
||||
args: ["API创建流程"],
|
||||
options: { yes: true },
|
||||
raw: ["workflow", "add", "API创建流程", "--yes"],
|
||||
},
|
||||
{ ...mockRuntime, jsonMode: true }
|
||||
);
|
||||
expect(jsonRes.exitCode).toBe(0);
|
||||
const parsedJson = JSON.parse(jsonRes.stdout);
|
||||
expect(parsedJson.id).toBe("pipe_new_777");
|
||||
expect(parsedJson.state).toBe(0);
|
||||
|
||||
// 6. Create from template file
|
||||
const tmpFile = `/tmp/test_wf_${Date.now()}.json`;
|
||||
await (await import("fs/promises")).writeFile(
|
||||
tmpFile,
|
||||
JSON.stringify({
|
||||
label: "文件模板流程",
|
||||
state: 1,
|
||||
desc: "来自模板文件",
|
||||
tasks: [
|
||||
{
|
||||
name: "trigger_webhook",
|
||||
type: "webhook",
|
||||
label: "Webhook触发",
|
||||
groupName: "trigger",
|
||||
orderIndex: 0,
|
||||
levelIndex: [0],
|
||||
status: "active",
|
||||
},
|
||||
],
|
||||
})
|
||||
);
|
||||
|
||||
const fileRes = await handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "create",
|
||||
args: [],
|
||||
options: { file: tmpFile, yes: true },
|
||||
raw: ["workflow", "create", `--file=${tmpFile}`, "--yes"],
|
||||
},
|
||||
mockRuntime
|
||||
);
|
||||
expect(fileRes.exitCode).toBe(0);
|
||||
expect(createdPayload.label).toBe("文件模板流程");
|
||||
expect(createdPayload.state).toBe(0);
|
||||
expect(createdPayload.desc).toBe("来自模板文件");
|
||||
expect(createdPayload.tasks[0].urlPath).toBeDefined();
|
||||
|
||||
await (await import("fs/promises")).unlink(tmpFile).catch(() => {});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,89 @@
|
||||
import { createWorkflow } from "../../../domains/platform/services.js";
|
||||
import { stringOption } from "../../parser.js";
|
||||
import { printJson, printKeyValue } from "../../printer.js";
|
||||
import { isDryRun, mutationPreview, requireMutationConfirmation } from "../../mutation.js";
|
||||
import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js";
|
||||
import { createWorkflowDetailClient } from "./shared.js";
|
||||
|
||||
export async function handleCreate(
|
||||
parsed: CliParsedArgs,
|
||||
runtime: CliRuntime
|
||||
): Promise<CliResult> {
|
||||
const name = parsed.args[0] || stringOption(parsed.options, ["name", "label", "n"]);
|
||||
const trigger = stringOption(parsed.options, ["trigger", "t"]);
|
||||
const platformId = stringOption(parsed.options, ["platform-id", "platformId", "app-id", "appId"]);
|
||||
const file = stringOption(parsed.options, ["file", "f"]);
|
||||
const desc = stringOption(parsed.options, ["desc", "description", "d"]);
|
||||
|
||||
requireMutationConfirmation(
|
||||
parsed,
|
||||
runtime,
|
||||
"创建工作流",
|
||||
`hose-use workflow create${name ? ` --name="${name}"` : ""}${trigger ? ` --trigger=${trigger}` : ""}${platformId ? ` --platform-id=${platformId}` : ""}${file ? ` --file=${file}` : ""} --yes`
|
||||
);
|
||||
|
||||
if (isDryRun(parsed, runtime)) {
|
||||
return mutationPreview(parsed, runtime, "创建工作流", name || "未命名流程", {
|
||||
name: name || "未命名流程",
|
||||
trigger: trigger || (file ? "from-file" : "none"),
|
||||
platformId: platformId || "-",
|
||||
fromFile: file || "-",
|
||||
initialState: 0,
|
||||
});
|
||||
}
|
||||
|
||||
const { client } = await createWorkflowDetailClient(parsed, runtime, "");
|
||||
|
||||
const result = await createWorkflow(client, {
|
||||
label: name,
|
||||
trigger,
|
||||
platformId,
|
||||
file,
|
||||
desc,
|
||||
});
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson(result, runtime.outputOptions),
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
const triggerNode = result.tasks?.[0];
|
||||
const triggerSummary = triggerNode ? `${triggerNode.label || triggerNode.name} (${triggerNode.type})` : "无首节点 (需在设计器配置)";
|
||||
const webhookUrl = triggerNode?.type === "webhook" && (triggerNode as any).urlPath
|
||||
? `https://piper.ekuaibao.com/api/webhook/${(triggerNode as any).urlPath}`
|
||||
: undefined;
|
||||
|
||||
const kvData: Record<string, string> = {
|
||||
"新工作流 ID (pipelineId)": result.id,
|
||||
"工作流名称": result.label,
|
||||
"自建应用 ID (platformId)": String((result as any).platformId || "-"),
|
||||
"首节点触发器": triggerSummary,
|
||||
};
|
||||
|
||||
if (webhookUrl) {
|
||||
kvData["Webhook 调用地址"] = webhookUrl;
|
||||
}
|
||||
|
||||
kvData["节点总数"] = String(result.tasks?.length || 0);
|
||||
kvData["版本号"] = result.version || "26.10.1";
|
||||
kvData["当前状态"] = "⚪ 草稿未发布 (停用中)";
|
||||
|
||||
const out = [
|
||||
`✨ 工作流创建成功!新工作流已生成为草稿状态 (state = 0):`,
|
||||
printKeyValue(kvData),
|
||||
"",
|
||||
"下一步操作建议:",
|
||||
` 查看明细: hose-use workflow get ${result.id}`,
|
||||
` 测试执行: hose-use workflow test ${result.id}`,
|
||||
` 发布上线: hose-use workflow publish ${result.id} --yes`,
|
||||
].join("\n");
|
||||
|
||||
return {
|
||||
stdout: `${out}\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import { handleWorkflowDiff, handleWorkflowDiffJob } from "./diff.js";
|
||||
import { handleWorkflowTest } from "./test.js";
|
||||
import { handleCopy } from "./copy.js";
|
||||
import { handleDelete } from "./delete.js";
|
||||
import { handleCreate } from "./create.js";
|
||||
|
||||
export async function handleWorkflowCommand(
|
||||
parsed: CliParsedArgs,
|
||||
@@ -84,10 +85,14 @@ export async function handleWorkflowCommand(
|
||||
case "rm":
|
||||
case "remove":
|
||||
return handleDelete(parsed, runtime);
|
||||
case "create":
|
||||
case "new":
|
||||
case "add":
|
||||
return handleCreate(parsed, runtime);
|
||||
default:
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `未知 workflow 子命令: ${action}。支持: list, get, find, analyze, toggle, publish, patch, save, history, logs, job, diff, diff-job, check, test, copy, delete\n`,
|
||||
stderr: `未知 workflow 子命令: ${action}。支持: list, get, find, analyze, toggle, publish, patch, save, history, logs, job, diff, diff-job, check, test, copy, create, delete\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user