Skip to content

Commit 355ca78

Browse files
committed
Fixing code quality check.
1 parent 7d6ae34 commit 355ca78

File tree

1 file changed

+71
-63
lines changed

1 file changed

+71
-63
lines changed

.github/workflows/code-quality.yml

Lines changed: 71 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
# This does the following:
2-
# 1. Detects modified plugins that have phpcs.xml configuration
3-
# 2. Runs PHP Code Quality checks on those plugins using the custom action
4-
# 3. Creates a matrix job for each plugin that has a quality configuration
5-
# Bonus: This means you can have plugin specific badges e.g.
6-
# [![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)
1+
# This workflow correctly detects all modified plugins with a quality config
2+
# and runs checks for each one individually.
73

84
name: Code Quality
95

@@ -12,88 +8,100 @@ on:
128
branches:
139
- main
1410
paths:
15-
- 'plugins/*/**.php'
11+
- "plugins/*/**.php"
12+
- "plugins/*/phpcs.xml" # Trigger if a config file changes
1613
pull_request:
1714
paths:
18-
- 'plugins/*/**.php'
15+
- "plugins/*/**.php"
16+
- "plugins/*/phpcs.xml" # Trigger if a config file changes
1917

2018
jobs:
21-
detect-plugins:
19+
# This job finds all modified plugins and creates a JSON list for the matrix
20+
detect-modified-plugins:
2221
runs-on: ubuntu-latest
23-
name: Detect plugins has php code quality configuration
22+
name: Detect modified plugins with quality config
2423
outputs:
2524
plugins: ${{ steps.detect.outputs.plugins }}
26-
has-plugins: ${{ steps.detect.outputs.has-plugins }}
27-
php-version: ${{ steps.detect-php-version.outputs.php-version }}
2825
steps:
29-
- name: Checkout
26+
- name: Checkout code
3027
uses: actions/checkout@v4
28+
with:
29+
# Fetch full history to ensure git diff is accurate
30+
fetch-depth: 0
3131

