Skip to content

Commit ba19aa1

Browse files
authored
Add playground + zip comments on every pull request (#80)
1 parent 25a24fc commit ba19aa1

File tree

2 files changed

+138
-0
lines changed

2 files changed

+138
-0
lines changed

.github/workflows/playground.yml

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
name: Playground Comment
2+
3+
on:
4+
pull_request:
5+
6+
jobs:
7+
playground:
8+
runs-on: ubuntu-latest
9+
permissions:
10+
contents: read
11+
pull-requests: write
12+
actions: read
13+
14+
steps:
15+
- uses: actions/checkout@v5
16+
17+
- name: Setup PHP
18+
uses: shivammathur/setup-php@v2
19+
with:
20+
php-version: '8.3'
21+
22+
# Prepare a folder named exactly like the repo as the plugin root.
23+
# If the repo already has such a folder (common for WP plugins), use it.
24+
# Otherwise, stage one and copy root files into it (excluding CI junk).
25+
- name: Prepare plugin folder
26+
id: prep
27+
run: |
28+
set -euxo pipefail
29+
REPO="${{ github.event.repository.name }}"
30+
if [ -d "$REPO" ]; then
31+
# Plugin already lives in a subfolder named like the repo
32+
echo "PKG_DIR=$REPO" >> "$GITHUB_OUTPUT"
33+
else
34+
# Create a clean staging dir to avoid copying into itself
35+
STAGE="${REPO}-pkg"
36+
mkdir -p "$STAGE/$REPO"
37+
rsync -a \
38+
--exclude='.git' \
39+
--exclude='.github' \
40+
--exclude='phpcs.xml.dist' \
41+
--exclude="${STAGE}" \
42+
./ "$STAGE/$REPO/"
43+
echo "PKG_DIR=$STAGE/$REPO" >> "$GITHUB_OUTPUT"
44+
45+
fi
46+
47+
- name: Update plugin version with PR number
48+
run: |
49+
set -euxo pipefail
50+
PLUGIN_FILE="${{ steps.prep.outputs.PKG_DIR }}/plugin.php"
51+
PR_NUMBER="${{ github.event.number }}"
52+
53+
# Extract current version
54+
CURRENT_VERSION=$(grep -o "Version:[[:space:]]*[0-9.]*" "$PLUGIN_FILE" | sed 's/Version:[[:space:]]*//')
55+
56+
# Increment patch version if it exists, otherwise increment minor version
57+
# Handle versions like 2.1.5 or 2.1
58+
if [[ "$CURRENT_VERSION" =~ ^([0-9]+)\.([0-9]+)(\.([0-9]+))?$ ]]; then
59+
MAJOR="${BASH_REMATCH[1]}"
60+
MINOR="${BASH_REMATCH[2]}"
61+
PATCH="${BASH_REMATCH[4]}"
62+
63+
# Build new version with 'b' suffix
64+
if [ -n "$PATCH" ]; then
65+
# If patch exists, increment patch by 1
66+
PATCH=$((PATCH + 1))
67+
NEW_VERSION="${MAJOR}.${MINOR}.${PATCH}b - PR ${PR_NUMBER}"
68+
else
69+
# If no patch, increment minor by 1
70+
MINOR=$((MINOR + 1))
71+
NEW_VERSION="${MAJOR}.${MINOR}b - PR ${PR_NUMBER}"
72+
fi
73+
else
74+
# Fallback: if version format is unexpected, just add .1 and 'b'
75+
NEW_VERSION="${CURRENT_VERSION}.1b - PR ${PR_NUMBER}"
76+
fi
77+
78+
# Replace the version line
79+
sed -i "s/Version:[[:space:]]*[0-9.]*/Version: ${NEW_VERSION}/" "$PLUGIN_FILE"
80+
81+
- name: Prepare blueprint with artifact link
82+
id: blueprint
83+
run: |
84+
set -euxo pipefail
85+
ARTIFACT_URL="https://nightly.link/${{ github.repository }}/actions/runs/${{ github.run_id }}/${{ github.event.repository.name }}.zip"
86+
87+
# Use Node.js to parse, modify, and stringify the JSON
88+
BLUEPRINT=$(node -e "
89+
const fs = require('fs');
90+
const blueprint = JSON.parse(fs.readFileSync('assets/blueprint.json', 'utf8'));
91+
blueprint.plugins = blueprint.plugins.map(plugin =>
92+
plugin === '${{ github.event.repository.name }}' ? '$ARTIFACT_URL' : plugin
93+
);
94+
console.log(JSON.stringify(blueprint));
95+
")
96+
97+
# Base64 encode the blueprint
98+
ENCODED_BLUEPRINT=$(echo -n "$BLUEPRINT" | base64 -w 0)
99+
100+
echo "blueprint=$ENCODED_BLUEPRINT" >> "$GITHUB_OUTPUT"
101+
102+
- name: Run Composer install
103+
run: |
104+
set -euxo pipefail
105+
cd "${{ steps.prep.outputs.PKG_DIR }}"
106+
composer install
107+
108+
# Upload the FOLDER (not a .zip). The artifact service zips it for us,
109+
# keeping the top-level folder name inside the archive.
110+
- name: Upload plugin artifact
111+
uses: actions/upload-artifact@v5
112+
with:
113+
name: ${{ github.event.repository.name }}
114+
path: ${{ steps.prep.outputs.PKG_DIR }}
115+
# Optional: faster uploads for already-compressed assets
116+
compression-level: 0
117+
118+
# Comment with a Playground link that installs from the artifact ZIP
119+
- uses: mshick/add-pr-comment@v2
120+
with:
121+
message: |
122+
**Test on Playground**
123+
[Test this pull request on the Playground](https://playground.wordpress.net/#${{ steps.blueprint.outputs.blueprint }})
124+
or [download the zip](https://nightly.link/${{ github.repository }}/actions/runs/${{ github.run_id }}/${{ github.event.repository.name }}.zip)

assets/blueprint.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"landingPage": "wp-admin/admin.php?page=fair-beacon",
3+
"features": {
4+
"networking": true
5+
},
6+
"plugins": [
7+
"fair-beacon"
8+
],
9+
"steps": [
10+
{
11+
"step": "login"
12+
}
13+
]
14+
}

0 commit comments

Comments
 (0)