Initial import of streamline
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
// Drive a batch of work items to CLOSED as their current operator.
|
||||
// Reason: 历史bug无法复现 (bug_resolution = 3 无法复现,无需修复)
|
||||
// Modes: --probe read-only (state + operator + outgoing transitions)
|
||||
// (default) execute the close flow
|
||||
import { readFileSync } from "node:fs";
|
||||
|
||||
const env = Object.fromEntries(
|
||||
readFileSync(new URL("../.env", import.meta.url), "utf8")
|
||||
.split("\n").map((l) => l.trim())
|
||||
.filter((l) => l && !l.startsWith("#") && l.includes("="))
|
||||
.map((l) => { const i = l.indexOf("="); return [l.slice(0, i), l.slice(i + 1)]; }),
|
||||
);
|
||||
|
||||
// .env stores a bare host; normalize to a full origin. project/type aren't in
|
||||
// .env — default to the known tiktok/issue (same as scripts/meego_check.sh).
|
||||
const rawHost = env.MEEGO_HOST || "meego.larkoffice.com";
|
||||
const ORIGIN = /^https?:\/\//.test(rawHost) ? rawHost : `https://${rawHost}`;
|
||||
const PROJECT = env.MEEGO_PROJECT || "tiktok";
|
||||
const TYPE = env.MEEGO_WORK_ITEM_TYPE || "issue";
|
||||
const DEFAULT_USER = env.MEEGO_USER_KEY || "jiaopengwen";
|
||||
|
||||
const REASON = "历史bug无法复现";
|
||||
const PROBE = process.argv.includes("--probe");
|
||||
const NO_COMMENT = process.argv.includes("--no-comment");
|
||||
const IDS = process.argv.slice(2).filter((a) => !a.startsWith("--"));
|
||||
|
||||
const RESOLUTION_VALUE = "3"; // 无法复现,无需修复
|
||||
// known select-option seeds for adaptive required-field fill on transitions
|
||||
const SELECT_SEEDS = {
|
||||
bug_resolution: "3",
|
||||
field_b45e9c: "t758tdlJp", // 线下逃逸分析: 非漏测-线下阶段无法覆盖
|
||||
};
|
||||
const NUMBER_FIELDS = new Set(["field_11605e", "field_239c4a"]); // 修复/解决耗时
|
||||
|
||||
let token = null;
|
||||
async function getToken() {
|
||||
if (token) return token;
|
||||
const r = await fetch(`${ORIGIN}/open_api/authen/plugin_token`, {
|
||||
method: "POST", headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
plugin_id: env.MEEGO_PLUGIN_ID, plugin_secret: env.MEEGO_PLUGIN_SECRET,
|
||||
type: Number(env.MEEGO_TOKEN_TYPE ?? "0"),
|
||||
}),
|
||||
});
|
||||
const d = await r.json();
|
||||
token = d.data?.token ?? d.token;
|
||||
if (!token) throw new Error("token failed " + JSON.stringify(d));
|
||||
return token;
|
||||
}
|
||||
async function api(path, body, userKey = DEFAULT_USER) {
|
||||
const t = await getToken();
|
||||
const r = await fetch(`${ORIGIN}${path}`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json", "X-PLUGIN-TOKEN": t, "X-USER-KEY": userKey },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
return r.json();
|
||||
}
|
||||
const codeOf = (r) => r?.err_code ?? r?.error?.code ?? r?.err?.code ?? 0;
|
||||
const msgOf = (r) => r?.err?.msg || r?.err_msg || r?.error?.msg || r?.err?.message || "";
|
||||
|
||||
function firstUserKey(v) {
|
||||
if (Array.isArray(v) && v.length) {
|
||||
const f = v[0];
|
||||
if (typeof f === "string" || typeof f === "number") return String(f);
|
||||
if (f && typeof f === "object") return String(f.user_key ?? f.value ?? f.key ?? "");
|
||||
}
|
||||
if (typeof v === "string" || typeof v === "number") return String(v);
|
||||
if (v && typeof v === "object") return String(v.value ?? "");
|
||||
return "";
|
||||
}
|
||||
async function queryItem(id) {
|
||||
const q = await api(`/open_api/${PROJECT}/work_item/${TYPE}/query`, { work_item_ids: [Number(id)] });
|
||||
const item = q.data?.[0];
|
||||
if (!item) throw new Error("not found / read failed: " + JSON.stringify(msgOf(q) || q));
|
||||
const field = (k) => item.fields?.find?.((f) => f.field_key === k)?.field_value;
|
||||
const operator = firstUserKey(field("current_status_operator")) || firstUserKey(field("issue_operator"));
|
||||
// state_key comes back UPPERCASE (RESOLVED/CLOSED/...); normalize to lowercase
|
||||
// so the comparisons below (and target_state_key matching) line up.
|
||||
return { item, state: String(item.work_item_status?.state_key || "").toLowerCase(), operator, name: item.name };
|
||||
}
|
||||
async function outgoing(id, userKey, state) {
|
||||
const wf = await api(`/open_api/${PROJECT}/work_item/${TYPE}/${id}/workflow/query`, { flow_type: 1 }, userKey);
|
||||
const conns = wf.data?.connections || wf.data?.workflow_confs || [];
|
||||
const want = String(state || "").toLowerCase();
|
||||
return conns.filter((c) => String(c.source_state_key || "").toLowerCase() === want);
|
||||
}
|
||||
|
||||
// adaptive state_change reacting to required/illegal field errors
|
||||
async function stateChange(id, transitionId, userKey) {
|
||||
const fields = new Map([["bug_resolution", { value: RESOLUTION_VALUE }]]);
|
||||
for (let i = 0; i < 12; i++) {
|
||||
const body = { transition_id: Number(transitionId), fields: [...fields].map(([field_key, field_value]) => ({ field_key, field_value })) };
|
||||
const res = await api(`/open_api/${PROJECT}/workflow/${TYPE}/${id}/node/state_change`, body, userKey);
|
||||
if (codeOf(res) === 0) return { ok: true, used: body.fields };
|
||||
const msg = msgOf(res);
|
||||
const m = msg.match(/\[?([a-zA-Z0-9_]+)\]?[^]*?\b(required|illegal|empty)/i);
|
||||
if (!m) return { ok: false, msg: msg || JSON.stringify(res) };
|
||||
const key = m[1], kind = m[2].toLowerCase();
|
||||
if (kind === "illegal") {
|
||||
if (NUMBER_FIELDS.has(key) && typeof fields.get(key) !== "number") fields.set(key, 0);
|
||||
else if (fields.has(key)) fields.delete(key);
|
||||
else return { ok: false, msg: `illegal field ${key} not in payload: ${msg}` };
|
||||
} else {
|
||||
if (SELECT_SEEDS[key]) fields.set(key, { value: SELECT_SEEDS[key] });
|
||||
else if (NUMBER_FIELDS.has(key)) fields.set(key, 0);
|
||||
else return { ok: false, msg: `unknown required field ${key}: ${msg}` };
|
||||
}
|
||||
}
|
||||
return { ok: false, msg: "exceeded adaptive iterations" };
|
||||
}
|
||||
|
||||
for (const id of IDS) {
|
||||
try {
|
||||
let { state, operator, name } = await queryItem(id);
|
||||
const actor = operator || DEFAULT_USER;
|
||||
if (PROBE) {
|
||||
const outs = await outgoing(id, actor, state);
|
||||
console.log(`${id}\tstate=${state}\toperator=${operator || "(none)"}\tname="${name}"`);
|
||||
console.log(` out: ${outs.map((c) => `${c.target_state_key}#${c.transition_id}`).join(" ") || "(none)"}`);
|
||||
continue;
|
||||
}
|
||||
console.log(`\n### ${id} "${name}" start=${state} operator=${operator || "(none)"} acting=${actor}`);
|
||||
if (state === "closed") { console.log(" already CLOSED — skip"); continue; }
|
||||
|
||||
if (!NO_COMMENT) {
|
||||
const c = await api(`/open_api/${PROJECT}/work_item/${TYPE}/${id}/comment/create`, { content: REASON }, actor);
|
||||
console.log(` comment: ${codeOf(c) === 0 ? "ok" : msgOf(c)}`);
|
||||
}
|
||||
|
||||
// walk toward closed: open→resolved→closed (find transition ids live)
|
||||
for (let hop = 0; hop < 3 && state !== "closed"; hop++) {
|
||||
const outs = await outgoing(id, operator || DEFAULT_USER, state);
|
||||
const tgt = (c) => String(c.target_state_key || "").toLowerCase();
|
||||
const toClosed = outs.find((c) => tgt(c) === "closed");
|
||||
const toResolved = outs.find((c) => tgt(c) === "resolved");
|
||||
const next = toClosed || toResolved;
|
||||
if (!next) { console.log(` no path forward from ${state} (out: ${outs.map((o) => o.target_state_key).join(",") || "none"})`); break; }
|
||||
const r = await stateChange(id, next.transition_id, operator || DEFAULT_USER);
|
||||
console.log(` ${state}→${next.target_state_key} (#${next.transition_id}): ${r.ok ? "ok " + JSON.stringify(r.used) : "FAIL " + r.msg}`);
|
||||
if (!r.ok) break;
|
||||
({ state, operator } = await queryItem(id)); // operator can change after a transition
|
||||
}
|
||||
const fin = await queryItem(id);
|
||||
console.log(` FINAL state=${fin.state} ${fin.state === "closed" ? "✅" : "⚠️"}`);
|
||||
} catch (e) {
|
||||
console.log(`### ${id} ERROR ${e.message}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user