From d531cdb90a1419f04c905644ebcf034dd3849340 Mon Sep 17 00:00:00 2001 From: MaggieCabrera Date: Mon, 15 Apr 2024 11:13:50 +0200 Subject: [PATCH 1/9] added playground workflows --- .github/scripts/create-preview-links.js | 112 ++++++++++++++++++++++++ .github/workflows/preview-theme.yml | 71 +++++++++++++++ atlas/patterns/hidden-404.php | 1 + poetry/patterns/header.php | 1 + 4 files changed, 185 insertions(+) create mode 100644 .github/scripts/create-preview-links.js create mode 100644 .github/workflows/preview-theme.yml diff --git a/.github/scripts/create-preview-links.js b/.github/scripts/create-preview-links.js new file mode 100644 index 00000000..f478a63c --- /dev/null +++ b/.github/scripts/create-preview-links.js @@ -0,0 +1,112 @@ +const fs = require('fs'); + +/* + * This function creates a WordPress Playground blueprint JSON string for a theme. + * + * @param {string} themeSlug - The slug of the theme to create a blueprint for. + * @param {string} branch - The branch where the theme changes are located. + * @returns {string} - A JSON string representing the blueprint. + */ +function createBlueprint(themeSlug, branch) { + const template = { + steps: [ + { + step: 'login', + username: 'admin', + password: 'password', + }, + { + step: 'installTheme', + themeZipFile: { + resource: 'url', + url: `https://github-proxy.com/partial/Wordpress/community-themes/${themeSlug}?branch=${branch}`, + }, + }, + { + step: 'activateTheme', + themeFolderName: themeSlug, + }, + ], + }; + + return JSON.stringify(template); +} + +/* + * This function reads the `style.css` file of a theme and returns the theme name. + * + * @param {string} themeSlug - The slug of the theme to get the name of. + * @returns {string} - The name of the theme as defined in the `style.css` file. + */ +function getThemeName(themeSlug) { + const styleCss = fs.readFileSync(`${themeSlug}/style.css`, 'utf8'); + const themeName = styleCss.match(/Theme Name:(.*)/i)[1].trim(); + return themeName; +} + +/* + * This function creates a comment on a PR with preview links for the changed themes. + * It is used by `preview-theme` workflow. + * + * @param {object} github - An authenticated instance of the GitHub API. + * @param {object} context - The context of the event that triggered the action. + * @param {string} changedThemeSlugs - A space-separated string of theme slugs that have changed. + */ +async function createPreviewLinksComment(github, context, changedThemeSlugs) { + const changedThemes = changedThemeSlugs.split(' '); + const previewLinks = changedThemes + .map((themeSlug) => { + return `- [Preview changes for **${getThemeName( + themeSlug + )}**](https://playground.wordpress.net/#${createBlueprint( + themeSlug, + context.payload.pull_request.head.ref + )})`; + }) + .join('\n'); + const comment = ` +I've detected changes to the following themes in this PR: ${changedThemes + .map((themeSlug) => getThemeName(themeSlug)) + .join(', ')}. +You can preview these changes by following the links below: +${previewLinks} +I will update this comment with the latest preview links as you push more changes to this PR. +**⚠️ Note:** The preview sites are created using [WordPress Playground](https://wordpress.org/playground/). You can add content, edit settings, and test the themes as you would on a real site, but please note that changes are not saved between sessions. +`; + + const repoData = { + owner: context.repo.owner, + repo: context.repo.repo, + }; + + // Check if a comment already exists and update it if it does + const { data: comments } = await github.rest.issues.listComments({ + issue_number: context.payload.pull_request.number, + ...repoData, + }); + const existingComment = comments.find( + (comment) => + comment.user.login === 'github-actions[bot]' && + comment.body.startsWith('### Preview changes') + ); + const commentObject = { + body: `### Preview changes\n${comment}`, + ...repoData, + }; + + if (existingComment) { + await github.rest.issues.updateComment({ + comment_id: existingComment.id, + ...commentObject, + }); + return; + } + + // Create a new comment if one doesn't exist + github.rest.issues.createComment({ + issue_number: context.payload.pull_request.number, + ...commentObject, + }); +} + +module.exports = createPreviewLinksComment; \ No newline at end of file diff --git a/.github/workflows/preview-theme.yml b/.github/workflows/preview-theme.yml new file mode 100644 index 00000000..a1f56a57 --- /dev/null +++ b/.github/workflows/preview-theme.yml @@ -0,0 +1,71 @@ +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 + # save only the basename + unique_dirs[$dir_name]=$(basename $dir_name) + echo $unique_dirs + fi + 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 78 # Use neutral exit code + 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_THEME_DIRS" + - 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: + 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 + }); + } + \ No newline at end of file diff --git a/atlas/patterns/hidden-404.php b/atlas/patterns/hidden-404.php index 96beb939..3a0bd4c9 100644 --- a/atlas/patterns/hidden-404.php +++ b/atlas/patterns/hidden-404.php @@ -4,6 +4,7 @@ * Slug: atlas/hidden-404 * Inserter: no */ +//test ?> diff --git a/poetry/patterns/header.php b/poetry/patterns/header.php index 8f882573..da2300df 100644 --- a/poetry/patterns/header.php +++ b/poetry/patterns/header.php @@ -5,6 +5,7 @@ * Categories: hidden * Inserter: no */ +//test ?>
From 402063ca0148b0ead59560ce0904a3da4f95fb1d Mon Sep 17 00:00:00 2001 From: MaggieCabrera Date: Mon, 15 Apr 2024 11:16:09 +0200 Subject: [PATCH 2/9] testing change --- atlas/patterns/hidden-404.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atlas/patterns/hidden-404.php b/atlas/patterns/hidden-404.php index 3a0bd4c9..9f2cde03 100644 --- a/atlas/patterns/hidden-404.php +++ b/atlas/patterns/hidden-404.php @@ -4,7 +4,7 @@ * Slug: atlas/hidden-404 * Inserter: no */ -//test +//test ?> From 2f428fa1333eb379a1fa77408e5c8a25e404887c Mon Sep 17 00:00:00 2001 From: MaggieCabrera Date: Mon, 15 Apr 2024 11:18:11 +0200 Subject: [PATCH 3/9] testing change --- atlas/patterns/hidden-404.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atlas/patterns/hidden-404.php b/atlas/patterns/hidden-404.php index 9f2cde03..3a0bd4c9 100644 --- a/atlas/patterns/hidden-404.php +++ b/atlas/patterns/hidden-404.php @@ -4,7 +4,7 @@ * Slug: atlas/hidden-404 * Inserter: no */ -//test +//test ?> From 554d3c719e406125c7bfef79bb504ea590cad26a Mon Sep 17 00:00:00 2001 From: MaggieCabrera Date: Mon, 15 Apr 2024 11:20:21 +0200 Subject: [PATCH 4/9] testing change --- atlas/patterns/hidden-404.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atlas/patterns/hidden-404.php b/atlas/patterns/hidden-404.php index 3a0bd4c9..9f2cde03 100644 --- a/atlas/patterns/hidden-404.php +++ b/atlas/patterns/hidden-404.php @@ -4,7 +4,7 @@ * Slug: atlas/hidden-404 * Inserter: no */ -//test +//test ?> From 831b515308f1df2d3c769048fdcba01e7cf800aa Mon Sep 17 00:00:00 2001 From: MaggieCabrera Date: Mon, 15 Apr 2024 12:32:53 +0200 Subject: [PATCH 5/9] check previous folders if changed files are not in the root --- .github/workflows/preview-theme.yml | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/.github/workflows/preview-theme.yml b/.github/workflows/preview-theme.yml index a1f56a57..9039de9b 100644 --- a/.github/workflows/preview-theme.yml +++ b/.github/workflows/preview-theme.yml @@ -20,13 +20,15 @@ jobs: # 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 - # save only the basename - unique_dirs[$dir_name]=$(basename $dir_name) - echo $unique_dirs + 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 From 5d50c4dba2802790cf077a2e8f8624c85437c35c Mon Sep 17 00:00:00 2001 From: MaggieCabrera Date: Mon, 15 Apr 2024 12:38:47 +0200 Subject: [PATCH 6/9] added spacing to action text --- .github/scripts/create-preview-links.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/scripts/create-preview-links.js b/.github/scripts/create-preview-links.js index f478a63c..807373bb 100644 --- a/.github/scripts/create-preview-links.js +++ b/.github/scripts/create-preview-links.js @@ -68,9 +68,12 @@ async function createPreviewLinksComment(github, context, changedThemeSlugs) { I've detected changes to the following themes in this PR: ${changedThemes .map((themeSlug) => getThemeName(themeSlug)) .join(', ')}. + You can preview these changes by following the links below: ${previewLinks} + I will update this comment with the latest preview links as you push more changes to this PR. + **⚠️ Note:** The preview sites are created using [WordPress Playground](https://wordpress.org/playground/). You can add content, edit settings, and test the themes as you would on a real site, but please note that changes are not saved between sessions. `; From f5df1650daa359b99d66b3339f5fcfe2f78e2286 Mon Sep 17 00:00:00 2001 From: Maggie Date: Mon, 15 Apr 2024 17:30:30 +0200 Subject: [PATCH 7/9] Update .github/workflows/preview-theme.yml Co-authored-by: Vicente Canales <1157901+vcanales@users.noreply.github.com> --- .github/workflows/preview-theme.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/preview-theme.yml b/.github/workflows/preview-theme.yml index 9039de9b..bf82cb33 100644 --- a/.github/workflows/preview-theme.yml +++ b/.github/workflows/preview-theme.yml @@ -39,7 +39,7 @@ jobs: # 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_THEME_DIRS" + 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' }} From d0c667936449e3eac164eab23692ddf51ffd7c54 Mon Sep 17 00:00:00 2001 From: MaggieCabrera Date: Mon, 15 Apr 2024 17:31:42 +0200 Subject: [PATCH 8/9] removed tests changes --- atlas/patterns/hidden-404.php | 1 - poetry/patterns/header.php | 1 - 2 files changed, 2 deletions(-) diff --git a/atlas/patterns/hidden-404.php b/atlas/patterns/hidden-404.php index 9f2cde03..96beb939 100644 --- a/atlas/patterns/hidden-404.php +++ b/atlas/patterns/hidden-404.php @@ -4,7 +4,6 @@ * Slug: atlas/hidden-404 * Inserter: no */ -//test ?> diff --git a/poetry/patterns/header.php b/poetry/patterns/header.php index da2300df..8f882573 100644 --- a/poetry/patterns/header.php +++ b/poetry/patterns/header.php @@ -5,7 +5,6 @@ * Categories: hidden * Inserter: no */ -//test ?>
From 253a4ddcf3151ab732ec6f0b4270802bf711dcaa Mon Sep 17 00:00:00 2001 From: MaggieCabrera Date: Mon, 15 Apr 2024 17:38:59 +0200 Subject: [PATCH 9/9] pass checks if no themes are changed --- .github/workflows/preview-theme.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/preview-theme.yml b/.github/workflows/preview-theme.yml index bf82cb33..32110a34 100644 --- a/.github/workflows/preview-theme.yml +++ b/.github/workflows/preview-theme.yml @@ -34,7 +34,7 @@ jobs: if [[ ${#unique_dirs[@]} -eq 0 ]]; then echo "No theme changes detected" echo "HAS_THEME_CHANGES=false" >> $GITHUB_OUTPUT - exit 78 # Use neutral exit code + exit 0 # Use exit code 0 for successful completion fi # Output list of theme slugs with changes echo "HAS_THEME_CHANGES=true" >> $GITHUB_OUTPUT