* feat(app): add pull request panel context actions Attach pull request comments, reviews, and failed check details to chat context. The PR panel now lives in its own module so the activity, query, and context policies have one home. * fix(server): mark capped check details as truncated * fix(app): preserve concurrent PR panel attachments * fix(server): preserve annotation truncation state * fix(app): keep matrix check attachments distinct * fix(app): streamline pull request context attachments * fix(app): simplify pull request feedback rendering * feat(app): group pull request review threads * fix(app): tidy pull request panel boundaries * fix: use worktree cwd for dev services * fix(app): keep git query keys UI-free * feat(app): collapse resolved comments and add chat actions to headers * fix: address PR feedback and Playwright flakes * fix(app): hide check chat action without attachment scope * fix(server): dedupe inline review timeline comments
295 lines
7.4 KiB
TypeScript
295 lines
7.4 KiB
TypeScript
import type {
|
|
CheckoutPrStatusResponse,
|
|
PullRequestTimelineResponse,
|
|
} from "@getpaseo/protocol/messages";
|
|
|
|
export type PrState = "open" | "draft" | "merged" | "closed";
|
|
export type CheckStatus = "success" | "failure" | "pending" | "skipped";
|
|
export type ReviewState = "approved" | "changes_requested" | "commented";
|
|
export type ActivityKind = "review" | "comment";
|
|
export type PullRequestProvider = "github";
|
|
|
|
export interface PullRequestProviderMetadata {
|
|
id: PullRequestProvider;
|
|
label: string;
|
|
url?: string | null;
|
|
}
|
|
|
|
const GITHUB_PROVIDER: PullRequestProviderMetadata = { id: "github", label: "GitHub" };
|
|
|
|
export interface PrPaneCheck {
|
|
provider: PullRequestProvider;
|
|
name: string;
|
|
workflow?: string;
|
|
status: CheckStatus;
|
|
duration?: string;
|
|
url: string;
|
|
github?: {
|
|
checkRunId?: number;
|
|
workflowRunId?: number;
|
|
};
|
|
}
|
|
|
|
export interface PrPaneActivity {
|
|
provider: PullRequestProvider;
|
|
id: string;
|
|
kind: ActivityKind;
|
|
author: string;
|
|
authorUrl?: string | null;
|
|
avatarColor: string;
|
|
avatarUrl?: string | null;
|
|
reviewState?: ReviewState;
|
|
body: string;
|
|
age: string;
|
|
url: string;
|
|
/** For inline review comments: the review this comment was submitted with. */
|
|
reviewId?: string;
|
|
location?: {
|
|
path: string;
|
|
line?: number;
|
|
startLine?: number;
|
|
threadId?: string;
|
|
isResolved?: boolean;
|
|
isOutdated?: boolean;
|
|
};
|
|
}
|
|
|
|
export interface PrPaneData {
|
|
provider: PullRequestProviderMetadata;
|
|
number: number;
|
|
repoOwner?: string;
|
|
repoName?: string;
|
|
title: string;
|
|
state: PrState;
|
|
url: string;
|
|
reviewDecision: "approved" | "changes_requested" | "pending";
|
|
awaitingReviewers: string[];
|
|
checks: PrPaneCheck[];
|
|
activity: PrPaneActivity[];
|
|
}
|
|
|
|
type CheckoutPrStatus = CheckoutPrStatusResponse["payload"]["status"];
|
|
type PullRequestTimeline = PullRequestTimelineResponse["payload"];
|
|
type PullRequestTimelineItem = PullRequestTimeline["items"][number];
|
|
|
|
const AVATAR_COLORS = [
|
|
"#8b5cf6",
|
|
"#f97316",
|
|
"#0ea5e9",
|
|
"#10b981",
|
|
"#ef4444",
|
|
"#eab308",
|
|
"#ec4899",
|
|
"#6366f1",
|
|
];
|
|
|
|
export function mapPrPaneData(
|
|
status: CheckoutPrStatus,
|
|
timeline: PullRequestTimeline | null | undefined,
|
|
nowMs = Date.now(),
|
|
): PrPaneData | null {
|
|
if (!status) {
|
|
return null;
|
|
}
|
|
|
|
const number = status.number ?? parsePullRequestNumber(status.url);
|
|
if (number === null) {
|
|
return null;
|
|
}
|
|
|
|
const timelineMatchesStatus = timeline?.prNumber === number;
|
|
|
|
return {
|
|
provider: GITHUB_PROVIDER,
|
|
number,
|
|
repoOwner: status.repoOwner,
|
|
repoName: status.repoName,
|
|
title: status.title,
|
|
state: derivePrState(status),
|
|
url: status.url,
|
|
reviewDecision: mapReviewDecision(status.reviewDecision),
|
|
// Requested reviewers are intentionally unwired until the server exposes them.
|
|
awaitingReviewers: [],
|
|
checks: (status.checks ?? []).flatMap(mapCheck),
|
|
activity: timelineMatchesStatus
|
|
? timeline.items.flatMap((item) => mapActivity(item, nowMs))
|
|
: [],
|
|
};
|
|
}
|
|
|
|
export function deriveAvatarColor(login: string): string {
|
|
return AVATAR_COLORS[hashLogin(login) % AVATAR_COLORS.length];
|
|
}
|
|
|
|
export function formatAge(createdAtMs: number, nowMs = Date.now()): string {
|
|
const elapsedMs = Math.max(0, nowMs - createdAtMs);
|
|
const elapsedSeconds = Math.floor(elapsedMs / 1000);
|
|
|
|
if (elapsedSeconds < 60) {
|
|
return "just now";
|
|
}
|
|
|
|
const elapsedMinutes = Math.floor(elapsedSeconds / 60);
|
|
if (elapsedMinutes < 60) {
|
|
return `${elapsedMinutes}m ago`;
|
|
}
|
|
|
|
const elapsedHours = Math.floor(elapsedMinutes / 60);
|
|
if (elapsedHours < 24) {
|
|
return `${elapsedHours}h ago`;
|
|
}
|
|
|
|
const elapsedDays = Math.floor(elapsedHours / 24);
|
|
if (elapsedDays < 30) {
|
|
return `${elapsedDays}d ago`;
|
|
}
|
|
|
|
if (elapsedDays < 365) {
|
|
return `${Math.floor(elapsedDays / 30)}mo ago`;
|
|
}
|
|
|
|
return `${Math.floor(elapsedDays / 365)}y ago`;
|
|
}
|
|
|
|
function derivePrState(status: NonNullable<CheckoutPrStatus>): PrState {
|
|
if (status.isMerged || status.state === "merged") {
|
|
return "merged";
|
|
}
|
|
if (status.state !== "open") {
|
|
return "closed";
|
|
}
|
|
if (status.isDraft) {
|
|
return "draft";
|
|
}
|
|
return "open";
|
|
}
|
|
|
|
function mapCheck(check: NonNullable<CheckoutPrStatus>["checks"][number]): PrPaneCheck[] {
|
|
if (check.url === null) {
|
|
return [];
|
|
}
|
|
|
|
return [
|
|
{
|
|
provider: "github",
|
|
name: check.name,
|
|
status: mapCheckStatus(check.status),
|
|
url: check.url,
|
|
...(check.workflow ? { workflow: check.workflow } : {}),
|
|
...(check.duration ? { duration: check.duration } : {}),
|
|
...(check.checkRunId !== undefined || check.workflowRunId !== undefined
|
|
? {
|
|
github: {
|
|
...(check.checkRunId !== undefined ? { checkRunId: check.checkRunId } : {}),
|
|
...(check.workflowRunId !== undefined ? { workflowRunId: check.workflowRunId } : {}),
|
|
},
|
|
}
|
|
: {}),
|
|
},
|
|
];
|
|
}
|
|
|
|
function mapCheckStatus(status: string): CheckStatus {
|
|
if (
|
|
status === "success" ||
|
|
status === "failure" ||
|
|
status === "pending" ||
|
|
status === "skipped"
|
|
) {
|
|
return status;
|
|
}
|
|
if (status === "cancelled") {
|
|
return "skipped";
|
|
}
|
|
return "pending";
|
|
}
|
|
|
|
function mapActivity(item: PullRequestTimelineItem, nowMs: number): PrPaneActivity[] {
|
|
if (item.kind === "comment") {
|
|
if (item.body.trim() === "") {
|
|
return [];
|
|
}
|
|
return [
|
|
{
|
|
id: item.id,
|
|
provider: "github",
|
|
kind: "comment",
|
|
author: item.author,
|
|
authorUrl: item.authorUrl,
|
|
avatarColor: deriveAvatarColor(item.author),
|
|
avatarUrl: item.avatarUrl,
|
|
body: item.body,
|
|
age: formatAge(item.createdAt, nowMs),
|
|
url: item.url,
|
|
reviewId: item.reviewId,
|
|
location: item.location,
|
|
},
|
|
];
|
|
}
|
|
|
|
if (item.reviewState === "commented" && item.body.trim() === "") {
|
|
return [];
|
|
}
|
|
|
|
return [
|
|
{
|
|
id: item.id,
|
|
provider: "github",
|
|
kind: "review",
|
|
author: item.author,
|
|
authorUrl: item.authorUrl,
|
|
avatarColor: deriveAvatarColor(item.author),
|
|
avatarUrl: item.avatarUrl,
|
|
reviewState: item.reviewState,
|
|
body: item.body,
|
|
age: formatAge(item.createdAt, nowMs),
|
|
url: item.url,
|
|
},
|
|
];
|
|
}
|
|
|
|
function mapReviewDecision(
|
|
reviewDecision: NonNullable<CheckoutPrStatus>["reviewDecision"],
|
|
): PrPaneData["reviewDecision"] {
|
|
if (reviewDecision === "approved" || reviewDecision === "changes_requested") {
|
|
return reviewDecision;
|
|
}
|
|
return "pending";
|
|
}
|
|
|
|
function parsePullRequestNumber(url: string): number | null {
|
|
try {
|
|
const match = new URL(url).pathname.match(/\/pull\/(\d+)(?:\/|$)/);
|
|
if (!match) {
|
|
return null;
|
|
}
|
|
|
|
const number = Number.parseInt(match[1], 10);
|
|
return Number.isFinite(number) ? number : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function hashLogin(login: string): number {
|
|
let hash = 0;
|
|
for (const character of login.toLowerCase()) {
|
|
hash = (hash * 31 + character.charCodeAt(0)) >>> 0;
|
|
}
|
|
return hash;
|
|
}
|
|
|
|
export function getStateLabel(state: PrState): string {
|
|
if (state === "draft") return "Draft";
|
|
if (state === "merged") return "Merged";
|
|
if (state === "closed") return "Closed";
|
|
return "Open";
|
|
}
|
|
|
|
export function getActivityVerb(item: Pick<PrPaneActivity, "kind" | "reviewState">): string {
|
|
if (item.kind === "comment") return "Commented";
|
|
if (item.reviewState === "approved") return "Approved";
|
|
if (item.reviewState === "changes_requested") return "Requested changes";
|
|
return "Reviewed";
|
|
}
|