Skip to content

Commit dee0420

Browse files
committed
Add live playground links using woocommerce as inspiration
1 parent 7a968e8 commit dee0420

File tree

3 files changed

+228
-0
lines changed

3 files changed

+228
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
name: Build Live Branch
2+
on:
3+
pull_request:
4+
types:
5+
- opened
6+
- reopened
7+
- synchronize
8+
- ready_for_review
9+
paths-ignore:
10+
- 'docs/**'
11+
- 'examples/**'
12+
- 'bin/**'
13+
- '**/tests/**'
14+
- '**/*.md'
15+
- '.github/**'
16+
- '!.github/workflows/pr-build-live-branch.yml'
17+
18+
concurrency:
19+
# Cancel concurrent jobs on pull_request but not push, by including the run_id in the concurrency group for the latter.
20+
group: build-${{ github.event_name == 'push' && github.run_id || 'pr' }}-${{ github.ref }}
21+
cancel-in-progress: true
22+
23+
env:
24+
FORCE_COLOR: 1
25+
26+
permissions: {}
27+
28+
jobs:
29+
build:
30+
if: github.repository_owner == 'Automattic' && github.event.pull_request.draft == false && github.event.pull_request.user.login != 'github-actions[bot]'
31+
runs-on: ubuntu-latest
32+
permissions:
33+
contents: write
34+
pull-requests: write
35+
repository-projects: write
36+
id-token: write
37+
steps:
38+
- uses: actions/checkout@v4
39+
with:
40+
fetch-depth: 0
41+
42+
- name: Get current version
43+
id: version
44+
uses: actions/[email protected]
45+
with:
46+
script:
47+
const { getVersion } = require( './.github/workflows/scripts/get-plugin-version' );
48+
const version = await getVersion( 'remote-data-blocks' );
49+
core.setOutput( 'version', version );
50+
51+
- name: Prepare plugin zips
52+
id: prepare
53+
env:
54+
CURRENT_VERSION: ${{ steps.version.outputs.version }}
55+
run: |
56+
57+
# Current version must compare greater than any previously used current version for this PR.
58+
# Assume GH run IDs are monotonic.
59+
VERSUFFIX="${GITHUB_RUN_ID}-g$(git rev-parse --short HEAD)"
60+
61+
CURRENT_VERSION="$CURRENT_VERSION-$VERSUFFIX"
62+
63+
sed -i -e 's/Version: .*$/Version: '"$CURRENT_VERSION"'/' "$GITHUB_WORKSPACE/plugins/remote-data-blocks/remote-data-blocks.php"
64+
echo "$CURRENT_VERSION" > "$GITHUB_WORKSPACE/plugins/remote-data-blocks/version.txt"
65+
66+
cd "$GITHUB_WORKSPACE/plugins/remote-data-blocks"
67+
PLUGIN_SLUG='remote-data-blocks' bash bin/build-zip.sh
68+
69+
mkdir "$GITHUB_WORKSPACE/zips"
70+
mv "$GITHUB_WORKSPACE/plugins/remote-data-blocks/remote-data-blocks.zip" "$GITHUB_WORKSPACE/zips/remote-data-blocks.zip"
71+
72+
mkdir -p "$GITHUB_WORKSPACE/unzips/remote-data-blocks"
73+
cd "$GITHUB_WORKSPACE/zips"
74+
unzip -qq remote-data-blocks.zip
75+
mv "$GITHUB_WORKSPACE/zips/remote-data-blocks" "$GITHUB_WORKSPACE/unzips/remote-data-blocks/remote-data-blocks"
76+
77+
# Plugin data is passed as a JSON object.
78+
PLUGIN_DATA="{}"
79+
PLUGIN_DATA=$( jq -c --arg slug "remote-data-blocks" --arg ver "$CURRENT_VERSION" '.[ $slug ] = { version: $ver }' <<<"$PLUGIN_DATA" )
80+
echo "plugin-data=$PLUGIN_DATA" >> $GITHUB_OUTPUT
81+
82+
- name: Create plugins artifact
83+
uses: actions/upload-artifact@v4
84+
if: steps.prepare.outputs.plugin-data != '{}'
85+
with:
86+
name: plugins
87+
path: zips
88+
# Only need to retain for a day since the beta builder slurps it up to distribute.
89+
retention-days: 1
90+
91+
- name: Create playground artifact
92+
uses: actions/upload-artifact@v4
93+
if: steps.prepare.outputs.plugin-data != '{}'
94+
with:
95+
name: plugins-${{ github.run_id }}
96+
path: unzips/remote-data-blocks
97+
retention-days: 30
98+
99+
- name: Comment on PR with WordPress Playground details
100+
uses: actions/github-script@v7
101+
if: steps.prepare.outputs.plugin-data != '{}' && ! github.event.pull_request.head.repo.fork
102+
with:
103+
script: |
104+
const { run } = require('./.github/workflows/scripts/generate-playground-blueprint');
105+
run({ github, context, core });
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
const https = require( 'https' );
2+
3+
const generateWordpressPlaygroundBlueprint = ( runId, prNumber ) => {
4+
const defaultSchema = {
5+
schema: 'https://playground.wordpress.net/blueprint-schema.json',
6+
7+
meta: {
8+
title: 'Remote Data Blocks (PR)',
9+
description: 'Installs a remote-data-blocks plugin PR to WordPress Playground',
10+
author: 'WordPress VIP',
11+
categories: [ 'Content' ],
12+
},
13+
14+
features: {
15+
networking: true,
16+
},
17+
18+
landingPage: '/wp-admin/admin.php?page=remote-data-blocks-settings',
19+
20+
login: true,
21+
22+
preferredVersions: {
23+
php: '8.2',
24+
wp: 'latest',
25+
},
26+
27+
steps: [
28+
{
29+
step: 'setSiteOptions',
30+
options: {
31+
blogname: 'Remote Data Blocks (PR)',
32+
blogdescription: 'Explore the Remote Data Blocks plugin in a WordPress Playground',
33+
},
34+
},
35+
{
36+
step: 'defineWpConfigConsts',
37+
consts: {
38+
USE_PLAYGROUND_CORS_PROXY: true,
39+
},
40+
},
41+
{
42+
step: 'installPlugin',
43+
pluginZipFile: {
44+
resource: 'url',
45+
url: `https://playground.wordpress.net/plugin-proxy.php?org=Automattice&repo=remote-data-blocks&workflow=Build%20Live%20Branch&artifact=plugins-${ runId }&pr=${ prNumber }`,
46+
},
47+
options: {
48+
activate: true,
49+
targetFolderName: 'remote-data-blocks',
50+
},
51+
},
52+
],
53+
};
54+
55+
return defaultSchema;
56+
};
57+
58+
async function run( { github, context, core } ) {
59+
const commentInfo = {
60+
owner: context.repo.owner,
61+
repo: context.repo.repo,
62+
issue_number: context.issue.number,
63+
};
64+
65+
const comments = ( await github.rest.issues.listComments( commentInfo ) )
66+
.data;
67+
let existingCommentId = null;
68+
69+
for ( const currentComment of comments ) {
70+
if (
71+
currentComment.user.type === 'Bot' &&
72+
currentComment.body.includes( 'Test using WordPress Playground' )
73+
) {
74+
existingCommentId = currentComment.id;
75+
break;
76+
}
77+
}
78+
79+
const defaultSchema = generateWordpressPlaygroundBlueprint(
80+
context.runId,
81+
context.issue.number
82+
);
83+
84+
const url = `https://playground.wordpress.net/#${ JSON.stringify(
85+
defaultSchema
86+
) }`;
87+
88+
const body = `
89+
## Test using WordPress Playground
90+
The changes in this pull request can be previewed and tested using a [WordPress Playground](https://developer.wordpress.org/playground/) instance.
91+
[WordPress Playground](https://developer.wordpress.org/playground/) is an experimental project that creates a full WordPress instance entirely within the browser.
92+
93+
[Test this pull request with WordPress Playground](${ url }).
94+
95+
Note that this URL is valid for 30 days from when this comment was last updated. You can update it by closing/reopening the PR or pushing a new commit.
96+
`;
97+
98+
if ( existingCommentId ) {
99+
await github.rest.issues.updateComment( {
100+
owner: commentInfo.owner,
101+
repo: commentInfo.repo,
102+
comment_id: existingCommentId,
103+
body: body,
104+
} );
105+
} else {
106+
commentInfo.body = body;
107+
await github.rest.issues.createComment( commentInfo );
108+
}
109+
}
110+
111+
module.exports = { run };
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const { readFile } = require( 'fs/promises' );
2+
const { join } = require( 'path' );
3+
4+
exports.getVersion = async plugin => {
5+
const filePath = join(
6+
process.env.GITHUB_WORKSPACE,
7+
`plugins/${ plugin }/${ plugin }.php`
8+
);
9+
const pluginFileContents = await readFile( filePath, 'utf8' );
10+
const versionMatch = pluginFileContents.match( /Version: (\d+\.\d+\.\d+.*)\n/m );
11+
return versionMatch && versionMatch[1];
12+
};

0 commit comments

Comments
 (0)