Skip to content

Commit

Permalink
PHPCS only on changed files and lines
Browse files Browse the repository at this point in the history
  • Loading branch information
kmgalanakis committed May 9, 2024
1 parent c01da6c commit cb17f74
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 6 deletions.
8 changes: 8 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ inputs:
description: 'Passing extra arguments to the phpcs command'
required: false
default: ''
only_changed_files:
description: 'Run linter on changed files only'
required: false
default: ''
only_changed_lines:
description: 'Report errors only for changed lines'
required: false
default: ''
runs:
using: 'docker'
image: 'Dockerfile'
Expand Down
121 changes: 115 additions & 6 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,50 @@ cp /action/problem-matcher.json /github/workflow/problem-matcher.json

git clone --depth 1 -b 2.3.0 https://github.com/WordPress/WordPress-Coding-Standards.git ~/wpcs

INPUT_ONLY_CHANGED_FILES=${INPUT_ONLY_CHANGED_FILES:-${INPUT_ONLY_CHANGED_LINES:-"false"}}

if [ "${INPUT_ONLY_CHANGED_FILES}" = "true" ]; then
if [ "${GITHUB_EVENT_NAME}" = "pull_request" ]; then
COMPARE_FROM=origin/${GITHUB_BASE_REF}
COMPARE_TO=origin/${GITHUB_HEAD_REF}

COMPARE_FROM_REF=$(git merge-base "${COMPARE_FROM}" "${COMPARE_TO}")
COMPARE_TO_REF=${COMPARE_TO}
else
COMPARE_FROM="HEAD^"
COMPARE_TO="HEAD"
COMPARE_FROM_REF="HEAD^"
COMPARE_TO_REF="HEAD"
fi
echo "Will only check changed files (${COMPARE_FROM_REF} -> ${COMPARE_TO_REF})"
set +e
CHANGED_FILES=$(git diff --name-only --diff-filter=d "${COMPARE_FROM_REF}" "${COMPARE_TO_REF}" | xargs -rt ls -1d 2>/dev/null)
set -e
echo "Will check files:"
echo "${CHANGED_FILES}"
else
echo "Will check all files"
fi

