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
91 changes: 91 additions & 0 deletions dashboard/e2e/preview-access.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { expect, test, type Page, type Route } from "@playwright/test";

// A repository's preview access (its address template and the secret a
// protected preview takes), from Settings > Repositories.
// Every API call is answered here; nothing reaches a real backend.

interface Backend {
role: string;
saved: Record<string, unknown> | null;
calls: { method: string; path: string; body: Record<string, unknown> | null }[];
}

async function mockApi(page: Page, init: Partial<Backend> = {}): Promise<Backend> {
const db: Backend = { role: "admin", saved: null, calls: [], ...init };
const json = (route: Route, body: unknown, status = 200) =>
route.fulfill({ status, contentType: "application/json", body: JSON.stringify(body) });
await page.route((url) => url.hostname !== "localhost", (route) => route.abort());
await page.route(
(url) => url.hostname === "localhost" && /^\/(api|auth)\//.test(url.pathname),
async (route) => {
const req = route.request();
const p = new URL(req.url()).pathname;
const method = req.method();
const body = req.postData() ? (JSON.parse(req.postData() as string) as Record<string, unknown>) : null;
db.calls.push({ method, path: p, body });
if (p === "/api/me")
return json(route, { user_id: "u_1", team_id: "t_1", github_login: null, email: "person@example.com", avatar_url: "", role: db.role, auth_provider: "email", mfa_enabled: false });
if (p === "/api/banners") return json(route, { banners: [] });
if (p === "/api/teams") return json(route, { teams: [{ team_id: "t_1", org: "your-team", status: "active", current: true }] });
if (p === "/api/repos")
return json(route, { repos: [{ name: "your-team/web", enabled: true, ticket_source: "github", onboard_status: "ready" }, { name: "your-team/api", enabled: false, ticket_source: "github" }] });
if (p === "/api/github/installation-status") return json(route, { status: "connected", github_org: "your-team", installation_id: 7 });
if (p === "/api/repos/your-team/web/preview-access") {
if (method === "GET") return json(route, db.saved ?? { configured: false, repo: "your-team/web" });
if (method === "PUT") {
db.saved = { configured: true, repo: "your-team/web", kind: body?.kind, address: body?.address, secret_set: body?.kind !== "none" };
return json(route, db.saved);
}
if (method === "DELETE") {
db.saved = null;
return json(route, { configured: false, repo: "your-team/web" });
}
}
return json(route, { error: `not mocked: ${method} ${p}` }, 404);
},
);
return db;
}

test("an admin saves a preview address and a header secret that is never shown again", async ({ page }) => {
const db = await mockApi(page);
await page.goto("/settings/repos/");
await page.getByRole("button", { name: "Preview access for your-team/web" }).click();
const dialog = page.getByTestId("preview-access-dialog");
await expect(dialog).toBeVisible();
// A fixed address is a shared environment, never a pull request's own.
await dialog.getByLabel(/Preview address/).fill("https://staging.your-domain.example");
await expect(dialog.getByText(/needs \{pr\}/)).toBeVisible();
await expect(dialog.getByRole("button", { name: "Save" })).toBeDisabled();
await dialog.getByLabel(/Preview address/).fill("https://pr-{pr}.preview.your-domain.example");
await dialog.getByLabel("How the preview is protected").click();
await page.getByRole("option", { name: /A header/ }).click();
await dialog.getByLabel("Header name", { exact: true }).fill("X-Preview-Bypass");
await dialog.getByLabel("Value").first().fill("s3cr3t-value");
await dialog.getByRole("button", { name: "Save" }).click();
await expect(dialog).toBeHidden();
const put = db.calls.find((c) => c.method === "PUT");
expect(put?.body).toMatchObject({ kind: "header", address: "https://pr-{pr}.preview.your-domain.example", secret: "X-Preview-Bypass: s3cr3t-value" });
await page.getByRole("button", { name: "Preview access for your-team/web" }).click();
await expect(dialog.getByLabel("Value").first()).toHaveValue("");
await expect(dialog.getByRole("button", { name: "Remove" })).toBeVisible();
});

