From 5a4e98f46b8cd0b5d636d4a5cd1553a3ac97b56d Mon Sep 17 00:00:00 2001 From: Nate Date: Sat, 7 Feb 2026 20:29:51 -0800 Subject: [PATCH 1/3] feat: add `cn checks` CLI command Adds a new command to view CI check statuses, suggested diffs, and accept/reject pending suggestions for a PR directly from the terminal. Usage: cn checks [pr-url] # List checks cn checks --diff [pr-url] # Show diffs inline cn checks accept [pr-url] # Accept pending suggestions cn checks reject [pr-url] # Reject pending suggestions Auto-detects PR from current git branch when no URL is given. Co-Authored-By: Claude Opus 4.6 --- extensions/cli/src/commands/checks.ts | 331 ++++++++++++++++++++++++++ extensions/cli/src/index.ts | 18 ++ 2 files changed, 349 insertions(+) create mode 100644 extensions/cli/src/commands/checks.ts diff --git a/extensions/cli/src/commands/checks.ts b/extensions/cli/src/commands/checks.ts new file mode 100644 index 000000000..91c8cfc93 --- /dev/null +++ b/extensions/cli/src/commands/checks.ts @@ -0,0 +1,331 @@ +import chalk from "chalk"; + +import { get, post } from "../util/apiClient.js"; +import { gracefulExit } from "../util/exit.js"; +import { getGitBranch, getGitRemoteUrl } from "../util/git.js"; +import { logger } from "../util/logger.js"; + +interface CheckStatus { + name: string; + state: "pending" | "success" | "failure"; + description: string; + sessionId: string; + commitMessage: string | null; + suggestionStatus: string | null; + agentStatus: string; +} + +interface ChecksStatusResponse { + checks: CheckStatus[]; + pullRequestUrl: string; +} + +interface ChecksOptions { + diff?: boolean; + json?: boolean; +} + +/** + * Parse owner/repo from a git remote URL. + * Supports HTTPS and SSH formats. + */ +function parseOwnerRepo( + remoteUrl: string, +): { owner: string; repo: string } | null { + // HTTPS: https://github.com/owner/repo.git + const httpsMatch = remoteUrl.match( + /github\.com\/([^/]+)\/([^/.]+?)(?:\.git)?$/, + ); + if (httpsMatch && httpsMatch[1] && httpsMatch[2]) { + return { owner: httpsMatch[1], repo: httpsMatch[2] }; + } + // SSH: git@github.com:owner/repo.git + const sshMatch = remoteUrl.match(/github\.com:([^/]+)\/([^/.]+?)(?:\.git)?$/); + if (sshMatch && sshMatch[1] && sshMatch[2]) { + return { owner: sshMatch[1], repo: sshMatch[2] }; + } + return null; +} + +/** + * Auto-detect the PR URL from the current git branch using the GitHub API. + */ +async function detectPrUrl(): Promise { + const branch = getGitBranch(); + if (!branch) { + return null; + } + + const remoteUrl = getGitRemoteUrl(); + if (!remoteUrl) { + return null; + } + + const parsed = parseOwnerRepo(remoteUrl); + if (!parsed) { + return null; + } + + try { + const response = await fetch( + `https://api.github.com/repos/${parsed.owner}/${parsed.repo}/pulls?head=${parsed.owner}:${branch}&state=open`, + { + headers: { + Accept: "application/vnd.github.v3+json", + ...(process.env.GITHUB_TOKEN + ? { Authorization: `token ${process.env.GITHUB_TOKEN}` } + : {}), + }, + }, + ); + + if (!response.ok) { + logger.debug(`GitHub API returned ${response.status}`); + return null; + } + + const prs = (await response.json()) as Array<{ html_url: string }>; + if (prs.length > 0 && prs[0]) { + return prs[0].html_url; + } + } catch (err) { + logger.debug(`Failed to detect PR URL: ${err}`); + } + + return null; +} + +/** + * Resolve the PR URL from the argument or auto-detect from git. + */ +async function resolvePrUrl(prUrlArg: string | undefined): Promise { + if (prUrlArg) { + return prUrlArg; + } + + console.log(chalk.dim("Auto-detecting PR from current branch...")); + const detected = await detectPrUrl(); + if (!detected) { + console.error( + chalk.red( + "Could not detect a PR for the current branch. Please provide a PR URL.", + ), + ); + await gracefulExit(1); + throw new Error("unreachable"); + } + + console.log(chalk.dim(`Found PR: ${detected}`)); + return detected; +} + +const STATE_ICONS: Record = { + success: chalk.green("\u2714"), + failure: chalk.red("\u2716"), + pending: chalk.yellow("\u25CB"), +}; + +/** + * List check statuses for a PR. + */ +async function listChecks( + prUrl: string, + options: ChecksOptions, +): Promise { + const response = await get( + `api/checks/status?pullRequestUrl=${encodeURIComponent(prUrl)}`, + ); + const { checks } = response.data; + + if (options.json) { + console.log(JSON.stringify(response.data, null, 2)); + return; + } + + if (checks.length === 0) { + console.log(chalk.dim("No checks found for this PR.")); + return; + } + + console.log(chalk.bold(`\nChecks for ${chalk.cyan(prUrl)}\n`)); + + for (const check of checks) { + const icon = STATE_ICONS[check.state] || "?"; + console.log(`${icon} ${chalk.bold(check.name)}`); + console.log(` ${chalk.dim(check.description)}`); + + if (check.commitMessage) { + console.log(` Commit: ${check.commitMessage}`); + } + + if (check.suggestionStatus) { + const statusColor = + check.suggestionStatus === "pending" + ? chalk.yellow + : check.suggestionStatus === "accepted" + ? chalk.green + : chalk.red; + console.log(` Suggestion: ${statusColor(check.suggestionStatus)}`); + } + + console.log(); + } + + if (options.diff) { + for (const check of checks) { + if (!check.commitMessage) { + continue; + } + try { + const diffResponse = await get<{ diff: string }>( + `agents/${check.sessionId}/diff`, + ); + if (diffResponse.data.diff) { + console.log(chalk.bold(`--- Diff: ${check.name} ---`)); + console.log(diffResponse.data.diff); + console.log(); + } + } catch (err) { + logger.debug(`Failed to fetch diff for ${check.sessionId}: ${err}`); + } + } + } + + // Summary line + const pending = checks.filter((c) => c.state === "pending").length; + const failures = checks.filter((c) => c.state === "failure").length; + const successes = checks.filter((c) => c.state === "success").length; + + const parts: string[] = []; + if (successes > 0) parts.push(chalk.green(`${successes} passed`)); + if (failures > 0) parts.push(chalk.red(`${failures} failing`)); + if (pending > 0) parts.push(chalk.yellow(`${pending} pending`)); + console.log(parts.join(", ")); + + // Exit code: 0=all pass, 1=any failure, 2=still pending + if (failures > 0) { + await gracefulExit(1); + } else if (pending > 0) { + await gracefulExit(2); + } +} + +/** + * Accept all pending suggestions for a PR. + */ +async function acceptChecks(prUrl: string): Promise { + const response = await get( + `api/checks/status?pullRequestUrl=${encodeURIComponent(prUrl)}`, + ); + const { checks } = response.data; + + const pending = checks.filter( + (c) => c.suggestionStatus === "pending" && c.commitMessage, + ); + + if (pending.length === 0) { + console.log(chalk.dim("No pending suggestions to accept.")); + return; + } + + console.log( + chalk.bold(`Accepting ${pending.length} pending suggestion(s)...\n`), + ); + + for (const check of pending) { + try { + await post(`agents/${check.sessionId}/accept`); + console.log(chalk.green(`\u2714 Accepted: ${check.name}`)); + } catch (err) { + const msg = err instanceof Error ? err.message : String(err); + console.error( + chalk.red(`\u2716 Failed to accept ${check.name}: ${msg}`), + ); + } + } +} + +/** + * Reject all pending suggestions for a PR. + */ +async function rejectChecks(prUrl: string): Promise { + const response = await get( + `api/checks/status?pullRequestUrl=${encodeURIComponent(prUrl)}`, + ); + const { checks } = response.data; + + const pending = checks.filter( + (c) => c.suggestionStatus === "pending" && c.commitMessage, + ); + + if (pending.length === 0) { + console.log(chalk.dim("No pending suggestions to reject.")); + return; + } + + console.log( + chalk.bold(`Rejecting ${pending.length} pending suggestion(s)...\n`), + ); + + for (const check of pending) { + try { + await post(`agents/${check.sessionId}/reject`); + console.log(chalk.red(`\u2716 Rejected: ${check.name}`)); + } catch (err) { + const msg = err instanceof Error ? err.message : String(err); + console.error( + chalk.red(`\u2716 Failed to reject ${check.name}: ${msg}`), + ); + } + } +} + +/** + * Main entry point for `cn checks` command. + * + * Usage: + * cn checks [pr-url] - List checks + * cn checks accept [pr-url] - Accept pending suggestions + * cn checks reject [pr-url] - Reject pending suggestions + * cn checks [pr-url] --diff - Show diffs inline + * cn checks [pr-url] --json - JSON output + */ +export async function checks( + actionOrUrl: string | undefined, + prUrlArg: string | undefined, + options: ChecksOptions, +): Promise { + try { + // Determine if first arg is an action or a PR URL + let action: "list" | "accept" | "reject" = "list"; + let rawPrUrl: string | undefined = prUrlArg; + + if (actionOrUrl === "accept" || actionOrUrl === "reject") { + action = actionOrUrl; + // prUrlArg already has the right value from Commander + } else if (actionOrUrl) { + // First arg is a PR URL, not an action + rawPrUrl = actionOrUrl; + } + + const prUrl = await resolvePrUrl(rawPrUrl); + + switch (action) { + case "accept": + await acceptChecks(prUrl); + break; + case "reject": + await rejectChecks(prUrl); + break; + default: + await listChecks(prUrl, options); + break; + } + } catch (err) { + if (err instanceof Error && err.name === "AuthenticationRequiredError") { + console.error(chalk.red(err.message)); + await gracefulExit(1); + } + throw err; + } +} diff --git a/extensions/cli/src/index.ts b/extensions/cli/src/index.ts index 39e0fcb69..3037f3046 100644 --- a/extensions/cli/src/index.ts +++ b/extensions/cli/src/index.ts @@ -6,6 +6,7 @@ import "./init.js"; import { Command } from "commander"; import { chat } from "./commands/chat.js"; +import { checks } from "./commands/checks.js"; import { login } from "./commands/login.js"; import { logout } from "./commands/logout.js"; import { listSessionsCommand } from "./commands/ls.js"; @@ -420,6 +421,23 @@ program await remoteTest(prompt, options.url); }); +// Checks subcommand +program + .command("checks [action] [pr-url]") + .description("Show CI check statuses for a PR") + .option("--diff", "Show suggested diffs inline") + .option("--json", "Output in JSON format") + .action( + async ( + action: string | undefined, + prUrl: string | undefined, + options: any, + ) => { + await posthogService.capture("cliCommand", { command: "checks" }); + await checks(action, prUrl, options); + }, + ); + // Review subcommand program .command("review") From 2587e61e60982762d4c3666f2645d5922690cf72 Mon Sep 17 00:00:00 2001 From: Nate Date: Sat, 7 Feb 2026 20:58:58 -0800 Subject: [PATCH 2/3] refactor: remove --diff and --json flags, always show diffs Diffs are now always included in the output for checks with commits. Removes unnecessary --json and --diff flags to keep the CLI simple. Co-Authored-By: Claude Opus 4.6 --- extensions/cli/src/commands/checks.ts | 42 +++++++-------------------- extensions/cli/src/index.ts | 16 +++------- 2 files changed, 15 insertions(+), 43 deletions(-) diff --git a/extensions/cli/src/commands/checks.ts b/extensions/cli/src/commands/checks.ts index 91c8cfc93..2190c56e8 100644 --- a/extensions/cli/src/commands/checks.ts +++ b/extensions/cli/src/commands/checks.ts @@ -20,11 +20,6 @@ interface ChecksStatusResponse { pullRequestUrl: string; } -interface ChecksOptions { - diff?: boolean; - json?: boolean; -} - /** * Parse owner/repo from a git remote URL. * Supports HTTPS and SSH formats. @@ -126,22 +121,14 @@ const STATE_ICONS: Record = { }; /** - * List check statuses for a PR. + * List check statuses for a PR, including diffs for checks with commits. */ -async function listChecks( - prUrl: string, - options: ChecksOptions, -): Promise { +async function listChecks(prUrl: string): Promise { const response = await get( `api/checks/status?pullRequestUrl=${encodeURIComponent(prUrl)}`, ); const { checks } = response.data; - if (options.json) { - console.log(JSON.stringify(response.data, null, 2)); - return; - } - if (checks.length === 0) { console.log(chalk.dim("No checks found for this PR.")); return; @@ -168,27 +155,23 @@ async function listChecks( console.log(` Suggestion: ${statusColor(check.suggestionStatus)}`); } - console.log(); - } - - if (options.diff) { - for (const check of checks) { - if (!check.commitMessage) { - continue; - } + if (check.commitMessage) { try { const diffResponse = await get<{ diff: string }>( `agents/${check.sessionId}/diff`, ); if (diffResponse.data.diff) { - console.log(chalk.bold(`--- Diff: ${check.name} ---`)); - console.log(diffResponse.data.diff); - console.log(); + console.log(`\n${chalk.bold(` Diff:`)}`); + for (const line of diffResponse.data.diff.split("\n")) { + console.log(` ${line}`); + } } } catch (err) { logger.debug(`Failed to fetch diff for ${check.sessionId}: ${err}`); } } + + console.log(); } // Summary line @@ -284,16 +267,13 @@ async function rejectChecks(prUrl: string): Promise { * Main entry point for `cn checks` command. * * Usage: - * cn checks [pr-url] - List checks + * cn checks [pr-url] - List checks with diffs * cn checks accept [pr-url] - Accept pending suggestions * cn checks reject [pr-url] - Reject pending suggestions - * cn checks [pr-url] --diff - Show diffs inline - * cn checks [pr-url] --json - JSON output */ export async function checks( actionOrUrl: string | undefined, prUrlArg: string | undefined, - options: ChecksOptions, ): Promise { try { // Determine if first arg is an action or a PR URL @@ -318,7 +298,7 @@ export async function checks( await rejectChecks(prUrl); break; default: - await listChecks(prUrl, options); + await listChecks(prUrl); break; } } catch (err) { diff --git a/extensions/cli/src/index.ts b/extensions/cli/src/index.ts index 3037f3046..a3a718e3e 100644 --- a/extensions/cli/src/index.ts +++ b/extensions/cli/src/index.ts @@ -425,18 +425,10 @@ program program .command("checks [action] [pr-url]") .description("Show CI check statuses for a PR") - .option("--diff", "Show suggested diffs inline") - .option("--json", "Output in JSON format") - .action( - async ( - action: string | undefined, - prUrl: string | undefined, - options: any, - ) => { - await posthogService.capture("cliCommand", { command: "checks" }); - await checks(action, prUrl, options); - }, - ); + .action(async (action: string | undefined, prUrl: string | undefined) => { + await posthogService.capture("cliCommand", { command: "checks" }); + await checks(action, prUrl); + }); // Review subcommand program From ff124c293e060d76e02400fa2be0d84a02ef3ae9 Mon Sep 17 00:00:00 2001 From: Nate Date: Sat, 7 Feb 2026 21:05:29 -0800 Subject: [PATCH 3/3] fix: extract printCheckDiff helper to satisfy max-depth lint rule Co-Authored-By: Claude Opus 4.6 --- extensions/cli/src/commands/checks.ts | 34 +++++++++++++++++---------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/extensions/cli/src/commands/checks.ts b/extensions/cli/src/commands/checks.ts index 2190c56e8..6e0a79fba 100644 --- a/extensions/cli/src/commands/checks.ts +++ b/extensions/cli/src/commands/checks.ts @@ -120,6 +120,26 @@ const STATE_ICONS: Record = { pending: chalk.yellow("\u25CB"), }; +/** + * Fetch and print the diff for a check, indented under the check output. + */ +async function printCheckDiff(check: CheckStatus): Promise { + try { + const diffResponse = await get<{ diff: string }>( + `agents/${check.sessionId}/diff`, + ); + if (!diffResponse.data.diff) { + return; + } + console.log(`\n${chalk.bold(` Diff:`)}`); + for (const line of diffResponse.data.diff.split("\n")) { + console.log(` ${line}`); + } + } catch (err) { + logger.debug(`Failed to fetch diff for ${check.sessionId}: ${err}`); + } +} + /** * List check statuses for a PR, including diffs for checks with commits. */ @@ -156,19 +176,7 @@ async function listChecks(prUrl: string): Promise { } if (check.commitMessage) { - try { - const diffResponse = await get<{ diff: string }>( - `agents/${check.sessionId}/diff`, - ); - if (diffResponse.data.diff) { - console.log(`\n${chalk.bold(` Diff:`)}`); - for (const line of diffResponse.data.diff.split("\n")) { - console.log(` ${line}`); - } - } - } catch (err) { - logger.debug(`Failed to fetch diff for ${check.sessionId}: ${err}`); - } + await printCheckDiff(check); } console.log();