peppa/packages/core/src/preview.ts
2026-11-16 09:52:35 +05:00

62 lines
2.7 KiB
TypeScript

// What the PM needs to review a story without a terminal (PEP-44): the preview stand of its
// PR (deployed by the project's CD, PEP-57), the diff, the checks and the screenshots the CI
// step took of the stand. Also the Gitea issue the story came from (PEP-46).
import { m as tr } from "@peppa/shared/i18n";
import { releases, type Db } from "@peppa/db";
import { loadEnv } from "@peppa/shared";
import { and, desc, eq } from "drizzle-orm";
import { taskCiSummary, taskPrNumber } from "./ci.ts";
import type { TaskRef } from "./tasks.ts";
export interface ReviewPreview {
/** https URL of the healthy preview stand of the story's PR. */
previewUrl: string | null;
/** The release behind it (screenshots are served by its id). */
releaseId: number | null;
/** Commit deployed to the stand. */
sha: string | null;
diffUrl: string | null;
/** "Проверки: 4 из 4 пройдены". */
checks: string;
screenshots: Array<{ n: number; path: string }>;
issueUrl: string | null;
}
export async function reviewPreview(db: Db, ref: TaskRef): Promise<ReviewPreview> {
const pr = taskPrNumber(ref.project, ref.task.prUrl);
const [rel] = pr
? await db
.select()
.from(releases)
.where(and(eq(releases.projectId, ref.project.id), eq(releases.env, "preview"), eq(releases.prNumber, pr), eq(releases.status, "healthy")))
.orderBy(desc(releases.id))
.limit(1)
: [];
const issueUrl = ref.task.giteaIssue
? `${loadEnv().GITEA_URL.replace(/\/$/, "")}/${ref.project.giteaOwner}/${ref.project.giteaRepo}/issues/${ref.task.giteaIssue}`
: null;
return {
previewUrl: rel?.host ? `https://${rel.host}` : null,
releaseId: rel?.id ?? null,
sha: rel?.sha ?? null,
diffUrl: ref.task.prUrl ? `${ref.task.prUrl.replace(/\/$/, "")}/files` : null,
checks: (await taskCiSummary(db, ref)).label,
screenshots: (rel?.screenshots ?? []).map((s, n) => ({ n, path: s.path })),
issueUrl,
};
}
/** Lines for the "ready for review" message: look, diff, checks (empty parts left out). */
export function reviewPreviewNote(p: ReviewPreview): string {
const lines: string[] = [];
if (p.previewUrl) lines.push(
p.screenshots.length
? tr("👀 Посмотреть: {url} (скриншотов: {n} — в карточке)", { url: p.previewUrl, n: p.screenshots.length })
: tr("👀 Посмотреть: {url}", { url: p.previewUrl }),
);
if (p.diffUrl) lines.push(tr("📄 Изменения: {url}", { url: p.diffUrl }));
// The emoji is part of the key: a bare "🧪 …" line would be caught by a loose template first.
if (p.checks) lines.push(tr("🧪 {checks}", { checks: `${p.checks.charAt(0).toUpperCase()}${p.checks.slice(1)}` }));
return lines.join("\n");
}