Skip to content

Generate playground previews for theme changes #36

Generate playground previews for theme changes

Generate playground previews for theme changes #36

Workflow file for this run

name: Preview Theme Changes
on:
pull_request:
jobs:
check-for-changes-to-themes:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Retrieved Theme Changes
id: check-for-changes
run: |
# Retrieve list of all changed files
git fetch origin trunk:trunk
changed_files=$(git diff --name-only HEAD origin/trunk)
# Loop through changed files and identify parent directories
declare -A unique_dirs
for file in $changed_files; do
dir_name=$(dirname "$file")
echo $dir_name
if [[ -f "$dir_name/style.css" ]]; then # Check if the directory contains a theme
unique_dirs["$dir_name"]=1 # Use associative array to keep directories unique
echo $unique_dirs
fi
done
# Check if any themes have changed
if [[ ${#unique_dirs[@]} -eq 0 ]]; then
echo "No themes have changed"
echo "HAS_THEME_CHANGES=false" >> $GITHUB_OUTPUT
exit 78 # Exit with neutral status code
fi
echo "HAS_THEME_CHANGES=true" >> $GITHUB_OUTPUT
# Generate preview links for each theme
preview_links=()
for dir in "${!unique_dirs[@]}"; do
theme_slug=$(basename "$dir")
preview_links+=($(bash ./bin/generate-preview-link.sh "$theme_slug" "${{ github.event.pull_request.head.ref }}"))
done
# parse preview links array into a json array, and minify the output into a single line
preview_links=$(jq --compact-output --null-input '$ARGS.positional' --args -- "${preview_links[@]}")
echo $preview_links
echo "PREVIEW_LINKS=$preview_links" >> $GITHUB_ENV
- name: Comment on PR
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 previewLinks = JSON.parse(process.env.PREVIEW_LINKS);
console.log(previewLinks)
const comment = previewLinks.join('\n');
// Check if a comment already exists
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 links for theme changes:'));
if (existingComment) {
await github.rest.issues.updateComment({
comment_id: existingComment.id,
owner: context.repo.owner,
repo: context.repo.repo,
body: `Preview links for theme changes:\n${comment}`
});
return;
}
github.rest.issues.createComment({
issue_number: context.payload.pull_request.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `Preview links for theme changes:\n${comment}`
});