Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

ci: PR report links the branch preview at once and measures the base from main's build (#8892)

Cindy Zhang25f61ea59008
diff --git a/.github/scripts/ci-tooling-routing.test.mjs b/.github/scripts/ci-tooling-routing.test.mjs
index a2cad768b6..95673a78e0 100644
--- a/.github/scripts/ci-tooling-routing.test.mjs
+++ b/.github/scripts/ci-tooling-routing.test.mjs
@@ -235,6 +235,43 @@ describe('Node-tooling CI routing', () => {
);
});
+ it("measures the merge base from main's kept build, building it only without one", () => {
+ const main = load('.github/workflows/deploy.yml');
+ const keep = step(
+ main.jobs.typecheck,
+ "Keep this commit's package builds for PR size reports",
+ );
+ expect(keep.with.name).toBe(
+ 'dists-main-${{ needs.main-commit.outputs.sha }}',
+ );
+ const job = ci.jobs['size-base'];
+ expect(job.permissions.actions).toBe('read');
+ expect(
+ step(job, "Find main's build of the merge base")['continue-on-error'],
+ ).toBe(true);
+ const download = step(job, "Download main's build of the merge base");
+ expect(download['continue-on-error']).toBe(true);
+ expect(download.with.name).toBe(
+ 'dists-main-${{ steps.merge-base.outputs.sha }}',
+ );
+ const build = step(job, "Build the merge base's component packages");
+ expect(build.if).toContain("steps.download.outcome != 'success'");
+ expect(build.run).not.toContain('pnpm install');
+ // Measuring needs the base's dependencies on both paths.
+ const install = step(job, "Install the merge base's dependencies");
+ expect(install.run).toBe('pnpm install --frozen-lockfile');
+ expect(install.if).not.toContain('steps.download');
+ // Only Main's own run on main for the merge-base commit counts.
+ const find = step(job, "Find main's build of the merge base").with.script;
+ expect(find).toContain("candidate.workflow_run?.head_branch === 'main'");
+ expect(find).toContain(
+ 'candidate.workflow_run?.head_sha === process.env.BASE_SHA',
+ );
+ expect(find).toContain(
+ 'candidate.workflow_run?.head_repository_id === candidate.workflow_run?.repository_id',
+ );
+ });
+
it('builds no Sandbox and builds Storybook once per pull-request run', () => {
// Vercel builds the Sandbox preview; nothing downloaded CI's copy.
expect(ci.jobs['build-sandbox']).toBeUndefined();
diff --git a/.github/scripts/generate-pr-comment.js b/.github/scripts/generate-pr-comment.js
index c90dc19a78..addace1fea 100644
--- a/.github/scripts/generate-pr-comment.js
+++ b/.github/scripts/generate-pr-comment.js
@@ -321,12 +321,17 @@ if (sandboxUrl) {
`;
}
-// Both previews share the exact-head Vercel deployment, never a Pages, canary,
-// production, or earlier-head fallback.
+// Both previews come from the branch's Vercel alias, which serves the
+// branch's latest deployment; until a push's deployment is ready it still
+// shows the previous head. Vercel shortens a long alias with a hash, and then there are
+// no links.
let previewAvailabilitySection = '';
if (previewState) {
if (!storybookUrl || !sandboxUrl)
- previewAvailabilitySection = `> **Preview availability:** Storybook and Sandbox are not both ready on the exact-head Vercel preview.${sourceConclusion && sourceConclusion !== 'success' ? ` CI concluded ${sourceConclusion}.` : ''}\n\n`;
+ previewAvailabilitySection = `> **Preview availability:** this branch's preview address cannot be known in advance; open the preview from Vercel's comment on this pull request.${sourceConclusion && sourceConclusion !== 'success' ? ` CI concluded ${sourceConclusion}.` : ''}\n\n`;
+ else
+ previewAvailabilitySection =
+ "> **Preview:** the links below open this branch's latest Vercel deployment, which can trail a new push until that push's deployment is ready.\n\n";
}
// Build footer with links
diff --git a/.github/scripts/lib/pr-preview.mjs b/.github/scripts/lib/pr-preview.mjs
index 90170834ae..fafbfb4a8c 100644
--- a/.github/scripts/lib/pr-preview.mjs
+++ b/.github/scripts/lib/pr-preview.mjs
@@ -6,12 +6,6 @@ import path from 'node:path';
import process from 'node:process';
import {fileURLToPath} from 'node:url';
-import {
- probeVercelPreview,
- resolveVercelPreview,
- waitForPreviewRoutes,
-} from './vercel-preview.mjs';
-
export const PR_ANALYSIS_MARKER = '<!-- astryx-pr-analysis -->';
function headMarker(sha) {
@@ -26,6 +20,31 @@ const GENERATOR = path.resolve(
'generate-pr-comment.js',
);
+// Vercel keeps a branch alias, `<project>-git-<branch>-<team>.vercel.app`
+// (`git-fork-<owner>-<branch>` for a fork), that serves the branch's latest
+// deployment. It is known before any deployment finishes, so the report links
+// it at once. A label past DNS's 63 characters is shortened by Vercel with a
+// hash no one can compute, and then the report links nothing.
+const VERCEL_PROJECT = 'astryx';
+const VERCEL_TEAM = 'fbopensource';
+
+function vercelSlug(value) {
+ return value
+ .toLowerCase()
+ .replace(/[^a-z0-9]+/g, '-')
+ .replace(/^-+|-+$/g, '');
+}
+
+export function branchPreviewOrigin({headRef, headRepository, baseRepository}) {
+ const owner = headRepository.split('/')[0];
+ const branch =
+ headRepository === baseRepository
+ ? vercelSlug(headRef)
+ : `fork-${vercelSlug(owner)}-${vercelSlug(headRef)}`;
+ const label = `${VERCEL_PROJECT}-git-${branch}-${VERCEL_TEAM}`;
+ return label.length <= 63 ? `https://${label}.vercel.app` : null;
+}
+
function refuse(message) {
throw new Error(`PR preview refused: ${message}`);
}
@@ -403,13 +422,20 @@ function previewState(storybook, sandbox) {
return 'none';
}
-// Both preview links come from one verified exact-head Vercel deployment.
-// No branch-backed preview is considered a link source.
+// The same preview wording as the generator's report: the branch alias
+// serves the branch's latest deployment, which can trail a new push. A draft
+// or an update-only reconcile (no alias attempted) says nothing about previews.
+const PREVIEW_NOTE =
+ "> **Preview:** the links below open this branch's latest Vercel deployment, which can trail a new push until that push's deployment is ready.";
+const PREVIEW_POINTER =
+ "> **Preview availability:** this branch's preview address cannot be known in advance; open the preview from Vercel's comment on this pull request.";
+
function safeCurrentBody({
identity,
runUrl,
message: overrideMessage,
previewOrigin,
+ aliasAttempted,
}) {
const conclusion = identity.sourceConclusion;
const message =
@@ -423,9 +449,10 @@ function safeCurrentBody({
`[View Sandbox for this PR](${previewOrigin}/sandbox/)`,
]
: [];
- const previewMessage = links.length
- ? `\n\n${links.join(' · ')}`
- : '\n\nThe exact-head preview is not available yet.';
+ let previewMessage = '';
+ if (links.length)
+ previewMessage = `\n\n${PREVIEW_NOTE}\n\n${links.join(' · ')}`;
+ else if (aliasAttempted) previewMessage = `\n\n${PREVIEW_POINTER}`;
return `## PR Analysis Report\n${PR_ANALYSIS_MARKER}\n${headMarker(identity.headSha)}\n\n> **Current run:** ${message}${previewMessage}\n\n---\n\n<sub>Generated by PR Enrichment workflow | <a href="${runUrl}" target="_blank" rel="noopener noreferrer">View current CI run</a></sub>\n`;
}
@@ -487,9 +514,6 @@ export async function reconcilePrComment({
visualPath = 'trusted-visual/verdict.json',
createIfMissing = true,
fallbackMessage,
- lookupPreview = resolveVercelPreview,
- probePreview = probeVercelPreview,
- probeWaitMs = 90_000,
generator = GENERATOR,
execute = execFileSync,
}) {
@@ -514,34 +538,10 @@ export async function reconcilePrComment({
identity,
core,
});
- const candidateOrigin =
- createIfMissing && !identity.draft
- ? await lookupPreview({
- github,
- owner,
- repo,
- prNumber: identity.prNumber,
- headSha: identity.headSha,
- })
- : null;
- const routesReady =
- candidateOrigin &&
- (await waitForPreviewRoutes(candidateOrigin, {
- probe: probePreview,
- waitMs: probeWaitMs,
- }));
- const previewOrigin =
- routesReady &&
- (await lookupPreview({
- github,
- owner,
- repo,
- prNumber: identity.prNumber,
- headSha: identity.headSha,
- waitMs: 0,
- })) === candidateOrigin
- ? candidateOrigin
- : null;
+ // A draft, or a reconcile that only updates an existing report, links no
+ // preview and says nothing about one.
+ const aliasAttempted = createIfMissing && !identity.draft;
+ const previewOrigin = aliasAttempted ? branchPreviewOrigin(identity) : null;
const storybook = previewOrigin !== null;
const sandbox = storybook;
const comments = await allComments(github, owner, repo, identity.prNumber);
@@ -571,8 +571,9 @@ export async function reconcilePrComment({
...(fs.existsSync(visualPath) ? ['--visual', visualPath] : []),
...(storybook ? ['--storybook-url', `${previewOrigin}/storybook/`] : []),
...(sandbox ? ['--sandbox-url', `${previewOrigin}/sandbox/`] : []),
- '--preview-state',
- previewState(storybook, sandbox),
+ ...(aliasAttempted
+ ? ['--preview-state', previewState(storybook, sandbox)]
+ : []),
'--source-conclusion',
identity.sourceConclusion,
'--run-url',
@@ -592,6 +593,7 @@ export async function reconcilePrComment({
runUrl,
message: fallbackMessage,
previewOrigin,
+ aliasAttempted,
});
}
} else {
@@ -600,13 +602,13 @@ export async function reconcilePrComment({
runUrl,
message: fallbackMessage,
previewOrigin,
+ aliasAttempted,
});
}
- // Vercel readiness can take minutes after the source run finishes. Confirm
- // the reported attempt's identity again, and stand down if a newer attempt
- // of the run has started since: an earlier attempt's report must not replace
- // the latest attempt's.
+ // Rendering takes a moment. Confirm the reported attempt's identity again,
+ // and stand down if a newer attempt of the run has started since: an
+ // earlier attempt's report must not replace the latest attempt's.
if (
(await confirmOrStandDown({
github,
@@ -621,20 +623,6 @@ export async function reconcilePrComment({
if (await supersededByNewerAttempt({github, owner, repo, identity, core})) {
return {action: 'none', body: null, identity};
}
- if (
- previewOrigin &&
- (await lookupPreview({
- github,
- owner,
- repo,
- prNumber: identity.prNumber,
- headSha: identity.headSha,
- waitMs: 0,
- })) !== previewOrigin
- ) {
- core.info('A newer exact-head deployment superseded this preview.');
- return {action: 'none', body: null, identity};
- }
if (botComment) {
await github.rest.issues.updateComment({
owner,
@@ -655,135 +643,3 @@ export async function reconcilePrComment({
core.info(`Posted PR Analysis Report on #${identity.prNumber}.`);
return {action: 'created', body, identity};
}
-
-export async function reconcileEarlyPreviewComment({
- github,
- owner,
- repo,
- prNumber,
- headSha,
- origin,
- lookupPreview = resolveVercelPreview,
- probePreview = probeVercelPreview,
- probeWaitMs = 90_000,
-}) {
- const currentOrigin = await lookupPreview({
- github,
- owner,
- repo,
- prNumber,
- headSha,
- waitMs: 0,
- });
- if (
- currentOrigin !== origin ||
- !(await waitForPreviewRoutes(origin, {
- probe: probePreview,
- waitMs: probeWaitMs,
- }))
- )
- return {action: 'none'};
-
- const comments = await allComments(github, owner, repo, prNumber);
- const existing = comments.find(
- comment =>
- comment.user?.type === 'Bot' &&
- comment.body?.includes(PR_ANALYSIS_MARKER),
- );
- const links = `[View Storybook for this PR](${origin}/storybook/) · [View Sandbox for this PR](${origin}/sandbox/)`;
- const marker = headMarker(headSha);
- const legacySandboxURLs = new Set([
- `https://${owner}.github.io/${repo}/pr/${prNumber}/sandbox/`,
- `https://facebook.github.io/astryx/pr/${prNumber}/sandbox/`,
- ]);
- const replaceLegacySandboxURLs = value => {
- for (const url of legacySandboxURLs) {
- value = value.replaceAll(url, `${origin}/sandbox/`);
- }
- return value;
- };
- let body = `## PR Analysis Report\n${PR_ANALYSIS_MARKER}\n${marker}\n\n> **Previews ready:** Storybook and Sandbox were built with this PR's docsite. CI evidence will be added when available.\n\n${links}\n`;
- if (existing?.body.includes(marker)) {
- if (
- existing.body.includes(`${origin}/storybook/`) &&
- existing.body.includes(`${origin}/sandbox/`)
- ) {
- return {action: 'unchanged'};
- }
- const oldOrigins = new Set(
- existing.body.match(
- /https:\/\/astryx-[a-z0-9]{9}-fbopensource\.vercel\.app/g,
- ) ?? [],
- );
- if (
- oldOrigins.size === 1 &&
- existing.body.includes('View Storybook for this PR')
- ) {
- // Same-head redeploy: preserve existing CI/a11y/visual content and
- // change only the exact-head preview URLs.
- body = replaceLegacySandboxURLs(
- existing.body.replaceAll([...oldOrigins][0], origin),
- );
- if (!body.includes(`${origin}/sandbox/`)) {
- body = body.replace(
- marker,
- `${marker}\n\n[View Sandbox for this PR](${origin}/sandbox/)`,
- );
- }
- } else if (!existing.body.includes('View Storybook for this PR')) {
- // CI may finish before Vercel; add links to the current-head report.
- body = existing.body
- .replace(/^> \*\*Preview availability:\*\*[^\n]*\n\n/m, '')
- .replace('The exact-head preview is not available yet.', '')
- .replace(marker, `${marker}\n\n${links}`);
- }
- }
-
- // Replace older same-head preview URLs without dropping CI, a11y, or visual
- // evidence in the surrounding report.
- body = replaceLegacySandboxURLs(body).replace(
- /^> \*\*Preview availability:\*\*[^\n]*\n\n/m,
- '',
- );
-
- const {data: current} = await github.rest.pulls.get({
- owner,
- repo,
- pull_number: prNumber,
- });
- if (
- current.state !== 'open' ||
- current.draft ||
- current.head?.sha !== headSha
- ) {
- return {action: 'none'};
- }
- if (
- (await lookupPreview({
- github,
- owner,
- repo,
- prNumber,
- headSha,
- waitMs: 0,
- })) !== origin
- ) {
- return {action: 'none'};
- }
- if (existing) {
- await github.rest.issues.updateComment({
- owner,
- repo,
- comment_id: existing.id,
- body,
- });
- return {action: 'updated', body};
- }
- await github.rest.issues.createComment({
- owner,
- repo,
- issue_number: prNumber,
- body,
- });
- return {action: 'created', body};
-}
diff --git a/.github/scripts/lib/pr-preview.test.mjs b/.github/scripts/lib/pr-preview.test.mjs
index b9ffe20a7b..9e89018876 100644
--- a/.github/scripts/lib/pr-preview.test.mjs
+++ b/.github/scripts/lib/pr-preview.test.mjs
@@ -1,5 +1,6 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.
+import {execFileSync} from 'node:child_process';
import fs from 'node:fs';
import {createRequire} from 'node:module';
import os from 'node:os';
@@ -9,10 +10,10 @@ import {afterEach, describe, expect, it, vi} from 'vitest';
import yaml from 'yaml';
import {
+ branchPreviewOrigin,
confirmOrStandDown,
confirmSourceRunIdentity,
PR_ANALYSIS_MARKER,
- reconcileEarlyPreviewComment,
reconcilePrComment,
resolveWorkflowRunPullRequest,
validateAnalysisMetadata,
@@ -20,7 +21,10 @@ import {
const HEAD = 'a'.repeat(40);
const BASE = 'b'.repeat(40);
-const VERCEL_ORIGIN = 'https://astryx-3s4xgbci4-fbopensource.vercel.app';
+// The branch alias of identity() opened from the base repository; from the
+// fork in identity() the alias passes 63 characters and has no link.
+const VERCEL_ORIGIN =
+ 'https://astryx-git-fix-failed-ci-preview-links-fbopensource.vercel.app';
const ROOT = path.resolve(import.meta.dirname, '../../..');
const REQUIRE = createRequire(import.meta.url);
const ASYNC_FUNCTION = Object.getPrototypeOf(async function () {}).constructor;
@@ -114,21 +118,6 @@ function githubFixture({value = identity(), comments = []} = {}) {
get: vi.fn(async () => ({data: pull(value)})),
list: vi.fn(async () => ({data: [pull(value)]})),
},
- repos: {
- listDeployments: vi.fn(async () => ({
- data: [
- {
- id: 11,
- sha: value.headSha,
- environment: 'Preview',
- creator: {login: 'vercel[bot]'},
- },
- ],
- })),
- listDeploymentStatuses: vi.fn(async () => ({
- data: [{state: 'success', environment_url: VERCEL_ORIGIN}],
- })),
- },
issues: {
listComments: vi.fn(async ({issue_number}) => {
state.listedIssues.push(issue_number);
@@ -202,17 +191,21 @@ function fixture(value = identity(), {analysis = true, a11y = true} = {}) {
}
async function reconcile({
- value = identity(),
+ value: requested = identity(),
previewAvailable = false,
analysis = true,
a11y = true,
comments = [],
- githubFixtureValue = value,
+ githubFixtureValue: fixtureValue,
createIfMissing,
fallbackMessage,
execute,
baseStats,
} = {}) {
+ const value = previewAvailable
+ ? {...requested, headRepository: requested.baseRepository}
+ : requested;
+ const githubFixtureValue = fixtureValue ?? value;
const paths = fixture(value, {analysis, a11y});
const baseStatsPath = path.join(
path.dirname(paths.analysis),
@@ -236,8 +229,6 @@ async function reconcile({
metadataPath: paths.metadata,
baseStatsPath,
a11yPath: paths.a11y,
- lookupPreview: async () => (previewAvailable ? VERCEL_ORIGIN : null),
- probePreview: async () => true,
visualPath: paths.visual,
createIfMissing,
fallbackMessage,
@@ -485,7 +476,6 @@ describe('trusted PR preview identity', () => {
.map(([name]) => name);
expect(writeCapableJobs).toEqual([
- 'preview-comment',
'spec-only-reconcile',
'comment',
'report',
@@ -496,11 +486,9 @@ describe('trusted PR preview identity', () => {
statuses: 'write',
});
for (const name of writeCapableJobs.filter(job => job !== 'report')) {
- const expected =
- name === 'preview-comment'
- ? "needs.resolve-preview.outputs.valid == 'true'"
- : "needs.resolve.outputs.valid == 'true'";
- expect(PR_COMMENT_WORKFLOW.jobs[name].if, name).toContain(expected);
+ expect(PR_COMMENT_WORKFLOW.jobs[name].if, name).toContain(
+ "needs.resolve.outputs.valid == 'true'",
+ );
}
});
@@ -815,9 +803,9 @@ describe('analysis metadata compatibility', () => {
describe('PR comment preview reconciliation', () => {
it.each([
- ['not ready', false],
- ['exact-head Vercel ready', true],
- ])('shows both or neither exact-head links: %s', async (_label, ready) => {
+ ['alias past 63 characters', false],
+ ['alias known', true],
+ ])('shows both or neither branch links: %s', async (_label, ready) => {
const {result} = await reconcile({previewAvailable: ready});
expect(result.body.includes('View Storybook for this PR')).toBe(ready);
expect(result.body.includes('View Sandbox for this PR')).toBe(ready);
@@ -829,6 +817,53 @@ describe('PR comment preview reconciliation', () => {
expect(result.body).not.toContain('facebook.github.io/astryx/pr/');
});
+ describe('the fallback report, when there is no trusted analysis', () => {
+ const failRender = () => {
+ throw new Error('malformed analysis');
+ };
+
+ it('links the branch alias under the same preview note as the report', async () => {
+ const {result} = await reconcile({
+ previewAvailable: true,
+ createIfMissing: true,
+ execute: failRender,
+ });
+ expect(result.body).toContain('trusted analysis is unavailable');
+ expect(result.body).toContain('> **Preview:** the links below open');
+ expect(result.body).toContain('View Storybook for this PR');
+ });
+
+ it('points to Vercel when the alias cannot be computed', async () => {
+ const {result} = await reconcile({
+ value: identity({headRef: `feat/${'long-branch-name-'.repeat(4)}`}),
+ previewAvailable: true,
+ createIfMissing: true,
+ execute: failRender,
+ });
+ expect(result.body).toContain('> **Preview availability:**');
+ expect(result.body).not.toContain('View Storybook for this PR');
+ });
+
+ it('says nothing about previews on a draft', async () => {
+ const {result} = await reconcile({
+ value: identity({draft: true}),
+ previewAvailable: true,
+ createIfMissing: true,
+ execute: failRender,
+ });
+ expect(result.body).toContain('trusted analysis is unavailable');
+ expect(result.body).not.toContain('Preview');
+ expect(result.body).not.toContain("Vercel's comment");
+ expect(result.body).not.toContain('View Storybook for this PR');
+ });
+ });
+
+ it('says nothing about previews on a draft', async () => {
+ const {result} = await reconcile({value: identity({draft: true})});
+ expect(result.body).not.toContain('Preview availability');
+ expect(result.body).not.toContain('View Storybook for this PR');
+ });
+
it('says the accessibility audit did not run when there is no report', async () => {
const {result} = await reconcile({a11y: false});
expect(result.body).toContain('did not run on this pull request');
@@ -846,7 +881,7 @@ describe('PR comment preview reconciliation', () => {
expect(result.body).not.toContain('View Sandbox for this PR');
});
- it('shows the exact Vercel preview even when unrelated CI analysis fails', async () => {
+ it('shows the branch preview even when unrelated CI analysis fails', async () => {
const value = identity({sourceConclusion: 'failure'});
const {result} = await reconcile({
value,
@@ -978,7 +1013,6 @@ describe('PR comment preview reconciliation', () => {
metadataPath: firstPaths.metadata,
a11yPath: firstPaths.a11y,
visualPath: firstPaths.visual,
- probePreview: async () => true,
});
expect(result.action).toBe('none');
@@ -990,7 +1024,7 @@ describe('PR comment preview reconciliation', () => {
});
it('updates one same-PR comment across repeated source attempts', async () => {
- const first = identity();
+ const first = identity({headRepository: 'meta/astryx'});
const harness = githubFixture({value: first});
const firstPaths = fixture(first);
const core = {info: vi.fn(), warning: vi.fn()};
@@ -1002,7 +1036,6 @@ describe('PR comment preview reconciliation', () => {
serverUrl: 'https://github.com',
},
visualPath: firstPaths.visual,
- probePreview: async () => true,
};
await reconcilePrComment({
@@ -1013,7 +1046,10 @@ describe('PR comment preview reconciliation', () => {
a11yPath: firstPaths.a11y,
});
- const second = identity({sourceRunAttempt: 2});
+ const second = identity({
+ headRepository: 'meta/astryx',
+ sourceRunAttempt: 2,
+ });
harness.github.rest.actions.getWorkflowRun.mockResolvedValue({
data: sourceRun(second),
});
@@ -1066,7 +1102,7 @@ describe('PR comment preview reconciliation', () => {
expect(state.listedIssues).toHaveLength(0);
});
- it('stands down without writing when the head moves during Vercel readiness', async () => {
+ it('stands down without writing when the head moves while the report renders', async () => {
const expected = identity();
const current = identity({headSha: 'f'.repeat(40)});
const paths = fixture(expected);
@@ -1084,11 +1120,10 @@ describe('PR comment preview reconciliation', () => {
metadataPath: paths.metadata,
a11yPath: paths.a11y,
visualPath: paths.visual,
- lookupPreview: async () => {
+ execute: (command, args, options) => {
github.rest.pulls.get.mockResolvedValue({data: pull(current)});
- return VERCEL_ORIGIN;
+ return execFileSync(command, args, options);
},
- probePreview: async () => true,
});
expect(result.action).toBe('none');
expect(core.info).toHaveBeenCalledWith(
@@ -1125,7 +1160,7 @@ describe('PR comment preview reconciliation', () => {
expect(state.updated).toHaveLength(0);
});
- it('renders the current successful path from exact Vercel proof', async () => {
+ it('renders the current successful path with the branch links', async () => {
const value = identity();
const {result, state} = await reconcile({
value,
@@ -1140,241 +1175,35 @@ describe('PR comment preview reconciliation', () => {
expect(state.listedIssues).toEqual([5697]);
});
- it('never advertises either link when one preview route probe fails', async () => {
- const value = identity();
- const paths = fixture(value);
- const {github, state} = githubFixture({value});
- const context = {
- repo: {owner: 'meta', repo: 'astryx'},
- serverUrl: 'https://github.com',
- };
- const report = await reconcilePrComment({
- github,
- core: {info: vi.fn(), warning: vi.fn()},
- context,
- expectedIdentity: value,
- analysisPath: paths.analysis,
- metadataPath: paths.metadata,
- a11yPath: paths.a11y,
- visualPath: paths.visual,
- lookupPreview: async () => VERCEL_ORIGIN,
- probePreview: async () => false,
- probeWaitMs: 0,
- });
- expect(report.body).not.toContain('View Storybook for this PR');
- expect(report.body).not.toContain('View Sandbox for this PR');
- const early = await reconcileEarlyPreviewComment({
- github,
- owner: 'meta',
- repo: 'astryx',
- prNumber: value.prNumber,
- headSha: value.headSha,
- origin: VERCEL_ORIGIN,
- lookupPreview: async () => VERCEL_ORIGIN,
- probePreview: async () => false,
- probeWaitMs: 0,
- });
- expect(early.action).toBe('none');
- expect(state.updated).toHaveLength(0);
- });
-
- it('does not publish an older same-head origin after a newer redeploy starts', async () => {
- const value = identity();
- const paths = fixture(value);
- const {github, state} = githubFixture({value});
- let lookups = 0;
- const lookupPreview = async () => (++lookups === 1 ? VERCEL_ORIGIN : null);
- const report = await reconcilePrComment({
- github,
- core: {info: vi.fn(), warning: vi.fn()},
- context: {
- repo: {owner: 'meta', repo: 'astryx'},
- serverUrl: 'https://github.com',
- },
- expectedIdentity: value,
- analysisPath: paths.analysis,
- metadataPath: paths.metadata,
- a11yPath: paths.a11y,
- visualPath: paths.visual,
- lookupPreview,
- probePreview: async () => true,
- });
- expect(report.body).not.toContain('View Storybook for this PR');
- expect(report.body).not.toContain('View Sandbox for this PR');
- expect(state.created).toHaveLength(1);
-
- let earlyLookups = 0;
- const early = await reconcileEarlyPreviewComment({
- github,
- owner: 'meta',
- repo: 'astryx',
- prNumber: value.prNumber,
- headSha: value.headSha,
- origin: VERCEL_ORIGIN,
- lookupPreview: async () => (++earlyLookups === 1 ? VERCEL_ORIGIN : null),
- probePreview: async () => true,
- });
- expect(early.action).toBe('none');
- expect(state.updated).toHaveLength(0);
- });
-
- it('posts preview links before CI and later enriches the same report', async () => {
- const value = identity();
- const paths = fixture(value);
- const {github, state} = githubFixture({value});
- const early = () =>
- reconcileEarlyPreviewComment({
- github,
- owner: 'meta',
- repo: 'astryx',
- prNumber: value.prNumber,
- headSha: value.headSha,
- origin: VERCEL_ORIGIN,
- lookupPreview: async () => VERCEL_ORIGIN,
- probePreview: async () => true,
- });
- expect((await early()).action).toBe('created');
- expect(state.comments[0].body).toContain(`${VERCEL_ORIGIN}/storybook/`);
- expect(state.comments[0].body).toContain('View Sandbox for this PR');
- expect(state.comments[0].body).not.toContain('Modified Components');
- expect((await early()).action).toBe('unchanged');
- expect(state.created).toHaveLength(1);
-
- const enriched = await reconcilePrComment({
- github,
- core: {info: vi.fn(), warning: vi.fn()},
- context: {
- repo: {owner: 'meta', repo: 'astryx'},
- serverUrl: 'https://github.com',
- },
- expectedIdentity: value,
- analysisPath: paths.analysis,
- metadataPath: paths.metadata,
- a11yPath: paths.a11y,
- visualPath: paths.visual,
- lookupPreview: async () => VERCEL_ORIGIN,
- probePreview: async () => true,
- });
- expect(enriched.action).toBe('updated');
- expect(state.comments).toHaveLength(1);
- expect(state.comments[0].body).toContain('Modified Components');
- expect(state.comments[0].body).toContain(`${VERCEL_ORIGIN}/storybook/`);
- expect(state.comments[0].body).toContain(`${VERCEL_ORIGIN}/sandbox/`);
- });
-
- it('adds a late preview without discarding same-head CI and visual evidence', async () => {
- const value = identity();
- const {result, github, state} = await reconcile({
- value,
- previewAvailable: false,
- });
- expect(result.body).toContain('Modified Components');
- expect(result.body).toContain('Preview availability');
- const early = await reconcileEarlyPreviewComment({
- github,
- owner: 'meta',
- repo: 'astryx',
- prNumber: value.prNumber,
- headSha: value.headSha,
- origin: VERCEL_ORIGIN,
- lookupPreview: async () => VERCEL_ORIGIN,
- probePreview: async () => true,
- });
- expect(early.action).toBe('updated');
- expect(state.comments[0].body).toContain('Modified Components');
- expect(state.comments[0].body).not.toContain('Preview availability');
- expect(state.comments[0].body).toContain(`${VERCEL_ORIGIN}/storybook/`);
- expect(state.comments[0].body).toContain(`${VERCEL_ORIGIN}/sandbox/`);
- });
-
- it('replaces a same-head Pages fallback without duplicating its Sandbox link', async () => {
- const value = identity();
- const oldOrigin = VERCEL_ORIGIN;
- const nextOrigin = 'https://astryx-atz4b1yim-fbopensource.vercel.app';
- const legacySandbox = 'https://facebook.github.io/astryx/pr/5697/sandbox/';
- const evidence = 'CI, a11y and visual evidence remains here';
- const {github, state} = githubFixture({
- value,
- comments: [
- {
- id: 777,
- user: {type: 'Bot'},
- body: `## PR Analysis Report\n${PR_ANALYSIS_MARKER}\n<!-- astryx-pr-head:${value.headSha} -->\n\n[View Storybook for this PR](${oldOrigin}/storybook/) · [View Sandbox for this PR](${legacySandbox})\n\n${evidence}`,
- },
- ],
- });
- const result = await reconcileEarlyPreviewComment({
- github,
- owner: 'meta',
- repo: 'astryx',
- prNumber: value.prNumber,
- headSha: value.headSha,
- origin: nextOrigin,
- lookupPreview: async () => nextOrigin,
- probePreview: async () => true,
- });
- expect(result.action).toBe('updated');
- expect(state.comments[0].body).toContain(evidence);
- expect(state.comments[0].body).toContain(`${nextOrigin}/storybook/`);
- expect(state.comments[0].body).toContain(`${nextOrigin}/sandbox/`);
- expect(state.comments[0].body).not.toContain(legacySandbox);
- expect(
- state.comments[0].body.match(/View Sandbox for this PR/g),
- ).toHaveLength(1);
- });
-
- it('refreshes a same-head redeployment without losing CI, a11y, or visual evidence', async () => {
- const value = identity();
- const {github, state} = await reconcile({
- value,
- previewAvailable: true,
- });
- const prior = state.comments[0].body;
- const visualEvidence =
- '[Visual evidence](https://facebook.github.io/astryx/pr/5697/visual/example/)';
- state.comments[0].body += `\n${visualEvidence}`;
- const nextOrigin = 'https://astryx-atz4b1yim-fbopensource.vercel.app';
-
- const result = await reconcileEarlyPreviewComment({
- github,
- owner: 'meta',
- repo: 'astryx',
- prNumber: value.prNumber,
- headSha: value.headSha,
- origin: nextOrigin,
- lookupPreview: async () => nextOrigin,
- probePreview: async () => true,
- });
- expect(result.action).toBe('updated');
- expect(state.comments).toHaveLength(1);
- expect(state.comments[0].body).toContain('Modified Components');
- expect(state.comments[0].body).toContain('Accessibility Audit');
- expect(state.comments[0].body).toContain(visualEvidence);
- expect(state.comments[0].body).toContain(`${nextOrigin}/storybook/`);
- expect(state.comments[0].body).toContain(`${nextOrigin}/sandbox/`);
- expect(state.comments[0].body).not.toContain(VERCEL_ORIGIN);
- expect(prior).toContain(VERCEL_ORIGIN);
- });
-
- it('refuses an early comment when the deployment head becomes stale or draft', async () => {
- for (const value of [
- identity({headSha: 'f'.repeat(40)}),
- identity({draft: true}),
- ]) {
- const {github, state} = githubFixture({value});
- const result = await reconcileEarlyPreviewComment({
- github,
- owner: 'meta',
- repo: 'astryx',
- prNumber: 5697,
- headSha: HEAD,
- origin: VERCEL_ORIGIN,
- lookupPreview: async () => VERCEL_ORIGIN,
- probePreview: async () => true,
- });
- expect(result.action).toBe('none');
- expect(state.created).toHaveLength(0);
- expect(state.updated).toHaveLength(0);
- }
- });
+ it.each([
+ [
+ 'a branch in the repository',
+ 'meta/astryx',
+ 'fix/dropdown-section-submenu',
+ 'https://astryx-git-fix-dropdown-section-submenu-fbopensource.vercel.app',
+ ],
+ [
+ 'a fork',
+ 'kyu-rong/astryx',
+ 'Fix_Slider',
+ 'https://astryx-git-fork-kyu-rong-fix-slider-fbopensource.vercel.app',
+ ],
+ [
+ 'an alias past 63 characters',
+ 'kyu-rong/astryx',
+ 'fix-slider-keep-caller-aria-labelledby',
+ null,
+ ],
+ ])(
+ 'links the Vercel branch alias for %s',
+ (_label, headRepository, headRef, origin) => {
+ expect(
+ branchPreviewOrigin({
+ headRef,
+ headRepository,
+ baseRepository: 'meta/astryx',
+ }),
+ ).toBe(origin);
+ },
+ );
});
diff --git a/.github/scripts/lib/vercel-preview.mjs b/.github/scripts/lib/vercel-preview.mjs
deleted file mode 100644
index 3a489088b5..0000000000
--- a/.github/scripts/lib/vercel-preview.mjs
+++ /dev/null
@@ -1,229 +0,0 @@
-// Copyright (c) Meta Platforms, Inc. and affiliates.
-
-/**
- * @file Resolve the docsite deployment for an exact pull-request head.
- * @input Trusted PR identity and GitHub's Vercel deployment/status records.
- * @output A validated deployment origin, or null if this head has no ready preview.
- * @position Trusted PR comment; never falls back to a branch or older PR head.
- */
-
-const VERCEL_CREATOR = 'vercel[bot]';
-const PREVIEW_ENVIRONMENT = 'Preview';
-
-export function previewOrigin(deployment, status, headSha) {
- if (
- deployment?.sha !== headSha ||
- deployment.environment !== PREVIEW_ENVIRONMENT ||
- deployment.creator?.login !== VERCEL_CREATOR ||
- status?.state !== 'success'
- ) {
- return null;
- }
- try {
- const url = new URL(status.environment_url);
- if (
- url.protocol !== 'https:' ||
- url.username ||
- url.password ||
- !/^astryx-[a-z0-9]{9}-fbopensource\.vercel\.app$/.test(url.hostname) ||
- (url.pathname !== '/' && url.pathname !== '') ||
- url.search ||
- url.hash
- ) {
- return null;
- }
- return url.origin;
- } catch {
- return null;
- }
-}
-
-export async function probeVercelPreview(origin, fetchRoute = fetch) {
- // Check both static apps and a real Sandbox deep link before publishing any
- // PR link. A redirect (including an auth/login redirect) or Next's 404 page
- // cannot count as ready. The origin has already passed previewOrigin().
- const routes = [
- '/storybook/',
- '/storybook/iframe.html',
- '/sandbox/',
- '/sandbox/pages/component-scores/',
- ];
- try {
- const responses = await Promise.all(
- routes.map(route =>
- fetchRoute(`${origin}${route}`, {
- redirect: 'manual',
- signal: AbortSignal.timeout(12000),
- }),
- ),
- );
- return responses.every(
- response =>
- response.status === 200 &&
- response.headers.get('content-type')?.includes('text/html'),
- );
- } catch {
- return false;
- }
-}
-
-export async function waitForPreviewRoutes(
- origin,
- {
- probe = probeVercelPreview,
- waitMs = 90_000,
- pollMs = 5_000,
- now = Date.now,
- sleep = ms => new Promise(resolve => setTimeout(resolve, ms)),
- } = {},
-) {
- // Vercel can report deployment success shortly before every static route
- // reaches the edge. Retry within this bounded window instead of dropping the
- // one deployment_status event on a transient 404.
- const deadline = now() + waitMs;
- while (true) {
- try {
- if (await probe(origin)) return true;
- } catch {
- // A failed network probe is not evidence of a ready preview.
- }
- if (now() >= deadline) return false;
- await sleep(Math.min(pollMs, deadline - now()));
- }
-}
-
-export async function resolveVercelDeploymentEvent({
- github,
- owner,
- repo,
- deployment,
- status,
- probePreview = probeVercelPreview,
- probeWaitMs = 90_000,
-}) {
- const origin = previewOrigin(deployment, status, deployment?.sha);
- if (!origin) return null;
-
- // GitHub's commit→PR association omits fork-only heads. The open-PR list
- // includes both forks and same-repo heads, so bind an exact, unique match.
- const pulls = await github.paginate(github.rest.pulls.list, {
- owner,
- repo,
- state: 'open',
- per_page: 100,
- });
- const matches = pulls.filter(
- pull =>
- pull.state === 'open' &&
- !pull.draft &&
- pull.head?.sha === deployment.sha &&
- pull.base?.repo?.full_name === `${owner}/${repo}`,
- );
- if (matches.length !== 1) return null;
- const prNumber = matches[0].number;
- const currentOrigin = await resolveVercelPreview({
- github,
- owner,
- repo,
- prNumber,
- headSha: deployment.sha,
- waitMs: 0,
- });
- if (
- currentOrigin !== origin ||
- !(await waitForPreviewRoutes(origin, {
- probe: probePreview,
- waitMs: probeWaitMs,
- }))
- )
- return null;
- const {data: current} = await github.rest.pulls.get({
- owner,
- repo,
- pull_number: prNumber,
- });
- if (
- current.state !== 'open' ||
- current.draft ||
- current.head?.sha !== deployment.sha
- )
- return null;
- // A newer deployment for this *same head* may have started while we waited
- // for edge routes. Never promote an older successful origin over it.
- const latestOrigin = await resolveVercelPreview({
- github,
- owner,
- repo,
- prNumber,
- headSha: deployment.sha,
- waitMs: 0,
- });
- if (latestOrigin !== origin) return null;
- return {prNumber, headSha: deployment.sha, origin};
-}
-
-export async function resolveVercelPreview({
- github,
- owner,
- repo,
- prNumber,
- headSha,
- waitMs = 5 * 60 * 1000,
- pollMs = 10000,
- now = Date.now,
- sleep = ms => new Promise(resolve => setTimeout(resolve, ms)),
-}) {
- const deadline = now() + waitMs;
- while (true) {
- const {data: pull} = await github.rest.pulls.get({
- owner,
- repo,
- pull_number: prNumber,
- });
- if (pull.state !== 'open' || pull.draft || pull.head?.sha !== headSha) {
- return null;
- }
- const {data: deployments} = await github.rest.repos.listDeployments({
- owner,
- repo,
- sha: headSha,
- environment: PREVIEW_ENVIRONMENT,
- per_page: 100,
- });
- const latest = deployments
- .filter(
- deployment =>
- deployment.sha === headSha &&
- deployment.environment === PREVIEW_ENVIRONMENT &&
- deployment.creator?.login === VERCEL_CREATOR,
- )
- .sort((a, b) => b.id - a.id)[0];
- if (latest) {
- const {data: statuses} = await github.rest.repos.listDeploymentStatuses({
- owner,
- repo,
- deployment_id: latest.id,
- per_page: 1,
- });
- const origin = previewOrigin(latest, statuses[0], headSha);
- if (origin) {
- // A head push or close during the status request must not gain links.
- const {data: current} = await github.rest.pulls.get({
- owner,
- repo,
- pull_number: prNumber,
- });
- if (
- current.state === 'open' &&
- !current.draft &&
- current.head?.sha === headSha
- ) {
- return origin;
- }
- return null;
- }
- }
- if (now() >= deadline) return null;
- await sleep(Math.min(pollMs, deadline - now()));
- }
-}
diff --git a/.github/scripts/lib/vercel-preview.test.mjs b/.github/scripts/lib/vercel-preview.test.mjs
deleted file mode 100644
index 9e89d84796..0000000000
--- a/.github/scripts/lib/vercel-preview.test.mjs
+++ /dev/null
@@ -1,398 +0,0 @@
-// Copyright (c) Meta Platforms, Inc. and affiliates.
-
-import {describe, expect, it, vi} from 'vitest';
-
-import {
- previewOrigin,
- probeVercelPreview,
- resolveVercelDeploymentEvent,
- resolveVercelPreview,
- waitForPreviewRoutes,
-} from './vercel-preview.mjs';
-
-const SHA = 'a'.repeat(40);
-const OLD_SHA = 'b'.repeat(40);
-const ORIGIN = 'https://astryx-3s4xgbci4-fbopensource.vercel.app';
-
-function fixture({
- head = SHA,
- draft = false,
- state = 'open',
- deployments,
- statuses,
- candidates = [
- {
- number: 123,
- state: 'open',
- draft: false,
- head: {sha: SHA, repo: {full_name: 'contributor/astryx'}},
- base: {repo: {full_name: 'meta/astryx'}},
- },
- ],
-} = {}) {
- const github = {
- rest: {
- pulls: {
- get: vi.fn(async () => ({data: {state, draft, head: {sha: head}}})),
- list: vi.fn(async () => ({data: candidates})),
- },
- repos: {
- listDeployments: vi.fn(async () => ({
- data: deployments ?? [
- {
- id: 7,
- sha: SHA,
- environment: 'Preview',
- creator: {login: 'vercel[bot]'},
- },
- ],
- })),
- listDeploymentStatuses: vi.fn(async () => ({
- data: statuses ?? [{state: 'success', environment_url: ORIGIN}],
- })),
- },
- },
- paginate: vi.fn(async (method, request) => (await method(request)).data),
- };
- return github;
-}
-
-function resolve(github, options = {}) {
- return resolveVercelPreview({
- github,
- owner: 'meta',
- repo: 'astryx',
- prNumber: 123,
- headSha: SHA,
- waitMs: 0,
- ...options,
- });
-}
-
-describe('exact-head Vercel preview', () => {
- it('probes both static apps, iframe and a Sandbox deep link; rejects redirects and 404s', async () => {
- const paths = [];
- const fetchRoute = vi.fn(async (url, options) => {
- paths.push(new URL(url).pathname);
- expect(options.redirect).toBe('manual');
- return {
- status: 200,
- headers: new Headers({'content-type': 'text/html; charset=utf-8'}),
- };
- });
- expect(await probeVercelPreview(ORIGIN, fetchRoute)).toBe(true);
- expect(paths).toEqual([
- '/storybook/',
- '/storybook/iframe.html',
- '/sandbox/',
- '/sandbox/pages/component-scores/',
- ]);
- fetchRoute.mockImplementationOnce(async () => ({
- status: 308,
- headers: new Headers(),
- }));
- expect(await probeVercelPreview(ORIGIN, fetchRoute)).toBe(false);
- fetchRoute.mockImplementationOnce(async () => {
- throw new Error('network unavailable');
- });
- expect(await probeVercelPreview(ORIGIN, fetchRoute)).toBe(false);
- });
-
- it('retries transient route failures within a bounded window', async () => {
- let time = 0;
- const probe = vi
- .fn()
- .mockResolvedValueOnce(false)
- .mockResolvedValueOnce(false)
- .mockResolvedValueOnce(true);
- const sleep = vi.fn(async ms => {
- time += ms;
- });
- expect(
- await waitForPreviewRoutes(ORIGIN, {
- probe,
- waitMs: 20,
- pollMs: 5,
- now: () => time,
- sleep,
- }),
- ).toBe(true);
- expect(probe).toHaveBeenCalledTimes(3);
- expect(sleep).toHaveBeenCalledTimes(2);
- expect(
- await waitForPreviewRoutes(ORIGIN, {
- probe: async () => false,
- waitMs: 0,
- }),
- ).toBe(false);
- });
-
- it('refuses a successful status if either static tree is not actually ready', async () => {
- const github = fixture();
- const deployment = {
- id: 7,
- sha: SHA,
- environment: 'Preview',
- creator: {login: 'vercel[bot]'},
- };
- const status = {state: 'success', environment_url: ORIGIN};
- expect(
- await resolveVercelDeploymentEvent({
- github,
- owner: 'meta',
- repo: 'astryx',
- deployment,
- status,
- probePreview: async () => false,
- probeWaitMs: 0,
- }),
- ).toBeNull();
- });
-
- it('refuses an old origin if a newer same-head deployment begins during route probes', async () => {
- const github = fixture();
- const deployment = {
- id: 7,
- sha: SHA,
- environment: 'Preview',
- creator: {login: 'vercel[bot]'},
- };
- const newer = {...deployment, id: 8};
- github.rest.repos.listDeploymentStatuses.mockImplementation(
- async ({deployment_id}) => ({
- data:
- deployment_id === 8
- ? [{state: 'pending', environment_url: ORIGIN}]
- : [{state: 'success', environment_url: ORIGIN}],
- }),
- );
- expect(
- await resolveVercelDeploymentEvent({
- github,
- owner: 'meta',
- repo: 'astryx',
- deployment,
- status: {state: 'success', environment_url: ORIGIN},
- probePreview: async () => {
- github.rest.repos.listDeployments.mockResolvedValue({
- data: [newer, deployment],
- });
- return true;
- },
- }),
- ).toBeNull();
- });
-
- it('only accepts a successful Vercel Preview deployment for the exact SHA', () => {
- const deployment = {
- sha: SHA,
- environment: 'Preview',
- creator: {login: 'vercel[bot]'},
- };
- const status = {state: 'success', environment_url: ORIGIN};
- expect(previewOrigin(deployment, status, SHA)).toBe(ORIGIN);
- expect(
- previewOrigin(
- deployment,
- {
- ...status,
- environment_url: 'https://astryx-git-main-fbopensource.vercel.app',
- },
- SHA,
- ),
- ).toBeNull();
- expect(
- previewOrigin({...deployment, sha: OLD_SHA}, status, SHA),
- ).toBeNull();
- expect(
- previewOrigin({...deployment, environment: 'canary'}, status, SHA),
- ).toBeNull();
- expect(
- previewOrigin({...deployment, creator: {login: 'attacker'}}, status, SHA),
- ).toBeNull();
- expect(
- previewOrigin(deployment, {...status, state: 'pending'}, SHA),
- ).toBeNull();
- expect(
- previewOrigin(
- deployment,
- {...status, environment_url: 'https://evil.example/'},
- SHA,
- ),
- ).toBeNull();
- expect(
- previewOrigin(
- deployment,
- {...status, environment_url: `${ORIGIN}.evil.example`},
- SHA,
- ),
- ).toBeNull();
- expect(
- previewOrigin(
- deployment,
- {...status, environment_url: `${ORIGIN}/other`},
- SHA,
- ),
- ).toBeNull();
- });
-
- it('returns only the exact-head deployment origin and checks the head twice', async () => {
- const github = fixture();
- expect(await resolve(github)).toBe(ORIGIN);
- expect(github.rest.repos.listDeployments).toHaveBeenCalledWith({
- owner: 'meta',
- repo: 'astryx',
- sha: SHA,
- environment: 'Preview',
- per_page: 100,
- });
- expect(github.rest.pulls.get).toHaveBeenCalledTimes(2);
- });
-
- it('refuses old-head, draft, and closed PRs without querying deployments', async () => {
- for (const options of [{head: OLD_SHA}, {draft: true}, {state: 'closed'}]) {
- const github = fixture(options);
- expect(await resolve(github)).toBeNull();
- expect(github.rest.repos.listDeployments).not.toHaveBeenCalled();
- }
- });
-
- it('does not reuse an older head or an older successful deployment while the newest deploy is pending', async () => {
- const old = {
- id: 6,
- sha: OLD_SHA,
- environment: 'Preview',
- creator: {login: 'vercel[bot]'},
- };
- const current = {
- id: 8,
- sha: SHA,
- environment: 'Preview',
- creator: {login: 'vercel[bot]'},
- };
- const github = fixture({
- deployments: [old, current],
- statuses: [{state: 'pending', environment_url: ORIGIN}],
- });
- expect(await resolve(github)).toBeNull();
- expect(github.rest.repos.listDeploymentStatuses).toHaveBeenCalledWith({
- owner: 'meta',
- repo: 'astryx',
- deployment_id: 8,
- per_page: 1,
- });
- });
-
- it('waits for readiness within a bounded window and stops on a new head', async () => {
- const github = fixture({statuses: [{state: 'pending'}]});
- let time = 0;
- const sleep = vi.fn(async ms => {
- time += ms;
- github.rest.repos.listDeploymentStatuses.mockResolvedValue({
- data: [{state: 'success', environment_url: ORIGIN}],
- });
- });
- expect(
- await resolve(github, {waitMs: 20, pollMs: 10, now: () => time, sleep}),
- ).toBe(ORIGIN);
- expect(sleep).toHaveBeenCalledTimes(1);
-
- github.rest.pulls.get
- .mockResolvedValueOnce({
- data: {state: 'open', draft: false, head: {sha: SHA}},
- })
- .mockResolvedValueOnce({
- data: {state: 'open', draft: false, head: {sha: OLD_SHA}},
- });
- expect(await resolve(github)).toBeNull();
- });
-
- it('resolves fork and same-repository deployment events without source CI', async () => {
- const deployment = {
- id: 7,
- sha: SHA,
- environment: 'Preview',
- creator: {login: 'vercel[bot]'},
- };
- const status = {state: 'success', environment_url: ORIGIN};
- for (const headRepository of ['contributor/astryx', 'meta/astryx']) {
- const github = fixture({
- candidates: [
- {
- number: 123,
- state: 'open',
- draft: false,
- head: {sha: SHA, repo: {full_name: headRepository}},
- base: {repo: {full_name: 'meta/astryx'}},
- },
- ],
- });
- expect(
- await resolveVercelDeploymentEvent({
- github,
- owner: 'meta',
- repo: 'astryx',
- deployment,
- status,
- probePreview: async () => true,
- }),
- ).toEqual({prNumber: 123, headSha: SHA, origin: ORIGIN});
- expect(github.paginate).toHaveBeenCalledWith(github.rest.pulls.list, {
- owner: 'meta',
- repo: 'astryx',
- state: 'open',
- per_page: 100,
- });
- }
- });
-
- it('ignores unrelated deployments and refuses ambiguous or stale PR associations', async () => {
- const deployment = {
- id: 7,
- sha: SHA,
- environment: 'Preview',
- creator: {login: 'vercel[bot]'},
- };
- const status = {state: 'success', environment_url: ORIGIN};
- for (const invalid of [
- {deployment: {...deployment, environment: 'Production'}, status},
- {deployment, status: {...status, state: 'pending'}},
- {deployment: {...deployment, creator: {login: 'other'}}, status},
- ]) {
- const github = fixture();
- expect(
- await resolveVercelDeploymentEvent({
- github,
- owner: 'meta',
- repo: 'astryx',
- ...invalid,
- }),
- ).toBeNull();
- expect(github.paginate).not.toHaveBeenCalled();
- }
- const candidate = {
- number: 123,
- state: 'open',
- draft: false,
- head: {sha: SHA},
- base: {repo: {full_name: 'meta/astryx'}},
- };
- for (const candidates of [
- [{...candidate, draft: true}],
- [{...candidate, head: {sha: OLD_SHA}}],
- [candidate, {...candidate, number: 124}],
- ]) {
- const github = fixture({candidates});
- expect(
- await resolveVercelDeploymentEvent({
- github,
- owner: 'meta',
- repo: 'astryx',
- deployment,
- status,
- probePreview: async () => true,
- }),
- ).toBeNull();
- }
- });
-});
diff --git a/.github/scripts/visual-gate/workflow-concurrency.test.mjs b/.github/scripts/visual-gate/workflow-concurrency.test.mjs
index 9417f7a23a..352f875843 100644
--- a/.github/scripts/visual-gate/workflow-concurrency.test.mjs
+++ b/.github/scripts/visual-gate/workflow-concurrency.test.mjs
@@ -3,14 +3,12 @@
/**
* @file Guard PR reporting, preview staging, and remaining Pages writers.
* @input Workflow definitions plus the docsite builder and PR reconciler.
- * @output Node tests that keep exact-head Vercel links and visual evidence
+ * @output Node tests that keep branch-alias Vercel links and visual evidence
* separate from the remaining Pages publisher until its migration is proven.
* @position Repository workflow contract tests.
*/
-import {execFileSync} from 'node:child_process';
import fs from 'node:fs';
-import os from 'node:os';
import path from 'node:path';
import {fileURLToPath} from 'node:url';
@@ -52,85 +50,26 @@ describe('PR report and deployment workflow contracts', () => {
expect(reportScript).not.toMatch(/visual|verdict/i);
});
- it('serializes early preview and later CI enrichment per PR, not across PRs', () => {
+ it('serializes the report per PR, not across PRs, and waits for no deployment', () => {
const value = yaml.parse(workflow('pr-comment.yml'));
- expect(value.on.deployment_status).toBeDefined();
+ expect(Object.keys(value.on)).toEqual(['workflow_run']);
expect(value.on.workflow_run.types).toEqual(['completed']);
expect(value.concurrency).toBeUndefined();
- expect(value.jobs['preview-comment'].concurrency.group).toBe(
- 'pr-report-${{ needs.resolve-preview.outputs.pr_number }}',
- );
+ expect(value.jobs['resolve-preview']).toBeUndefined();
+ expect(value.jobs['preview-comment']).toBeUndefined();
for (const job of ['comment', 'spec-only-reconcile']) {
expect(value.jobs[job].concurrency.group).toBe(
'pr-report-${{ needs.resolve.outputs.pr_number }}',
);
expect(value.jobs[job].concurrency['cancel-in-progress']).toBe(false);
}
+ expect(value.jobs.comment.needs).toBe('resolve');
+ expect(value.jobs.comment.permissions.deployments).toBeUndefined();
expect(workflow('pr-comment.yml')).toContain(
'validateAnalysisMetadata(metadata, {',
);
});
- it('routes Vercel success directly to a trusted early comment and CI completion to enrichment', () => {
- const value = workflow('pr-comment.yml');
- const jobs = yaml.parse(value).jobs;
- expect(jobs['resolve-preview'].if).toContain(
- "github.event_name == 'deployment_status'",
- );
- expect(jobs['resolve-preview'].if).toContain(
- "github.event.deployment.environment == 'Preview'",
- );
- expect(jobs['resolve-preview'].if).toContain(
- "github.event.deployment.creator.login == 'vercel[bot]'",
- );
- expect(jobs['resolve-preview'].steps[0].with.ref).toBe('main');
- const guard = jobs['resolve-preview'].steps[1];
- expect(guard.run).toContain('available=false');
- expect(guard.run).toContain('Preview resolver is not on trusted main yet');
- expect(jobs['resolve-preview'].steps[2].if).toBe(
- "steps.trusted.outputs.available == 'true'",
- );
- expect(jobs['resolve-preview'].steps[2].with.script).toContain(
- 'resolveVercelDeploymentEvent',
- );
- expect(jobs['preview-comment'].needs).toBe('resolve-preview');
- expect(jobs['preview-comment'].steps[1].with.script).toContain(
- 'reconcileEarlyPreviewComment',
- );
- expect(jobs.resolve.if).toContain("github.event_name == 'workflow_run'");
- expect(jobs.comment.needs).toBe('resolve');
- expect(jobs.comment.permissions.deployments).toBe('read');
- });
-
- it('skips pre-merge deployment events when the resolver is absent from trusted main', () => {
- const guard = yaml.parse(workflow('pr-comment.yml')).jobs['resolve-preview']
- .steps[1];
- const root = fs.mkdtempSync(
- path.join(os.tmpdir(), 'astryx-preview-bootstrap-'),
- );
- const output = path.join(root, 'outputs');
- try {
- const run = () =>
- execFileSync('bash', ['-e', '-c', guard.run], {
- cwd: root,
- env: {...process.env, GITHUB_OUTPUT: output},
- encoding: 'utf8',
- });
- expect(run()).toContain('Preview resolver is not on trusted main yet');
- expect(fs.readFileSync(output, 'utf8')).toContain('available=false');
- fs.mkdirSync(path.join(root, '.github/scripts/lib'), {recursive: true});
- fs.writeFileSync(
- path.join(root, '.github/scripts/lib/vercel-preview.mjs'),
- 'trusted main',
- );
- fs.writeFileSync(output, '');
- expect(run()).toBe('');
- expect(fs.readFileSync(output, 'utf8')).toContain('available=true');
- } finally {
- fs.rmSync(root, {recursive: true, force: true});
- }
- });
-
it('reconciles spec-only comments without creating a preview or extra build', () => {
const value = workflow('pr-comment.yml');
const reconcile = value.slice(
@@ -191,14 +130,13 @@ describe('PR report and deployment workflow contracts', () => {
expect(routing).toContain("source: '/sandbox/:path+'");
expect(routing).toContain("destination: '/sandbox/:path+/index.html'");
const comment = workflow('pr-comment.yml');
- expect(comment).toContain('deployments: read');
expect(comment).not.toContain('deploy-preview.yml');
expect(comment).not.toContain('gh-pages-publisher.mjs');
const reconciler = fs.readFileSync(
path.join(ROOT, '.github/scripts/lib/pr-preview.mjs'),
'utf8',
);
- expect(reconciler).toContain('resolveVercelPreview');
+ expect(reconciler).toContain('branchPreviewOrigin(identity)');
expect(reconciler).toContain('`${previewOrigin}/storybook/`');
expect(reconciler).toContain('`${previewOrigin}/sandbox/`');
});
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index f807e9732c..4503097173 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -1027,7 +1027,7 @@ jobs:
if: steps.scope.outputs.docsite_only != 'true' && steps.scope.outputs.spec_only != 'true' && steps.scope.outputs.tooling_only != 'true' && github.event_name == 'pull_request'
run: |
# PR Comment reads this exact-run analysis. Storybook and Sandbox
- # preview links come from the exact-head Vercel deployment.
+ # preview links are the branch's Vercel alias.
cat > pr-meta.json <<EOF
{
"prNumber": "${{ steps.urls.outputs.pr_number }}",
@@ -1060,6 +1060,7 @@ jobs:
runs-on: ubuntu-latest
timeout-minutes: 15
permissions:
+ actions: read
contents: read
pull-requests: read
steps:
@@ -1113,17 +1114,58 @@ jobs:
echo "sha=$BASE_SHA" >> "$GITHUB_OUTPUT"
echo "found=true" >> "$GITHUB_OUTPUT"
- # Core and its build tooling always build, because the other component
- # packages depend on them; another package builds only when the PR
- # changes its source.
- - name: Build the merge base's component packages
+ # The merge base's dependencies install either way: measuring bundles
+ # each base component with esbuild, which resolves its imports there.
+ - name: Install the merge base's dependencies
if: (steps.merge-base.outputs.found == 'true') && steps.scope.outputs.docsite_only != 'true' && steps.scope.outputs.spec_only != 'true' && steps.scope.outputs.tooling_only != 'true'
working-directory: .size-base
+ run: pnpm install --frozen-lockfile
+
+ # Main keeps each commit's package builds for 14 days. A merge base on
+ # main within that window is measured from them, with no build.
+ - name: Find main's build of the merge base
+ id: cached
+ continue-on-error: true
+ if: (steps.merge-base.outputs.found == 'true') && steps.scope.outputs.docsite_only != 'true' && steps.scope.outputs.spec_only != 'true' && steps.scope.outputs.tooling_only != 'true'
+ uses: actions/github-script@v9
+ env:
+ BASE_SHA: ${{ steps.merge-base.outputs.sha }}
+ with:
+ retries: 3
+ script: |
+ const name = `dists-main-${process.env.BASE_SHA}`;
+ const {data} = await github.rest.actions.listArtifactsForRepo({...context.repo, name, per_page: 100});
+ // Only Main's own run on main for this exact commit: a pull
+ // request's run can upload an artifact under any name.
+ const artifact = data.artifacts.find(candidate =>
+ !candidate.expired &&
+ candidate.workflow_run?.head_branch === 'main' &&
+ candidate.workflow_run?.head_sha === process.env.BASE_SHA &&
+ candidate.workflow_run?.head_repository_id === candidate.workflow_run?.repository_id);
+ core.setOutput('run_id', artifact ? String(artifact.workflow_run.id) : '');
+ core.info(artifact ? `Using ${name} from run ${artifact.workflow_run.id}.` : `No ${name}; building the merge base.`);
+
+ - name: Download main's build of the merge base
+ id: download
+ if: steps.cached.outputs.run_id != ''
+ continue-on-error: true
+ uses: actions/download-artifact@v8
+ with:
+ name: dists-main-${{ steps.merge-base.outputs.sha }}
+ run-id: ${{ steps.cached.outputs.run_id }}
+ github-token: ${{ github.token }}
+ path: .size-base/packages
+
+ # Without a kept build: core and its build tooling always build, because
+ # the other component packages depend on them; another package builds
+ # only when the PR changes its source.
+ - name: Build the merge base's component packages
+ if: (steps.merge-base.outputs.found == 'true') && steps.download.outcome != 'success' && steps.scope.outputs.docsite_only != 'true' && steps.scope.outputs.spec_only != 'true' && steps.scope.outputs.tooling_only != 'true'
+ working-directory: .size-base
env:
BASE_SHA: ${{ steps.merge-base.outputs.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: |
- pnpm install --frozen-lockfile
pnpm -F @astryxdesign/build build
pnpm -F @astryxdesign/core build
for pkg in lab charts richtext vega; do
@@ -1241,8 +1283,8 @@ jobs:
[ "${{ needs.typecheck.result }}" = "success" ] || exit 1
# CI keeps Storybook's static artifact for visual, accessibility, and RTL
- # checks. The exact-head Vercel deployment independently hosts both human
- # Storybook and Sandbox previews; no branch-backed preview is published.
+ # checks. Vercel independently hosts both human Storybook and Sandbox
+ # previews; the PR report links them through the branch's alias.
# Accessibility audit
# Skipped when no components changed. Depends only on build-storybook, so it
diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml
index be6e9707d3..5f93ce343e 100644
--- a/.github/workflows/deploy.yml
+++ b/.github/workflows/deploy.yml
@@ -131,6 +131,17 @@ jobs:
- run: pnpm build
+ # A pull request measures its merge base's component sizes from this
+ # build instead of building the base again (ci.yml size-base).
+ - name: Keep this commit's package builds for PR size reports
+ uses: actions/upload-artifact@v7
+ with:
+ name: dists-main-${{ needs.main-commit.outputs.sha }}
+ path: |
+ packages/*/dist/
+ packages/themes/*/dist/
+ retention-days: 14
+
- name: Verify package exports
run: node scripts/verify-exports.mjs
diff --git a/.github/workflows/pr-comment.yml b/.github/workflows/pr-comment.yml
index 5f37af3a24..75c491e4ec 100644
--- a/.github/workflows/pr-comment.yml
+++ b/.github/workflows/pr-comment.yml
@@ -10,9 +10,8 @@
# drift between a fork path and a same-repo path.
#
# Safety: resolves scope and PR identity from the GitHub API. Privileged jobs
-# check out only trusted default-branch code. Both preview URLs are resolved
-# from one exact-head Vercel deployment after route probes; the Pages result
-# remains a separate source-CI artifact, never a preview-link fallback.
+# check out only trusted default-branch code. Both preview links are the
+# branch's Vercel alias, known without waiting for a deployment.
name: PR Comment
@@ -20,7 +19,6 @@ on:
workflow_run:
workflows: ['CI']
types: [completed]
- deployment_status:
permissions: {}
@@ -111,105 +109,6 @@ jobs:
core.setOutput('spec_only', String(changeScope.specOnly));
core.setOutput('tooling_only', String(changeScope.toolingOnly));
- resolve-preview:
- if: >-
- github.event_name == 'deployment_status' &&
- github.event.deployment_status.state == 'success' &&
- github.event.deployment.environment == 'Preview' &&
- github.event.deployment.creator.login == 'vercel[bot]'
- runs-on: ubuntu-slim
- permissions:
- contents: read
- pull-requests: read
- deployments: read
- outputs:
- valid: ${{ steps.preview.outputs.valid }}
- pr_number: ${{ steps.preview.outputs.pr_number }}
- head_sha: ${{ steps.preview.outputs.head_sha }}
- origin: ${{ steps.preview.outputs.origin }}
- steps:
- - name: Checkout trusted default-branch code
- uses: actions/checkout@v7
- with:
- ref: main
-
- - name: Check trusted resolver availability
- id: trusted
- run: |
- if [ -f .github/scripts/lib/vercel-preview.mjs ]; then
- echo "available=true" >> "$GITHUB_OUTPUT"
- else
- # A new deployment_status workflow can run before its helper lands
- # on main. Never load the untrusted PR branch as a substitute.
- echo "available=false" >> "$GITHUB_OUTPUT"
- echo "::notice::Preview resolver is not on trusted main yet; skipping this deployment."
- fi
-
- - name: Resolve current Vercel PR preview
- id: preview
- if: steps.trusted.outputs.available == 'true'
- uses: actions/github-script@v9
- with:
- retries: 3
- script: |
- const {pathToFileURL} = require('node:url');
- const {resolveVercelDeploymentEvent} = await import(
- pathToFileURL(`${process.env.GITHUB_WORKSPACE}/.github/scripts/lib/vercel-preview.mjs`).href
- );
- const preview = await resolveVercelDeploymentEvent({
- github,
- ...context.repo,
- deployment: context.payload.deployment,
- status: context.payload.deployment_status,
- });
- if (!preview) {
- core.notice('No current, non-draft PR matches this exact Vercel Preview deployment.');
- core.setOutput('valid', 'false');
- return;
- }
- core.setOutput('valid', 'true');
- core.setOutput('pr_number', String(preview.prNumber));
- core.setOutput('head_sha', preview.headSha);
- core.setOutput('origin', preview.origin);
-
- preview-comment:
- needs: resolve-preview
- if: needs.resolve-preview.outputs.valid == 'true'
- concurrency:
- group: pr-report-${{ needs.resolve-preview.outputs.pr_number }}
- cancel-in-progress: false
- runs-on: ubuntu-slim
- permissions:
- contents: read
- pull-requests: write
- deployments: read
- steps:
- - name: Checkout trusted default-branch code
- uses: actions/checkout@v7
- with:
- ref: main
-
- - name: Post exact-head preview links before CI completes
- uses: actions/github-script@v9
- env:
- PR_NUMBER: ${{ needs.resolve-preview.outputs.pr_number }}
- HEAD_SHA: ${{ needs.resolve-preview.outputs.head_sha }}
- PREVIEW_ORIGIN: ${{ needs.resolve-preview.outputs.origin }}
- with:
- retries: 3
- script: |
- const {pathToFileURL} = require('node:url');
- const {reconcileEarlyPreviewComment} = await import(
- pathToFileURL(`${process.env.GITHUB_WORKSPACE}/.github/scripts/lib/pr-preview.mjs`).href
- );
- await reconcileEarlyPreviewComment({
- github,
- ...context.repo,
- prNumber: Number(process.env.PR_NUMBER),
- headSha: process.env.HEAD_SHA,
- origin: process.env.PREVIEW_ORIGIN,
- });
-
spec-only-reconcile:
needs: resolve
concurrency:
@@ -289,7 +188,7 @@ jobs:
concurrency:
group: pr-report-${{ needs.resolve.outputs.pr_number }}
cancel-in-progress: false
- # Only the exact-head Vercel deployment supplies Storybook and Sandbox PR links.
+ # Storybook and Sandbox links are the branch's Vercel alias.
# Visual evidence remains attached to the exact CI run.
if: >-
always() &&
@@ -301,7 +200,6 @@ jobs:
permissions:
pull-requests: write
actions: read
- deployments: read # resolve the exact-head Vercel Preview deployment
# The artifact downloads are optional (a run may not produce them), so
# they are not failures of this job.
outputs:
@@ -513,8 +411,7 @@ jobs:
# main's latest commit. Main's checks must show main's health, not a pull
# request's. The jobs above therefore never fail the run: every fallible step
# continues on error, and this job reports their outcome as the PR Comment
- # status on the pull request's own head. (The deployment_status lane already
- # runs on the pull request's head commit.)
+ # status on the pull request's own head.
report:
needs: [resolve, spec-only-reconcile, comment]
if: >-