forked from chekalsky/phpcs-action
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathentrypoint.sh
More file actions
189 lines (162 loc) · 7.07 KB
/
entrypoint.sh
File metadata and controls
189 lines (162 loc) · 7.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/bin/sh
cp /action/problem-matcher.json /github/workflow/problem-matcher.json
git config --global --add safe.directory $(pwd)
composer global config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
composer global require --dev wp-coding-standards/wpcs:"^3.1.0" --update-with-dependencies
composer global require --dev 10up/phpcs-composer:"^3.0"
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 '<file name="(\/github\/workspace\/)?([^"]+)'; then
fileName=$(echo "$line" | sed -E 's/.*<file name="(\/github\/workspace\/)?([^"]+).*/\2/')
echo "$line"
elif echo "$line" | grep -q -E '<error line="([^"]+)'; then
fileLine=$(echo "$line" | sed -E 's/.*<error line="([^"]+).*/\1/')
if echo "$changedLines" | grep -qx "$fileName:$fileLine"; then
exitCode=1
echo "$line"
fi
else
echo "$line"
fi
done
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}"
}
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
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
COMPOSER_HOME="$(composer config home)/vendor"
WPCS_PATH="$(composer config home)/vendor/wp-coding-standards/wpcs,$(composer config home)/vendor/phpcsstandards/phpcsutils,$(composer config home)/vendor/phpcsstandards/phpcsextra"
if [ "${INPUT_STANDARD}" = "WordPress-VIP-Go" ] || [ "${INPUT_STANDARD}" = "WordPressVIPMinimum" ]; then
echo "Setting up VIPCS"
decide_all_files_or_changed "${WPCS_PATH},${COMPOSER_HOME}/automattic/vipwpcs,${COMPOSER_HOME}/sirbrillig/phpcs-variable-analysis"
elif [ "${INPUT_STANDARD}" = "10up-Default" ]; then
echo "Setting up 10up-Default"
decide_all_files_or_changed "${WPCS_PATH},${COMPOSER_HOME}/10up/phpcs-composer,${COMPOSER_HOME}/phpcompatibility/phpcompatibility-wp,${COMPOSER_HOME}/phpcompatibility/php-compatibility,${COMPOSER_HOME}/phpcompatibility/phpcompatibility-paragonie,${COMPOSER_HOME}/automattic/vipwpcs,${COMPOSER_HOME}/sirbrillig/phpcs-variable-analysis"
# Add the phpcs -i command to list installed standards
echo "Installed coding standards:"
phpcs -i
elif [ -z "${INPUT_STANDARD_REPO}" ] || [ "${INPUT_STANDARD_REPO}" = "false" ]; then
echo "Setting up default WPCS"
decide_all_files_or_changed "${WPCS_PATH}"
else
echo "Standard repository: ${INPUT_STANDARD_REPO}"
git clone -b ${INPUT_REPO_BRANCH} ${INPUT_STANDARD_REPO} ${HOME}/cs
decide_all_files_or_changed "${WPCS_PATH},${HOME}/cs"
fi
if [ -z "${INPUT_EXCLUDES}" ]; then
EXCLUDES="node_modules,vendor"
else
EXCLUDES="node_modules,vendor,${INPUT_EXCLUDES}"
fi
phpcs -i
echo "::add-matcher::${RUNNER_TEMP}/_github_workflow/problem-matcher.json"
if [ -z "${INPUT_ENABLE_WARNINGS}" ] || [ "${INPUT_ENABLE_WARNINGS}" = "false" ]; then
WARNING_FLAG="-n"
echo "Check for warnings disabled"
else
WARNING_FLAG=""
echo "Check for warnings enabled"
fi
# .phpcs.xml, phpcs.xml, .phpcs.xml.dist, phpcs.xml.dist
if [ -f ".phpcs.xml" ] || [ -f "phpcs.xml" ] || [ -f ".phpcs.xml.dist" ] || [ -f "phpcs.xml.dist" ]; then
HAS_CONFIG=true
else
HAS_CONFIG=false
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
set +e
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
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
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 "$(clean_diff_output)"
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
echo "::remove-matcher owner=phpcs::"
exit $status