import { getWorkflowDetail, getWorkflowAvailableNodes } from "../../../domains/platform/workflow/services.js"; import { analyzeWorkflow, extractWorkflowSpecificationIds, type WorkflowFormSpecLike, } from "../../../domains/platform/workflow/analysis/index.js"; import { getFormSpecification } from "../../../domains/bill/tools.js"; import { stringOption, booleanOption } from "../../parser.js"; import { printJson, printTable, printKeyValue } from "../../printer.js"; import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js"; import { createWorkflowDetailClient } from "./shared.js"; import { isClientGoodOpened } from "../../../domains/store/tools.js"; export { isClientGoodOpened }; export async function handleAnalyze(parsed: CliParsedArgs, runtime: CliRuntime): Promise { const pipelineId = parsed.args[0] || stringOption(parsed.options, ["id", "pipelineId"]); if (!pipelineId) return { stdout: "", stderr: "用法: hose-use workflow analyze \n", exitCode: 1 }; const { client, clients } = await createWorkflowDetailClient(parsed, runtime, pipelineId); const detail = await getWorkflowDetail(client, pipelineId); const specIds = extractWorkflowSpecificationIds(detail); const specifications: Record = {}; const evidenceErrors: Array<{ source: string; message: string }> = []; if (specIds.length > 0) { const corpId = runtime.selectedCorp!.corpId || runtime.selectedCorp!.id; await Promise.all( specIds.map(async (specId) => { try { const spec = await getFormSpecification(clients.ekbClient, corpId, specId); if (spec) specifications[specId] = spec; else evidenceErrors.push({ source: `specification:${specId}`, message: "未返回表单规格" }); } catch (error) { evidenceErrors.push({ source: `specification:${specId}`, message: error instanceof Error ? error.message : String(error) }); } }) ); } let availableNodeTypes: Set | undefined; try { const available = await getWorkflowAvailableNodes(client); availableNodeTypes = available.allTypes; } catch (error) { evidenceErrors.push({ source: "availableNodeTypes", message: error instanceof Error ? error.message : String(error) }); } let openedPowerCodes: Set | undefined; try { const { fetchOpenedPowerCodes } = await import("../../../domains/store/tools.js"); openedPowerCodes = await fetchOpenedPowerCodes(runtime.selectedCorp!); } catch (error) { evidenceErrors.push({ source: "openedPowerCodes", message: error instanceof Error ? error.message : String(error) }); } const result = analyzeWorkflow(detail, { specifications, availableNodeTypes, openedPowerCodes }); const failOnAlert = booleanOption(parsed.options, ["fail", "fail-on-alert", "failOnAlert"]); const exitCode = failOnAlert && !result.summary.passed ? 2 : 0; if (runtime.jsonMode) return { stdout: printJson({ ...result, evidenceComplete: evidenceErrors.length === 0, evidenceErrors }, runtime.outputOptions), stderr: "", exitCode }; const gateDecision = result.summary.hardBlocks > 0 ? `❌ 发现 ${result.summary.hardBlocks} 项硬门禁阻断 (禁止发布上线)` : result.summary.softAdvices > 0 ? `⚠️ 硬门禁全通过,存在 ${result.summary.softAdvices} 项优化建议` : "✅ 已执行的静态检查未发现阻断项"; const diagnosticRows = result.diagnostics.map((item) => [ item.category === "hard_block" ? "⛔ 硬门禁" : "💡 软建议", item.severity, item.code, item.path, item.message, ]); const dependencyRows = result.dependencies.map((item) => [item.path, item.taskType, item.references.join(", ")]); return { stdout: [ `====== 工作流静态分析 (${detail.label || detail.id}) ======`, printKeyValue({ "工作流 ID": detail.id, "状态": detail.state === 1 ? "🟢 启用 (state=1)" : "⚪ 停用/草稿 (state=0)", "门禁裁决": gateDecision, "硬门禁数": `${result.summary.hardBlocks} 项 (致命配置/无权限/崩溃)`, "软建议数": `${result.summary.softAdvices} 项 (规范与最佳实践)`, "根节点数": result.ast.length, "依赖节点数": result.dependencies.length, }), `\n依赖引用:\n${dependencyRows.length ? printTable(["节点路径", "类型", "引用"], dependencyRows) : "暂无代码引用"}`, `\n诊断列表 (硬门禁 ⛔ / 软建议 💡):\n${diagnosticRows.length ? printTable(["分类", "级别", "代码", "节点路径", "说明"], diagnosticRows) : "未发现明显配置问题"}`, ...(evidenceErrors.length ? [`\n⚠️ 分析证据不完整:${evidenceErrors.map((item) => `${item.source}: ${item.message}`).join(";")}`] : []), "", ].join("\n"), stderr: "", exitCode, }; }