Skip to content

Commit

Permalink
refactor to use github proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Vicente Canales committed Apr 12, 2024
1 parent 31c2de5 commit 7d56a56
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 80 deletions.
85 changes: 50 additions & 35 deletions .github/workflows/preview-theme.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,63 @@ on:
push:
branches:
- add/playground-theme-preview
pull_request:

jobs:
prepare-and-upload-zips:
check-for-changes-to-themes:
runs-on: ubuntu-latest
outputs:
zip_paths: ${{ steps.prepare_zips.outputs.zip_paths }}
has_artifacts: ${{ steps.prepare_zips.outputs.zip_paths != '' && 'true' || 'false' }}
steps:
- name: Checkout Code
- name: Checkout
uses: actions/checkout@v2

- name: Prepare Zips and Determine Paths
id: prepare_zips
run: bash bin/prepare-zips.sh

# Debugging: Print the ZIP paths generated
- name: Print ZIP Paths
- name: Retrieved Theme Changes
id: check-for-changes
run: |
echo "ZIP Paths: ${{ steps.prepare_zips.outputs.zip_paths }}"
# 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
- name: Upload Zips as Artifacts
if: steps.prepare_zips.outputs.zip_paths != ''
uses: actions/upload-artifact@v2
with:
name: theme-zips
path: ${{ steps.prepare_zips.outputs.zip_paths }}
# 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
generate-and-post-preview-links:
needs: prepare-and-upload-zips
if: needs.prepare-and-upload-zips.outputs.has_artifacts == 'true'
runs-on: ubuntu-latest
steps:
- name: Download Artifacts
uses: actions/download-artifact@v2
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
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@v3
with:
name: theme-zips

- name: Checkout Code
uses: actions/checkout@v2

- name: Generate Preview Links
id: generate_links
run: bash bin/generate-preview-links.sh "${{ github.event.pull_request.number }}" "${{ needs.prepare-and-upload-zips.outputs.zip_paths }}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const previewLinks = process.env.PREVIEW_LINKS.split(' ');
const comment = previewLinks.map(link => `- [Preview](${link})`).join('\n');
github.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: `Preview links for theme changes:\n${comment}`
});
40 changes: 29 additions & 11 deletions bin/generate-preview-links.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
#!/bin/bash

pr_number=$1
zip_paths=$2
# Parse args
theme_slug=$1
branch=$2

# Placeholder for generating preview links logic
# Assuming each zip_path has been processed to generate a corresponding preview link
# Generate the blueprint
blueprint=$(cat <<EOF
{
"steps": [
{ "step": "login", "username": "admin", "password": "password" },
{
"step": "installTheme",
"themeZipFile": {
"resource": "url",
"url": "https://github-proxy.com/partial/Automattic/themes/$theme_slug?branch=$branch"
}
},
{
"step": "activateTheme",
"themeSlug": "$theme_slug"
}
]
}
EOF
)

# Minify the blueprint
minified_blueprint=$(echo "$blueprint" | jq -c -M '.')

# Return the preview link.
echo "- [Preview changes for $theme_slug](https://playground.wordpress.net/#$minified_blueprint)"

# Using jq to format the JSON body for the comment
preview_links_json=$(jq -n --arg links "$preview_links" '{body: $links}')

# Posting the comment using GitHub API
curl -s -H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
-d "$preview_links_json" \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/${pr_number}/comments"
34 changes: 0 additions & 34 deletions bin/prepare-zips.sh

This file was deleted.

0 comments on commit 7d56a56

Please sign in to comment.