Skip to content

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

TypeScript

ci(pr-comment): a rerun of the trusted source run is not a new identity (#7377)

Cindy Zhangac4cb8cd1571
diff --git a/.github/scripts/lib/pr-preview.mjs b/.github/scripts/lib/pr-preview.mjs
index 1158f9f538..b4dbc5416d 100644
--- a/.github/scripts/lib/pr-preview.mjs
+++ b/.github/scripts/lib/pr-preview.mjs
@@ -205,11 +205,14 @@ export async function resolveWorkflowRunPullRequest({
}
if (
matches.length === 0 &&
- candidates.every(pull => pull?.state !== 'open')
+ candidates.every(
+ pull => pull?.state !== 'open' || pull?.head?.sha !== run?.head_sha,
+ )
) {
- // A source run can be rerun after its pull request merges. No privileged
- // work remains when there is no open candidate, so finish without turning
- // the trusted default-branch workflow red.
+ // A source run can be rerun after its pull request merges, or after the
+ // branch moved to a new head. Either way the run's report has no current
+ // pull request: no privileged work remains, so finish without turning the
+ // trusted default-branch workflow red. The newer head has its own run.
return null;
}
if (matches.length !== 1) {
@@ -228,8 +231,24 @@ export async function confirmSourceRunIdentity({
}) {
const sourceRunId = positiveInteger(expected.sourceRunId, 'source run id');
const prNumber = positiveInteger(expected.prNumber, 'pull request number');
+ const attemptNumber = positiveInteger(
+ expected.sourceRunAttempt,
+ 'source run attempt',
+ );
+ // Read the exact attempt the trusted event reported. `getWorkflowRun`
+ // describes the LATEST attempt, so a rerun started after the event would make
+ // the same run look like a different identity (attempt and conclusion).
+ const readRun =
+ typeof github.rest.actions.getWorkflowRunAttempt === 'function'
+ ? github.rest.actions.getWorkflowRunAttempt({
+ owner,
+ repo,
+ run_id: sourceRunId,
+ attempt_number: attemptNumber,
+ })
+ : github.rest.actions.getWorkflowRun({owner, repo, run_id: sourceRunId});
const [{data: run}, {data: pull}] = await Promise.all([
- github.rest.actions.getWorkflowRun({owner, repo, run_id: sourceRunId}),
+ readRun,
github.rest.pulls.get({owner, repo, pull_number: prNumber}),
]);
const actual = identityFromPullAndRun({
@@ -241,6 +260,38 @@ export async function confirmSourceRunIdentity({
return actual;
}
+/**
+ * The run's latest attempt number, or null when the client cannot say. A
+ * report for an earlier attempt must not replace the latest attempt's: that
+ * attempt's own completion publishes.
+ */
+export async function latestAttemptOf({github, owner, repo, sourceRunId}) {
+ const {data: run} = await github.rest.actions.getWorkflowRun({
+ owner,
+ repo,
+ run_id: positiveInteger(sourceRunId, 'source run id'),
+ });
+ const attempt = Number(run?.run_attempt);
+ return Number.isSafeInteger(attempt) && attempt > 0 ? attempt : null;
+}
+
+/** True when a newer attempt of the reported run exists. */
+async function supersededByNewerAttempt({github, owner, repo, identity, core}) {
+ const latest = await latestAttemptOf({
+ github,
+ owner,
+ repo,
+ sourceRunId: identity.sourceRunId,
+ });
+ if (latest != null && latest > identity.sourceRunAttempt) {
+ core.info(
+ `Attempt ${latest} of source run ${identity.sourceRunId} supersedes the reported attempt ${identity.sourceRunAttempt}; its own completion publishes.`,
+ );
+ return true;
+ }
+ return false;
+}
+
export function validateAnalysisMetadata(metadata, identity) {
if (String(metadata?.prNumber) !== String(identity.prNumber)) {
refuse('analysis pull request does not match');
@@ -364,6 +415,9 @@ export async function reconcilePrComment({
repo,
expected: expectedIdentity,
});
+ if (await supersededByNewerAttempt({github, owner, repo, identity, core})) {
+ return {action: 'none', body: null, identity};
+ }
const runUrl = `${context.serverUrl}/${owner}/${repo}/actions/runs/${identity.sourceRunId}`;
const analysisReady = trustedAnalysis({
analysisPath,
@@ -464,14 +518,19 @@ export async function reconcilePrComment({
});
}
- // Vercel readiness can take minutes after the source run finishes. Check the
- // current PR and CI attempt again before changing any comment.
+ // 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.
await confirmSourceRunIdentity({
github,
owner,
repo,
expected: expectedIdentity,
});
+ if (await supersededByNewerAttempt({github, owner, repo, identity, core})) {
+ return {action: 'none', body: null, identity};
+ }
if (
previewOrigin &&
(await lookupPreview({
diff --git a/.github/scripts/lib/pr-preview.test.mjs b/.github/scripts/lib/pr-preview.test.mjs
index 046274f902..dc6100023f 100644
--- a/.github/scripts/lib/pr-preview.test.mjs
+++ b/.github/scripts/lib/pr-preview.test.mjs
@@ -9,6 +9,7 @@ import {afterEach, describe, expect, it, vi} from 'vitest';
import yaml from 'yaml';
import {
+ confirmSourceRunIdentity,
PR_ANALYSIS_MARKER,
reconcileEarlyPreviewComment,
reconcilePrComment,
@@ -396,8 +397,24 @@ describe('trusted PR preview identity', () => {
}
});
+ it('skips a stale rerun whose pull request has moved to a newer head', async () => {
+ // The branch was pushed after the source run was rerun: the run's report
+ // has no current pull request, and the newer head has its own run.
+ const trusted = identity();
+ const moved = identity({headSha: 'e'.repeat(40)});
+ const {github} = githubFixture({value: moved});
+
+ await expect(
+ resolveWorkflowRunPullRequest({
+ github,
+ owner: 'facebook',
+ repo: 'astryx',
+ run: sourceRun(trusted),
+ }),
+ ).resolves.toBeNull();
+ });
+
it.each([
- ['wrong head', {headSha: 'e'.repeat(40)}],
['wrong head repository', {headRepository: 'someone/astryx'}],
['wrong base repository', {baseRepository: 'someone/astryx'}],
])('rejects a candidate with the %s', async (_label, overrides) => {
@@ -431,6 +448,76 @@ describe('trusted PR preview identity', () => {
});
});
+describe('confirmSourceRunIdentity reads the attempt the event reported', () => {
+ it('accepts the reported attempt after a newer rerun of the same run started', async () => {
+ const expected = identity({
+ sourceRunAttempt: 1,
+ sourceConclusion: 'failure',
+ });
+ const {github} = githubFixture({value: expected});
+ // The latest attempt is a rerun still in progress; the event was for attempt 1.
+ github.rest.actions.getWorkflowRun.mockResolvedValue({
+ data: {...sourceRun(expected), run_attempt: 2, conclusion: null},
+ });
+ github.rest.actions.getWorkflowRunAttempt = vi.fn(
+ async ({attempt_number}) => ({
+ data: {...sourceRun(expected), run_attempt: attempt_number},
+ }),
+ );
+
+ await expect(
+ confirmSourceRunIdentity({
+ github,
+ owner: 'facebook',
+ repo: 'astryx',
+ expected,
+ }),
+ ).resolves.toMatchObject({
+ sourceRunId: expected.sourceRunId,
+ sourceRunAttempt: 1,
+ headSha: expected.headSha,
+ });
+ expect(github.rest.actions.getWorkflowRunAttempt).toHaveBeenCalledWith(
+ expect.objectContaining({
+ run_id: expected.sourceRunId,
+ attempt_number: 1,
+ }),
+ );
+ });
+
+ it('still refuses when the reported attempt tested a different head', async () => {
+ const expected = identity({sourceRunAttempt: 1});
+ const {github} = githubFixture({value: expected});
+ github.rest.actions.getWorkflowRunAttempt = vi.fn(async () => ({
+ data: {...sourceRun(expected), head_sha: 'e'.repeat(40)},
+ }));
+
+ await expect(
+ confirmSourceRunIdentity({
+ github,
+ owner: 'facebook',
+ repo: 'astryx',
+ expected,
+ }),
+ ).rejects.toThrow(/does not match/);
+ });
+
+ it('falls back to the latest attempt when the client cannot read one attempt', async () => {
+ const expected = identity({sourceRunAttempt: 1});
+ const {github} = githubFixture({value: expected});
+ delete github.rest.actions.getWorkflowRunAttempt;
+
+ await expect(
+ confirmSourceRunIdentity({
+ github,
+ owner: 'facebook',
+ repo: 'astryx',
+ expected,
+ }),
+ ).resolves.toMatchObject({sourceRunAttempt: 1});
+ });
+});
+
describe('analysis metadata compatibility', () => {
it('accepts legacy metadata only when trusted run, PR, and head prefix match', () => {
const value = identity();
@@ -599,6 +686,42 @@ describe('PR comment preview reconciliation', () => {
);
});
+ it('stands down when an earlier attempt reports after a newer attempt exists', async () => {
+ const first = identity({sourceRunAttempt: 1, sourceConclusion: 'failure'});
+ const harness = githubFixture({value: first});
+ const firstPaths = fixture(first);
+ const core = {info: vi.fn(), warning: vi.fn()};
+ // Attempt 2 is the run's latest; the event being processed is attempt 1's.
+ harness.github.rest.actions.getWorkflowRun.mockResolvedValue({
+ data: {...sourceRun(first), run_attempt: 2, conclusion: 'success'},
+ });
+ harness.github.rest.actions.getWorkflowRunAttempt = vi.fn(async () => ({
+ data: sourceRun(first),
+ }));
+
+ const result = await reconcilePrComment({
+ github: harness.github,
+ core,
+ context: {
+ repo: {owner: 'facebook', repo: 'astryx'},
+ serverUrl: 'https://github.com',
+ },
+ expectedIdentity: first,
+ analysisPath: firstPaths.analysis,
+ metadataPath: firstPaths.metadata,
+ a11yPath: firstPaths.a11y,
+ visualPath: firstPaths.visual,
+ probePreview: async () => true,
+ });
+
+ expect(result.action).toBe('none');
+ expect(harness.state.created).toHaveLength(0);
+ expect(harness.state.updated).toHaveLength(0);
+ expect(core.info).toHaveBeenCalledWith(
+ expect.stringContaining('supersedes the reported attempt 1'),
+ );
+ });
+
it('updates one same-PR comment across repeated source attempts', async () => {
const first = identity();
const harness = githubFixture({value: first});