32-
- name: Get changed plugin directory
33-
id: plugin
32+
- name: Find modified plugins with phpcs.xml
33+
id: detect
3434
run: |
35-
if [ "${{ github.event_name }}" = "push" ]; then
36-
bash .github/scripts/get_plugin_slug.sh main
37-
else
38-
bash .github/scripts/get_plugin_slug.sh \
39-
${{ github.event.pull_request.base.sha }} \
40-
${{ github.event.pull_request.head.sha }}
35+
# Establish the correct base and head commits for the diff
36+
if [[ "${{ github.event_name }}" == "pull_request" ]]; then
37+
BASE_SHA="${{ github.event.pull_request.base.sha }}"
38+
HEAD_SHA="${{ github.event.pull_request.head.sha }}"
39+
else # This handles the 'push' event
40+
BASE_SHA="${{ github.event.before }}"
41+
HEAD_SHA="${{ github.event.after }}"
4142
fi
4243
43-
- name: Detect changed plugins with quality config
44-
id: detect-plugin-slug
45-
run: |
46-
if [ -z "${{ steps.plugin.outputs.slug }}" ]; then
47-
echo "No plugin slug detected"
48-
echo "plugins=[]" >> $GITHUB_OUTPUT
49-
echo "has-plugins=false" >> $GITHUB_OUTPUT
50-
exit 0
51-
fi
44+
echo "Detecting changed plugins between $BASE_SHA and $HEAD_SHA..."
5245
53-
PLUGIN="${{ steps.plugin.outputs.slug }}"
46+
# Get changed files, extract unique plugin slugs, and check for phpcs.xml
47+
PLUGINS_WITH_CONFIG=$(git diff --name-only "$BASE_SHA" "$HEAD_SHA" \
48+
| grep -E '^plugins/[^/]+/' \
49+
| sed -E 's|plugins/([^/]+)/.*|\1|' \
50+
| sort -u \
51+
| {
52+
plugins_json="["
53+
first=true
54+
while read -r plugin; do
55+
if [[ -f "plugins/$plugin/phpcs.xml" ]]; then
56+
echo "✅ Found phpcs.xml for changed plugin: $plugin"
57+
if [[ "$first" = false ]]; then plugins_json="$plugins_json,"; fi
58+
plugins_json="$plugins_json\"$plugin\""
59+
first=false
60+
else
61+
echo "ℹ️ No phpcs.xml for changed plugin: $plugin. Skipping."
62+
fi
63+
done
64+
plugins_json="$plugins_json]"
65+
echo "$plugins_json"
66+
})
5467
55-
if [ -f "plugins/$PLUGIN/phpcs.xml" ]; then
56-
echo "plugins=[\"$PLUGIN\"]" >> $GITHUB_OUTPUT
57-
echo "has-plugins=true" >> $GITHUB_OUTPUT
58-
echo "✅ Found phpcs.xml for plugin: $PLUGIN"
59-
else
60-
echo "plugins=[]" >> $GITHUB_OUTPUT
61-
echo "has-plugins=false" >> $GITHUB_OUTPUT
62-
echo "ℹ️ No phpcs.xml found for plugin: $PLUGIN, skipping quality checks"
63-
fi
64-
- name: Detect PHP version from composer.json
65-
id: detect-php-version
66-
run: |
67-
PLUGIN="${{ steps.plugin.outputs.slug }}"
68-
PHP_VERSION="7.4"
69-
if [ -f "plugins/$PLUGIN/composer.json" ]; then
70-
DETECTED_VERSION=$(jq -r '.require["php"] // empty' plugins/$PLUGIN/composer.json | grep -oE '[0-9]+\.[0-9]+' | head -1)
71-
if [ -n "$DETECTED_VERSION" ]; then
72-
PHP_VERSION="$DETECTED_VERSION"
73-
echo "Detected PHP version $PHP_VERSION from composer.json"
74-
else
75-
echo "No PHP version found in composer.json, using default $PHP_VERSION"
76-
fi
77-
else
78-
echo "No composer.json found, using default PHP version $PHP_VERSION"
79-
fi
80-
echo "php-version=$PHP_VERSION" >> $GITHUB_OUTPUT
68+
echo "plugins=${PLUGINS_WITH_CONFIG}" >> "$GITHUB_OUTPUT"
69+
echo "Matrix for next job: ${PLUGINS_WITH_CONFIG}"
70+
71+
# This job runs if the matrix from the previous job is not empty
8172
quality-checks:
82-
needs: detect-plugins
83-
if: needs.detect-plugins.outputs.has-plugins == 'true'
73+
needs: detect-modified-plugins
74+
# A cleaner condition: run only if the plugins list is not empty
75+
if: needs.detect-modified-plugins.outputs.plugins != '[]'
8476
runs-on: ubuntu-latest
8577
strategy:
8678
matrix:
87-
plugin: ${{ fromJson(needs.detect-plugins.outputs.plugins) }}
79+
plugin: ${{ fromJson(needs.detect-modified-plugins.outputs.plugins) }}
8880
fail-fast: false
8981
name: ${{ matrix.plugin }} php code quality checks
9082
steps:
91-
- name: Checkout
83+
- name: Checkout code
9284
uses: actions/checkout@v4
9385

86+
# This step detects the PHP version for each specific plugin in the matrix
87+
- name: Detect PHP version for ${{ matrix.plugin }}
88+
id: get-php-version
89+
run: |
90+
PHP_VERSION="7.4" # Set a default PHP version
91+
COMPOSER_FILE="plugins/${{ matrix.plugin }}/composer.json"
92+
if [[ -f "$COMPOSER_FILE" ]]; then
93+
# Safely parse composer.json for the PHP version
94+
DETECTED_VERSION=$(jq -r '.require.php // ""' "$COMPOSER_FILE" | grep -oE '[0-9]+\.[0-9]+' | head -1)
95+
if [[ -n "$DETECTED_VERSION" ]]; then
96+
PHP_VERSION="$DETECTED_VERSION"
97+
fi
98+
fi
99+
echo "php-version=${PHP_VERSION}" >> "$GITHUB_OUTPUT"
100+
echo "Using PHP version ${PHP_VERSION} for ${{ matrix.plugin }}"
101+
94102
- name: PHP Code Quality for ${{ matrix.plugin }}
95103
uses: ./.github/actions/code-quality
96104
with:
97105
working-directory: plugins/${{ matrix.plugin }}
98-
php-version: ${{ needs.detect-plugins.outputs.php-version }}
99-
composer-options: '--no-progress --no-suggest'
106+
php-version: ${{ steps.get-php-version.outputs.php-version }}
107+
composer-options: "--no-progress --no-suggest"

0 commit comments

Comments
 (0)