|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { execFileSync } from "node:child_process"; |
| 4 | +import fs from "node:fs"; |
| 5 | +import path from "node:path"; |
| 6 | +import YAML from "yaml"; |
| 7 | + |
| 8 | +const REPO = "pwrdrvr/openclaw-codex-app-server"; |
| 9 | +const PROJECT_OWNER = "pwrdrvr"; |
| 10 | +const PROJECT_NUMBER = 7; |
| 11 | +const PROJECT_URL = "https://github.com/orgs/pwrdrvr/projects/7"; |
| 12 | +const TRACKER_PATH = path.resolve(".local/work-items.yaml"); |
| 13 | + |
| 14 | +function runGh(args) { |
| 15 | + return execFileSync("gh", args, { |
| 16 | + cwd: process.cwd(), |
| 17 | + encoding: "utf8", |
| 18 | + stdio: ["ignore", "pipe", "pipe"], |
| 19 | + }); |
| 20 | +} |
| 21 | + |
| 22 | +function loadExistingTracker() { |
| 23 | + if (!fs.existsSync(TRACKER_PATH)) { |
| 24 | + return { version: 1, last_synced_at: null, items: [] }; |
| 25 | + } |
| 26 | + |
| 27 | + const raw = fs.readFileSync(TRACKER_PATH, "utf8"); |
| 28 | + const parsed = YAML.parse(raw) ?? {}; |
| 29 | + return { |
| 30 | + version: parsed.version ?? 1, |
| 31 | + last_synced_at: parsed.last_synced_at ?? null, |
| 32 | + items: Array.isArray(parsed.items) ? parsed.items : [], |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +function normalizeNumber(value) { |
| 37 | + return typeof value === "number" && Number.isFinite(value) ? value : 0; |
| 38 | +} |
| 39 | + |
| 40 | +function nextLocalId(existingItems) { |
| 41 | + let max = 0; |
| 42 | + for (const item of existingItems) { |
| 43 | + const match = |
| 44 | + typeof item?.local_id === "string" ? item.local_id.match(/^ocas-(\d{4,})$/) : null; |
| 45 | + if (match) { |
| 46 | + max = Math.max(max, Number(match[1])); |
| 47 | + } |
| 48 | + } |
| 49 | + return `ocas-${String(max + 1).padStart(4, "0")}`; |
| 50 | +} |
| 51 | + |
| 52 | +function mergeExistingItem(existingByIssue, issueNumber, fallbackLocalId) { |
| 53 | + const current = existingByIssue.get(issueNumber); |
| 54 | + if (current && typeof current === "object" && current !== null) { |
| 55 | + return structuredClone(current); |
| 56 | + } |
| 57 | + return { |
| 58 | + local_id: fallbackLocalId, |
| 59 | + title: "", |
| 60 | + repo: REPO, |
| 61 | + source_note: "", |
| 62 | + github: {}, |
| 63 | + state: {}, |
| 64 | + notes: [], |
| 65 | + }; |
| 66 | +} |
| 67 | + |
| 68 | +function main() { |
| 69 | + const existing = loadExistingTracker(); |
| 70 | + const existingByIssue = new Map(); |
| 71 | + for (const item of existing.items) { |
| 72 | + const issueNumber = normalizeNumber(item?.github?.issue_number); |
| 73 | + if (issueNumber > 0) { |
| 74 | + existingByIssue.set(issueNumber, item); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + const issueList = JSON.parse( |
| 79 | + runGh(["issue", "list", "--repo", REPO, "--state", "all", "--limit", "500", "--json", "number,state,title,url"]), |
| 80 | + ); |
| 81 | + const issueByNumber = new Map(issueList.map((issue) => [issue.number, issue])); |
| 82 | + |
| 83 | + const projectData = JSON.parse( |
| 84 | + runGh(["project", "item-list", String(PROJECT_NUMBER), "--owner", PROJECT_OWNER, "--format", "json"]), |
| 85 | + ); |
| 86 | + |
| 87 | + const items = []; |
| 88 | + for (const item of projectData.items ?? []) { |
| 89 | + const content = item?.content ?? {}; |
| 90 | + if (content.type !== "Issue") { |
| 91 | + continue; |
| 92 | + } |
| 93 | + if (content.repository !== REPO) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + const issueNumber = normalizeNumber(content.number); |
| 97 | + if (issueNumber <= 0) { |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + const issue = issueByNumber.get(issueNumber); |
| 102 | + const merged = mergeExistingItem(existingByIssue, issueNumber, nextLocalId(existing.items)); |
| 103 | + merged.title = content.title ?? issue?.title ?? merged.title ?? ""; |
| 104 | + merged.repo = REPO; |
| 105 | + merged.source_note = typeof merged.source_note === "string" ? merged.source_note : ""; |
| 106 | + if (typeof merged.raw_example !== "string") { |
| 107 | + delete merged.raw_example; |
| 108 | + } |
| 109 | + merged.github = { |
| 110 | + ...(merged.github && typeof merged.github === "object" ? merged.github : {}), |
| 111 | + issue_number: issueNumber, |
| 112 | + issue_url: content.url ?? issue?.url ?? "", |
| 113 | + project_number: PROJECT_NUMBER, |
| 114 | + project_url: PROJECT_URL, |
| 115 | + project_item_id: item.id ?? "", |
| 116 | + }; |
| 117 | + merged.state = { |
| 118 | + ...(merged.state && typeof merged.state === "object" ? merged.state : {}), |
| 119 | + issue_state: issue?.state ?? "OPEN", |
| 120 | + project_status: item.status ?? "", |
| 121 | + workflow: item.workflow ?? "", |
| 122 | + priority: item.priority ?? "", |
| 123 | + size: item.size ?? "", |
| 124 | + branch: typeof merged.state?.branch === "string" ? merged.state.branch : "", |
| 125 | + pr_number: normalizeNumber(merged.state?.pr_number), |
| 126 | + pr_url: typeof merged.state?.pr_url === "string" ? merged.state.pr_url : "", |
| 127 | + }; |
| 128 | + if (!Array.isArray(merged.notes)) { |
| 129 | + merged.notes = []; |
| 130 | + } |
| 131 | + items.push(merged); |
| 132 | + } |
| 133 | + |
| 134 | + items.sort((a, b) => normalizeNumber(a.github?.issue_number) - normalizeNumber(b.github?.issue_number)); |
| 135 | + |
| 136 | + const usedIds = new Set(); |
| 137 | + let counter = 1; |
| 138 | + for (const item of items) { |
| 139 | + if (typeof item.local_id !== "string" || usedIds.has(item.local_id)) { |
| 140 | + while (usedIds.has(`ocas-${String(counter).padStart(4, "0")}`)) { |
| 141 | + counter += 1; |
| 142 | + } |
| 143 | + item.local_id = `ocas-${String(counter).padStart(4, "0")}`; |
| 144 | + counter += 1; |
| 145 | + } |
| 146 | + usedIds.add(item.local_id); |
| 147 | + } |
| 148 | + |
| 149 | + const output = { |
| 150 | + version: 1, |
| 151 | + last_synced_at: new Date().toISOString().replace(/\.\d{3}Z$/, "Z"), |
| 152 | + items, |
| 153 | + }; |
| 154 | + |
| 155 | + fs.mkdirSync(path.dirname(TRACKER_PATH), { recursive: true }); |
| 156 | + fs.writeFileSync(TRACKER_PATH, YAML.stringify(output, { lineWidth: 0 }), "utf8"); |
| 157 | + process.stdout.write(`Synced ${items.length} items to ${TRACKER_PATH}\n`); |
| 158 | +} |
| 159 | + |
| 160 | +main(); |
0 commit comments