#!/usr/bin/env bun /** * 合并重建:把 UI 走查以前一条一单转出、还没开跑的修复单,按面合并重建(口径见 shared/src/audit_fix_plan.ts)。 * * 只动「还没开始」的单:待确认执行、评估中,以及进了修复队列但一次都没跑过的(attempts = 0)。 * 修复中、待复核、已修好的不动;P0 / P1 和功能类本来就该一条一单,也不动。 * * 合并后只剩一条的组不动(重建了还是一张单)。 * * 默认只读:列出每份走查有几张单会被归档、会重建成几张。加 --apply 才写: * 1. 逐张归档老单(系统原因「已并入合并单」,备注写明;archive_reasons.ts::SYSTEM_ARCHIVE_REASONS); * 2. 调 POST /api/verifications/:id/fix-batch 按面重建 —— 服务端把指向已归档单的问题当成没转过。 * 建单接口要 SSO,--apply 时把自己的会话放进环境变量 STREAMLINE_SSO_SESSION(x-tt-session-v2 的值), * 建单人就是跑脚本的人。 * * 这是生产写操作,--apply 之前先看一遍预览、跟负责人确认。 * * bun scripts/audit_refile.ts # 自动找出所有带待重建单的走查,预览 * bun scripts/audit_refile.ts <验证 id>… # 只看这几份 * bun scripts/audit_refile.ts <验证 id>… --apply # 执行 * --api # 默认 https://streamline-internal-api.bytedance.net */ import { parseAuditData, type AuditData } from "../shared/src/audit_report"; import { auditPickKey, planAuditFixGroups, standsAlone, type AuditFixPick } from "../shared/src/audit_fix_plan"; const args = Bun.argv.slice(2); const apply = args.includes("--apply"); const apiAt = args.indexOf("--api"); const API = (apiAt >= 0 ? args[apiAt + 1] : "https://streamline-internal-api.bytedance.net").replace(/\/$/, ""); const ids = args.filter((a, i) => !a.startsWith("--") && args[i - 1] !== "--api"); const SESSION = process.env.STREAMLINE_SSO_SESSION || ""; interface Note { pageId: string; issueN: number; lang: string; filedTaskId: string | null; author?: string | null } interface TaskRow { id: string; title: string; status: string; attempts: number; startedAt: number | null; description?: string } async function get(path: string): Promise { const res = await fetch(`${API}${path}`); if (!res.ok) throw new Error(`GET ${path} → ${res.status} ${await res.text().catch(() => "")}`); return res.json() as Promise; } async function post(path: string, body: unknown): Promise { const res = await fetch(`${API}${path}`, { method: "POST", headers: { "content-type": "application/json", ...(SESSION ? { "x-tt-session-v2": SESSION } : {}) }, body: JSON.stringify(body) }); const text = await res.text(); if (!res.ok) throw new Error(`POST ${path} → ${res.status} ${text}`); return JSON.parse(text) as T; } /** 还没开始修的单。 */ const unstarted = (t: TaskRow) => t.status === "pending_confirmation" || t.status === "initializing" || (t.status === "awaiting_fix" && !t.attempts && !t.startedAt); async function discover(): Promise { const { tasks } = await get<{ tasks: TaskRow[] }>("/api/tasks"); const out = new Set(); for (const t of tasks.filter((x) => x.title.startsWith("[UI]") && unstarted(x))) { const full = await get(`/api/tasks/${t.id}`).catch(() => null); const m = /来源:验证任务「.*?」\(([0-9a-f-]{36})\)/.exec(full?.description ?? ""); if (m) out.add(m[1]); } return [...out]; } async function one(vid: string) { const data = parseAuditData(JSON.stringify(await get(`/api/verifications/${vid}/audit-report`))); if (!data) { console.log(`${vid}: 没有走查报告,跳过`); return; } const { notes } = await get<{ notes: Note[] }>(`/api/verifications/${vid}/audit-notes`); const perTask = new Map(); for (const n of notes) if (n.filedTaskId) perTask.set(n.filedTaskId, [...(perTask.get(n.filedTaskId) ?? []), n]); const issueOf = (n: Note) => data.report.pages.find((p) => p.id === n.pageId)?.issues.find((i) => i.n === n.issueN); const old: { task: TaskRow; note: Note }[] = []; for (const [tid, rows] of perTask) { if (rows.length !== 1) continue; // 已经是合并单 const iss = issueOf(rows[0]); if (!iss || standsAlone(iss)) continue; const task = await get(`/api/tasks/${tid}`).catch(() => null); if (task && unstarted(task)) old.push({ task, note: rows[0] }); } // 只剩一条的组重建了也还是一张单,白折腾一次:留着原单不动。 const all = planAuditFixGroups(old.map(({ note }) => ({ pageId: note.pageId, n: note.issueN, lang: note.lang })), data.report); const groups = all.filter((g) => g.picks.length > 1); const keep = new Set(groups.flatMap((g) => g.picks.map(auditPickKey))); const moving = old.filter(({ note }) => keep.has(auditPickKey({ pageId: note.pageId, n: note.issueN }))); console.log(`\n== ${data.report.title}(${vid})`); console.log(` ${moving.length} 张一条一单的老单 → ${groups.length} 张合并单(另有 ${old.length - moving.length} 张单独成组,不动)`); for (const g of groups) console.log(` - [${g.picks.length}] ${g.title}`); if (!apply || !moving.length) return; // 先拦住:归档老单之后才发现建不了新单,就成了只归档没重建。 if (!SESSION) throw new Error("--apply 需要 STREAMLINE_SSO_SESSION:建单接口要 SSO"); const archived: AuditFixPick[] = []; for (const { task, note } of moving) { try { await post(`/api/tasks/${task.id}/archive`, { reason: "merged", note: "合并重建:并入同一个面的走查合并单" }); archived.push({ pageId: note.pageId, n: note.issueN, lang: note.lang }); } catch (err) { console.log(` ! 归档 ${task.id} 失败,这条不重建:${(err as Error).message}`); } } if (!archived.length) return; const res = await post<{ batchTag: string; created: { taskId: string; title: string; picks: AuditFixPick[] }[]; skipped: { pick: AuditFixPick; reason: string }[]; failed: { title: string; error: string }[] }>( `/api/verifications/${vid}/fix-batch`, { picks: archived }, ); console.log(` ✓ 批次 ${res.batchTag}:建了 ${res.created.length} 张`); for (const c of res.created) console.log(` ${c.taskId} [${c.picks.length}] ${c.title}`); for (const s of res.skipped) console.log(` - 跳过 ${auditPickKey(s.pick)}:${s.reason}`); for (const f of res.failed) console.log(` ! 没建成「${f.title}」:${f.error}`); } const targets = ids.length ? ids : await discover(); if (!targets.length) { console.log("没有带待重建单的走查。"); process.exit(0); } console.log(`${apply ? "执行" : "预览(加 --apply 才会写)"} · ${API}`); for (const vid of targets) await one(vid);