-
Notifications
You must be signed in to change notification settings - Fork 28
Github Action: Add phpcs checks #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
baf70c1
Add github action, phpcs-changed, and pre-commit hook via Husky
kraftbj d1afd5f
Remove debug line
kraftbj d8a3ecc
Use env for GH variables
kraftbj 8bc0758
Remove unneeded character
kraftbj 111a5b5
Update git diff per code review https://github.com/WordPress/secure-c…
kraftbj dbaea5d
Remove output of remaining errors after PHPCBF. It isn't helpful give…
kraftbj b292056
Remove unneeded code per review https://github.com/WordPress/secure-c…
kraftbj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
name: Code Standards | ||
on: | ||
pull_request: | ||
|
||
jobs: | ||
phpcs: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 # Required for phpcs-changed to compare with base branch | ||
|
||
- name: Setup PHP | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: '8.1' | ||
tools: composer:v2 | ||
|
||
- name: Install Dependencies | ||
run: composer install | ||
|
||
- name: Run PHPCS on changed files | ||
env: | ||
BASE_REF: ${{ github.base_ref }} | ||
run: | | ||
CHANGED_FILES=$(git diff --name-only --no-renames --diff-filter=d "origin/$BASE_REF" '*.php' || true) | ||
if [[ -n "$CHANGED_FILES" ]]; then | ||
vendor/bin/phpcs-changed \ | ||
--git-base="origin/$BASE_REF" \ | ||
--phpcs-path=vendor/bin/phpcs \ | ||
--standard=.phpcs.xml.dist \ | ||
$CHANGED_FILES | ||
else | ||
echo "No PHP files changed" | ||
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/sh | ||
. .husky/pre-commit-phpcbf.sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#!/bin/sh | ||
|
||
# Disable exit on error since we handle exit codes manually | ||
set +e | ||
|
||
# Get list of staged PHP files | ||
STAGED_PHP_FILES=$(git diff --cached --name-only --no-renames --diff-filter=d '*.php' ) | ||
|
||
# Exit if no PHP files are staged | ||
if [ -z "$STAGED_PHP_FILES" ]; then | ||
exit 0 | ||
fi | ||
|
||
echo "Running PHPCBF on staged files..." | ||
|
||
# First try to fix what we can with PHPCBF | ||
echo "$STAGED_PHP_FILES" | xargs vendor/bin/phpcbf | ||
|
||
# Check the return code | ||
PHPCBF_STATUS=$? | ||
|
||
# If files were fixed, add them back to staging | ||
if [ $PHPCBF_STATUS -ne 3 ]; then | ||
echo "$STAGED_PHP_FILES" | xargs git add | ||
|
||
if [ $PHPCBF_STATUS -eq 1 ] || [ $PHPCBF_STATUS -eq 2 ]; then | ||
echo "" | ||
echo "Some errors were fixed by PHPCBF, but not all. Continuing with phpcs-changed for remaining errors on lines changed by this PR." | ||
fi | ||
fi | ||
|
||
# Now run phpcs-changed to check only modified lines | ||
echo "Checking modified lines with phpcs-changed..." | ||
|
||
vendor/bin/phpcs-changed\ | ||
--git-staged\ | ||
--phpcs-path=vendor/bin/phpcs\ | ||
--standard=.phpcs.xml.dist\ | ||
$STAGED_PHP_FILES | ||
|
||
PHPCS_STATUS=$? | ||
|
||
if [ $PHPCS_STATUS -ne 0 ]; then | ||
echo "⛔️ Found coding standards violations in changed lines. Please fix them before committing." | ||
exit 1 | ||
fi | ||
|
||
echo "✅ All coding standards checks passed!" | ||
exit 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.