Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 23 additions & 53 deletions apps/cli/src/commands/approve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
* equivalent to `fw pr review <pr> --approve`.
*/

import { exitWithError } from "@outfitter/cli/output";
import {
type GitHubClient,
approveHandler,
getDatabase,
loadConfig,
type FirewatchConfig,
} from "@outfitter/firewatch-core";
import { silentLogger } from "@outfitter/firewatch-shared";
import { Command, Option } from "commander";

import { createAuthenticatedClient } from "../auth-client";
import { applyCommonOptions } from "../query-helpers";
import { parseRepoInput, parsePrNumber, resolveRepoOrThrow } from "../repo";
import { parsePrNumber, resolveRepoOrThrow } from "../repo";
import { outputStructured } from "../utils/json";
import { shouldOutputJson } from "../utils/tty";

Expand All @@ -27,77 +28,46 @@ export interface ApproveCommandOptions {
noColor?: boolean;
}

interface ApproveContext {
client: GitHubClient;
config: FirewatchConfig;
repo: string;
owner: string;
name: string;
outputJson: boolean;
}

async function createContext(
options: ApproveCommandOptions
): Promise<ApproveContext> {
const config = await loadConfig();
const repo = await resolveRepoOrThrow(options.repo);
const { owner, name } = parseRepoInput(repo);

const { client } = await createAuthenticatedClient(config.github_token);

return {
client,
config,
repo,
owner,
name,
outputJson: shouldOutputJson(options, config.output?.default_format),
};
}

