fix: add REST PR file summary CLI

This commit is contained in:
Codex
2026-05-23 05:17:33 +00:00
parent 081db589f2
commit c33dd9a7b3
6 changed files with 409 additions and 7 deletions
+3
View File
@@ -325,6 +325,7 @@ export function runChecks(config: UniDeskConfig, options: CheckOptions = default
fileItem("scripts/decision-center-desired-state-contract-test.ts"),
fileItem("scripts/code-queue-prompt-observation-test.ts"),
fileItem("scripts/gh-cli-issue-guard-contract-test.ts"),
fileItem("scripts/gh-cli-pr-files-contract-test.ts"),
fileItem("scripts/gh-cli-pr-contract-test.ts"),
fileItem("scripts/code-queue-pr-preflight-example.ts"),
fileItem("scripts/schedule-cli-contract-test.ts"),
@@ -370,6 +371,7 @@ export function runChecks(config: UniDeskConfig, options: CheckOptions = default
items.push(commandItem("schedule:cli-contract", ["bun", "scripts/schedule-cli-contract-test.ts"], 30_000));
items.push(commandItem("server:cleanup-plan-contract", ["bun", "scripts/server-cleanup-plan-contract-test.ts"], 30_000));
items.push(commandItem("gh:issue-guard-contract", ["bun", "scripts/gh-cli-issue-guard-contract-test.ts"], 30_000));
items.push(commandItem("gh:pr-files-contract", ["bun", "scripts/gh-cli-pr-files-contract-test.ts"], 30_000));
items.push(commandItem("gh:pr-contract", ["bun", "scripts/gh-cli-pr-contract-test.ts"], 30_000));
items.push(commandItem("auth-broker:p0-contract", ["bun", "scripts/auth-broker-contract-test.ts"], 30_000));
} else {
@@ -396,6 +398,7 @@ export function runChecks(config: UniDeskConfig, options: CheckOptions = default
items.push(skippedItem("schedule:cli-contract", "Schedule CLI contract is opt-in with script checks", "--scripts-typecheck or --full"));
items.push(skippedItem("server:cleanup-plan-contract", "Server cleanup dry-run contract is opt-in with script checks", "--scripts-typecheck or --full"));
items.push(skippedItem("gh:issue-guard-contract", "GitHub issue CLI contract is opt-in with script checks", "--scripts-typecheck or --full"));
items.push(skippedItem("gh:pr-files-contract", "GitHub PR files/stat contract is opt-in with script checks", "--scripts-typecheck or --full"));
items.push(skippedItem("gh:pr-contract", "GitHub PR CLI contract is opt-in with script checks", "--scripts-typecheck or --full"));
items.push(skippedItem("auth-broker:p0-contract", "Auth Broker P0 skeleton and CLI adapter contract is opt-in with script checks", "--scripts-typecheck or --full"));
}
+172 -3
View File
@@ -13,6 +13,7 @@ const DEFAULT_COMMANDER_BRIEF_CLAUDEQQ_BASE_URL = "http://backend-core:8080/api/
const DEFAULT_COMMANDER_BRIEF_CLAUDEQQ_USER_ID = "645275593";
const CODE_QUEUE_BOARD_TARGET_ISSUE = 20;
const COMMANDER_BRIEF_TARGET_ISSUE = 24;
const MAX_PR_FILES_LIMIT = 3000;
const DEFAULT_BOARD_KNOWN_META_ISSUES = [CODE_QUEUE_BOARD_TARGET_ISSUE, COMMANDER_BRIEF_TARGET_ISSUE] as const;
const BOARD_AUDIT_REQUIRED_COLUMNS = ["branch", "acceptance", "relatedTask", "progress"] as const;
const BOARD_ROW_FIELDS = ["progress", "status", "validation", "branch", "tasks", "focus"] as const;
@@ -404,12 +405,30 @@ interface GitHubPullRequest {
user?: { login?: string };
head?: { ref?: string; sha?: string };
base?: { ref?: string; sha?: string };
additions?: number;
deletions?: number;
changed_files?: number;
commits?: number;
mergeable?: string | null;
merge_state_status?: string | null;
created_at?: string;
updated_at?: string;
}
interface GitHubPullRequestFile {
sha?: string;
filename: string;
status?: string;
additions?: number;
deletions?: number;
changes?: number;
blob_url?: string;
raw_url?: string;
contents_url?: string;
previous_filename?: string;
patch?: string;
}
interface GitHubPullRequestGraphqlStatusContext {
__typename?: string;
name?: string | null;
@@ -640,7 +659,7 @@ function parseBoardRowUpsertValues(args: string[]): BoardRowUpsertValues {
function validateKnownOptions(args: string[]): void {
const valueOptions = new Set(["--repo", "--limit", "--board-issue", "--known-meta-issue", "--ignore-issue", "--title", "--body-file", "--body", "--base", "--head", "--json", "--state", "--mode", "--expect-updated-at", "--expect-body-sha", "--body-profile", "--label", "--field", "--value", "--section", "--to", "--status", "--row-file", "--category", "--branch", "--tasks", "--summary", "--focus", "--validation", "--progress"]);
const flagOptions = new Set(["--dry-run", "--draft", "--notify-claudeqq-brief-diff", "--allow-short-body", "--raw", "--full"]);
const flagOptions = new Set(["--dry-run", "--draft", "--notify-claudeqq-brief-diff", "--allow-short-body", "--raw", "--full", "--stat"]);
for (let index = 0; index < args.length; index += 1) {
const arg = args[index];
if (!arg.startsWith("--")) continue;
@@ -657,12 +676,13 @@ function parseOptions(args: string[]): GitHubOptions {
validateKnownOptions(args);
const [top, sub] = args;
const requestedJsonFields = commaListOption(args, "--json");
const limitMax = top === "pr" && (sub === "files" || sub === "diff") ? MAX_PR_FILES_LIMIT : 100;
return {
repo: optionValue(args, "--repo") ?? DEFAULT_REPO,
dryRun: hasFlag(args, "--dry-run"),
raw: hasFlag(args, "--raw"),
full: hasFlag(args, "--full"),
limit: positiveIntegerOption(args, "--limit", top === "issue" && sub === "board-audit" ? 100 : 30, 100),
limit: positiveIntegerOption(args, "--limit", top === "issue" && sub === "board-audit" ? 100 : 30, limitMax),
boardIssue: positiveIntegerSingleOption(args, "--board-issue", CODE_QUEUE_BOARD_TARGET_ISSUE),
knownMetaIssues: positiveIntegerValuesOption(args, "--known-meta-issue"),
ignoredIssues: positiveIntegerValuesOption(args, "--ignore-issue"),
@@ -3557,6 +3577,49 @@ function prSummary(pr: GitHubPullRequest): Record<string, unknown> {
};
}
function numberOrNull(value: number | undefined): number | null {
return typeof value === "number" && Number.isFinite(value) ? value : null;
}
function prCompactSummary(pr: GitHubPullRequest): Record<string, unknown> {
return {
id: pr.id,
number: pr.number,
title: pr.title,
state: pr.state,
draft: pr.draft ?? false,
url: pr.html_url,
author: pr.user?.login ?? null,
head: { ref: pr.head?.ref ?? null, sha: pr.head?.sha ?? null },
base: { ref: pr.base?.ref ?? null, sha: pr.base?.sha ?? null },
createdAt: pr.created_at ?? null,
updatedAt: pr.updated_at ?? null,
};
}
function prFileSummary(file: GitHubPullRequestFile): Record<string, unknown> {
return {
filename: file.filename,
status: file.status ?? null,
additions: numberOrNull(file.additions),
deletions: numberOrNull(file.deletions),
changes: numberOrNull(file.changes),
previousFilename: file.previous_filename ?? null,
sha: file.sha ?? null,
blobUrl: file.blob_url ?? null,
contentsUrl: file.contents_url ?? null,
};
}
function sumPrFileStats(files: GitHubPullRequestFile[]): { additions: number; deletions: number; changes: number } {
return files.reduce((accumulator, file) => {
accumulator.additions += file.additions ?? 0;
accumulator.deletions += file.deletions ?? 0;
accumulator.changes += file.changes ?? ((file.additions ?? 0) + (file.deletions ?? 0));
return accumulator;
}, { additions: 0, deletions: 0, changes: 0 });
}
function selectedPrJson(summary: Record<string, unknown>, fields: readonly string[]): Record<string, unknown> {
const selected: Record<string, unknown> = {};
for (const field of fields) selected[field] = summary[field];
@@ -4839,6 +4902,75 @@ async function prList(repo: string, token: string, state: PrListState, limit: nu
};
}
async function prFiles(repo: string, token: string, number: number, limit: number, commandName = "pr files"): Promise<GitHubCommandResult> {
const { owner, name } = repoParts(repo);
const boundedLimit = Math.min(limit, MAX_PR_FILES_LIMIT);
const pr = await githubRequest<GitHubPullRequest>(token, "GET", `/repos/${owner}/${name}/pulls/${number}`);
if (isGitHubError(pr)) return commandError(commandName, repo, pr, { number });
const perPage = Math.max(1, Math.min(100, boundedLimit));
const files: GitHubPullRequestFile[] = [];
let page = 1;
while (files.length < boundedLimit) {
const remaining = boundedLimit - files.length;
const pageSize = Math.min(perPage, remaining);
const path = `/repos/${owner}/${name}/pulls/${number}/files?per_page=${pageSize}&page=${page}`;
const pageFiles = await githubRequest<GitHubPullRequestFile[]>(token, "GET", path);
if (isGitHubError(pageFiles)) return commandError(commandName, repo, pageFiles, { number, phase: "fetch-pr-files", filesReturned: files.length });
files.push(...pageFiles);
if (pageFiles.length < pageSize || pageFiles.length === 0) break;
page += 1;
}
const totalFiles = numberOrNull(pr.changed_files);
const listedStats = sumPrFileStats(files);
const totalAdditions = numberOrNull(pr.additions);
const totalDeletions = numberOrNull(pr.deletions);
const fullStatsAvailable = totalAdditions !== null && totalDeletions !== null;
const totalChanges = fullStatsAvailable ? totalAdditions + totalDeletions : listedStats.changes;
const truncated = totalFiles !== null ? files.length < totalFiles : files.length >= boundedLimit;
const nextLimit = totalFiles === null ? MAX_PR_FILES_LIMIT : Math.min(totalFiles, MAX_PR_FILES_LIMIT);
const nextCommand = truncated
? `bun scripts/cli.ts gh pr files ${number} --repo ${repo} --limit ${nextLimit}`
: `bun scripts/cli.ts gh pr read ${number} --repo ${repo} --json body,title,state,head,base`;
return {
ok: true,
command: commandName,
repo,
readOnly: true,
rawDiffIncluded: false,
pullRequest: prCompactSummary(pr),
summary: {
files: totalFiles ?? files.length,
additions: totalAdditions ?? listedStats.additions,
deletions: totalDeletions ?? listedStats.deletions,
changes: totalChanges,
commits: numberOrNull(pr.commits),
source: fullStatsAvailable && totalFiles !== null ? "pull-request-rest" : "listed-files-rest",
},
files: files.map(prFileSummary),
filesReturned: files.length,
limit: boundedLimit,
truncation: {
truncated,
requestedLimit: limit,
appliedLimit: boundedLimit,
returned: files.length,
totalFiles,
maxLimit: MAX_PR_FILES_LIMIT,
},
next: {
command: nextCommand,
purpose: truncated ? "Fetch a larger bounded file summary page." : "Fetch full PR metadata/body; raw diffs remain intentionally excluded.",
},
request: {
method: "GET",
paths: [
`/repos/${owner}/${name}/pulls/${number}`,
`/repos/${owner}/${name}/pulls/${number}/files`,
],
},
};
}
async function prRead(repo: string, token: string, number: number, jsonFields: PrReadJsonField[] | undefined, commandName = "pr read", disclosure: Record<string, unknown> | null = null): Promise<GitHubCommandResult> {
const { owner, name } = repoParts(repo);
const pr = await githubRequest<GitHubPullRequest>(token, "GET", `/repos/${owner}/${name}/pulls/${number}`);
@@ -4889,6 +5021,8 @@ export function ghHelp(): unknown {
"bun scripts/cli.ts gh issue board-row move <issueNumber> [--repo owner/name] --board-issue 20 --to open|closed [--status OPEN|CLOSED] [--dry-run] [--expect-body-sha sha256]",
"bun scripts/cli.ts gh issue board-row delete <issueNumber> [--repo owner/name] --board-issue 20 [--dry-run] [--expect-body-sha sha256]",
"bun scripts/cli.ts gh pr list [--repo owner/name] [--state open|closed|all] [--limit N] [--json number,title,state,url,updatedAt,createdAt,author,head,base,draft]",
"bun scripts/cli.ts gh pr files <number> [--repo owner/name] [--limit N]",
"bun scripts/cli.ts gh pr diff <number> --stat [--repo owner/name] [--limit N] [compatibility alias for pr files; no raw diff]",
"bun scripts/cli.ts gh pr read <number|owner/repo#number> [--repo owner/name] [--json body,title,state,head,base,draft,headRefName,baseRefName,mergeable,mergeStateStatus,statusCheckRollup] [--raw|--full]",
"bun scripts/cli.ts gh pr view <number|owner/repo#number> [--repo owner/name] [--raw|--full] [compatibility alias for pr read]",
"bun scripts/cli.ts gh pr create --title <title> --body-file <file>|--body <text> --base <branch> --head <branch> [--repo owner/name] [--draft] [--dry-run]",
@@ -4924,6 +5058,7 @@ export function ghHelp(): unknown {
"issue edit 24 --notify-claudeqq-brief-diff remains the legacy #24 notification helper: it reads the old issue body, PATCHes the new body, and sends only newly added '## ... ' sections to ClaudeQQ; ClaudeQQ failure does not roll back GitHub.",
"Commander brief ClaudeQQ defaults to private target 645275593 through backend-core /api/microservices/claudeqq/proxy; UNIDESK_COMMANDER_BRIEF_CLAUDEQQ_* env vars can override target, base URL, timeout, and enabled state.",
"comment delete is supported because GitHub supports deleting issue comments; issue/pr hard delete is unsupported and close is the lifecycle alternative.",
"PR files is the canonical compact changed-file/stat summary. It uses GitHub REST, returns bounded file rows, additions/deletions/changes when available, truncation metadata, and a next command for full details. Raw diff patches are not emitted by default; gh pr diff <number> --stat is a compatibility alias for the same JSON summary.",
"PR read is the canonical read path; view remains a compatibility alias. PR read/view accept owner/repo#number shorthand and derive --repo unless an explicit conflicting --repo is supplied, which fails structurally with suggested commands. PR read/view supports closeout fields headRefName, baseRefName, mergeable, mergeStateStatus, and statusCheckRollup; mergeability and status rollup are fetched through GitHub GraphQL only when requested or when --raw/--full requests full disclosure.",
"PR edit/update PATCHes /repos/{owner}/{repo}/pulls/{number} through REST only, never GitHub Projects Classic GraphQL/projectCards, and returns low-noise JSON with repo, PR number, changedFields, url, and body size/SHA metadata instead of echoing the full body.",
"PR create/update/comment are safe-write operations with dry-run planning; merge is intentionally unsupported in this phase.",
@@ -4971,6 +5106,15 @@ export async function runGhCommand(args: string[]): Promise<GitHubCommandResult
],
});
}
if (optionWasProvided(args, "--stat") && !(top === "pr" && sub === "diff")) {
const command = [top, sub].filter((value): value is string => value !== undefined).join(" ") || "gh";
return validationError(command, options.repo, "--stat is only supported by gh pr diff <number> --stat.", {
supportedCommands: [
"bun scripts/cli.ts gh pr files <number> --repo owner/name --limit 30",
"bun scripts/cli.ts gh pr diff <number> --stat --repo owner/name --limit 30",
],
});
}
if (optionWasProvided(args, "--mode") && !((top === "issue" && (sub === "update" || sub === "edit")) || (top === "pr" && (sub === "update" || sub === "edit")))) {
const command = [top, sub].filter((value): value is string => value !== undefined).join(" ") || "gh";
return validationError(command, options.repo, "--mode is only supported by gh issue update/edit and gh pr update/edit");
@@ -5110,6 +5254,31 @@ export async function runGhCommand(args: string[]): Promise<GitHubCommandResult
}
if (top === "pr") {
if (sub === "diff") {
const number = parseNumberForCommand(options.repo, third, "pr diff");
if (typeof number !== "number") return number;
if (!optionWasProvided(args, "--stat")) {
return unsupportedCommand("pr diff", options.repo, "Raw PR diff output is intentionally unsupported by UniDesk CLI; use gh pr diff <number> --stat or gh pr files for a bounded REST file/stat summary.", {
rawDiffIncluded: false,
supportedCommands: [
`bun scripts/cli.ts gh pr files ${number} --repo ${options.repo} --limit 30`,
`bun scripts/cli.ts gh pr diff ${number} --stat --repo ${options.repo} --limit 30`,
],
});
}
const { token, probe } = resolveToken(true);
const missing = authRequired(options.repo, "pr diff --stat", probe);
if (missing !== null || token === null) return missing ?? authRequired(options.repo, "pr diff --stat", { present: false, source: null, ghFallbackAttempted: true });
return prFiles(options.repo, token, number, options.limit, "pr diff --stat");
}
if (sub === "files") {
const number = parseNumberForCommand(options.repo, third, "pr files");
if (typeof number !== "number") return number;
const { token, probe } = resolveToken(true);
const missing = authRequired(options.repo, "pr files", probe);
if (missing !== null || token === null) return missing ?? authRequired(options.repo, "pr files", { present: false, source: null, ghFallbackAttempted: true });
return prFiles(options.repo, token, number, options.limit, "pr files");
}
if (sub === "delete") return unsupportedCommand("pr delete", options.repo, "GitHub REST does not support hard-deleting pull requests; use gh pr close for lifecycle deletion semantics.");
if (sub === "comment" && third === "delete") {
const commentId = parseNumberForCommand(options.repo, args[3], "pr comment delete");
@@ -5172,7 +5341,7 @@ export async function runGhCommand(args: string[]): Promise<GitHubCommandResult
return unsupportedCommand("pr merge", options.repo, "PR merge is intentionally unsupported in this phase; use create/comment/read only.");
}
if (sub !== "list" && !isPrReadCommand(sub)) {
return unsupportedCommand(`pr ${sub ?? ""}`.trim(), options.repo, "PR supported commands are list, read/view, create, update/edit, close, reopen, comment create/delete, and unsupported merge/delete.");
return unsupportedCommand(`pr ${sub ?? ""}`.trim(), options.repo, "PR supported commands are list, files, diff --stat, read/view, create, update/edit, close, reopen, comment create/delete, and unsupported merge/delete.");
}
if (sub === "read" || sub === "view") {
const resolved = resolveReadViewNumberReference("pr", sub, third, options, args);