-
-
Notifications
You must be signed in to change notification settings - Fork 20
/
script.sh
executable file
·115 lines (98 loc) · 4.22 KB
/
script.sh
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
#!/usr/bin/env bash
set -u
echo '::group:: Installing shellcheck ... https://github.com/koalaman/shellcheck'
TEMP_PATH="$(mktemp -d)"
cd "${TEMP_PATH}" || exit
mkdir bin
WINDOWS_TARGET=zip
LINUX_TARGET=linux.x86_64.tar.xz
MACOS_TARGET=darwin.x86_64.tar.xz
if [[ $(uname -s) == "Linux" ]]; then
curl -sL "https://github.com/koalaman/shellcheck/releases/download/v${SHELLCHECK_VERSION}/shellcheck-v${SHELLCHECK_VERSION}.${LINUX_TARGET}" | tar -xJf -
cp "shellcheck-v$SHELLCHECK_VERSION/shellcheck" ./bin
elif [[ $(uname -s) == "Darwin" ]]; then
curl -sL "https://github.com/koalaman/shellcheck/releases/download/v${SHELLCHECK_VERSION}/shellcheck-v${SHELLCHECK_VERSION}.${MACOS_TARGET}" | tar -xJf -
cp "shellcheck-v$SHELLCHECK_VERSION/shellcheck" ./bin
else
curl -sL "https://github.com/koalaman/shellcheck/releases/download/v${SHELLCHECK_VERSION}/shellcheck-v${SHELLCHECK_VERSION}.${WINDOWS_TARGET}" -o "shellcheck-v${SHELLCHECK_VERSION}.${WINDOWS_TARGET}" && unzip "shellcheck-v${SHELLCHECK_VERSION}.${WINDOWS_TARGET}" && rm "shellcheck-v${SHELLCHECK_VERSION}.${WINDOWS_TARGET}"
cp "shellcheck.exe" ./bin
fi
PATH="${TEMP_PATH}/bin:$PATH"
echo '::endgroup::'
cd "${GITHUB_WORKSPACE}" || exit
export REVIEWDOG_GITHUB_API_TOKEN="${INPUT_GITHUB_TOKEN}"
paths=()
while read -r pattern; do
[[ -n ${pattern} ]] && paths+=("${pattern}")
done <<< "${INPUT_PATH:-.}"
names=()
if [[ "${INPUT_PATTERN:-*}" != '*' ]]; then
while read -r pattern; do
[[ -n ${pattern} ]] && names+=(-o -name "${pattern}")
done <<< "${INPUT_PATTERN}"
(( ${#names[@]} )) && { names[0]='('; names+=(')'); }
fi
excludes=()
while read -r pattern; do
[[ -n ${pattern} ]] && excludes+=(-not -path "${pattern}")
done <<< "${INPUT_EXCLUDE:-}"
# Match all files matching the pattern
files_with_pattern=$(find "${paths[@]}" "${excludes[@]}" -type f "${names[@]}")
# Match all files with a shebang (e.g. "#!/usr/bin/env zsh" or even "#!bash") in the first line of a file
# Ignore files which match "$pattern" in order to avoid duplicates
if [ "${INPUT_CHECK_ALL_FILES_WITH_SHEBANGS}" = "true" ]; then
files_with_shebang=$(find "${paths[@]}" "${excludes[@]}" -not "${names[@]}" -type f -print0 | xargs -0 awk 'FNR==1 && /^#!.*sh/ { print FILENAME }')
fi
# Exit early if no files have been found
if [ -z "${files_with_pattern}" ] && [ -z "${files_with_shebang:-}" ]; then
echo "No matching files found to check."
exit 0
fi
FILES="${files_with_pattern} ${files_with_shebang:-}"
echo '::group:: Running shellcheck ...'
if [ "${INPUT_REPORTER}" = 'github-pr-review' ]; then
# erroformat: https://git.io/JeGMU
# shellcheck disable=SC2086
shellcheck -f json ${INPUT_SHELLCHECK_FLAGS:-'--external-sources'} ${FILES} \
| jq -r '.[] | "\(.file):\(.line):\(.column):\(.level):\(.message) [SC\(.code)](https://github.com/koalaman/shellcheck/wiki/SC\(.code))"' \
| reviewdog \
-efm="%f:%l:%c:%t%*[^:]:%m" \
-name="shellcheck" \
-reporter=github-pr-review \
-filter-mode="${INPUT_FILTER_MODE}" \
-fail-on-error="${INPUT_FAIL_ON_ERROR}" \
-level="${INPUT_LEVEL}" \
${INPUT_REVIEWDOG_FLAGS}
EXIT_CODE=$?
else
# github-pr-check,github-check (GitHub Check API) doesn't support markdown annotation.
# shellcheck disable=SC2086
shellcheck -f checkstyle ${INPUT_SHELLCHECK_FLAGS:-'--external-sources'} ${FILES} \
| reviewdog \
-f="checkstyle" \
-name="shellcheck" \
-reporter="${INPUT_REPORTER:-github-pr-check}" \
-filter-mode="${INPUT_FILTER_MODE}" \
-fail-on-error="${INPUT_FAIL_ON_ERROR}" \
-level="${INPUT_LEVEL}" \
${INPUT_REVIEWDOG_FLAGS}
EXIT_CODE=$?
fi
echo '::endgroup::'
echo '::group:: Running shellcheck (suggestion) ...'
# -reporter must be github-pr-review for the suggestion feature.
# shellcheck disable=SC2086
shellcheck -f diff ${FILES} \
| reviewdog \
-name="shellcheck (suggestion)" \
-f=diff \
-f.diff.strip=1 \
-reporter="github-pr-review" \
-filter-mode="${INPUT_FILTER_MODE}" \
-fail-on-error="${INPUT_FAIL_ON_ERROR}" \
${INPUT_REVIEWDOG_FLAGS}
EXIT_CODE_SUGGESTION=$?
echo '::endgroup::'
if [ "${EXIT_CODE}" -ne 0 ] || [ "${EXIT_CODE_SUGGESTION}" -ne 0 ]; then
exit $((EXIT_CODE + EXIT_CODE_SUGGESTION))
fi