From 4b4f2877290813633ab4a43cd93e2468f7020c55 Mon Sep 17 00:00:00 2001 From: Konstantinos Galanakis Date: Thu, 9 May 2024 15:53:25 +0300 Subject: [PATCH 1/6] PHPCS only on changed files and lines --- action.yml | 8 +++ entrypoint.sh | 190 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 191 insertions(+), 7 deletions(-) diff --git a/action.yml b/action.yml index e3dc303..7cdf682 100644 --- a/action.yml +++ b/action.yml @@ -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' diff --git a/entrypoint.sh b/entrypoint.sh index 2c21f81..f7defd7 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -4,11 +4,99 @@ 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 +git config --global --add safe.directory $(pwd) + +diff_lines() { + path="" + line="" + while IFS= read -r REPLY; do + esc=$(printf '\033') + if echo "$REPLY" | grep -qE '\-\-\- (a/)?.*'; then + continue + elif echo "$REPLY" | grep -qE '\+\+\+ (b/)?([^[:blank:]'"$esc"']+).*'; then + path=$(echo "$REPLY" | sed -E 's/\+\+\+ (b\/)?([^[:blank:]'"$esc"']+).*/\2/') + elif echo "$REPLY" | grep -qE '@@ -[0-9]+(,[0-9]+)? \+([0-9]+)(,[0-9]+)? @@.*'; then + line=$(echo "$REPLY" | sed -E 's/@@ -[0-9]+(,[0-9]+)? \+([0-9]+)(,[0-9]+)? @@.*/\2/') + elif echo "$REPLY" | grep -qE '^('"$esc"'\[[0-9;]*m)*([\\ +-])'; then + echo "$path:$line:$REPLY" + if echo "${REPLY}" | grep -qE '^('"$esc"'\[[0-9;]*m)*([\ +-])'; then + line=$((line+1)) + fi + fi + done +} + +filter_by_changed_lines() { + changedLines="$1" + fileName= + fileLine= + exitCode=0 + + while IFS= read -r line; do + if echo "$line" | grep -q -E ' ${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. + step5=$(echo "${step4}" | sed 's/:\\.*//') + 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 "${step5}" + status=$? + set -e + else + echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/vipcs,${HOME}/variable-analysis" + status=$? + fi + else + ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/vipcs,${HOME}/variable-analysis" + status=$? + fi elif [ "${INPUT_STANDARD}" = "10up-Default" ]; then echo "Setting up 10up-Default" git clone https://github.com/10up/phpcs-composer ${HOME}/10up @@ -18,13 +106,67 @@ 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. + step5=$(echo "${step4}" | sed 's/:\\.*//') + 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 "${step5}" + status=$? + 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" + status=$? + 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" + status=$? + 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. + step5=$(echo "${step4}" | sed 's/:\\.*//') + set +e + echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths ~/wpcs | filter_by_changed_lines "${step5}" + status=$? + set -e + else + echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths ~/wpcs + status=$? + fi + else + ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths ~/wpcs + status=$? + 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. + step5=$(echo "${step4}" | sed 's/:\\.*//') + set +e + echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/cs" | filter_by_changed_lines "${step5}" + status=$? + set -e + else + echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/cs" + status=$? + fi + else + ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/cs" + status=$? + fi fi if [ -z "${INPUT_EXCLUDES}" ]; then @@ -53,13 +195,47 @@ 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. + step5=$(echo "${step4}" | sed 's/:\\.*//') + set +e + echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} ${WARNING_FLAG} --report=checkstyle ${INPUT_EXTRA_ARGS} | filter_by_changed_lines "${step5}" + status=$? + set -e + else + echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} ${WARNING_FLAG} --report=checkstyle ${INPUT_EXTRA_ARGS} + status=$? + fi + else + ${INPUT_PHPCS_BIN_PATH} ${WARNING_FLAG} --report=checkstyle ${INPUT_EXTRA_ARGS} + status=$? + fi else + 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. + step5=$(echo "${step4}" | sed 's/:\\.*//') + set +e + echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} ${WARNING_FLAG} --report=checkstyle --standard=${INPUT_STANDARD} --extensions=php ${INPUT_EXTRA_ARGS} | filter_by_changed_lines "${step5}" + status=$? + set -e + else + echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} ${WARNING_FLAG} --report=checkstyle --standard=${INPUT_STANDARD} --extensions=php ${INPUT_EXTRA_ARGS} + status=$? + fi + else ${INPUT_PHPCS_BIN_PATH} ${WARNING_FLAG} --report=checkstyle --standard=${INPUT_STANDARD} --ignore=${EXCLUDES} --extensions=php ${INPUT_PATHS} ${INPUT_EXTRA_ARGS} + status=$? + fi fi -status=$? - echo "::remove-matcher owner=phpcs::" exit $status From a61b08982197ff1403103d5f09b11d485d312d8c Mon Sep 17 00:00:00 2001 From: Konstantinos Galanakis Date: Fri, 10 May 2024 11:07:22 +0300 Subject: [PATCH 2/6] Calculate diff files with a function --- entrypoint.sh | 52 ++++++++++++++++----------------------------------- 1 file changed, 16 insertions(+), 36 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index f7defd7..38cc97b 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -49,6 +49,16 @@ filter_by_changed_lines() { exit "$exitCode" } +clean_diff_output() { + 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. + step5=$(echo "${step4}" | sed 's/:\\.*//') + + echo "${step5}" +} + INPUT_ONLY_CHANGED_FILES=${INPUT_ONLY_CHANGED_FILES:-${INPUT_ONLY_CHANGED_LINES:-"false"}} if [ "${INPUT_ONLY_CHANGED_FILES}" = "true" ]; then @@ -80,13 +90,8 @@ if [ "${INPUT_STANDARD}" = "WordPress-VIP-Go" ] || [ "${INPUT_STANDARD}" = "Word git clone https://github.com/sirbrillig/phpcs-variable-analysis ${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. - step5=$(echo "${step4}" | sed 's/:\\.*//') 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 "${step5}" + echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/vipcs,${HOME}/variable-analysis" | filter_by_changed_lines "${clean_diff_output}" status=$? set -e else @@ -108,13 +113,8 @@ elif [ "${INPUT_STANDARD}" = "10up-Default" ]; then git clone https://github.com/sirbrillig/phpcs-variable-analysis ${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. - step5=$(echo "${step4}" | sed 's/:\\.*//') 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 "${step5}" + 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 "${clean_diff_output}" status=$? set -e else @@ -128,13 +128,8 @@ elif [ "${INPUT_STANDARD}" = "10up-Default" ]; then elif [ -z "${INPUT_STANDARD_REPO}" ] || [ "${INPUT_STANDARD_REPO}" = "false" ]; then 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. - step5=$(echo "${step4}" | sed 's/:\\.*//') set +e - echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths ~/wpcs | filter_by_changed_lines "${step5}" + echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths ~/wpcs | filter_by_changed_lines "${clean_diff_output}" status=$? set -e else @@ -150,13 +145,8 @@ else git clone -b ${INPUT_REPO_BRANCH} ${INPUT_STANDARD_REPO} ${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. - step5=$(echo "${step4}" | sed 's/:\\.*//') set +e - echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/cs" | filter_by_changed_lines "${step5}" + echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/cs" | filter_by_changed_lines "${clean_diff_output}" status=$? set -e else @@ -197,13 +187,8 @@ fi if [ "${HAS_CONFIG}" = true ] && [ "${INPUT_USE_LOCAL_CONFIG}" = "true" ] ; then 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. - step5=$(echo "${step4}" | sed 's/:\\.*//') set +e - echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} ${WARNING_FLAG} --report=checkstyle ${INPUT_EXTRA_ARGS} | filter_by_changed_lines "${step5}" + echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} ${WARNING_FLAG} --report=checkstyle ${INPUT_EXTRA_ARGS} | filter_by_changed_lines "${clean_diff_output}" status=$? set -e else @@ -217,13 +202,8 @@ if [ "${HAS_CONFIG}" = true ] && [ "${INPUT_USE_LOCAL_CONFIG}" = "true" ] ; then else 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. - step5=$(echo "${step4}" | sed 's/:\\.*//') set +e - echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} ${WARNING_FLAG} --report=checkstyle --standard=${INPUT_STANDARD} --extensions=php ${INPUT_EXTRA_ARGS} | filter_by_changed_lines "${step5}" + echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} ${WARNING_FLAG} --report=checkstyle --standard=${INPUT_STANDARD} --extensions=php ${INPUT_EXTRA_ARGS} | filter_by_changed_lines "$(clean_diff_output)" status=$? set -e else From 3811af0c97060ce1e6b87c82a8efaddaa27fab63 Mon Sep 17 00:00:00 2001 From: Konstantinos Galanakis Date: Fri, 10 May 2024 17:13:03 +0300 Subject: [PATCH 3/6] Remove redundant status set --- entrypoint.sh | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 38cc97b..8a8a380 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -92,15 +92,12 @@ if [ "${INPUT_STANDARD}" = "WordPress-VIP-Go" ] || [ "${INPUT_STANDARD}" = "Word if [ "${INPUT_ONLY_CHANGED_LINES}" = "true" ]; then 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 "${clean_diff_output}" - status=$? set -e else echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/vipcs,${HOME}/variable-analysis" - status=$? fi else ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/vipcs,${HOME}/variable-analysis" - status=$? fi elif [ "${INPUT_STANDARD}" = "10up-Default" ]; then echo "Setting up 10up-Default" @@ -115,30 +112,24 @@ elif [ "${INPUT_STANDARD}" = "10up-Default" ]; then if [ "${INPUT_ONLY_CHANGED_LINES}" = "true" ]; then 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 "${clean_diff_output}" - status=$? 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" - status=$? 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" - status=$? fi elif [ -z "${INPUT_STANDARD_REPO}" ] || [ "${INPUT_STANDARD_REPO}" = "false" ]; then if [ "${INPUT_ONLY_CHANGED_FILES}" = "true" ]; then if [ "${INPUT_ONLY_CHANGED_LINES}" = "true" ]; then set +e echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths ~/wpcs | filter_by_changed_lines "${clean_diff_output}" - status=$? set -e else echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths ~/wpcs - status=$? fi else ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths ~/wpcs - status=$? fi else echo "Standard repository: ${INPUT_STANDARD_REPO}" @@ -147,15 +138,12 @@ else if [ "${INPUT_ONLY_CHANGED_LINES}" = "true" ]; then set +e echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/cs" | filter_by_changed_lines "${clean_diff_output}" - status=$? set -e else echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/cs" - status=$? fi else ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/cs" - status=$? fi fi From ab7ec3fa19715edd77a503ef26b5db6d078fc339 Mon Sep 17 00:00:00 2001 From: Konstantinos Galanakis Date: Fri, 10 May 2024 17:32:06 +0300 Subject: [PATCH 4/6] Convert multiple usages of set of commands to a function --- entrypoint.sh | 71 ++++++++++++++++++--------------------------------- 1 file changed, 25 insertions(+), 46 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 8a8a380..8d09a66 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -59,6 +59,22 @@ clean_diff_output() { echo "${step5}" } +decide_all_files_or_changed() { + standards="$1" + + if [ "${INPUT_ONLY_CHANGED_FILES}" = "true" ]; then + if [ "${INPUT_ONLY_CHANGED_LINES}" = "true" ]; then + set +e + echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${standards}" | filter_by_changed_lines "${clean_diff_output}" + set -e + else + echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${standards}" + fi + else + ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${standards}" + fi +} + INPUT_ONLY_CHANGED_FILES=${INPUT_ONLY_CHANGED_FILES:-${INPUT_ONLY_CHANGED_LINES:-"false"}} if [ "${INPUT_ONLY_CHANGED_FILES}" = "true" ]; then @@ -88,17 +104,8 @@ if [ "${INPUT_STANDARD}" = "WordPress-VIP-Go" ] || [ "${INPUT_STANDARD}" = "Word 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 - if [ "${INPUT_ONLY_CHANGED_FILES}" = "true" ]; then - if [ "${INPUT_ONLY_CHANGED_LINES}" = "true" ]; then - 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 "${clean_diff_output}" - 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 + + decide_all_files_or_changed "${HOME}/wpcs,${HOME}/vipcs,${HOME}/variable-analysis" elif [ "${INPUT_STANDARD}" = "10up-Default" ]; then echo "Setting up 10up-Default" git clone https://github.com/10up/phpcs-composer ${HOME}/10up @@ -108,43 +115,15 @@ 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 - if [ "${INPUT_ONLY_CHANGED_FILES}" = "true" ]; then - if [ "${INPUT_ONLY_CHANGED_LINES}" = "true" ]; then - 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 "${clean_diff_output}" - 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 + + decide_all_files_or_changed "${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" elif [ -z "${INPUT_STANDARD_REPO}" ] || [ "${INPUT_STANDARD_REPO}" = "false" ]; then - if [ "${INPUT_ONLY_CHANGED_FILES}" = "true" ]; then - if [ "${INPUT_ONLY_CHANGED_LINES}" = "true" ]; then - set +e - echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths ~/wpcs | filter_by_changed_lines "${clean_diff_output}" - 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 + decide_all_files_or_changed "~/wpcs" else - echo "Standard repository: ${INPUT_STANDARD_REPO}" - git clone -b ${INPUT_REPO_BRANCH} ${INPUT_STANDARD_REPO} ${HOME}/cs - if [ "${INPUT_ONLY_CHANGED_FILES}" = "true" ]; then - if [ "${INPUT_ONLY_CHANGED_LINES}" = "true" ]; then - set +e - echo "${CHANGED_FILES}" | xargs -rt ${INPUT_PHPCS_BIN_PATH} --config-set installed_paths "${HOME}/wpcs,${HOME}/cs" | filter_by_changed_lines "${clean_diff_output}" - 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 + echo "Standard repository: ${INPUT_STANDARD_REPO}" + git clone -b ${INPUT_REPO_BRANCH} ${INPUT_STANDARD_REPO} ${HOME}/cs + + decide_all_files_or_changed "${HOME}/wpcs,${HOME}/cs" fi if [ -z "${INPUT_EXCLUDES}" ]; then From 8193c65201bb5828bb5261fcc91c6a1a47fc48e3 Mon Sep 17 00:00:00 2001 From: Konstantinos Galanakis Date: Sat, 11 May 2024 00:11:55 +0300 Subject: [PATCH 5/6] Add the new args and an example to the README file --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index d5fe11a..00ea62f 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,8 @@ Available options (with default value): phpcs_bin_path: 'phpcs' # Custom PHPCS bin path use_local_config: 'false' # Use local config if available extra_args: '' # Extra arguments passing to the command + only_changed_files: '' # Run the linter only on the changed files. Accepts true|false + only_changed_lines: '' # Run the linter only on the changed lines. Accepts true|false ``` ## Examples @@ -137,6 +139,28 @@ jobs: standard: 'WordPress' # Standard to use. Accepts WordPress|WordPress-Core|WordPress-Docs|WordPress-Extra|WordPress-VIP-Go|WordPressVIPMinimum|10up-Default. use_local_config: 'true' ``` +### Run linter on changed lines + +```yaml +name: WPCS check + +on: pull_request + +jobs: + phpcs: + name: WPCS + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 # This is very important. Without this the linting of only the changed lines will not work. + - name: WPCS check + uses: 10up/wpcs-action@stable + with: + standard: 10up-Default + enable_warnings: true + only_changed_lines: true +``` ## Support Level From 0b489c210a18d495a1ec11620547b6782979c76a Mon Sep 17 00:00:00 2001 From: Konstantinos Galanakis Date: Wed, 29 May 2024 14:20:03 +0300 Subject: [PATCH 6/6] Clone the 1.0.11 tag of PHPCSUtils because v1.0.12 throws errors related to undefined constants --- entrypoint.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/entrypoint.sh b/entrypoint.sh index 8d09a66..4116d7c 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -112,7 +112,7 @@ elif [ "${INPUT_STANDARD}" = "10up-Default" ]; then git clone https://github.com/PHPCompatibility/PHPCompatibilityWP ${HOME}/phpcompatwp git clone https://github.com/PHPCompatibility/PHPCompatibility ${HOME}/phpcompat git clone https://github.com/PHPCompatibility/PHPCompatibilityParagonie ${HOME}/phpcompat-paragonie - git clone https://github.com/PHPCSStandards/PHPCSUtils ${HOME}/phpcsutils + git clone --depth 1 --branch 1.0.11 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