生产启停此前用 ok() 判定结果,该函数只读 response.status 是否等于 200: - 漏掉 HTTP 200 携带 code != 200 的业务失败,与 README「HTTP 200 不等于业务成功」冲突; - 反向也会错,服务端 204 成功会被判成失败。 本次调整: - 删除 transport/response.ts 的 ok(),消除这一判定来源; - toggleWorkflowState 改用 readEasoWriteResponse 校验 EASO 业务码; - management toggleWorkflow 写操作后回读详情确认状态,未变化抛 BusinessError (WORKFLOW_STATE_NOT_APPLIED),回读失败返回 verified: false,不谎称已生效; - lifecycle handleToggle 此前完全不看返回值,无条件输出 [SUCCESS] 且 exitCode 0, 现按 verified 给出 stderr 与退出码;handlePublish 同步调整; - 新增 WorkflowToggleResult 模型并从 services 导出; - 测试覆盖四种情形:回读确认成功、状态未生效、业务失败、回读失败。
79 lines
3.3 KiB
TypeScript
79 lines
3.3 KiB
TypeScript
import { toggleWorkflow } from "../../../domains/platform/workflow/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 handleToggle(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
|
const pipelineId = parsed.args[0] || stringOption(parsed.options, ["id", "pipelineId"]);
|
|
const stateArg = stringOption(parsed.options, ["state", "s"]);
|
|
|
|
if (!pipelineId || stateArg === undefined) {
|
|
return {
|
|
stdout: "",
|
|
stderr: "用法: hose-use workflow toggle <pipelineId> --state=<1|0> (1启用, 0停用)\n",
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
|
|
const state = (stateArg === "1" || stateArg === "true" ? 1 : 0) as 0 | 1;
|
|
requireMutationConfirmation(
|
|
parsed,
|
|
runtime,
|
|
"切换工作流状态",
|
|
`hose-use workflow toggle ${pipelineId} --state=${state} --yes`
|
|
);
|
|
if (isDryRun(parsed, runtime)) {
|
|
return mutationPreview(parsed, runtime, "切换工作流状态", pipelineId, { state });
|
|
}
|
|
const { client } = await createWorkflowDetailClient(parsed, runtime, pipelineId);
|
|
const result = await toggleWorkflow(client, pipelineId, state);
|
|
const label = state === 1 ? "🟢 启用" : "⚪ 停用";
|
|
|
|
if (runtime.jsonMode) {
|
|
return {
|
|
stdout: printJson(result, runtime.outputOptions),
|
|
stderr: "",
|
|
exitCode: result.verified ? 0 : 1,
|
|
};
|
|
}
|
|
|
|
// 启停未回读确认时必须如实告知:不得把「请求已提交」呈现为「已生效」。
|
|
if (!result.verified) {
|
|
return {
|
|
stdout: "",
|
|
stderr: `[WARNING] 工作流 ${pipelineId} 启停请求已提交,但${result.message}\n`,
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
|
|
return {
|
|
stdout: `[SUCCESS] 工作流 ${pipelineId} 状态已更新为: ${label}(已回读确认)\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
export async function handlePublish(parsed: CliParsedArgs, runtime: CliRuntime): Promise<CliResult> {
|
|
const pipelineId = parsed.args[0] || stringOption(parsed.options, ["id", "pipelineId"]);
|
|
if (!pipelineId) return { stdout: "", stderr: "用法: hose-use workflow publish <pipelineId>\n", exitCode: 1 };
|
|
requireMutationConfirmation(
|
|
parsed,
|
|
runtime,
|
|
"发布并启用工作流",
|
|
`hose-use workflow publish ${pipelineId} --yes`
|
|
);
|
|
if (isDryRun(parsed, runtime)) {
|
|
return mutationPreview(parsed, runtime, "发布并启用工作流", pipelineId, { state: 1 });
|
|
}
|
|
const { client } = await createWorkflowDetailClient(parsed, runtime, pipelineId);
|
|
const result = await toggleWorkflow(client, pipelineId, 1);
|
|
const output = { success: result.verified, pipelineId, published: result.verified, state: 1, verified: result.verified, message: result.message };
|
|
if (runtime.jsonMode) return { stdout: printJson(output, runtime.outputOptions), stderr: "", exitCode: result.verified ? 0 : 1 };
|
|
if (!result.verified) {
|
|
return { stdout: "", stderr: `❌ 工作流 [${pipelineId}] 发布状态未能确认:${result.message}\n`, exitCode: 1 };
|
|
}
|
|
return { stdout: `✅ 工作流 [${pipelineId}] 已发布并启用(已回读确认)。\n`, stderr: "", exitCode: 0 };
|
|
}
|