Enhance CI/CD workflow with code quality checks and PHPCBF integration

This commit is contained in:
2025-04-21 04:02:27 +01:00
parent 497c44c3c6
commit 5cd2a7d374
6 changed files with 152 additions and 12 deletions

View File

@@ -41,7 +41,7 @@ This workspace may contain multiple repository folders. Always focus ONLY on the
This project follows the [WordPress Coding Standards](https://developer.wordpress.org/coding-standards/wordpress-coding-standards/): This project follows the [WordPress Coding Standards](https://developer.wordpress.org/coding-standards/wordpress-coding-standards/):
- Use tabs for indentation, not spaces - Use 4 spaces for indentation, not tabs (this is a project-specific override of WordPress standards)
- Follow WordPress naming conventions: - Follow WordPress naming conventions:
- Class names: `Class_Name` - Class names: `Class_Name`
- Function names: `function_name` - Function names: `function_name`
@@ -51,6 +51,38 @@ This project follows the [WordPress Coding Standards](https://developer.wordpres
- Validate and sanitize all inputs - Validate and sanitize all inputs
- Escape all outputs - Escape all outputs
### Code Quality Tools
This project uses several automated code quality tools to ensure high standards:
1. **PHP_CodeSniffer (PHPCS)**: Checks PHP code against the WordPress Coding Standards
```bash
composer run phpcs
```
2. **PHP Code Beautifier and Fixer (PHPCBF)**: Automatically fixes some coding standard violations
```bash
composer run phpcbf
```
3. **ESLint**: Checks JavaScript code against the WordPress Coding Standards
```bash
npm run lint:js
```
4. **Stylelint**: Checks CSS code against the WordPress Coding Standards
```bash
npm run lint:css
```
5. **Continuous Integration Tools**: The project integrates with several code quality tools:
- **CodeRabbit**: AI-powered code review tool
- **CodeFactor**: Continuous code quality monitoring
- **Codacy**: Code quality and static analysis
- **SonarCloud**: Code quality and security analysis
Always run PHPCS and PHPCBF locally before committing code to ensure it meets the project's coding standards.
## Common Tasks ## Common Tasks
For detailed instructions on common tasks like creating releases, adding features, fixing bugs, and testing previous versions, see **@.ai-workflows/release-process.md**. For detailed instructions on common tasks like creating releases, adding features, fixing bugs, and testing previous versions, see **@.ai-workflows/release-process.md**.

79
.github/workflows/code-quality.yml vendored Normal file
View File

@@ -0,0 +1,79 @@
name: Code Quality - Run automated code quality checks
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
phpcs:
name: PHP CodeSniffer
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
clean: 'true'
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
extensions: mbstring, intl, zip
tools: composer:v2, phpcs
- name: Install dependencies
run: composer install --prefer-dist --no-progress
- name: Run PHPCS
run: composer run phpcs
continue-on-error: true
- name: Run PHPCBF (report only)
run: |
echo "Running PHPCBF in dry-run mode to show what would be fixed"
composer run phpcbf -- --dry-run
continue-on-error: true
sonarcloud:
name: SonarCloud Analysis
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
codacy:
name: Codacy Analysis
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Run Codacy Analysis CLI
uses: codacy/codacy-analysis-cli-action@master
with:
project-token: ${{ secrets.CODACY_PROJECT_TOKEN }}
verbose: true
output: results.sarif
format: sarif
# Adjust the below patterns based on your project structure
gh-code-scanning-compat: true
max-allowed-issues: 2147483647
continue-on-error: true
- name: Upload SARIF results file
uses: github/codeql-action/upload-sarif@v2
with:
sarif_file: results.sarif
continue-on-error: true

View File

@@ -63,5 +63,12 @@ jobs:
- name: Install dependencies - name: Install dependencies
run: composer install --prefer-dist --no-progress run: composer install --prefer-dist --no-progress
# - name: Run PHPCS - name: Run PHPCS
# run: ./vendor/bin/phpcs --standard=WordPress ./includes ./admin ./wp-plugin-starter-template.php run: composer run phpcs
continue-on-error: true
- name: Run PHPCBF (report only)
run: |
echo "Running PHPCBF in dry-run mode to show what would be fixed"
composer run phpcbf -- --dry-run
continue-on-error: true

View File

@@ -20,6 +20,19 @@ echo "Creating build directory..."
rm -rf "$BUILD_DIR" rm -rf "$BUILD_DIR"
mkdir -p "$BUILD_DIR" mkdir -p "$BUILD_DIR"
# Run code quality checks
echo "Running code quality checks..."
if command -v composer &> /dev/null; then
echo "Running PHPCS..."
composer run phpcs || { echo "⚠️ PHPCS found issues. Consider running 'composer run phpcbf' to fix them."; }
# Uncomment the following line to automatically fix coding standards issues
# echo "Running PHPCBF..."
# composer run phpcbf
else
echo "⚠️ Composer not found, skipping code quality checks"
fi
# Install composer dependencies # Install composer dependencies
echo "Installing composer dependencies..." echo "Installing composer dependencies..."
composer install --no-dev --optimize-autoloader composer install --no-dev --optimize-autoloader

View File

@@ -37,8 +37,12 @@
} }
}, },
"scripts": { "scripts": {
"phpcs": "phpcs --standard=WordPress", "phpcs": "phpcs --standard=phpcs.xml",
"phpcbf": "phpcbf --standard=WordPress", "phpcs:simple": "phpcs --standard=phpcs-simple.xml",
"test": "phpunit" "phpcbf": "phpcbf --standard=phpcs.xml",
"phpcbf:simple": "phpcbf --standard=phpcs-simple.xml",
"test": "phpunit",
"lint": ["@phpcs"],
"fix": ["@phpcbf"]
} }
} }

View File

@@ -12,8 +12,13 @@
"test:e2e:headless": "cypress run --config-file tests/e2e/cypress.json", "test:e2e:headless": "cypress run --config-file tests/e2e/cypress.json",
"build": "./build.sh", "build": "./build.sh",
"lint:php": "composer run-script phpcs", "lint:php": "composer run-script phpcs",
"lint:php:simple": "composer run-script phpcs:simple",
"fix:php": "composer run-script phpcbf", "fix:php": "composer run-script phpcbf",
"test:php": "composer run-script test" "fix:php:simple": "composer run-script phpcbf:simple",
"test:php": "composer run-script test",
"lint": "npm run lint:php",
"fix": "npm run fix:php",
"quality": "npm run lint && npm run test:php"
}, },
"repository": { "repository": {
"type": "git", "type": "git",