Skip to content
Merged
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
31 changes: 31 additions & 0 deletions packages/core/src/git/router-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,37 @@ export const updatePrByUrlOutput = z.object({
});
export type UpdatePrByUrlOutput = z.infer<typeof updatePrByUrlOutput>;

export type {
ApprovePrOutput,
GetPrChecksOutput,
GetPrCommentsOutput,
MergePrOutput,
PrCheck,
PrCheckBucket,
PrConversationComment,
PrInfoByUrlOutput,
PrMergeMethod,
} from "@posthog/shared";
// Native PR review schemas (PR overview, approve/merge, CI checks,
// conversation comments) are defined once in `@posthog/shared`'s git domain
// and re-exported here for the host router and UI.
export {
approvePrInput,
approvePrOutput,
getPrChecksInput,
getPrChecksOutput,
getPrCommentsInput,
getPrCommentsOutput,
getPrInfoByUrlInput,
getPrInfoByUrlOutput,
mergePrInput,
mergePrOutput,
prCheckBucketSchema,
prCheckSchema,
prConversationCommentSchema,
prMergeMethodSchema,
} from "@posthog/shared";

export const getBranchChangedFilesInput = z.object({
repo: z.string(),
branch: z.string(),
Expand Down
16 changes: 15 additions & 1 deletion packages/git/src/gh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,17 @@ export interface GhExecOptions {
* MCP tool awaiting it — indefinitely. Omit for no timeout.
*/
timeoutMs?: number;
/**
* Max stdout/stderr bytes before the child is killed. Node's execFile
* default is 1 MiB, which paginated `gh api` calls (PR files, comments)
* blow past on busy PRs — the call then dies with "maxBuffer length
* exceeded" instead of returning data.
*/
maxBuffer?: number;
}

const DEFAULT_MAX_BUFFER = 32 * 1024 * 1024;

export function execGh(
args: string[],
options: GhExecOptions = {},
Expand All @@ -37,7 +46,12 @@ export function execGh(
const child = childProcess.execFile(
"gh",
args,
{ cwd: options.cwd, env, timeout: options.timeoutMs ?? 0 },
{
cwd: options.cwd,
env,
timeout: options.timeoutMs ?? 0,
maxBuffer: options.maxBuffer ?? DEFAULT_MAX_BUFFER,
},
(error, stdout, stderr) => {
if (!error) {
resolve({ stdout, stderr, exitCode: 0 });
Expand Down
56 changes: 56 additions & 0 deletions packages/host-router/src/routers/git.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
GIT_WORKSPACE_CLIENT,
} from "@posthog/core/git/identifiers";
import {
approvePrInput,
approvePrOutput,
checkoutBranchInput,
checkoutBranchOutput,
cloneRepositoryInput,
Expand Down Expand Up @@ -58,10 +60,16 @@ import {
getLocalBranchChangedFilesOutput,
getPrChangedFilesInput,
getPrChangedFilesOutput,
getPrChecksInput,
getPrChecksOutput,
getPrCommentsInput,
getPrCommentsOutput,
getPrDetailsByUrlInput,
getPrDetailsByUrlOutput,
getPrDiffStatsBatchInput,
getPrDiffStatsBatchOutput,
getPrInfoByUrlInput,
getPrInfoByUrlOutput,
getPrReviewCommentsInput,
getPrReviewCommentsOutput,
getPrTemplateInput,
Expand All @@ -72,6 +80,8 @@ import {
ghStatusOutput,
gitStateSnapshotSchema,
gitStatusOutput,
mergePrInput,
mergePrOutput,
openPrInput,
openPrOutput,
prStatusInput,
Expand Down Expand Up @@ -529,6 +539,52 @@ export const gitRouter = router({
}),
),

getPrInfoByUrl: publicProcedure
.input(getPrInfoByUrlInput)
.output(getPrInfoByUrlOutput.nullable())
.query(({ ctx, input }) =>
getWorkspaceClient(ctx.container).git.getPrInfoByUrl.query({
prUrl: input.prUrl,
}),
),

getPrChecks: publicProcedure
.input(getPrChecksInput)
.output(getPrChecksOutput)
.query(({ ctx, input }) =>
getWorkspaceClient(ctx.container).git.getPrChecks.query({
prUrl: input.prUrl,
}),
),

getPrComments: publicProcedure
.input(getPrCommentsInput)
.output(getPrCommentsOutput)
.query(({ ctx, input }) =>
getWorkspaceClient(ctx.container).git.getPrComments.query({
prUrl: input.prUrl,
}),
),

approvePr: publicProcedure
.input(approvePrInput)
.output(approvePrOutput)
.mutation(({ ctx, input }) =>
getWorkspaceClient(ctx.container).git.approvePr.mutate({
prUrl: input.prUrl,
}),
),

mergePr: publicProcedure
.input(mergePrInput)
.output(mergePrOutput)
.mutation(({ ctx, input }) =>
getWorkspaceClient(ctx.container).git.mergePr.mutate({
prUrl: input.prUrl,
method: input.method,
}),
),

getPrReviewComments: publicProcedure
.input(getPrReviewCommentsInput)
.output(getPrReviewCommentsOutput)
Expand Down
102 changes: 102 additions & 0 deletions packages/shared/src/git-domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,105 @@ export type GithubPullRequest = GithubRef;
// git-interaction UI (PR status menu actions).
export const prActionTypeSchema = z.enum(["close", "reopen", "ready", "draft"]);
export type PrActionType = z.infer<typeof prActionTypeSchema>;

// Native PR review schemas (PR overview, approve/merge, CI checks,
// conversation comments). Defined once here and re-exported by
// `@posthog/core/git/router-schemas` and workspace-server's git schemas so
// the tRPC layers on both sides of the boundary share one source of truth.

/** Full PR overview (title/body/branches/stats) for the native in-app PR view. */
export const getPrInfoByUrlInput = z.object({ prUrl: z.string() });

export const getPrInfoByUrlOutput = z.object({
number: z.number(),
title: z.string(),
body: z.string(),
author: z.string().nullable(),
state: z.string(),
merged: z.boolean(),
draft: z.boolean(),
/** GitHub computes mergeability asynchronously; null until it settles. */
mergeable: z.boolean().nullable(),
/**
* GitHub's `mergeable_state`: "clean" | "unstable" | "blocked" | "dirty" |
* "behind" | "draft" | "unknown". "blocked" means branch protection forbids
* the merge for this viewer — e.g. a required approving review is missing
* (authors can't approve their own PRs) or required checks are failing.
* Kept as a plain string so an undocumented value can't fail the parse.
*/
mergeStateStatus: z.string().catch("unknown"),
baseRefName: z.string().nullable(),
headRefName: z.string().nullable(),
additions: z.number(),
deletions: z.number(),
changedFiles: z.number(),
});

export type PrInfoByUrlOutput = z.infer<typeof getPrInfoByUrlOutput>;

export const approvePrInput = z.object({ prUrl: z.string() });

export const approvePrOutput = z.object({
success: z.boolean(),
message: z.string(),
});

export type ApprovePrOutput = z.infer<typeof approvePrOutput>;

export const prMergeMethodSchema = z.enum(["merge", "squash", "rebase"]);
export type PrMergeMethod = z.infer<typeof prMergeMethodSchema>;

export const mergePrInput = z.object({
prUrl: z.string(),
method: prMergeMethodSchema,
});

export const mergePrOutput = z.object({
success: z.boolean(),
message: z.string(),
});

export type MergePrOutput = z.infer<typeof mergePrOutput>;

// CI check runs / commit statuses for a PR, via `gh pr checks`.
export const prCheckBucketSchema = z.enum([
"fail",
"cancel",
"pending",
"pass",
"skipping",
]);
export type PrCheckBucket = z.infer<typeof prCheckBucketSchema>;

export const prCheckSchema = z.object({
name: z.string(),
bucket: prCheckBucketSchema,
link: z.string().nullable(),
workflow: z.string().nullable(),
description: z.string().nullable(),
});
export type PrCheck = z.infer<typeof prCheckSchema>;

export const getPrChecksInput = z.object({ prUrl: z.string() });
/** Null means the checks couldn't be fetched; [] means none reported. */
export const getPrChecksOutput = z.array(prCheckSchema).nullable();
export type GetPrChecksOutput = z.infer<typeof getPrChecksOutput>;

// Conversation (issue) comments and review summaries on a PR. Inline review
// comments live in `prReviewThreadSchema` above.
export const prConversationCommentSchema = z.object({
id: z.number(),
author: z.string(),
avatarUrl: z.string().nullable(),
body: z.string(),
createdAt: z.string(),
url: z.string().nullable(),
});
export type PrConversationComment = z.infer<typeof prConversationCommentSchema>;

export const getPrCommentsInput = z.object({ prUrl: z.string() });
/** Null means the comments couldn't be fetched; [] means none. */
export const getPrCommentsOutput = z
.array(prConversationCommentSchema)
.nullable();
export type GetPrCommentsOutput = z.infer<typeof getPrCommentsOutput>;
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { type FileDiffMetadata, processFile } from "@pierre/diffs";
import type { PrCommentThread } from "@posthog/core/code-review/types";
import { isBinaryFile } from "@posthog/shared";
import type { ChangedFile } from "@posthog/shared/domain-types";
import { useMemo } from "react";
import { type ReactNode, useMemo } from "react";
import { DeferredDiffPlaceholder, DiffFileHeader } from "../reviewShellParts";
import type { DiffOptions } from "../types";
import { InteractiveFileDiff } from "./InteractiveFileDiff";
Expand All @@ -17,6 +17,8 @@ interface PatchedFileDiffProps {
externalUrl?: string;
prUrl?: string | null;
commentThreads?: Map<number, PrCommentThread>;
/** Extra controls in the file header row (e.g. a "Viewed" toggle). */
headerTrailing?: ReactNode;
}

export function PatchedFileDiff({
Expand All @@ -29,6 +31,7 @@ export function PatchedFileDiff({
externalUrl,
prUrl,
commentThreads,
headerTrailing,
}: PatchedFileDiffProps) {
const fileDiff = useMemo((): FileDiffMetadata | undefined => {
if (!file.patch) return undefined;
Expand Down Expand Up @@ -60,6 +63,7 @@ export function PatchedFileDiff({
collapsed={collapsed}
onToggle={onToggle}
externalUrl={externalUrl}
headerTrailing={headerTrailing}
/>
);
}
Expand All @@ -74,6 +78,7 @@ export function PatchedFileDiff({
collapsed={collapsed}
onToggle={onToggle}
externalUrl={externalUrl}
headerTrailing={headerTrailing}
/>
);
}
Expand All @@ -90,6 +95,7 @@ export function PatchedFileDiff({
fileDiff={fd}
collapsed={collapsed}
onToggle={onToggle}
trailing={headerTrailing}
/>
)}
/>
Expand Down
18 changes: 16 additions & 2 deletions packages/ui/src/features/code-review/reviewShellParts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export {

const STICKY_HEADER_CSS = `[data-diffs-header] { position: sticky; top: 0; z-index: 1; background: var(--gray-2); }`;

function useDiffOptions() {
export function useDiffOptions() {
const viewMode = useDiffViewerStore((s) => s.viewMode);
const wordWrap = useDiffViewerStore((s) => s.wordWrap);
const loadFullFiles = useDiffViewerStore((s) => s.loadFullFiles);
Expand Down Expand Up @@ -209,6 +209,7 @@ export function DiffFileHeader({
onDiscard,
onStage,
staged,
trailing,
}: {
fileDiff: FileDiffMetadata;
collapsed: boolean;
Expand All @@ -217,6 +218,8 @@ export function DiffFileHeader({
onDiscard?: () => void;
onStage?: () => void;
staged?: boolean;
/** Extra controls rendered after the action buttons (e.g. a "Viewed" toggle). */
trailing?: ReactNode;
}) {
const fullPath =
fileDiff.prevName && fileDiff.prevName !== fileDiff.name
Expand All @@ -234,7 +237,7 @@ export function DiffFileHeader({
collapsed={collapsed}
onToggle={onToggle}
trailing={
(onStage || onDiscard || onOpenFile) && (
(onStage || onDiscard || onOpenFile || trailing) && (
<span className="ml-auto inline-flex items-center gap-[2px]">
{onStage && (
<Tooltip content={staged ? "Unstage" : "Stage"}>
Expand Down Expand Up @@ -278,6 +281,7 @@ export function DiffFileHeader({
</button>
</Tooltip>
)}
{trailing}
</span>
)
}
Expand All @@ -294,6 +298,7 @@ export function DeferredDiffPlaceholder({
onToggle,
onShow,
externalUrl,
headerTrailing,
}: {
filePath: string;
linesAdded: number;
Expand All @@ -303,6 +308,8 @@ export function DeferredDiffPlaceholder({
onToggle: () => void;
onShow?: () => void;
externalUrl?: string;
/** Extra controls in the header row (e.g. a "Viewed" toggle). */
headerTrailing?: ReactNode;
}) {
const { dirPath, fileName } = splitFilePath(filePath);

Expand All @@ -315,6 +322,13 @@ export function DeferredDiffPlaceholder({
deletions={linesRemoved}
collapsed={collapsed}
onToggle={onToggle}
trailing={
headerTrailing && (
<span className="ml-auto inline-flex items-center">
{headerTrailing}
</span>
)
}
/>
{!collapsed && (
<div className="w-full border-b border-b-(--gray-5) bg-(--gray-2) p-[16px] text-center text-(--gray-9) text-xs">
Expand Down
Loading
Loading