From 7d56a563fa2ce6df10fa88216cf748bc5e94bf99 Mon Sep 17 00:00:00 2001 From: Vicente Canales Date: Fri, 12 Apr 2024 13:15:21 -0400 Subject: [PATCH] refactor to use github proxy --- .github/workflows/preview-theme.yml | 85 +++++++++++++++++------------ bin/generate-preview-links.sh | 40 ++++++++++---- bin/prepare-zips.sh | 34 ------------ 3 files changed, 79 insertions(+), 80 deletions(-) delete mode 100755 bin/prepare-zips.sh diff --git a/.github/workflows/preview-theme.yml b/.github/workflows/preview-theme.yml index bfc7f4a1da..4ce66455fe 100644 --- a/.github/workflows/preview-theme.yml +++ b/.github/workflows/preview-theme.yml @@ -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 }} \ No newline at end of file + 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}` + }); diff --git a/bin/generate-preview-links.sh b/bin/generate-preview-links.sh index 7bc8aac1fd..0180f9f005 100755 --- a/bin/generate-preview-links.sh +++ b/bin/generate-preview-links.sh @@ -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 <