Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .github/scripts/playground-blueprint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Builds the Playground blueprint for PR preview links.
*
* Usage: node .github/scripts/playground-blueprint.js <plugin-zip-url>
*
* Env:
* REPO_NAME - plugin slug in playground.json to replace with the zip URL.
* PR_LABELS - JSON array of PR labels ({ name }). Every `plugin:<slug>`
* 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 <plugin-zip-url>' );
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 ) );
16 changes: 6 additions & 10 deletions .github/workflows/playground-merged.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,15 @@ jobs:

- name: Prepare blueprint with artifact link
id: blueprint
env:
REPO_NAME: ${{ github.event.repository.name }}
# Same plugin:<slug> 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)
Expand Down
32 changes: 22 additions & 10 deletions .github/workflows/playground.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,21 @@ name: Playground Comment

on:
pull_request:
# labeled/unlabeled: a `plugin:<slug>` 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
Expand Down Expand Up @@ -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:<slug> 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)
Expand Down
3 changes: 1 addition & 2 deletions .wordpress-org/blueprints/playground.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"networking": true
},
"plugins": [
"progress-planner",
"wordpress-seo"
"progress-planner"
],
"steps": [
{
Expand Down
Loading