Skip to content

Commit 7cc5076

Browse files
committed
Build/Test Tools: Preserve the last docs preview link
A shared proxy failure made previous-preview discovery return nothing. Failure reporting then erased the last successful link even though its assets remained. Recover only the publisher-rendered link and source identity from the existing bot comment. A candidate failure must report the failure without making the previous preview unavailable.
1 parent 65fa146 commit 7cc5076

4 files changed

Lines changed: 103 additions & 2 deletions

File tree

‎.github/scripts/docs-playground-preview/lib/publisher.mjs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,32 @@ export function readPreviewCommentSource( body ) {
281281
return match ? { repository: match[ 1 ], sha: match[ 2 ] } : null;
282282
}
283283

284+
export function readPreviewCommentSuccess( body ) {
285+
if ( typeof body !== 'string' ) {
286+
return null;
287+
}
288+
const link = body.match(
289+
/\[(?:Open Code Reference preview|Latest successful docs preview)\]\((https:\/\/playground\.wordpress\.net\/[^\s)]+)\)/
290+
);
291+
if ( ! link ) {
292+
return null;
293+
}
294+
const previousSource = body.match(
295+
/Latest successful docs preview\]\([^)]+\) — built from \[([0-9a-f]{40})\]\(https:\/\/github\.com\/([A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+)\/commit\/\1\)/
296+
);
297+
const source = previousSource
298+
? { repository: previousSource[ 2 ], sha: previousSource[ 1 ] }
299+
: readPreviewCommentSource( body );
300+
if ( ! source ) {
301+
return null;
302+
}
303+
return {
304+
sourceRepository: source.repository,
305+
sourceSha: source.sha,
306+
publication: { playgroundUrl: link[ 1 ] },
307+
};
308+
}
309+
284310
export function renderPreviewComment( state ) {
285311
if ( state.status === 'ready' ) {
286312
const header = commentHeader( {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
assertLatestAuthorized,
1010
createPublishedMetadata,
1111
inspectHandoff,
12+
readPreviewCommentSuccess,
1213
renderPreviewComment,
1314
validateCandidateFile,
1415
validatePublicationContext,
@@ -152,7 +153,13 @@ async function runIndependently( operations, message ) {
152153
}
153154

154155
async function reportFailure( session, excluded = new Set() ) {
155-
const previous = await findPreviousPreview( session, excluded );
156+
let previous = await findPreviousPreview( session, excluded );
157+
if ( ! previous ) {
158+
const comment = await session.api.findPreviewComment(
159+
session.context.pullRequestNumber
160+
);
161+
previous = readPreviewCommentSuccess( comment?.body );
162+
}
156163
const body = renderPreviewComment( {
157164
status: 'failed',
158165
sourceRepository: session.context.sourceRepository,

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

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import os from 'node:os';
55
import path from 'node:path';
66
import { test } from 'node:test';
77

8-
import { createPublishedMetadata } from '../lib/publisher.mjs';
8+
import {
9+
createPublishedMetadata,
10+
renderPreviewComment,
11+
} from '../lib/publisher.mjs';
912
import {
1013
PLAYGROUND_ORIGIN,
1114
createReuseHandoff,
@@ -375,6 +378,60 @@ test( 'public delivery failure keeps the previous healthy preview', async () =>
375378
assert.match( api.comments[ 0 ].body, new RegExp( 'c'.repeat( 40 ) ) );
376379
} );
377380

381+
test( 'a proxy outage cannot remove the previous comment link', async () => {
382+
const api = new FakeApi();
383+
const oldBytes = Buffer.from( 'old snapshot' );
384+
const oldBuild = buildMetadata( {
385+
sourceSha: 'c'.repeat( 40 ),
386+
workflowRunId: '300',
387+
runUrl: `https://github.com/${ repository }/actions/runs/300`,
388+
snapshotFilename: snapshotAssetName( {
389+
pullRequestNumber: 123,
390+
sourceSha: 'c'.repeat( 40 ),
391+
workflowRunId: 300,
392+
workflowRunAttempt: 1,
393+
} ),
394+
snapshotBytes: oldBytes.byteLength,
395+
snapshotSha256: createHash( 'sha256' )
396+
.update( oldBytes )
397+
.digest( 'hex' ),
398+
} );
399+
const oldPreview = installPublishedPreview( api, oldBuild, oldBytes );
400+
api.comments = [
401+
{
402+
id: 1,
403+
body: renderPreviewComment( {
404+
status: 'ready',
405+
preview: oldPreview,
406+
runUrl: oldBuild.runUrl,
407+
} ),
408+
},
409+
];
410+
411+
const current = buildMetadata();
412+
const directory = await handoffDirectory( current );
413+
await assert.rejects(
414+
publishPullRequest(
415+
options( api, directory, {
416+
fetchImplementation: publicFetch(
417+
api,
418+
new Set( [
419+
oldBuild.snapshotFilename,
420+
current.snapshotFilename,
421+
] )
422+
),
423+
} )
424+
),
425+
/digest/
426+
);
427+
assert.match( api.comments[ 0 ].body, /Latest attempt failed/ );
428+
assert.match( api.comments[ 0 ].body, /Latest successful docs preview/ );
429+
assert.match( api.comments[ 0 ].body, new RegExp( 'c'.repeat( 40 ) ) );
430+
assert.ok(
431+
api.comments[ 0 ].body.includes( oldPreview.publication.playgroundUrl )
432+
);
433+
} );
434+
378435
test( 'a retry cannot delete a same-name preview it did not upload', async () => {
379436
const api = new FakeApi();
380437
const current = buildMetadata();

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
assertLatestAuthorized,
1010
createPublishedMetadata,
1111
inspectHandoff,
12+
readPreviewCommentSuccess,
1213
readPreviewCommentSource,
1314
renderPreviewComment,
1415
validateCandidateFile,
@@ -261,6 +262,11 @@ test( 'the sticky comment has ready, failed, stale, and expired states', () => {
261262
repository: preview.sourceRepository,
262263
sha,
263264
} );
265+
assert.deepEqual( readPreviewCommentSuccess( ready ), {
266+
sourceRepository: preview.sourceRepository,
267+
sourceSha: sha,
268+
publication: { playgroundUrl: preview.publication.playgroundUrl },
269+
} );
264270
assert.equal( ready.match( /Open Code Reference preview/g ).length, 1 );
265271

266272
const failed = renderPreviewComment( {
@@ -273,6 +279,11 @@ test( 'the sticky comment has ready, failed, stale, and expired states', () => {
273279
} );
274280
assert.match( failed, /Latest attempt failed/ );
275281
assert.equal( failed.match( /Latest successful docs preview/g ).length, 1 );
282+
assert.deepEqual( readPreviewCommentSuccess( failed ), {
283+
sourceRepository: preview.sourceRepository,
284+
sourceSha: sha,
285+
publication: { playgroundUrl: preview.publication.playgroundUrl },
286+
} );
276287

277288
const stale = renderPreviewComment( {
278289
status: 'stale',

0 commit comments

Comments
 (0)