|
| 1 | +/** Manage-phase event vocabulary — later phases append these to the local event ledger (#2325). */ |
| 2 | +export const MANAGE_STATUS_EVENT_TYPE = "manage_pr_update"; |
| 3 | + |
| 4 | +const portfolioPrIdentifierPattern = /^pr:(\d+)$/; |
| 5 | + |
| 6 | +function parsePortfolioPullNumber(identifier) { |
| 7 | + if (typeof identifier !== "string") return null; |
| 8 | + const match = portfolioPrIdentifierPattern.exec(identifier.trim()); |
| 9 | + if (!match) return null; |
| 10 | + const pullNumber = Number(match[1]); |
| 11 | + return Number.isInteger(pullNumber) && pullNumber > 0 ? pullNumber : null; |
| 12 | +} |
| 13 | + |
| 14 | +function rowKey(repoFullName, pullNumber) { |
| 15 | + return `${repoFullName}#${pullNumber}`; |
| 16 | +} |
| 17 | + |
| 18 | +function normalizeOptionalString(value) { |
| 19 | + if (value === undefined || value === null) return null; |
| 20 | + if (typeof value !== "string") return null; |
| 21 | + const trimmed = value.trim(); |
| 22 | + return trimmed ? trimmed : null; |
| 23 | +} |
| 24 | + |
| 25 | +function normalizePullNumber(value) { |
| 26 | + if (!Number.isInteger(value) || value <= 0) return null; |
| 27 | + return value; |
| 28 | +} |
| 29 | + |
| 30 | +function mergeManageFields(target, payload, eventCreatedAt) { |
| 31 | + const pullNumber = normalizePullNumber(payload?.pullNumber); |
| 32 | + if (pullNumber !== null) target.pullNumber = pullNumber; |
| 33 | + const branch = normalizeOptionalString(payload?.branch); |
| 34 | + if (branch !== null) target.branch = branch; |
| 35 | + const ciState = normalizeOptionalString(payload?.ciState); |
| 36 | + if (ciState !== null) target.ciState = ciState; |
| 37 | + const gateVerdict = normalizeOptionalString(payload?.gateVerdict); |
| 38 | + if (gateVerdict !== null) target.gateVerdict = gateVerdict; |
| 39 | + const outcome = normalizeOptionalString(payload?.outcome); |
| 40 | + if (outcome !== null) target.outcome = outcome; |
| 41 | + const lastPolledAt = |
| 42 | + normalizeOptionalString(payload?.lastPolledAt) ?? normalizeOptionalString(eventCreatedAt); |
| 43 | + if (lastPolledAt !== null) target.lastPolledAt = lastPolledAt; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * Aggregate manage-phase rows from the portfolio queue and append-only event ledger. Pure read/render input — |
| 48 | + * no network calls and no writes (#2325). |
| 49 | + */ |
| 50 | +export function buildManageStatusSnapshot(readers) { |
| 51 | + const rows = new Map(); |
| 52 | + |
| 53 | + for (const item of readers.listQueue()) { |
| 54 | + if (item.status === "done") continue; |
| 55 | + const pullNumber = parsePortfolioPullNumber(item.identifier); |
| 56 | + if (pullNumber === null) continue; |
| 57 | + const key = rowKey(item.repoFullName, pullNumber); |
| 58 | + rows.set(key, { |
| 59 | + repoFullName: item.repoFullName, |
| 60 | + pullNumber, |
| 61 | + branch: null, |
| 62 | + ciState: item.status === "in_progress" ? "unknown" : "unknown", |
| 63 | + gateVerdict: null, |
| 64 | + outcome: null, |
| 65 | + lastPolledAt: null, |
| 66 | + portfolioStatus: item.status, |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + for (const event of readers.readEvents()) { |
| 71 | + if (event.type !== MANAGE_STATUS_EVENT_TYPE) continue; |
| 72 | + if (!event.repoFullName) continue; |
| 73 | + const payload = event.payload; |
| 74 | + if (!payload || typeof payload !== "object" || Array.isArray(payload)) continue; |
| 75 | + const pullNumber = normalizePullNumber(payload.pullNumber); |
| 76 | + if (pullNumber === null) continue; |
| 77 | + const key = rowKey(event.repoFullName, pullNumber); |
| 78 | + const existing = rows.get(key); |
| 79 | + if (!existing) continue; |
| 80 | + mergeManageFields(existing, payload, event.createdAt); |
| 81 | + } |
| 82 | + |
| 83 | + return [...rows.values()].sort((left, right) => { |
| 84 | + const repoCompare = left.repoFullName |
| 85 | + .toLowerCase() |
| 86 | + .localeCompare(right.repoFullName.toLowerCase(), "en"); |
| 87 | + if (repoCompare !== 0) return repoCompare; |
| 88 | + return left.pullNumber - right.pullNumber; |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +export function formatManageStatusJson(rows) { |
| 93 | + return `${JSON.stringify({ rows }, null, 2)}\n`; |
| 94 | +} |
| 95 | + |
| 96 | +function pad(value, width) { |
| 97 | + const text = String(value ?? ""); |
| 98 | + return text.length >= width ? text : `${text}${" ".repeat(width - text.length)}`; |
| 99 | +} |
| 100 | + |
| 101 | +export function formatManageStatusTable(rows) { |
| 102 | + if (rows.length === 0) { |
| 103 | + return "No managed pull requests in the local portfolio.\n"; |
| 104 | + } |
| 105 | + const headers = [ |
| 106 | + "repo", |
| 107 | + "pr", |
| 108 | + "branch", |
| 109 | + "ci", |
| 110 | + "gate", |
| 111 | + "outcome", |
| 112 | + "last_polled_at", |
| 113 | + ]; |
| 114 | + const widths = [28, 6, 24, 10, 10, 10, 24]; |
| 115 | + const lines = [ |
| 116 | + headers.map((header, index) => pad(header, widths[index])).join(" "), |
| 117 | + widths.map((width) => "-".repeat(width)).join(" "), |
| 118 | + ]; |
| 119 | + for (const row of rows) { |
| 120 | + lines.push( |
| 121 | + [ |
| 122 | + pad(row.repoFullName, widths[0]), |
| 123 | + pad(row.pullNumber, widths[1]), |
| 124 | + pad(row.branch ?? "-", widths[2]), |
| 125 | + pad(row.ciState ?? "unknown", widths[3]), |
| 126 | + pad(row.gateVerdict ?? "-", widths[4]), |
| 127 | + pad(row.outcome ?? "-", widths[5]), |
| 128 | + pad(row.lastPolledAt ?? "-", widths[6]), |
| 129 | + ].join(" "), |
| 130 | + ); |
| 131 | + } |
| 132 | + return `${lines.join("\n")}\n`; |
| 133 | +} |
| 134 | + |
| 135 | +const globalCliFlags = new Set(["--json", "--no-update-check"]); |
| 136 | + |
| 137 | +export function parseManageStatusArgs(cliArgs) { |
| 138 | + const json = cliArgs.includes("--json"); |
| 139 | + const positional = cliArgs.filter((arg) => !globalCliFlags.has(arg)); |
| 140 | + if (positional.length > 0) { |
| 141 | + throw new Error(`unexpected_arguments:${positional.join(",")}`); |
| 142 | + } |
| 143 | + return { json }; |
| 144 | +} |
| 145 | + |
| 146 | +export function runManageStatus(readers, options = {}) { |
| 147 | + const rows = buildManageStatusSnapshot(readers); |
| 148 | + const output = options.json ? formatManageStatusJson(rows) : formatManageStatusTable(rows); |
| 149 | + return { rows, output, exitCode: 0 }; |
| 150 | +} |
0 commit comments