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, PROJECT = env.MEEGO_PROJECT, TYPE = env.MEEGO_WORK_ITEM_TYPE; let token = null; async function getToken() { if (token) return token; const r = 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 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 = env.MEEGO_USER_KEY) { const t = await getToken(); const r = await fetch(`${HOST}${path}`, { method: "POST", headers: { "Content-Type": "application/json", "X-PLUGIN-TOKEN": t, "X-USER-KEY": userKey }, body: JSON.stringify(body), }); return r.json(); } const id = process.argv[2]; console.log("--- query ---"); const q = await api(`/open_api/${PROJECT}/work_item/${TYPE}/query`, { work_item_ids: [Number(id)] }); console.log(JSON.stringify(q, null, 2).slice(0, 800)); console.log("--- workflow flow_type:1 ---"); console.log(JSON.stringify(await api(`/open_api/${PROJECT}/work_item/${TYPE}/${id}/workflow/query`, { flow_type: 1 }), null, 2)); console.log("--- workflow flow_type:2 ---"); console.log(JSON.stringify(await api(`/open_api/${PROJECT}/work_item/${TYPE}/${id}/workflow/query`, { flow_type: 2 }), null, 2));