Adjusted logic. #2
Workflow file for this run
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' | ||
jobs: | ||
phpcs: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- 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 | ||
- uses: actions/cache@v2 | ||
id: composer-cache | ||
with: | ||
path: ${{ steps.get-composer-cache-dir.outputs.dir }} | ||
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} | ||
restore-keys: | | ||
${{ runner.os }}-composer- | ||
- uses: "shivammathur/setup-php@v2" | ||
with: | ||
php-version: ${{ inputs.php_version }} | ||
- uses: "ramsey/composer-install@v2" | ||
- name: "Give permissions" | ||
run: | | ||
sudo chown -R root:root $GITHUB_WORKSPACE | ||
# ------------------------------------------------------------------------------ | ||
# Get changed files | ||
# ------------------------------------------------------------------------------ | ||
- name: Get list of changed files | ||
id: 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 | ||
# ------------------------------------------------------------------------------ | ||
# Check for spaces in file paths | ||
# ------------------------------------------------------------------------------ | ||
- name: Check for spaces in file paths | ||
run: | | ||
# Check for spaces in each file path | ||
echo "$CHANGED_FILES" | while read -r file; do | ||
if [[ "$file" =~ \ ]]; then | ||
echo "Error: No files may contain spaces in their paths:" | ||
echo $file | ||
exit 1 | ||
fi | ||
done | ||
# ------------------------------------------------------------------------------ | ||
# PHPCS | ||
# ------------------------------------------------------------------------------ | ||
- uses: reviewdog/action-setup@v1 | ||
with: | ||
reviewdog_version: latest # Optional. [latest,nightly,v.X.Y.Z] | ||
- name: Run reviewdog | ||
env: | ||
REVIEWDOG_GITHUB_API_TOKEN: ${{ secrets.access-token }} | ||
run: | | ||
set -x # Enable debug mode | ||
# Check if there are any changed PHP files | ||
if [ -z "${CHANGED_FILES}" ]; then | ||
echo "No added or modified PHP files found." | ||
exit 0 | ||
fi | ||
# Initialize JSON report variable | ||
JSON_REPORT='{ "files": {} }' | ||
# Create the file list as a string | ||
FILE_LIST="${CHANGED_FILES}" | ||
# Run PHPCS using the file list with progress shown and capture output in a variable | ||
PHPCS_OUTPUT=$(vendor/bin/phpcs -q -p --report=json $FILE_LIST) | ||
PHPCS_EXIT_CODE=$? | ||
# Check if the report is valid JSON | ||
if echo "$PHPCS_OUTPUT" | jq empty > /dev/null 2>&1; then | ||
JSON_REPORT=$(echo "$PHPCS_OUTPUT" | jq -c '.files') | ||
# Check for errors in the PHP files | ||
ERROR_FILES=$(echo "$PHPCS_OUTPUT" | jq -r '.files | to_entries[] | select(.value.errors > 0) | .key') | ||
if [ ! -z "$ERROR_FILES" ]; then | ||
echo "PHPCS encountered errors in the following files:" | ||
echo "$ERROR_FILES" | ||
fi | ||
else | ||
echo "PHPCS encountered an error processing the files." | ||
echo "PHPCS error details: $PHPCS_OUTPUT" | ||
exit 1 | ||
fi | ||
# Validate the final JSON report | ||
if ! echo "$JSON_REPORT" | jq empty; then | ||
echo "Invalid JSON" | ||
echo "$JSON_REPORT" | ||
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)`<br>\($msg.message)" ' | reviewdog -efm="%f:%l:%c:%m" -name="phpcs" -filter-mode="added" -fail-on-error=true -reporter=github-pr-review | ||
# Exit with PHPCS exit code if there were errors | ||
if [ $PHPCS_EXIT_CODE -ne 0 ]; then | ||
echo "PHPCS checks failed for some files." | ||
exit 1 | ||
fi | ||
echo "PHPCS check complete." | ||
set +x # Disable debug mode |