Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Theme preview workflow: use external github action #232

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
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
84 changes: 13 additions & 71 deletions .github/workflows/preview-theme.yml
Original file line number Diff line number Diff line change
@@ -1,81 +1,23 @@
name: Preview Theme Changes

on:
pull_request:
pull_request_target:
types: [opened, synchronize]
permissions:
pull-requests: write

jobs:
check-for-changes-to-themes:
preview-theme-changes:
runs-on: ubuntu-latest
steps:
- name: Check if PR is from a fork
run: |
if [ "${{ github.event.pull_request.head.repo.fork }}" == "true" ]; then
echo "This pull request is from a fork. Skipping the action."
exit 0
fi
- name: Checkout PR Head
- name: Checkout PR HEAD
uses: actions/checkout@v2
with:
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
- name: Retrieved Theme Changes
id: check-for-changes
run: |
# Retrieve list of all changed files
git fetch origin ${{ github.event.pull_request.base.ref }}:${{ github.event.pull_request.base.ref }}
changed_files=$(git diff --name-only HEAD origin/${{ github.event.pull_request.base.ref }})

# Loop through changed files and identify parent directories
declare -A unique_dirs
for file in $changed_files; do
dir_name=$(dirname "$file")
while [[ "$dir_name" != "." ]]; do
if [[ -f "$dir_name/style.css" ]]; then # Check if the parent directory contains a theme
# Save only the basename
unique_dirs[$dir_name]=$(basename $dir_name)
break
fi
dir_name=$(dirname "$dir_name")
done
done
# Check if themes have changed
if [[ ${#unique_dirs[@]} -eq 0 ]]; then
echo "No theme changes detected"
echo "HAS_THEME_CHANGES=false" >> $GITHUB_OUTPUT
exit 0 # Use exit code 0 for successful completion
fi
# Output list of theme slugs with changes
echo "HAS_THEME_CHANGES=true" >> $GITHUB_OUTPUT
echo "CHANGED_THEMES=$(echo ${unique_dirs[@]})" >> $GITHUB_ENV
echo "Theme directories with changes: $CHANGED_THEMES"
- name: Add Preview Links comment
id: comment-on-pr
if: ${{ steps.check-for-changes.outputs.HAS_THEME_CHANGES == 'true' }}
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const createPreviewLinks = require('.github/scripts/create-preview-links');
createPreviewLinks(github, context, process.env.CHANGED_THEMES);
- name: Remove comment if no changes are detected
if: ${{ steps.check-for-changes.outputs.HAS_THEME_CHANGES == 'false' }}
uses: actions/github-script@v7
with:
ref: ${{ github.event.pull_request.head.sha }}

- name: Preview Theme Changes
uses: WordPress/action-wp-playground-pr-preview@trunk
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { data: comments } = await github.rest.issues.listComments({
issue_number: context.payload.pull_request.number,
owner: context.repo.owner,
repo: context.repo.repo
});

const existingComment = comments.find(comment => comment.user.login === 'github-actions[bot]' && comment.body.startsWith('### Preview changes'));

if (existingComment) {
await github.rest.issues.deleteComment({
comment_id: existingComment.id,
owner: context.repo.owner,
repo: context.repo.repo
});
}

ref: ${{ github.event.pull_request.head.sha }}
base-branch: trunk