From 739a500aa94b8d9e6f02fca42c427a8b998b420e Mon Sep 17 00:00:00 2001 From: Colin Murphy Date: Fri, 27 Jun 2025 12:15:34 +0100 Subject: [PATCH 1/5] Updating code quality workflow and splitting into 2 processes so it will skip the code quality check if phpcs.xml does not exist. --- .github/workflows/code-quality.yml | 83 ++++++++++++++++++++++++------ plugins/hwp-previews/TESTING.md | 5 +- 2 files changed, 70 insertions(+), 18 deletions(-) diff --git a/.github/workflows/code-quality.yml b/.github/workflows/code-quality.yml index 02e93cea..08be527c 100644 --- a/.github/workflows/code-quality.yml +++ b/.github/workflows/code-quality.yml @@ -1,3 +1,10 @@ +# This does the following: +# 1. Detects plugins with a quality configuration (phpcs.xml) has changed +# 2. Runs PHP Code Quality checks on those plugins using the custom action +# 3. Creates a matrix job for each plugin that has a quality configuration +# Bonus: This means you can have plugin specific badges e.g. [![Code Quality](https://img.shields.io/github/check-runs/wpengine/hwptoolkit/main?checkName=hwp-previews%20quality)](https://github.com/wpengine/hwptoolkit/actions) + + name: Code Quality on: @@ -11,30 +18,76 @@ on: - 'plugins/**.php' jobs: - run: + detect-plugins: runs-on: ubuntu-latest - name: Check code quality - + name: Detect plugins with quality config + outputs: + plugins: ${{ steps.detect.outputs.plugins }} + has-plugins: ${{ steps.detect.outputs.has-plugins }} steps: - name: Checkout uses: actions/checkout@v4 - - name: Get changed plugin directory - id: plugin + - name: Detect changed plugins with quality config + id: detect run: | git fetch --prune --unshallow - plugin=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | grep '^plugins/' | head -1 | cut -d/ -f2) - echo "slug=$plugin" >> $GITHUB_OUTPUT + CHANGED_FILES=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | grep '^plugins/' || true) - # We should at least have a phpcs.xml file to run code quality checks - - name: Validate phpcs.xml - run: | - if [ ! -f "plugins/${{ steps.plugin.outputs.slug }}/phpcs.xml" ]; then - echo "Exiting as no phpcs.xml file found for /${{ steps.plugin.outputs.slug }}" - exit 1 + if [ -z "$CHANGED_FILES" ]; then + echo "No plugin files changed" + echo "plugins=[]" >> $GITHUB_OUTPUT + echo "has-plugins=false" >> $GITHUB_OUTPUT + exit 0 fi - - name: PHP Code Quality + # Extract unique plugin names and check for phpcs.xml + PLUGINS_WITH_CONFIG=() + CHECKED_PLUGINS=() + + for file in $CHANGED_FILES; do + plugin=$(echo $file | cut -d/ -f2) + + # Skip if we already checked this plugin + if [[ " ${CHECKED_PLUGINS[@]} " =~ " $plugin " ]]; then + continue + fi + + CHECKED_PLUGINS+=("$plugin") + + if [ -f "plugins/$plugin/phpcs.xml" ]; then + PLUGINS_WITH_CONFIG+=("$plugin") + echo "✅ Found phpcs.xml for plugin: $plugin" + else + echo "ℹ️ No phpcs.xml found for plugin: $plugin, skipping quality checks" + fi + done + + # Convert to JSON array + if [ ${#PLUGINS_WITH_CONFIG[@]} -gt 0 ]; then + PLUGINS_JSON=$(printf '%s\n' "${PLUGINS_WITH_CONFIG[@]}" | jq -R -s -c 'split("\n")[:-1]') + echo "plugins=${PLUGINS_JSON}" >> $GITHUB_OUTPUT + echo "has-plugins=true" >> $GITHUB_OUTPUT + echo "Found ${#PLUGINS_WITH_CONFIG[@]} plugin(s) with quality config: ${PLUGINS_WITH_CONFIG[*]}" + else + echo "plugins=[]" >> $GITHUB_OUTPUT + echo "has-plugins=false" >> $GITHUB_OUTPUT + echo "No plugins found with quality configuration" + fi + quality-checks: + needs: detect-plugins + if: needs.detect-plugins.outputs.has-plugins == 'true' + runs-on: ubuntu-latest + strategy: + matrix: + plugin: ${{ fromJson(needs.detect-plugins.outputs.plugins) }} + fail-fast: false + name: ${{ matrix.plugin }} quality + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: PHP Code Quality for ${{ matrix.plugin }} uses: ./.github/actions/code-quality with: - working-directory: plugins/${{ steps.plugin.outputs.slug }} \ No newline at end of file + working-directory: plugins/${{ matrix.plugin }} diff --git a/plugins/hwp-previews/TESTING.md b/plugins/hwp-previews/TESTING.md index a11c6496..5c8b9fe7 100644 --- a/plugins/hwp-previews/TESTING.md +++ b/plugins/hwp-previews/TESTING.md @@ -78,9 +78,8 @@ To generate an HTML coverage report: ```bash composer run test:unit:coverage-html ``` - -> [!IMPORTANT] -> You can also add coverage e.g. `sh bin/local/run-unit-tests.sh coverage --coverage-html` and the output will be saved in [tests/_output/coverage/dashboard.html](tests/_output/coverage/dashboard.html) +> [!NOTE] +> HTML code coverage can be found here [tests/_output/coverage/index.html](tests/_output/coverage/index.html) ### E2WTests From 94a2c4fbd8b80f00f050c8a00b2eb5be11abb4b1 Mon Sep 17 00:00:00 2001 From: Colin Murphy Date: Fri, 27 Jun 2025 12:17:46 +0100 Subject: [PATCH 2/5] Added link to the plugin homepage. --- plugins/hwp-previews/src/Plugin.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugins/hwp-previews/src/Plugin.php b/plugins/hwp-previews/src/Plugin.php index c760ce6f..7b25e59f 100644 --- a/plugins/hwp-previews/src/Plugin.php +++ b/plugins/hwp-previews/src/Plugin.php @@ -13,6 +13,8 @@ * * This class serves as the main entry point for the plugin, handling initialization, action and filter hooks. * + * @link https://github.com/wpengine/hwptoolkit/tree/main/plugins/hwp-previews + * * @package HWP\Previews */ final class Plugin { From 4e892ce6474b7751c254211f0561d08dd698e5f3 Mon Sep 17 00:00:00 2001 From: Colin Murphy Date: Fri, 27 Jun 2025 12:18:19 +0100 Subject: [PATCH 3/5] Added Changeset --- .changeset/metal-kings-look.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/metal-kings-look.md diff --git a/.changeset/metal-kings-look.md b/.changeset/metal-kings-look.md new file mode 100644 index 00000000..3a9e1c8f --- /dev/null +++ b/.changeset/metal-kings-look.md @@ -0,0 +1,5 @@ +--- +"@wpengine/hwp-previews-wordpress-plugin": patch +--- + +chore: Minor changes to testing docs and updating workflows. From be933c9ebdb9449d0b297669cbc4b4ccbe8c2a97 Mon Sep 17 00:00:00 2001 From: Colin Murphy Date: Fri, 27 Jun 2025 12:28:10 +0100 Subject: [PATCH 4/5] Updated workflow and hwp previews badge for php code quality checks --- .github/workflows/code-quality.yml | 8 ++++---- plugins/hwp-previews/README.md | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/code-quality.yml b/.github/workflows/code-quality.yml index 08be527c..b2035c5a 100644 --- a/.github/workflows/code-quality.yml +++ b/.github/workflows/code-quality.yml @@ -2,8 +2,8 @@ # 1. Detects plugins with a quality configuration (phpcs.xml) has changed # 2. Runs PHP Code Quality checks on those plugins using the custom action # 3. Creates a matrix job for each plugin that has a quality configuration -# Bonus: This means you can have plugin specific badges e.g. [![Code Quality](https://img.shields.io/github/check-runs/wpengine/hwptoolkit/main?checkName=hwp-previews%20quality)](https://github.com/wpengine/hwptoolkit/actions) - +# Bonus: This means you can have plugin specific badges e.g. +# [![Code Quality](https://img.shields.io/github/check-runs/wpengine/hwptoolkit/main?checkName=hwp-previews%20php%20code%20quality%20checks)](https://github.com/wpengine/hwptoolkit/actions) name: Code Quality @@ -20,7 +20,7 @@ on: jobs: detect-plugins: runs-on: ubuntu-latest - name: Detect plugins with quality config + name: Detect plugins has php code quality configuration outputs: plugins: ${{ steps.detect.outputs.plugins }} has-plugins: ${{ steps.detect.outputs.has-plugins }} @@ -82,7 +82,7 @@ jobs: matrix: plugin: ${{ fromJson(needs.detect-plugins.outputs.plugins) }} fail-fast: false - name: ${{ matrix.plugin }} quality + name: ${{ matrix.plugin }} php code quality checks steps: - name: Checkout uses: actions/checkout@v4 diff --git a/plugins/hwp-previews/README.md b/plugins/hwp-previews/README.md index 27f77411..69dcce03 100644 --- a/plugins/hwp-previews/README.md +++ b/plugins/hwp-previews/README.md @@ -13,7 +13,7 @@ ![GitHub forks](https://img.shields.io/github/forks/wpengine/hwptoolkit?style=social) ![GitHub stars](https://img.shields.io/github/stars/wpengine/hwptoolkit?style=social) [![Testing Integration](https://github.com/wpengine/hwptoolkit/workflows/Testing%20Integration/badge.svg)](https://github.com/wpengine/hwptoolkit/actions?query=workflow%3A%22Testing+Integration%22) -[![Code Quality](https://github.com/wpengine/hwptoolkit/workflows/Code%20Quality/badge.svg)](https://github.com/wpengine/hwptoolkit/actions?query=workflow%3A%22Code+Quality%22) +[![Code Quality](https://img.shields.io/github/check-runs/wpengine/hwptoolkit/main?checkName=hwp-previews%20php%20code%20quality%20checks)](https://github.com/wpengine/hwptoolkit/actions) [![End-to-End Tests](https://github.com/wpengine/hwptoolkit/workflows/End-to-End%20Tests/badge.svg)](https://github.com/wpengine/hwptoolkit/actions?query=workflow%3A%22End-to-End+Tests%22) ----- From e92d2832066fa2b9842a6a7455a2b6657f6c03d3 Mon Sep 17 00:00:00 2001 From: Colin Murphy Date: Fri, 27 Jun 2025 12:48:36 +0100 Subject: [PATCH 5/5] Split automated tests workflow in two so we can skip if there is no unit tests setup. --- .github/workflows/code-quality.yml | 4 +- .github/workflows/codeception.yml | 108 ++++++++++++++++++++++------- plugins/hwp-previews/README.md | 4 +- 3 files changed, 87 insertions(+), 29 deletions(-) diff --git a/.github/workflows/code-quality.yml b/.github/workflows/code-quality.yml index b2035c5a..2e0f07bc 100644 --- a/.github/workflows/code-quality.yml +++ b/.github/workflows/code-quality.yml @@ -1,9 +1,9 @@ # This does the following: -# 1. Detects plugins with a quality configuration (phpcs.xml) has changed +# 1. Detects modified plugins that have phpcs.xml configuration # 2. Runs PHP Code Quality checks on those plugins using the custom action # 3. Creates a matrix job for each plugin that has a quality configuration # Bonus: This means you can have plugin specific badges e.g. -# [![Code Quality](https://img.shields.io/github/check-runs/wpengine/hwptoolkit/main?checkName=hwp-previews%20php%20code%20quality%20checks)](https://github.com/wpengine/hwptoolkit/actions) +# [![Code Quality](https://img.shields.io/github/check-runs/wpengine/hwptoolkit/main?checkName=hwp-previews%20php%20code%20quality%20checks&label=Code%20Quality%20Checks)](https://github.com/wpengine/hwptoolkit/actions) name: Code Quality diff --git a/.github/workflows/codeception.yml b/.github/workflows/codeception.yml index d296b04b..dce9983f 100644 --- a/.github/workflows/codeception.yml +++ b/.github/workflows/codeception.yml @@ -1,3 +1,10 @@ +# This does the following: +# 1. Detects modified plugins that have codeception.dist.yml configuration setup +# 2. Runs Codeception tests on those plugins using the custom action +# 3. Creates a matrix job for each plugin that has a quality configuration +# Bonus: This means you can have plugin specific badges e.g. +# [![Testing Integration](https://img.shields.io/github/check-runs/wpengine/hwptoolkit/main?checkName=hwp-previews%20codeception%20tests&label=Automated%20Tests)](https://github.com/wpengine/hwptoolkit/actions) + name: Testing Integration on: @@ -18,12 +25,85 @@ concurrency: cancel-in-progress: true jobs: + detect-plugins: + runs-on: ubuntu-latest + name: Detect plugins with test config + outputs: + plugins: ${{ steps.detect.outputs.plugins }} + has-plugins: ${{ steps.detect.outputs.has-plugins }} + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Detect changed plugins with test config + id: detect + run: | + git fetch --prune --unshallow + + # Get changed files based on event type + if [ "${{ github.event_name }}" = "pull_request" ]; then + CHANGED_FILES=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | grep '^plugins/' || true) + else + CHANGED_FILES=$(git diff --name-only HEAD~1 HEAD | grep '^plugins/' || true) + fi + + if [ -z "$CHANGED_FILES" ]; then + echo "No plugin files changed" + echo "plugins=[]" >> $GITHUB_OUTPUT + echo "has-plugins=false" >> $GITHUB_OUTPUT + exit 0 + fi + + # Extract unique plugin names and check for test config + PLUGINS_WITH_CONFIG=() + CHECKED_PLUGINS=() + + for file in $CHANGED_FILES; do + plugin=$(echo $file | cut -d/ -f2) + + # Skip if we already checked this plugin + if [[ " ${CHECKED_PLUGINS[@]} " =~ " $plugin " ]]; then + continue + fi + + CHECKED_PLUGINS+=("$plugin") + + # Check for both codeception.dist.yml and composer.json + if [ -f "plugins/$plugin/codeception.dist.yml" ] && [ -f "plugins/$plugin/composer.json" ]; then + PLUGINS_WITH_CONFIG+=("$plugin") + echo "✅ Found test config for plugin: $plugin (codeception.dist.yml + composer.json)" + else + echo "ℹ️ Missing test config for plugin: $plugin, skipping tests" + if [ ! -f "plugins/$plugin/codeception.dist.yml" ]; then + echo " - Missing: codeception.dist.yml" + fi + if [ ! -f "plugins/$plugin/composer.json" ]; then + echo " - Missing: composer.json" + fi + fi + done + + # Convert to JSON array + if [ ${#PLUGINS_WITH_CONFIG[@]} -gt 0 ]; then + PLUGINS_JSON=$(printf '%s\n' "${PLUGINS_WITH_CONFIG[@]}" | jq -R -s -c 'split("\n")[:-1]') + echo "plugins=${PLUGINS_JSON}" >> $GITHUB_OUTPUT + echo "has-plugins=true" >> $GITHUB_OUTPUT + echo "Found ${#PLUGINS_WITH_CONFIG[@]} plugin(s) with test config: ${PLUGINS_WITH_CONFIG[*]}" + else + echo "plugins=[]" >> $GITHUB_OUTPUT + echo "has-plugins=false" >> $GITHUB_OUTPUT + echo "No plugins found with test configuration" + fi + continuous_integration: + needs: detect-plugins + if: needs.detect-plugins.outputs.has-plugins == 'true' runs-on: ubuntu-latest - name: WordPress ${{ matrix.wordpress }} on PHP ${{ matrix.php }} + name: ${{ matrix.plugin }} integration tests (WP ${{ matrix.wordpress }}, PHP ${{ matrix.php }}) strategy: matrix: + plugin: ${{ fromJson(needs.detect-plugins.outputs.plugins) }} php: ["8.3","8.2","8.1"] wordpress: ["6.8","6.7","6.6","6.5"] include: @@ -35,32 +115,10 @@ jobs: steps: - name: Checkout uses: actions/checkout@v4 - - - name: Get changed plugin directory - id: plugin - run: | - git fetch --prune --unshallow - plugin=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | grep '^plugins/' | head -1 | cut -d/ -f2) - echo "slug=$plugin" >> $GITHUB_OUTPUT - - - name: Validate codeception.dist.yml - run: | - if [ ! -f "plugins/${{ steps.plugin.outputs.slug }}/codeception.dist.yml" ]; then - echo "Exiting as no codeception file found for this plugin - /${{ steps.plugin.outputs.slug }}" - exit 1 - fi - - - name: Validate composer.json - run: | - if [ ! -f "plugins/${{ steps.plugin.outputs.slug }}/composer.json" ]; then - echo "Exiting as no composer file found for this plugin - ${{ steps.plugin.outputs.slug }}" - exit 1 - fi - - - name: Run Codeception Tests + - name: ${{ matrix.plugin }} codeception tests uses: ./.github/actions/codeception with: - working-directory: plugins/${{ steps.plugin.outputs.slug }} + working-directory: plugins/${{ matrix.plugin }} php: ${{ matrix.php }} wordpress: ${{ matrix.wordpress }} extensions: json,mbstring diff --git a/plugins/hwp-previews/README.md b/plugins/hwp-previews/README.md index 69dcce03..8ccd559e 100644 --- a/plugins/hwp-previews/README.md +++ b/plugins/hwp-previews/README.md @@ -12,8 +12,8 @@ [![License](https://img.shields.io/badge/license-GPLv2%2B-green)]() ![GitHub forks](https://img.shields.io/github/forks/wpengine/hwptoolkit?style=social) ![GitHub stars](https://img.shields.io/github/stars/wpengine/hwptoolkit?style=social) -[![Testing Integration](https://github.com/wpengine/hwptoolkit/workflows/Testing%20Integration/badge.svg)](https://github.com/wpengine/hwptoolkit/actions?query=workflow%3A%22Testing+Integration%22) -[![Code Quality](https://img.shields.io/github/check-runs/wpengine/hwptoolkit/main?checkName=hwp-previews%20php%20code%20quality%20checks)](https://github.com/wpengine/hwptoolkit/actions) +[![Testing Integration](https://img.shields.io/github/check-runs/wpengine/hwptoolkit/main?checkName=hwp-previews%20codeception%20tests&label=Automated%20Tests)](https://github.com/wpengine/hwptoolkit/actions) +[![Code Quality](https://img.shields.io/github/check-runs/wpengine/hwptoolkit/main?checkName=hwp-previews%20php%20code%20quality%20checks&label=Code%20Quality%20Checks)](https://github.com/wpengine/hwptoolkit/actions) [![End-to-End Tests](https://github.com/wpengine/hwptoolkit/workflows/End-to-End%20Tests/badge.svg)](https://github.com/wpengine/hwptoolkit/actions?query=workflow%3A%22End-to-End+Tests%22) -----