diff --git a/.github/scripts/playground-blueprint.js b/.github/scripts/playground-blueprint.js new file mode 100644 index 000000000..07cf452d5 --- /dev/null +++ b/.github/scripts/playground-blueprint.js @@ -0,0 +1,48 @@ +/** + * Builds the Playground blueprint for PR preview links. + * + * Usage: node .github/scripts/playground-blueprint.js + * + * Env: + * REPO_NAME - plugin slug in playground.json to replace with the zip URL. + * PR_LABELS - JSON array of PR labels ({ name }). Every `plugin:` + * label adds that WordPress.org plugin to the preview. + * + * Prints the blueprint JSON on stdout; diagnostics go to stderr so they + * never end up inside the link. + */ +const fs = require( 'fs' ); + +const zipUrl = process.argv[ 2 ]; +if ( ! zipUrl ) { + console.error( 'Usage: playground-blueprint.js ' ); + process.exit( 1 ); +} + +const blueprint = JSON.parse( + fs.readFileSync( '.wordpress-org/blueprints/playground.json', 'utf8' ) +); + +blueprint.plugins = blueprint.plugins.map( ( plugin ) => + plugin === process.env.REPO_NAME ? zipUrl : plugin +); + +let labels = []; +try { + labels = JSON.parse( process.env.PR_LABELS || '[]' ); +} catch ( e ) { + console.error( `Could not parse PR labels, adding no extra plugins: ${ e.message }` ); +} + +for ( const { name } of labels ) { + if ( typeof name !== 'string' || ! name.startsWith( 'plugin:' ) ) { + continue; + } + const slug = name.slice( 'plugin:'.length ).trim(); + if ( slug && ! blueprint.plugins.includes( slug ) ) { + blueprint.plugins.push( slug ); + } +} + +console.error( `Playground plugins: ${ blueprint.plugins.join( ', ' ) }` ); +process.stdout.write( JSON.stringify( blueprint ) ); diff --git a/.github/workflows/playground-merged.yml b/.github/workflows/playground-merged.yml index 0a129e50a..80d02c919 100644 --- a/.github/workflows/playground-merged.yml +++ b/.github/workflows/playground-merged.yml @@ -18,19 +18,15 @@ jobs: - name: Prepare blueprint with artifact link id: blueprint + env: + REPO_NAME: ${{ github.event.repository.name }} + # Same plugin: labels as the PR preview (playground.yml). + PR_LABELS: ${{ toJSON(github.event.pull_request.labels) }} run: | - set -euxo pipefail + set -euo pipefail ARTIFACT_URL="${{ github.server_url }}/${{ github.repository }}/archive/refs/heads/develop.zip" - # Use Node.js to parse, modify, and stringify the JSON - BLUEPRINT=$(node -e " - const fs = require('fs'); - const blueprint = JSON.parse(fs.readFileSync('.wordpress-org/blueprints/playground.json', 'utf8')); - blueprint.plugins = blueprint.plugins.map(plugin => - plugin === '${{ github.event.repository.name }}' ? '$ARTIFACT_URL' : plugin - ); - console.log(JSON.stringify(blueprint)); - ") + BLUEPRINT=$(node .github/scripts/playground-blueprint.js "$ARTIFACT_URL") # Base64 encode the blueprint ENCODED_BLUEPRINT=$(echo -n "$BLUEPRINT" | base64 -w 0) diff --git a/.github/workflows/playground.yml b/.github/workflows/playground.yml index cdfc5863c..32f9b0bff 100644 --- a/.github/workflows/playground.yml +++ b/.github/workflows/playground.yml @@ -2,9 +2,21 @@ name: Playground Comment on: pull_request: + # labeled/unlabeled: a `plugin:` label changes which plugins the + # preview installs, so the link has to be rebuilt without a new commit. + types: [opened, synchronize, reopened, labeled, unlabeled] + +# One preview per PR: a newer run (new commit or label) replaces a running one. +concurrency: + group: playground-${{ github.event.number }} + cancel-in-progress: true jobs: playground: + # Other labels (e.g. "needs review") don't change the preview. + if: >- + (github.event.action != 'labeled' && github.event.action != 'unlabeled') + || startsWith(github.event.label.name, 'plugin:') runs-on: ubuntu-latest permissions: contents: read @@ -75,19 +87,19 @@ jobs: - name: Prepare blueprint with artifact link id: blueprint + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + REPO_NAME: ${{ github.event.repository.name }} run: | - set -euxo pipefail + set -euo pipefail ARTIFACT_URL="https://nightly.link/${{ github.repository }}/actions/runs/${{ github.run_id }}/${{ github.event.repository.name }}.zip" - # Use Node.js to parse, modify, and stringify the JSON - BLUEPRINT=$(node -e " - const fs = require('fs'); - const blueprint = JSON.parse(fs.readFileSync('.wordpress-org/blueprints/playground.json', 'utf8')); - blueprint.plugins = blueprint.plugins.map(plugin => - plugin === '${{ github.event.repository.name }}' ? '$ARTIFACT_URL' : plugin - ); - console.log(JSON.stringify(blueprint)); - ") + # Read labels live: on a synchronize run the preview must keep the + # plugin: labels added earlier. + PR_LABELS=$(gh pr view "${{ github.event.number }}" --repo "${{ github.repository }}" --json labels --jq '.labels' || echo '[]') + export PR_LABELS + + BLUEPRINT=$(node .github/scripts/playground-blueprint.js "$ARTIFACT_URL") # Base64 encode the blueprint ENCODED_BLUEPRINT=$(echo -n "$BLUEPRINT" | base64 -w 0) diff --git a/.wordpress-org/blueprints/playground.json b/.wordpress-org/blueprints/playground.json index f1e183750..e570e5a5d 100644 --- a/.wordpress-org/blueprints/playground.json +++ b/.wordpress-org/blueprints/playground.json @@ -4,8 +4,7 @@ "networking": true }, "plugins": [ - "progress-planner", - "wordpress-seo" + "progress-planner" ], "steps": [ {