diff --git a/.github/scripts/create-preview-links.js b/.github/scripts/create-preview-links.js index c07ceeea..6c342593 100644 --- a/.github/scripts/create-preview-links.js +++ b/.github/scripts/create-preview-links.js @@ -65,6 +65,26 @@ function getParentThemeName(themeSlug) { : ''; } +async function createPlaygroundPreconfiguredLink(themeName = '', blueprint = '', existingPr = false) { + const url = new URL('https://wordpress.org/playground/'); + const searchParams = new URLSearchParams(); + + searchParams.set('storage', 'browser'); // We need to have the storage to be able to create/update the PR + searchParams.set('ghexport-repo-url', encodeURIComponent('https://github.com/WordPress/community-themes')); + searchParams.set('ghexport-content-type', 'theme'); + searchParams.set('ghexport-theme', themeName); + + if ( existingPr ) { + searchParams.set('ghexport-pr-action', 'update'); + searchParams.set('ghexport-pr-number', existingPr); // It's not supported yet - see PR WordPress/wordpress-playground# TODO + } + + url.search = searchParams.toString(); + url.hash = encodeURIComponent(blueprint); + + return url.toString(); +} + /* * This function creates a comment on a PR with preview links for the changed themes. * It is used by `preview-theme` workflow. @@ -78,16 +98,15 @@ async function createPreviewLinksComment(github, context, changedThemeSlugs) { const previewLinks = changedThemes .map((themeSlug) => { const parentThemeName = getParentThemeName(themeSlug); + const themeName = getThemeName( themeSlug ); + const blueprint = createBlueprint( themeSlug, context.payload.pull_request.head.ref ); + const playgroundUrl = createPlaygroundPreconfiguredLink( themeName, blueprint, context.payload.pull_request.number ); + const note = parentThemeName ? ` (child theme of **${parentThemeName}**)` : ''; - return `- [Preview changes for **${getThemeName( - themeSlug - )}**](https://playground.wordpress.net/#${createBlueprint( - themeSlug, - context.payload.pull_request.head.ref - )})${note}`; + return `- [Preview changes for **${ themeName }**](${playgroundUrl})${note}`; }) .join('\n');