{"draft":null,"from":22,"to":23,"files":[{"path":"client/src/issues.ts","added":87,"removed":1,"diff":"--- a/client/src/issues.ts\n+++ b/client/src/issues.ts\n@@ -7,7 +7,19 @@\n \n import { readFileSync, statSync } from \"node:fs\";\n import { basename, resolve } from \"node:path\";\n-import type { Api, Ask, Attachment, Draft, DraftBody, IssuePage, IssueRow, IssueSearchHit } from \"./api.ts\";\n+import {\n+\ttype Api,\n+\tApiError,\n+\ttype Ask,\n+\ttype Attachment,\n+\ttype CopyAnswer,\n+\ttype CopyBody,\n+\ttype Draft,\n+\ttype DraftBody,\n+\ttype IssuePage,\n+\ttype IssueRow,\n+\ttype IssueSearchHit,\n+} from \"./api.ts\";\n import { bool, type Parsed, str, UsageError } from \"./args.ts\";\n import { type Env, type Output, open, plural } from \"./commands.ts\";\n import { type Config, readMachineServers, trustedServers } from \"./config.ts\";\n@@ -325,6 +337,76 @@\n \t};\n }\n \n+/** The flags --copy-to goes with: anything else on the same command is refused, so a copy is never half an edit. */\n+const COPY_FLAGS = new Set([\"copy-to\", \"clean\", \"dry-run\"]);\n+\n+/** abso issue <N> --copy-to <project>: the server reads the issue and writes the copy. */\n+async function runCopy(api: Api, id: number, to: string, args: Parsed, env: Env): Promise<Output> {\n+\tconst other = [...args.flags.keys()].filter((f) => !COPY_FLAGS.has(f) && f !== \"json\");\n+\tif (other.length > 0)\n+\t\tthrow new UsageError(`--copy-to goes alone (with --clean and --dry-run), not with --${other[0]}`);\n+\tif (to === api.config.project)\n+\t\tthrow new UsageError(`issue ${id} is already in ${to}; --copy-to names another project`);\n+\tconst clean = str(args, \"clean\");\n+\tconst texts = clean === undefined ? {} : readClean(resolve(env.cwd, clean), clean);\n+\tconst dryRun = bool(args, \"dry-run\");\n+\tconst from = api.config.project;\n+\tlet answer: CopyAnswer;\n+\ttry {\n+\t\tanswer = await api.issueCopy(to, { ...texts, from, issue: id, ...(dryRun ? { dryRun: true } : {}) });\n+\t} catch (e) {\n+\t\tif (e instanceof ApiError && e.status === 403 && !dryRun)\n+\t\t\tthrow new ApiError(`${e.message}. --dry-run still shows what would go.`, e.status);\n+\t\tthrow e;\n+\t}\n+\tif (\"dryRun\" in answer) {\n+\t\tconst p = answer.plan;\n+\t\tconst edited = (list: { edited: boolean }[]) => list.filter((x) => x.edited).length;\n+\t\tconst lines = [\n+\t\t\t`A dry run: nothing was written. Copying ${from} issue ${id} into ${to} would make:`,\n+\t\t\t`  ${q(p.title)}${p.closed ? `, ${p.closed.how}` : \"\"}${p.edited.length > 0 ? ` (${p.edited.join(\" and \")} edited when copied)` : \"\"}`,\n+\t\t\t`  ${plural(p.comments.length, \"comment\")}${edited(p.comments) > 0 ? ` (${edited(p.comments)} edited)` : \"\"}, ${plural(p.asks.length, \"answered ask\")}${edited(p.asks) > 0 ? ` (${edited(p.asks)} edited)` : \"\"}${p.labels.length > 0 ? `, labelled ${p.labels.join(\", \")}` : \"\"}`,\n+\t\t\t...skippedLines(p.skipped),\n+\t\t\t`Run it again without --dry-run to copy it.`,\n+\t\t];\n+\t\treturn { text: lines.join(\"\\n\"), json: answer };\n+\t}\n+\treturn {\n+\t\ttext: [\n+\t\t\t`Copied ${from} issue ${id} to ${to} issue ${answer.id}. Its comments and asks keep their authors and times, and issue ${id} has a comment saying where it went.`,\n+\t\t\t...skippedLines(answer.skipped),\n+\t\t].join(\"\\n\"),\n+\t\tjson: answer,\n+\t};\n+}\n+\n+function skippedLines(skipped: { comments: number[]; asks: number[] }): string[] {\n+\tconst parts = [\n+\t\t...(skipped.comments.length > 0 ? [`comments ${skipped.comments.join(\", \")}`] : []),\n+\t\t...(skipped.asks.length > 0 ? [`asks ${skipped.asks.join(\", \")}`] : []),\n+\t];\n+\treturn parts.length > 0 ? [`  not copied: ${parts.join(\"; \")} (open asks, and what --clean left out)`] : [];\n+}\n+\n+/** --clean's file: cleaned texts by source id. The server checks each id against the issue it reads. */\n+function readClean(path: string, given: string): Omit<CopyBody, \"from\" | \"issue\"> {\n+\tlet raw: unknown;\n+\ttry {\n+\t\traw = JSON.parse(readFileSync(path, \"utf8\"));\n+\t} catch (e) {\n+\t\tthrow new UsageError(\n+\t\t\t`--clean ${given}: ${(e as NodeJS.ErrnoException).code === \"ENOENT\" ? \"no such file\" : \"not JSON\"}`,\n+\t\t);\n+\t}\n+\tif (typeof raw !== \"object\" || raw === null || Array.isArray(raw))\n+\t\tthrow new UsageError(`--clean ${given}: a JSON object with title, description, comments or asks`);\n+\tconst known = new Set([\"title\", \"description\", \"comments\", \"asks\"]);\n+\tconst unknown = Object.keys(raw).filter((k) => !known.has(k));\n+\tif (unknown.length > 0)\n+\t\tthrow new UsageError(`--clean ${given}: ${unknown[0]} isn't one of title, description, comments, asks`);\n+\treturn raw as Omit<CopyBody, \"from\" | \"issue\">;\n+}\n+\n export async function runIssue(args: Parsed, env: Env): Promise<Output> {\n \tconst [first, ...rest] = args.positionals;\n \tconst { api } = openIssues(env);\n@@ -347,6 +429,10 @@\n \t\t\t'abso issue <N> [--comment \"...\" | --object \"...\" | --drop | --reopen | --done [--done-by <version>] | an edit], or abso issue new \"<title>\"',\n \t\t);\n \tconst id = issueNumber(first);\n+\tconst copyTo = str(args, \"copy-to\");\n+\tif (copyTo !== undefined) return runCopy(api, id, copyTo, args, env);\n+\tif (str(args, \"clean\") !== undefined || bool(args, \"dry-run\"))\n+\t\tthrow new UsageError(\"--clean and --dry-run go with --copy-to <project>\");\n \tconst comment = str(args, \"comment\");\n \tconst object = str(args, \"object\");\n \tconst doneBy = str(args, \"done-by\");"}],"unmatched":[]}