diff --git a/.github/workflows/phpcs.yml b/.github/workflows/phpcs.yml index 3ac5887..241871d 100644 --- a/.github/workflows/phpcs.yml +++ b/.github/workflows/phpcs.yml @@ -15,11 +15,29 @@ 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: - - uses: actions/checkout@v3 + # ------------------------------------------------------------------------------ + # 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 with: ref: ${{ inputs.ref }} fetch-depth: 0 @@ -54,38 +72,106 @@ jobs: # Get changed files # ------------------------------------------------------------------------------ - name: Get list of changed files - id: files + id: get-changed-files run: | - 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 + 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<> $GITHUB_ENV + echo "${CHANGED_FILES}" >> $GITHUB_ENV + echo "EOF" >> $GITHUB_ENV + echo "Changed files (debug): ${CHANGED_FILES}" # ------------------------------------------------------------------------------ - # PHPCS + # Run PHPCS and Output Errors # ------------------------------------------------------------------------------ - uses: reviewdog/action-setup@v1 with: reviewdog_version: latest # Optional. [latest,nightly,v.X.Y.Z] - - name: Run reviewdog + - name: Run PHPCS and Output Errors env: REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.access-token }} run: | - # 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 + 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 fi - # Validate the JSON - if ! echo "$JSON_REPORT" | jq empty; then - echo "Invalid JSON" + debug "Relevant changed files: $changed_files" + + if echo "$changed_files" | grep -q ' '; then + echo "One or more files contain spaces. Exiting with error." exit 1 fi - # Process JSON and run reviewdog - echo "$JSON_REPORT" | jq -r ' .files | to_entries[] | .key as $path | .value.messages[] as $msg | "\($path):\($msg.line):\($msg.column):`\($msg.source)`
\($msg.message)" ' | reviewdog -efm="%f:%l:%c:%m" -name="phpcs" -filter-mode="added" -fail-on-error=true -reporter=github-pr-review + 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 "") - # Exit with the original phpcs exit code - exit $PHPCS_EXIT_CODE + # 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."