PR changes. #3
This file contains 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
name: 'PHPCS' | ||
on: | ||
workflow_call: | ||
secrets: | ||
access-token: | ||
description: 'GitHub Access Token' | ||
required: true | ||
inputs: | ||
ref: | ||
description: 'Git Commit Ref (branch, tag, or hash)' | ||
required: true | ||
type: string | ||
php_version: | ||
description: 'PHP Version' | ||
required: false | ||
type: string | ||
default: '7.4' | ||
debug: | ||
description: 'Enable debug logging' | ||
required: false | ||
type: boolean | ||
default: false | ||
jobs: | ||
phpcs: | ||
runs-on: ubuntu-latest | ||
# ------------------------------------------------------------------------------ | ||
# 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 | ||
# ------------------------------------------------------------------------------ | ||
# Prepare our cache directories | ||
# ------------------------------------------------------------------------------ | ||
- name: Get Composer Cache Directory | ||
id: get-composer-cache-dir | ||
run: | | ||
echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT | ||
- name: Cache Composer dependencies | ||
uses: actions/cache@v2 | ||
with: | ||
path: ${{ steps.get-composer-cache-dir.outputs.dir }} | ||
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} | ||
restore-keys: | | ||
${{ runner.os }}-composer- | ||
# ------------------------------------------------------------------------------ | ||
# Get changed files | ||
# ------------------------------------------------------------------------------ | ||
- name: Get list of changed files | ||
id: get-changed-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}" >> $GITHUB_ENV | ||
echo "EOF" >> $GITHUB_ENV | ||
echo "Changed files (debug): ${CHANGED_FILES}" | ||
# ------------------------------------------------------------------------------ | ||
# Run PHPCS and Output Errors | ||
# ------------------------------------------------------------------------------ | ||
- uses: reviewdog/action-setup@v1 | ||
with: | ||
reviewdog_version: latest # Optional. [latest,nightly,v.X.Y.Z] | ||
- name: Run PHPCS and Output Errors | ||
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 | ||
fi | ||
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 | ||
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." |