// read-only probe: current status + workflow graph for a batch of work items 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)]; }), ); const HOST = env.MEEGO_HOST; const PROJECT = env.MEEGO_PROJECT; const TYPE = env.MEEGO_WORK_ITEM_TYPE; let token = null; async function getToken() { if (token) return token; const res = await fetch(`${HOST}/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 res.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 = env.MEEGO_USER_KEY) { const t = await getToken(); const res = await fetch(`${HOST}${path}`, { method: "POST", headers: { "Content-Type": "application/json", "X-PLUGIN-TOKEN": t, "X-USER-KEY": userKey, }, body: JSON.stringify(body), }); return res.json(); } const IDS = process.argv.slice(2); for (const id of IDS) { try { const q = await api(`/open_api/${PROJECT}/work_item/${TYPE}/query`, { work_item_ids: [Number(id)], }); const item = q.data?.[0]; if (!item) { console.log(`${id}\tNOT_FOUND\t${JSON.stringify(q.err ?? q.error ?? q)}`); continue; } const wf = await api( `/open_api/${PROJECT}/work_item/${TYPE}/${id}/workflow/query`, { flow_type: 1 }, ); const conns = wf.data?.workflow_confs ?? wf.data?.connections ?? wf.data; const stateOperator = item.fields?.find?.((f) => f.field_key === "current_status_operator") ?.field_value ?? ""; console.log( `\n=== ${id} | name="${item.name}" | state=${item.work_item_status?.state_key} | operator=${JSON.stringify(stateOperator)}`, ); console.log(" workflow:", JSON.stringify(conns)); } catch (e) { console.log(`${id}\tERROR\t${e.message}`); } }