generated from reviewdog/action-template
-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
script.sh
executable file
·180 lines (150 loc) · 5.57 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
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
#!/bin/bash
# - Exit with error on any failed command.
# - Any unset variable is an immediate error.
# - Show trace executed statements
# - Show the executed script before executing it.
set -euxv
BASE_PATH="$(cd "$(dirname "$0")" && pwd)"
# shellcheck disable=SC2086,SC2089,SC2090
cd "${GITHUB_WORKSPACE}/${INPUT_WORKDIR}" || exit
TEMP_PATH="$(mktemp -d)"
PATH="${TEMP_PATH}:$PATH"
echo '::group::🐶 Installing reviewdog ... https://github.com/reviewdog/reviewdog'
curl -sfL https://raw.githubusercontent.com/reviewdog/reviewdog/master/install.sh | sh -s -- -b "${TEMP_PATH}" "${REVIEWDOG_VERSION}" 2>&1
echo '::endgroup::'
# check setup method
SETUP="false"
case "${INPUT_SETUP_METHOD}" in
"nothing")
SETUP="false"
;;
"install")
SETUP="true"
;;
*)
# adaptive and other invalid value
# Check execute_command is valid.
echo '::group:: Check command is executable'
echo "Execute command with version option: ${INPUT_EXECUTE_COMMAND} --version"
if ${INPUT_EXECUTE_COMMAND} --version > /dev/null 2>&1 ; then
echo 'Success command execution, skip installation.'
SETUP="false"
else
echo 'Failure command execution, execute installation.'
SETUP="true"
fi
echo '::endgroup::'
;;
esac
# Install mypy if needed.
if [[ "${SETUP}" == "true" ]] ; then
echo '::group:: Installing mypy ... https://github.com/python/mypy'
echo "Execute setup: ${INPUT_SETUP_COMMAND}"
${INPUT_SETUP_COMMAND}
echo '::endgroup::'
fi
echo '::group:: Prepare reviewdog/mypy'
# Version output.
echo "Execute command and version: ${INPUT_EXECUTE_COMMAND}"
${INPUT_EXECUTE_COMMAND} --version
export REVIEWDOG_GITHUB_API_TOKEN="${INPUT_GITHUB_TOKEN}"
# safe extract files/dirs
TARGETS_LIST="${INPUT_TARGET:-.}"
echo '::endgroup::'
# pre-run(missing stub detect)
if [[ "${INPUT_INSTALL_TYPES}" == "true" ]] ; then
echo '::group:: Installing types'
echo 'Pre-run and detect missing stubs'
# shellcheck disable=SC2086
mypy_check_output="$(${INPUT_EXECUTE_COMMAND} \
${TARGETS_LIST} 2>&1 \
)" || mypy_exit_val="$?"
# discard result
echo 'Install types'
${INPUT_EXECUTE_COMMAND} --install-types --non-interactive
echo '::endgroup::'
fi
# cleanup function
cleanup() {
if [ -n "${MYPYTMPDIR:-}" ] && [ -d "${MYPYTMPDIR:-}" ]; then
rm -rf "$MYPYTMPDIR"
fi
}
MYPYTMPDIR=$(mktemp -d)
trap cleanup EXIT
echo '::group:: Running mypy with reviewdog 🐶 ...'
mypy_exit_val="0"
reviewdog_exit_val="0"
# Below from this line, errors are handled.
# (mypy_exit_val and reviewdog_exit_val)
# Disable error report
set +e
# lint check
# Flags
# first, user flags
# second, set reviewdog supplement flags(abspath, column num) and suppress pretty flag
# same flag: win later
if [[ "${INPUT_OUTPUT_JSON}" != "true" ]] ; then
# Do not use JSON output
# shellcheck disable=SC2086
mypy_check_output="$(${INPUT_EXECUTE_COMMAND} \
${INPUT_MYPY_FLAGS} \
--show-column-numbers \
--show-absolute-path \
--no-pretty \
${TARGETS_LIST} 2>&1 \
)" || mypy_exit_val="$?"
# note ignore
IGNORE_NOTE_EFM_OPTION=("-efm=%-G%f:%l:%c: note: %m")
# shellcheck disable=SC2086
echo "${mypy_check_output}" | reviewdog \
"${IGNORE_NOTE_EFM_OPTION[@]}" \
-efm="%f:%l:%c: %t%*[^:]: %m" \
-efm="%f:%l: %t%*[^:]: %m" \
-efm="%f: %t%*[^:]: %m" \
-name="${INPUT_TOOL_NAME:-mypy}" \
-reporter="${INPUT_REPORTER:-github-pr-check}" \
-filter-mode="${INPUT_FILTER_MODE}" \
-fail-on-error="${INPUT_FAIL_ON_ERROR}" \
-level="${INPUT_LEVEL}" \
${INPUT_REVIEWDOG_FLAGS} || reviewdog_exit_val="$?"
else
# Use JSON output
# require mypy==1.11 or higher
# --hide-error-context : suppress error context NOTE: entry
# shellcheck disable=SC2086
${INPUT_EXECUTE_COMMAND} \
${INPUT_MYPY_FLAGS} \
--output json \
--hide-error-context \
--show-column-numbers \
--show-absolute-path \
--no-pretty \
${TARGETS_LIST} \
> ${MYPYTMPDIR}/mypy_output.json \
2> /dev/null \
|| mypy_exit_val="$?"
# echo "mypy output result:"
# cat "${MYPYTMPDIR}/mypy_output.json"
python3 "${BASE_PATH}/mypy_to_rdjson/mypy_to_rdjson.py" < "${MYPYTMPDIR}/mypy_output.json" > "${MYPYTMPDIR}/mypy_rdjson.json"
# echo "mypy output rdjson:"
# cat "${MYPYTMPDIR}/mypy_rdjson.json"
# shellcheck disable=SC2086
reviewdog \
-f=rdjson \
-name="${INPUT_TOOL_NAME:-mypy}" \
-reporter="${INPUT_REPORTER:-github-pr-check}" \
-filter-mode="${INPUT_FILTER_MODE}" \
-fail-on-error="${INPUT_FAIL_ON_ERROR}" \
-level="${INPUT_LEVEL}" \
${INPUT_REVIEWDOG_FLAGS} < "${MYPYTMPDIR}/mypy_rdjson.json" \
|| reviewdog_exit_val="$?"
fi
echo '::endgroup::'
# Throw error if an error occurred and fail_on_error is true
if [[ "${INPUT_FAIL_ON_ERROR}" == "true" \
&& ( "${mypy_exit_val}" != "0" \
|| "${reviewdog_exit_val}" != "0" ) \
]]; then
exit 1
fi