Skip to content

Commit 7670d3b

Browse files
committed
Build/Test Tools: Enforce fork preview validation
Fork pull_request runs cannot read the base repository's Actions variables. The old check silently left every fork in advisory mode, so DOCS_PREVIEW_ENFORCE did not enforce anything.\n\nApply the exact base-repository value in the trusted publisher after it records the failure and preserves the live preview.
1 parent b8059b0 commit 7670d3b

5 files changed

Lines changed: 52 additions & 10 deletions

File tree

‎.github/docs-playground-preview/README.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ The staging repository needs a `docs-preview` label and the Actions variables us
5656

5757
Behavioral validation is advisory unless the repository Actions variable `DOCS_PREVIEW_ENFORCE` is exactly `true`. Advisory failures emit workflow warnings and terminal PR state but cannot publish or replace a snapshot. Fatal build, handoff, identity, digest, size, and publication failures always fail.
5858

59+
GitHub does not expose base-repository Actions variables to an untrusted fork `pull_request` job. Such a job therefore records a behavioral failure in its handoff without enforcing it locally. The trusted publisher reads `DOCS_PREVIEW_ENFORCE` from the base repository, updates the sticky comment without publishing the invalid candidate, and then fails its workflow when enforcement is enabled. Same-repository and `trunk` builds can read the variable directly and fail their build workflow as well.
60+
5961
To enable enforcement:
6062

6163
1. Open repository **Settings**.

‎.github/scripts/docs-playground-preview/publish.mjs‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -366,17 +366,30 @@ export async function publishPullRequest( options ) {
366366
}
367367
}
368368

369+
export function enforceValidationResult( result, enforcementVariable ) {
370+
if ( result.status === 'invalid' && enforcementVariable === 'true' ) {
371+
throw new Error(
372+
'Code Reference validation failed with DOCS_PREVIEW_ENFORCE enabled.'
373+
);
374+
}
375+
return result;
376+
}
377+
369378
async function main() {
370379
const event = JSON.parse( await readFile( process.env.GITHUB_EVENT_PATH ) );
371-
const result = await publishPullRequest( {
372-
repository: process.env.GITHUB_REPOSITORY,
373-
stagingVariable: process.env.DOCS_PREVIEW_STAGING,
374-
triggerRunId: event.workflow_run.id,
375-
triggerRunAttempt: event.workflow_run.run_attempt,
376-
handoffDirectory: process.argv[ 2 ] || 'handoff',
377-
artifactAvailable: process.env.HANDOFF_DOWNLOAD_RESULT === 'success',
378-
token: process.env.GITHUB_TOKEN,
379-
} );
380+
const result = enforceValidationResult(
381+
await publishPullRequest( {
382+
repository: process.env.GITHUB_REPOSITORY,
383+
stagingVariable: process.env.DOCS_PREVIEW_STAGING,
384+
triggerRunId: event.workflow_run.id,
385+
triggerRunAttempt: event.workflow_run.run_attempt,
386+
handoffDirectory: process.argv[ 2 ] || 'handoff',
387+
artifactAvailable:
388+
process.env.HANDOFF_DOWNLOAD_RESULT === 'success',
389+
token: process.env.GITHUB_TOKEN,
390+
} ),
391+
process.env.DOCS_PREVIEW_ENFORCE
392+
);
380393
process.stdout.write( `Code Reference publisher: ${ result.status }.\n` );
381394
}
382395

‎.github/scripts/docs-playground-preview/test/config.test.mjs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,8 @@ test( 'only the untrusted staging pull-request build bypasses staging activation
220220
/github\.repository == 'sirreal\/wordpress-develop' && vars\.DOCS_PREVIEW_STAGING == 'true'/
221221
);
222222
}
223+
assert.match(
224+
publishWorkflow,
225+
/DOCS_PREVIEW_ENFORCE: \$\{\{ vars\.DOCS_PREVIEW_ENFORCE \}\}/
226+
);
223227
} );

‎.github/scripts/docs-playground-preview/test/publish.test.mjs‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ import {
1616
releaseAssetUrl,
1717
snapshotAssetName,
1818
} from '../lib/publication.mjs';
19-
import { publishPullRequest } from '../publish.mjs';
19+
import {
20+
enforceValidationResult,
21+
publishPullRequest,
22+
} from '../publish.mjs';
2023

2124
const repository = 'WordPress/wordpress-develop';
2225
const sourceRepository = 'contributor/wordpress-develop';
@@ -301,6 +304,20 @@ test( 'an advisory validation failure updates the comment but publishes nothing'
301304
assert.equal( api.labelRemovals, 1 );
302305
} );
303306

307+
test( 'the trusted publisher enforces invalid fork handoffs', () => {
308+
const invalid = { status: 'invalid' };
309+
assert.throws(
310+
() => enforceValidationResult( invalid, 'true' ),
311+
/DOCS_PREVIEW_ENFORCE/
312+
);
313+
assert.equal( enforceValidationResult( invalid, 'false' ), invalid );
314+
assert.equal( enforceValidationResult( invalid, 'TRUE' ), invalid );
315+
assert.equal(
316+
enforceValidationResult( { status: 'ready' }, 'true' ).status,
317+
'ready'
318+
);
319+
} );
320+
304321
test( 'a missing handoff always fails after its terminal comment', async () => {
305322
const api = new FakeApi();
306323
const directory = await mkdtemp(

‎.github/workflows/docs-playground-preview-publish.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,13 @@ jobs:
5454
digest-mismatch: error
5555

5656
- name: Validate and publish the handoff
57+
# Fork pull_request jobs cannot read the base repository's Actions
58+
# variables. The trusted publisher therefore applies enforcement
59+
# after reporting the invalid candidate and preserving the live link.
60+
# Administrator: enable enforcement in Settings > Secrets and variables >
61+
# Actions > Variables by creating or updating DOCS_PREVIEW_ENFORCE to true.
5762
env:
63+
DOCS_PREVIEW_ENFORCE: ${{ vars.DOCS_PREVIEW_ENFORCE }}
5864
DOCS_PREVIEW_STAGING: ${{ vars.DOCS_PREVIEW_STAGING }}
5965
GITHUB_TOKEN: ${{ github.token }}
6066
HANDOFF_DOWNLOAD_RESULT: ${{ steps.download.outcome }}

0 commit comments

Comments
 (0)