test("a member sees no preview access button", async ({ page }) => {
await mockApi(page, { role: "member" });
await page.goto("/settings/repos/");
await expect(page.getByRole("list", { name: "Repositories" }).getByRole("listitem")).toHaveCount(2);
await expect(page.getByRole("button", { name: /Preview access for/ })).toHaveCount(0);
});

for (const width of [375, 1440]) {
test(`the preview access dialog has no horizontal scroll at ${width}px`, async ({ page }) => {
await page.setViewportSize({ width, height: 900 });
await mockApi(page);
await page.goto("/settings/repos/");
await page.getByRole("button", { name: "Preview access for your-team/web" }).click();
await expect(page.getByTestId("preview-access-dialog")).toBeVisible();
const overflow = await page.evaluate(() => document.documentElement.scrollWidth - window.innerWidth);
expect(overflow).toBeLessThanOrEqual(0);
});
}
21 changes: 21 additions & 0 deletions dashboard/src/app/settings/repos/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { api, type Repo } from "@/lib/api";
import { githubAppUrl } from "@/lib/runtime-config";
import { RoleGuard } from "@/components/role-guard";
import { StagingCmsDialog } from "@/components/staging-cms-dialog";
import { PreviewAccessDialog } from "@/components/preview-access-dialog";
import { useToast } from "@/components/toast";
import { Page, PageHeader } from "@/components/page";
import { buttonClass, secondaryButton } from "@/components/ui";
Expand Down Expand Up @@ -78,6 +79,8 @@ function ReposPage() {
const [savingRole, setSavingRole] = useState<string | null>(null);
// The repository whose staging CMS connection is open, if any.
const [stagingRepo, setStagingRepo] = useState<string | null>(null);
// The repository whose preview access is open, if any.
const [previewRepo, setPreviewRepo] = useState<string | null>(null);
// Turning repos on or off, syncing and routing roles need `repos`.
const { ready, can } = useCan();
const canManage = can("repos");
Expand Down Expand Up @@ -312,6 +315,17 @@ function ReposPage() {
Staging content
</button>
)}
{repo.enabled && canManage && (
<button
type="button"
onClick={() => setPreviewRepo(repo.name)}
className={buttonClass("secondary", "sm")}
aria-label={`Preview access for ${repo.name}`}
title="The preview's address when nothing publishes it, and the secret a protected preview takes"
>
Preview access
</button>
)}
</>
}
/>
Expand All @@ -321,6 +335,13 @@ function ReposPage() {
)}
<LoadMore hasMore={!loading && filtered.length > shownCount} loading={false} onClick={() => setShownCount((n) => n + PAGE_SIZE)} />
</div>
{previewRepo && (
<PreviewAccessDialog
repo={previewRepo}
open={!!previewRepo}
onOpenChange={(v) => !v && setPreviewRepo(null)}
/>
)}
{stagingRepo && (
<StagingCmsDialog
repo={stagingRepo}
Expand Down
234 changes: 234 additions & 0 deletions dashboard/src/components/preview-access-dialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
"use client";

import { useEffect, useId, useState } from "react";
import { api, type PreviewAccess, type PreviewAccessKind } from "@/lib/api";
import { SecretInput } from "./field";
import { Modal, ModalFooter } from "./modal";
import { Select } from "./select";
import { inputClass, primaryButton, secondaryButton } from "./ui";

// A repository's preview access. Most previews need nothing here: Coderhelm
// finds the pull request's preview in its deployments, deploy statuses,
// preview checks and bot comments. This is for the two things only the team
// can give: the preview's address when nothing publishes it, and the secret
// a protected preview takes. The secret is never shown again and is sent
// only to the preview's own host.

const PLACEHOLDERS = ["{pr}", "{branch}", "{branch_slug}", "{sha}", "{short_sha}", "{owner}", "{repo}"];

/** The reason an address template is refused, or "" when it is fine. */
export function templateProblem(raw: string): string {
const t = raw.trim();
if (!t) return "";
if (!t.startsWith("https://")) return "The address starts with https://.";
const unknown = (t.match(/\{[^}]*\}/g) ?? []).find((b) => !PLACEHOLDERS.includes(b));
if (unknown) return `${unknown} isn't filled in; use ${PLACEHOLDERS.join(", ")}.`;
if (!["{pr}", "{branch}", "{branch_slug}", "{sha}", "{short_sha}"].some((p) => t.includes(p)))
return "The address needs {pr}, {branch}, {branch_slug}, {sha} or {short_sha}, so each pull request gets its own preview.";
return "";
}

const KINDS: { value: PreviewAccessKind; label: string }[] = [
{ value: "none", label: "Not protected" },
{ value: "header", label: "A header (a bypass token or a service token pair)" },
{ value: "query", label: "A query parameter" },
{ value: "basic", label: "A user name and password" },
];

export function PreviewAccessDialog({
repo,
open,
onOpenChange,
}: {
repo: string;
open: boolean;
onOpenChange: (open: boolean) => void;
}) {
const [saved, setSaved] = useState<PreviewAccess | null>(null);
const [address, setAddress] = useState("");
const [kind, setKind] = useState<PreviewAccessKind>("none");
// Header kind: up to two `name: value` pairs (a token, or an id and a secret).
const [names, setNames] = useState(["", ""]);
const [values, setValues] = useState(["", ""]);
// Query kind: `name=value`; password kind: user and password.
const [single, setSingle] = useState("");
const [user, setUser] = useState("");
const [busy, setBusy] = useState(false);
const [error, setError] = useState("");
const ids = { address: useId(), kind: useId(), single: useId(), user: useId(), h0: useId(), h1: useId(), v0: useId(), v1: useId() };

// Each opening reads what is saved; the secret is never sent back.
useEffect(() => {
if (!open) return;
setError("");
setSaved(null);
setNames(["", ""]);
setValues(["", ""]);
setSingle("");
setUser("");
api
.getPreviewAccess(repo)
.then((a) => {
setSaved(a);
setAddress(a.address ?? "");
setKind(a.kind ?? "none");
})
.catch(() => setError("Couldn't load the preview access. Try again."));
}, [open, repo]);

const problem = templateProblem(address);
const keepsSecret = !!saved?.secret_set && saved?.kind === kind;

const secret = (): string => {
if (kind === "header")
return names
.map((n, i) => (n.trim() && values[i].trim() ? `${n.trim()}: ${values[i].trim()}` : ""))
.filter(Boolean)
.join("\n");
if (kind === "basic") return user.trim() && single.trim() ? `${user.trim()}:${single.trim()}` : "";
if (kind === "query") return single.trim();
return "";
};

const save = async () => {
setBusy(true);
setError("");
try {
const s = secret();
const a = await api.savePreviewAccess(repo, { kind, address: address.trim(), secret: s || undefined });
setSaved(a);
onOpenChange(false);
} catch (e) {
setError(e instanceof Error && e.message ? e.message : "Couldn't save. Try again.");
}
setBusy(false);
};

const remove = async () => {
setBusy(true);
setError("");
try {
await api.deletePreviewAccess(repo);
onOpenChange(false);
} catch {
setError("Couldn't remove the preview access. Try again.");
}
setBusy(false);
};

const secretHint = keepsSecret ? "Saved. Enter a new one to replace it." : "";

return (
<Modal
open={open}
onOpenChange={(v) => !busy && onOpenChange(v)}
testId="preview-access-dialog"
title={`Preview access for ${repo}`}
description="Coderhelm finds each pull request's preview on its own, in the commit's deployments, deploy statuses, preview checks and bot comments, and checks the changed pages there. Set this only when nothing publishes the preview's address, or when the preview asks for a sign-in."
>
<form
id="preview-access-form"
className="space-y-4"
onSubmit={(e) => {
e.preventDefault();
if (!problem) void save();
}}
>
<div>
<label htmlFor={ids.address} className="mb-1 block text-sm font-medium text-zinc-200">
Preview address <span className="font-normal text-zinc-500">(optional)</span>
</label>
<input
id={ids.address}
type="text"
value={address}
onChange={(e) => setAddress(e.target.value)}
placeholder="https://pr-{pr}.preview.your-domain.example"
className={inputClass}
autoComplete="off"
aria-invalid={!!problem || undefined}
aria-describedby={`${ids.address}-help`}
/>
<p id={`${ids.address}-help`} className={`mt-1 text-xs ${problem ? "text-red-400" : "text-zinc-400"}`}>
{problem ||
"Used only when nothing on the commit publishes the address. {pr} is the pull request number, {branch_slug} the branch as hosts put it in a host name, {sha} and {short_sha} the commit."}
</p>
</div>
<div>
<label htmlFor={ids.kind} className="mb-1 block text-sm font-medium text-zinc-200">
How the preview is protected
</label>
<Select id={ids.kind} value={kind} onChange={(v) => setKind(v)} options={KINDS} />
</div>
{kind === "header" &&
[0, 1].map((i) => (
<div key={i} className="grid grid-cols-2 gap-2">
<div>
<label htmlFor={i ? ids.h1 : ids.h0} className="mb-1 block text-sm font-medium text-zinc-200">
Header name{i ? <span className="font-normal text-zinc-500"> (optional second)</span> : null}
</label>
<input
id={i ? ids.h1 : ids.h0}
type="text"
value={names[i]}
onChange={(e) => setNames((n) => n.map((x, j) => (j === i ? e.target.value : x)))}
className={inputClass}
autoComplete="off"
/>
</div>
<div>
<label htmlFor={i ? ids.v1 : ids.v0} className="mb-1 block text-sm font-medium text-zinc-200">
Value
</label>
<SecretInput
id={i ? ids.v1 : ids.v0}
value={values[i]}
onChange={(e) => setValues((v) => v.map((x, j) => (j === i ? e.target.value : x)))}
placeholder={i === 0 ? secretHint : ""}
/>
</div>
</div>
))}
{kind === "basic" && (
<div>
<label htmlFor={ids.user} className="mb-1 block text-sm font-medium text-zinc-200">
User name
</label>
<input id={ids.user} type="text" value={user} onChange={(e) => setUser(e.target.value)} className={inputClass} autoComplete="off" />
</div>
)}
{(kind === "basic" || kind === "query") && (
<div>
<label htmlFor={ids.single} className="mb-1 block text-sm font-medium text-zinc-200">
{kind === "basic" ? "Password" : "Parameters (name=value, join several with &)"}
</label>
<SecretInput id={ids.single} value={single} onChange={(e) => setSingle(e.target.value)} placeholder={secretHint} />
</div>
)}
{kind !== "none" && (
<p className="text-xs text-zinc-400">
Kept sealed, never shown again, and sent only to the preview&apos;s own host when Coderhelm opens its pages.
</p>
)}
{error && (
<p role="alert" className="text-sm text-red-400">
{error}
</p>
)}
<ModalFooter>
{saved?.configured && (
<button type="button" onClick={remove} disabled={busy} className={secondaryButton}>
Remove
</button>
)}
<button type="button" onClick={() => onOpenChange(false)} disabled={busy} className={secondaryButton}>
Cancel
</button>
<button type="submit" form="preview-access-form" disabled={busy || !!problem} className={primaryButton}>
{busy ? "Saving…" : "Save"}
</button>
</ModalFooter>
</form>
</Modal>
);
}
Loading
Loading