Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 20 additions & 106 deletions .github/workflows/phpcs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,11 @@ on:
required: false
type: string
default: '7.4'
debug:
description: 'Enable debug logging'
required: false
type: boolean
default: false
jobs:
phpcs:
runs-on: ubuntu-latest
steps:
# ------------------------------------------------------------------------------
# Enable Debug Logging
# ------------------------------------------------------------------------------
- name: Enable Debug Logging
run: |
if [ "${{ inputs.debug }}" = "true" ]; then
echo "ACTIONS_STEP_DEBUG=true" >> $GITHUB_ENV
fi

# ------------------------------------------------------------------------------
# Checkout the repository
# ------------------------------------------------------------------------------
- name: Checkout code
uses: actions/checkout@v3
- uses: actions/checkout@v3
with:
ref: ${{ inputs.ref }}
fetch-depth: 0
Expand Down Expand Up @@ -72,106 +54,38 @@ jobs:
# Get changed files
# ------------------------------------------------------------------------------
- name: Get list of changed files
id: get-changed-files
id: files
run: |
CHANGED_FILES=$(git diff --name-only --diff-filter=AM ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} -- . ':!tests' | grep '\.php$')
echo "CHANGED_FILES<<EOF" >> $GITHUB_ENV
echo "${CHANGED_FILES}" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
echo "Changed files (debug): ${CHANGED_FILES}"
echo "CHANGED_FILES=$(git diff --name-only --diff-filter=AM ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} -- . ':!tests' | grep '\.php$' | tr '\n' ' ')" >> $GITHUB_ENV

# ------------------------------------------------------------------------------
# Run PHPCS and Output Errors
# PHPCS
# ------------------------------------------------------------------------------
- uses: reviewdog/action-setup@v1
with:
reviewdog_version: latest # Optional. [latest,nightly,v.X.Y.Z]
- name: Run PHPCS and Output Errors
- name: Run reviewdog
env:
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.access-token }}
run: |
DEBUG=${{ inputs.debug }}
BATCH_SIZE=50
EXIT_CODE=0

debug() {
[[ "$DEBUG" == true ]] && echo "DEBUG: $1"
}

latest_commit="${{ github.event.pull_request.base.sha }}"
previous_commit="${{ github.event.pull_request.head.sha }}"
debug "Commit hashes: previous=$previous_commit, latest=$latest_commit"

debug "Getting the list of changed files..."
changed_files="${CHANGED_FILES}"

if [[ -z "$changed_files" ]]; then
echo "No relevant changed files found between $previous_commit and $latest_commit."
exit 0
# Run phpcs and capture both the output and the exit code
JSON_REPORT=$(vendor/bin/phpcs --report=json ${{ env.CHANGED_FILES }} || echo "")
PHPCS_EXIT_CODE=$?

# Check if phpcs produced a JSON report
if [ -z "$JSON_REPORT" ]; then
echo "No JSON report generated by phpcs"
exit $PHPCS_EXIT_CODE
fi

debug "Relevant changed files: $changed_files"

if echo "$changed_files" | grep -q ' '; then
echo "One or more files contain spaces. Exiting with error."
# Validate the JSON
if ! echo "$JSON_REPORT" | jq empty; then
echo "Invalid JSON"
exit 1
fi

debug "No files with spaces found."

# Output PHPCS version
debug "PHPCS version:"
debug $(vendor/bin/phpcs --version)

debug "Processing files in batches..."
files_array=($changed_files)
for ((i=0; i<${#files_array[@]}; i+=BATCH_SIZE)); do
batch=("${files_array[@]:i:BATCH_SIZE}")
debug "Processing batch: ${batch[*]}"

# Run phpcs on the current batch and append to PHPCS_OUTPUT
batch_output=$(vendor/bin/phpcs -q -p --report=json "${batch[@]}" || echo "")

# Check if the output is valid JSON
if ! echo "$batch_output" | jq empty > /dev/null 2>&1; then
echo "Invalid JSON output from phpcs."
exit 1
fi

# Merge JSON outputs
if [[ -z "$PHPCS_OUTPUT" ]]; then
PHPCS_OUTPUT=$batch_output
else
PHPCS_OUTPUT=$(echo "$PHPCS_OUTPUT" "$batch_output" | jq -s 'reduce .[] as $item ({}; . * $item)')
fi
done

# Output the PHPCS JSON report to the terminal (only in debug mode)
debug "PHPCS JSON output: $PHPCS_OUTPUT"

# Extract the list of files that have issues and their error counts
files_with_issues=$(echo "$PHPCS_OUTPUT" | jq -r '.files | to_entries[] | select(.value.errors > 0) | "\(.value.errors) errors found in \(.key)"')

# Check if any files have issues
if [[ -n "$files_with_issues" ]]; then
echo "Files with issues found by PHPCS:"
echo "$files_with_issues"
else
echo "No files with issues found by PHPCS."
fi

# Process JSON and run reviewdog
JSON_REPORT="$PHPCS_OUTPUT"
echo "$JSON_REPORT" | jq '.'
echo "$JSON_REPORT" | jq -r '.files | to_entries[] | .key as $path | .value.messages[] | "\($path):\(.line):\(.column):\(.source)\n\(.message)"' | reviewdog -efm="%f:%l:%c:%m" -name="phpcs" -filter-mode="added" -fail-on-error=false -reporter=github-pr-review

echo "Reviewdog done. Checking PHPCS errors next."

# Exit with PHPCS exit code if there were errors
PHPCS_EXIT_CODE=$(echo "$PHPCS_OUTPUT" | jq -r '.totals.errors')
if [[ "$PHPCS_EXIT_CODE" -ne 0 ]]; then
echo "PHPCS check failed: One or more files contain errors."
exit 1
fi

echo "PHPCS check completed without issues."
echo "$JSON_REPORT" | jq -r ' .files | to_entries[] | .key as $path | .value.messages[] as $msg | "\($path):\($msg.line):\($msg.column):`\($msg.source)`<br>\($msg.message)" ' | reviewdog -efm="%f:%l:%c:%m" -name="phpcs" -filter-mode="added" -fail-on-error=true -reporter=github-pr-review

# Exit with the original phpcs exit code
exit $PHPCS_EXIT_CODE