export async function approveAction(
pr: number,
options: ApproveCommandOptions
): Promise<void> {
applyCommonOptions(options);
try {
const ctx = await createContext(options);
const config = await loadConfig();
const db = getDatabase();
const repo = await resolveRepoOrThrow(options.repo);
const outputJson = shouldOutputJson(options, config.output?.default_format);

const reviewResult = await ctx.client.addReview(
ctx.owner,
ctx.name,
pr,
"approve",
options.body
const result = await approveHandler(
{ pr, repo, body: options.body },
{ config, db, logger: silentLogger }
);
if (reviewResult.isErr()) {
throw reviewResult.error;

if (result.isErr()) {
exitWithError(result.error);
}
const review = reviewResult.value;

const review = result.value;
const payload = {
ok: true,
repo: ctx.repo,
repo,
pr,
action: "approved",
...(review?.id && { review_id: review.id }),
...(review?.url && { url: review.url }),
...(review.id && { review_id: review.id }),
...(review.url && { url: review.url }),
};

if (ctx.outputJson) {
if (outputJson) {
await outputStructured(payload, "jsonl");
} else {
console.log(`Approved ${ctx.repo}#${pr}.`);
if (review?.url) {
console.log(`Approved ${repo}#${pr}.`);
if (review.url) {
console.log(review.url);
}
}
} catch (error) {
console.error(
"Approve failed:",
error instanceof Error ? error.message : error
);
process.exit(1);
exitWithError(error instanceof Error ? error : new Error(String(error)));
}
}

Expand Down
78 changes: 21 additions & 57 deletions apps/cli/src/commands/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
* equivalent to `fw pr comment`.
*/

import { exitWithError } from "@outfitter/cli/output";
import {
type GitHubClient,
commentHandler,
getDatabase,
loadConfig,
type FirewatchConfig,
} from "@outfitter/firewatch-core";
import { silentLogger } from "@outfitter/firewatch-shared";
import { Command, Option } from "commander";

import { createAuthenticatedClient } from "../auth-client";
import { applyCommonOptions } from "../query-helpers";
import { parseRepoInput, parsePrNumber, resolveRepoOrThrow } from "../repo";
import { parsePrNumber, resolveRepoOrThrow } from "../repo";
import { outputStructured } from "../utils/json";
import { shouldOutputJson } from "../utils/tty";

Expand All @@ -26,88 +27,51 @@ export interface CommentCommandOptions {
noColor?: boolean;
}

interface CommentContext {
client: GitHubClient;
config: FirewatchConfig;
repo: string;
owner: string;
name: string;
outputJson: boolean;
}

async function createContext(
options: CommentCommandOptions
): Promise<CommentContext> {
const config = await loadConfig();
const repo = await resolveRepoOrThrow(options.repo);
const { owner, name } = parseRepoInput(repo);

const { client } = await createAuthenticatedClient(config.github_token);

return {
client,
config,
repo,
owner,
name,
outputJson: shouldOutputJson(options, config.output?.default_format),
};
}

export async function commentAction(
pr: number,
body: string,
options: CommentCommandOptions
): Promise<void> {
applyCommonOptions(options);
if (!body.trim()) {
console.error("Comment body cannot be empty.");
process.exit(1);
exitWithError(new Error("Comment body cannot be empty."));
}

try {
const ctx = await createContext(options);
const config = await loadConfig();
const db = getDatabase();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Remove mandatory SQLite initialization from write-only commands

Initializing db here introduces a new hard dependency on local cache storage for a command that only performs a GitHub mutation. getDatabase() creates directories/files under the cache path (mkdirSync + openDatabase in packages/core/src/cache.ts), so fw comment can now fail before hitting GitHub in read-only/containerized environments where it previously worked.

Useful? React with 👍 / 👎.

const repo = await resolveRepoOrThrow(options.repo);
const outputJson = shouldOutputJson(options, config.output?.default_format);

const prIdResult = await ctx.client.fetchPullRequestId(
ctx.owner,
ctx.name,
pr
);
if (prIdResult.isErr()) {
throw prIdResult.error;
}
const commentResult = await ctx.client.addIssueComment(
prIdResult.value,
body
const result = await commentHandler(
{ pr, body, repo },
{ config, db, logger: silentLogger }
);
Comment on lines +46 to 49

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve transport errors when delegating comment creation

Routing comment through commentHandler changes failure semantics: commentHandler converts any fetchPullRequestId failure into NotFoundError (in packages/core/src/handlers/comment.ts), so auth failures or transient GitHub/network errors are now reported as “PR not found.” This breaks the new category-based exit behavior for real-world cases like expired tokens or API outages and makes retries/diagnosis incorrect.

Useful? React with 👍 / 👎.

if (commentResult.isErr()) {
throw commentResult.error;

if (result.isErr()) {
exitWithError(result.error);
}
const comment = commentResult.value;

const comment = result.value;
const payload = {
ok: true,
repo: ctx.repo,
repo,
pr,
action: "comment",
id: comment.id,
...(comment.url && { url: comment.url }),
};

if (ctx.outputJson) {
if (outputJson) {
await outputStructured(payload, "jsonl");
} else {
console.log(`Added comment to ${ctx.repo}#${pr}.`);
console.log(`Added comment to ${repo}#${pr}.`);
if (comment.url) {
console.log(comment.url);
}
}
} catch (error) {
console.error(
"Comment failed:",
error instanceof Error ? error.message : error
);
process.exit(1);
exitWithError(error instanceof Error ? error : new Error(String(error)));
}
}

Expand Down
10 changes: 4 additions & 6 deletions apps/cli/src/commands/doctor.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { exitWithError } from "@outfitter/cli/output";
import {
doctorHandler,
getDatabase,
Expand Down Expand Up @@ -36,8 +37,7 @@ export const doctorCommand = new Command("doctor")

const result = await doctorHandler({ fix: options.fix }, ctx);
if (result.isErr()) {
console.error("Doctor failed:", result.error.message);
process.exit(1);
exitWithError(result.error);
}

const output = result.value;
Expand Down Expand Up @@ -91,10 +91,8 @@ export const doctorCommand = new Command("doctor")
console.log("");
console.log(`${counts.ok} passed, ${counts.failed} failed`);
} catch (error) {
console.error(
"Doctor failed:",
error instanceof Error ? error.message : error
exitWithError(
error instanceof Error ? error : new Error(String(error))
);
process.exit(1);
}
});
Loading