feat(workflow): implement workflow copy and delete lifecycle commands

This commit is contained in:
gitea committed 2026-10-15 15:18:49 +08:00
1 parent fec25bff8a
commit b043cb614e
9 files changed
+521 -2

No files matched your search

+96
View File
@@ -0,0 +1,96 @@
import { copyWorkflow, getWorkflowDetail } 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 handleCopy(
parsed: CliParsedArgs,
runtime: CliRuntime
): Promise<CliResult> {
const pipelineId = parsed.args[0] || stringOption(parsed.options, ["id", "pipelineId", "source"]);
const targetName = stringOption(parsed.options, ["name", "label", "targetName"]);
const targetPlatformId = stringOption(parsed.options, ["platform-id", "platformId", "targetPlatformId"]);
if (!pipelineId) {
return {
stdout: "",
stderr: [
"用法: hose-use workflow copy <pipelineId> [选项]",
"",
"选项:",
" --name=<名称> 指定新工作流名称 (默认原名称加' 副本')",
" --platform-id=<应用ID> 指定目标自建应用 (默认原自建应用)",
" --dry-run 预览复制操作,不实际创建",
" --yes 确认执行复制",
"",
"别名: cp, clone",
"示例: hose-use workflow copy 896398895352487950 --name='发票风险监控-副本' --yes",
].join("\n") + "\n",
exitCode: 1,
};
}
requireMutationConfirmation(
parsed,
runtime,
"复制工作流",
`hose-use workflow copy ${pipelineId}${targetName ? ` --name="${targetName}"` : ""} --yes`
);
const { client } = await createWorkflowDetailClient(parsed, runtime, pipelineId);
if (isDryRun(parsed, runtime)) {
const detail = await getWorkflowDetail(client, pipelineId);
return mutationPreview(parsed, runtime, "复制工作流", pipelineId, {
sourceId: pipelineId,
sourceName: detail.label,
targetName: targetName || (detail.label.includes("副本") ? detail.label : `${detail.label} 副本`),
targetPlatformId: targetPlatformId || detail.platformId,
triggerType: detail.tasks?.[0]?.type || "unknown",
tasksCount: detail.tasks?.length || 0,
initialState: 0,
});
}
const result = await copyWorkflow(client, pipelineId, {
label: targetName,
platformId: targetPlatformId,
});
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 out = [
`✨ 工作流复制成功!新工作流已生成为草稿状态 (state = 0):`,
printKeyValue({
"新工作流 ID (pipelineId)": result.id,
"新工作流名称": result.label,
"自建应用 ID (platformId)": result.platformId || "-",
"首节点触发器": triggerSummary,
"节点总数": String(result.tasks?.length || 0),
"版本号": result.version || "26.10.1",
"当前状态": "⚪ 草稿未发布 (停用中)",
}),
"",
"下一步操作建议:",
` 查看明细: 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,
};
}
+64
View File
@@ -0,0 +1,64 @@
import { deleteWorkflow, getWorkflowDetail } from "../../../domains/platform/services.js";
import { stringOption } from "../../parser.js";
import { printJson } 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 handleDelete(
parsed: CliParsedArgs,
runtime: CliRuntime
): Promise<CliResult> {
const pipelineId = parsed.args[0] || stringOption(parsed.options, ["id", "pipelineId"]);
if (!pipelineId) {
return {
stdout: "",
stderr: [
"用法: hose-use workflow delete <pipelineId> [选项]",
"",
"选项:",
" --dry-run 预览删除操作,不实际删除",
" --yes 确认执行删除",
"",
"别名: rm, remove",
"示例: hose-use workflow delete 898588496146063385 --yes",
].join("\n") + "\n",
exitCode: 1,
};
}
requireMutationConfirmation(
parsed,
runtime,
"删除工作流",
`hose-use workflow delete ${pipelineId} --yes`
);
const { client } = await createWorkflowDetailClient(parsed, runtime, pipelineId);
if (isDryRun(parsed, runtime)) {
const detail = await getWorkflowDetail(client, pipelineId).catch(() => null);
return mutationPreview(parsed, runtime, "删除工作流", pipelineId, {
pipelineId,
name: detail?.label || "未知流程",
state: detail?.state === 1 ? "🟢 启用中" : "⚪ 停用中",
});
}
await deleteWorkflow(client, pipelineId);
if (runtime.jsonMode) {
return {
stdout: printJson({ success: true, pipelineId }, runtime.outputOptions),
stderr: "",
exitCode: 0,
};
}
return {
stdout: `🗑️ 工作流 [${pipelineId}] 已成功删除。\n`,
stderr: "",
exitCode: 0,
};
}
+11 -1
View File
@@ -8,6 +8,8 @@ import { handleCheck } from "./check.js";
import { handleAnalyze } from "./analyze.js";
import { handleWorkflowDiff, handleWorkflowDiffJob } from "./diff.js";
import { handleWorkflowTest } from "./test.js";
import { handleCopy } from "./copy.js";
import { handleDelete } from "./delete.js";
export async function handleWorkflowCommand(
parsed: CliParsedArgs,
@@ -74,10 +76,18 @@ export async function handleWorkflowCommand(
case "debug":
case "run-test":
return handleWorkflowTest(parsed, runtime);
case "copy":
case "cp":
case "clone":
return handleCopy(parsed, runtime);
case "delete":
case "rm":
case "remove":
return handleDelete(parsed, runtime);
default:
return {
stdout: "",
stderr: `未知 workflow 子命令: ${action}。支持: list, get, find, analyze, toggle, publish, patch, save, history, logs, job, diff, diff-job, check, test, debug\n`,
stderr: `未知 workflow 子命令: ${action}。支持: list, get, find, analyze, toggle, publish, patch, save, history, logs, job, diff, diff-job, check, test, copy, delete\n`,
exitCode: 1,
};
}