if [ "${INPUT_STANDARD}" = "WordPress-VIP-Go" ] || [ "${INPUT_STANDARD}" = "WordPressVIPMinimum" ]; then
echo "Setting up VIPCS"
git clone --depth 1 -b 2.3.3 https://github.com/Automattic/VIP-Coding-Standards.git ${HOME}/vipcs
git clone https://github.com/sirbrillig/phpcs-variable-analysis ${HOME}/variable-analysis
${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/vipcs,${HOME}/variable-analysis"
if [ "${INPUT_ONLY_CHANGED_FILES}" = "true" ]; then
if [ "${INPUT_ONLY_CHANGED_LINES}" = "true" ]; then
step1=$(git diff -U0 --diff-filter=d "${COMPARE_FROM_REF}" "${COMPARE_TO_REF}")
step2=$(echo "${step1}" | diff-lines)
step3=$(echo "${step2}" | grep -ve ':-')
step4=$(echo "${step3}" | sed 's/:+.*//') # On some platforms, sed needs to have + escaped. This isn't the case for Alpine sed.
set +e
echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/vipcs,${HOME}/variable-analysis" | filter-by-changed-lines "${step4}"
set -e
else
echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/vipcs,${HOME}/variable-analysis"
fi
else
${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/vipcs,${HOME}/variable-analysis"
fi
elif [ "${INPUT_STANDARD}" = "10up-Default" ]; then
echo "Setting up 10up-Default"
git clone https://github.com/10up/phpcs-composer ${HOME}/10up
Expand All @@ -18,13 +57,55 @@ elif [ "${INPUT_STANDARD}" = "10up-Default" ]; then
git clone https://github.com/PHPCSStandards/PHPCSUtils ${HOME}/phpcsutils
git clone https://github.com/Automattic/VIP-Coding-Standards ${HOME}/vipcs
git clone https://github.com/sirbrillig/phpcs-variable-analysis ${HOME}/variable-analysis
${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/10up/10up-Default,${HOME}/phpcompatwp/PHPCompatibilityWP,${HOME}/phpcompat/PHPCompatibility,${HOME}/phpcompat-paragonie/PHPCompatibilityParagonieSodiumCompat,${HOME}/phpcompat-paragonie/PHPCompatibilityParagonieRandomCompat,${HOME}/phpcsutils/PHPCSUtils,${HOME}/vipcs,${HOME}/variable-analysis"
if [ "${INPUT_ONLY_CHANGED_FILES}" = "true" ]; then
if [ "${INPUT_ONLY_CHANGED_LINES}" = "true" ]; then
step1=$(git diff -U0 --diff-filter=d "${COMPARE_FROM_REF}" "${COMPARE_TO_REF}")
step2=$(echo "${step1}" | diff-lines)
step3=$(echo "${step2}" | grep -ve ':-')
step4=$(echo "${step3}" | sed 's/:+.*//') # On some platforms, sed needs to have + escaped. This isn't the case for Alpine sed.
set +e
echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/10up/10up-Default,${HOME}/phpcompatwp/PHPCompatibilityWP,${HOME}/phpcompat/PHPCompatibility,${HOME}/phpcompat-paragonie/PHPCompatibilityParagonieSodiumCompat,${HOME}/phpcompat-paragonie/PHPCompatibilityParagonieRandomCompat,${HOME}/phpcsutils/PHPCSUtils,${HOME}/vipcs,${HOME}/variable-analysis" | filter-by-changed-lines "${step4}"
set -e
else
echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/10up/10up-Default,${HOME}/phpcompatwp/PHPCompatibilityWP,${HOME}/phpcompat/PHPCompatibility,${HOME}/phpcompat-paragonie/PHPCompatibilityParagonieSodiumCompat,${HOME}/phpcompat-paragonie/PHPCompatibilityParagonieRandomCompat,${HOME}/phpcsutils/PHPCSUtils,${HOME}/vipcs,${HOME}/variable-analysis"
fi
else
${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/10up/10up-Default,${HOME}/phpcompatwp/PHPCompatibilityWP,${HOME}/phpcompat/PHPCompatibility,${HOME}/phpcompat-paragonie/PHPCompatibilityParagonieSodiumCompat,${HOME}/phpcompat-paragonie/PHPCompatibilityParagonieRandomCompat,${HOME}/phpcsutils/PHPCSUtils,${HOME}/vipcs,${HOME}/variable-analysis"
fi
elif [ -z "${INPUT_STANDARD_REPO}" ] || [ "${INPUT_STANDARD_REPO}" = "false" ]; then
${INPUT_PHPCS_BIN_PATH} --config-set installed_paths ~/wpcs
if [ "${INPUT_ONLY_CHANGED_FILES}" = "true" ]; then
if [ "${INPUT_ONLY_CHANGED_LINES}" = "true" ]; then
step1=$(git diff -U0 --diff-filter=d "${COMPARE_FROM_REF}" "${COMPARE_TO_REF}")
step2=$(echo "${step1}" | diff-lines)
step3=$(echo "${step2}" | grep -ve ':-')
step4=$(echo "${step3}" | sed 's/:+.*//') # On some platforms, sed needs to have + escaped. This isn't the case for Alpine sed.
set +e
echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths ~/wpcs | filter-by-changed-lines "${step4}"
set -e
else
echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths ~/wpcs
fi
else
${INPUT_PHPCS_BIN_PATH} --config-set installed_paths ~/wpcs
fi
else
echo "Standard repository: ${INPUT_STANDARD_REPO}"
git clone -b ${INPUT_REPO_BRANCH} ${INPUT_STANDARD_REPO} ${HOME}/cs
${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/cs"
if [ "${INPUT_ONLY_CHANGED_FILES}" = "true" ]; then
if [ "${INPUT_ONLY_CHANGED_LINES}" = "true" ]; then
step1=$(git diff -U0 --diff-filter=d "${COMPARE_FROM_REF}" "${COMPARE_TO_REF}")
step2=$(echo "${step1}" | diff-lines)
step3=$(echo "${step2}" | grep -ve ':-')
step4=$(echo "${step3}" | sed 's/:+.*//') # On some platforms, sed needs to have + escaped. This isn't the case for Alpine sed.
set +e
echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/cs" | filter-by-changed-lines "${step4}"
set -e
else
echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/cs"
fi
else
${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/cs"
fi
fi

if [ -z "${INPUT_EXCLUDES}" ]; then
Expand Down Expand Up @@ -53,9 +134,37 @@ else
fi

if [ "${HAS_CONFIG}" = true ] && [ "${INPUT_USE_LOCAL_CONFIG}" = "true" ] ; then
${INPUT_PHPCS_BIN_PATH} ${WARNING_FLAG} --report=checkstyle ${INPUT_EXTRA_ARGS}
if [ "${INPUT_ONLY_CHANGED_FILES}" = "true" ]; then
if [ "${INPUT_ONLY_CHANGED_LINES}" = "true" ]; then
step1=$(git diff -U0 --diff-filter=d "${COMPARE_FROM_REF}" "${COMPARE_TO_REF}")
step2=$(echo "${step1}" | diff-lines)
step3=$(echo "${step2}" | grep -ve ':-')
step4=$(echo "${step3}" | sed 's/:+.*//') # On some platforms, sed needs to have + escaped. This isn't the case for Alpine sed.
set +e
echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} ${WARNING_FLAG} --report=checkstyle ${INPUT_EXTRA_ARGS} | filter-by-changed-lines "${step4}"
set -e
else
echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} ${WARNING_FLAG} --report=checkstyle ${INPUT_EXTRA_ARGS}
fi
else
${INPUT_PHPCS_BIN_PATH} ${WARNING_FLAG} --report=checkstyle ${INPUT_EXTRA_ARGS}
fi
else
${INPUT_PHPCS_BIN_PATH} ${WARNING_FLAG} --report=checkstyle --standard=${INPUT_STANDARD} --ignore=${EXCLUDES} --extensions=php ${INPUT_PATHS} ${INPUT_EXTRA_ARGS}
if [ "${INPUT_ONLY_CHANGED_FILES}" = "true" ]; then
if [ "${INPUT_ONLY_CHANGED_LINES}" = "true" ]; then
step1=$(git diff -U0 --diff-filter=d "${COMPARE_FROM_REF}" "${COMPARE_TO_REF}")
step2=$(echo "${step1}" | diff-lines)
step3=$(echo "${step2}" | grep -ve ':-')
step4=$(echo "${step3}" | sed 's/:+.*//') # On some platforms, sed needs to have + escaped. This isn't the case for Alpine sed.
set +e
echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} ${WARNING_FLAG} --report=checkstyle --standard=${INPUT_STANDARD} --ignore=${EXCLUDES} --extensions=php ${INPUT_PATHS} ${INPUT_EXTRA_ARGS} | filter-by-changed-lines "${step4}"
set -e
else
echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} ${WARNING_FLAG} --report=checkstyle --standard=${INPUT_STANDARD} --ignore=${EXCLUDES} --extensions=php ${INPUT_PATHS} ${INPUT_EXTRA_ARGS}
fi
else
${INPUT_PHPCS_BIN_PATH} ${WARNING_FLAG} --report=checkstyle --standard=${INPUT_STANDARD} --ignore=${EXCLUDES} --extensions=php ${INPUT_PATHS} ${INPUT_EXTRA_ARGS}
fi
fi

status=$?
Expand Down

0 comments on commit cb17f74

Please sign in to comment.