-
Notifications
You must be signed in to change notification settings - Fork 75
/
script.sh
executable file
·142 lines (119 loc) · 4.8 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
#!/usr/bin/env bash
set -e
set -o pipefail
cd "${GITHUB_WORKSPACE}/${INPUT_WORKDIR}" || exit
export REVIEWDOG_GITHUB_API_TOKEN="${INPUT_GITHUB_TOKEN}"
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::'
if [ "${INPUT_SKIP_INSTALL}" = "false" ]; then
echo '::group:: Installing rubocop with extensions ... https://github.com/rubocop/rubocop'
# if 'gemfile' rubocop version selected
if [ "${INPUT_RUBOCOP_VERSION}" = "gemfile" ]; then
# if Gemfile.lock is here
if [ -f 'Gemfile.lock' ]; then
# grep for rubocop version
RUBOCOP_GEMFILE_VERSION=$(ruby -ne 'print $& if /^\s{4}rubocop\s\(\K.*(?=\))/' Gemfile.lock)
# if rubocop version found, then pass it to the gem install
# left it empty otherwise, so no version will be passed
if [ -n "$RUBOCOP_GEMFILE_VERSION" ]; then
RUBOCOP_VERSION=$RUBOCOP_GEMFILE_VERSION
else
printf "Cannot get the rubocop's version from Gemfile.lock. The latest version will be installed."
fi
else
printf 'Gemfile.lock not found. The latest version will be installed.'
fi
else
# set desired rubocop version
RUBOCOP_VERSION=$INPUT_RUBOCOP_VERSION
fi
gem install -N rubocop --version "${RUBOCOP_VERSION}"
# Traverse over list of rubocop extensions
for extension in $INPUT_RUBOCOP_EXTENSIONS; do
# grep for name and version
INPUT_RUBOCOP_EXTENSION_NAME=$(echo "$extension" |awk 'BEGIN { FS = ":" } ; { print $1 }')
INPUT_RUBOCOP_EXTENSION_VERSION=$(echo "$extension" |awk 'BEGIN { FS = ":" } ; { print $2 }')
# if version is 'gemfile'
if [ "${INPUT_RUBOCOP_EXTENSION_VERSION}" = "gemfile" ]; then
# if Gemfile.lock is here
if [ -f 'Gemfile.lock' ]; then
# grep for rubocop extension version
RUBOCOP_EXTENSION_GEMFILE_VERSION=$(ruby -ne "print $& if /^\s{4}$INPUT_RUBOCOP_EXTENSION_NAME\s\(\K.*(?=\))/" Gemfile.lock)
# if rubocop extension version found, then pass it to the gem install
# left it empty otherwise, so no version will be passed
if [ -n "$RUBOCOP_EXTENSION_GEMFILE_VERSION" ]; then
RUBOCOP_EXTENSION_VERSION=$RUBOCOP_EXTENSION_GEMFILE_VERSION
else
printf "Cannot get the rubocop extension version from Gemfile.lock. The latest version will be installed."
fi
else
printf 'Gemfile.lock not found. The latest version will be installed.'
fi
else
# set desired rubocop extension version
RUBOCOP_EXTENSION_VERSION=$INPUT_RUBOCOP_EXTENSION_VERSION
fi
# Handle extensions with no version qualifier
if [ -z "${RUBOCOP_EXTENSION_VERSION}" ]; then
unset RUBOCOP_EXTENSION_VERSION_FLAG
else
RUBOCOP_EXTENSION_VERSION_FLAG="--version ${RUBOCOP_EXTENSION_VERSION}"
fi
# shellcheck disable=SC2086
gem install -N "${INPUT_RUBOCOP_EXTENSION_NAME}" ${RUBOCOP_EXTENSION_VERSION_FLAG}
done
echo '::endgroup::'
fi
export REVIEWDOG_GITHUB_API_TOKEN="${INPUT_GITHUB_TOKEN}"
if [ "${INPUT_USE_BUNDLER}" = "false" ]; then
BUNDLE_EXEC=""
else
BUNDLE_EXEC="bundle exec "
fi
if [ "${INPUT_ONLY_CHANGED}" = "true" ]; then
echo '::group:: Getting changed files list'
# check if commit is present in repository, otherwise fetch it
if ! git cat-file -e "${BASE_REF}"; then
git fetch --depth 1 origin "${BASE_REF}"
fi
# get intersection of changed files (excluding deleted) with target files for
# rubocop as an array
# shellcheck disable=SC2086
readarray -t CHANGED_FILES < <(
comm -12 \
<(git diff --diff-filter=d --name-only "${BASE_REF}..${HEAD_REF}" | sort || kill $$) \
<(${BUNDLE_EXEC}rubocop --list-target-files | sort || kill $$)
)
if (( ${#CHANGED_FILES[@]} == 0 )); then
echo "No relevant files for rubocop, skipping"
exit 0
fi
printf '%s\n' "${CHANGED_FILES[@]}"
if (( ${#CHANGED_FILES[@]} > 100 )); then
echo "More than 100 changed files (${#CHANGED_FILES[@]}), running rubocop on all files"
unset CHANGED_FILES
fi
echo '::endgroup::'
fi
echo '::group:: Running rubocop with reviewdog 🐶 ...'
# shellcheck disable=SC2086
${BUNDLE_EXEC}rubocop \
--require ${GITHUB_ACTION_PATH}/rdjson_formatter/rdjson_formatter.rb \
--format RdjsonFormatter \
--fail-level error \
${INPUT_RUBOCOP_FLAGS} \
"${CHANGED_FILES[@]}" \
| reviewdog -f=rdjson \
-name="${INPUT_TOOL_NAME}" \
-reporter="${INPUT_REPORTER}" \
-filter-mode="${INPUT_FILTER_MODE}" \
-fail-level="${INPUT_FAIL_LEVEL}" \
-fail-on-error="${INPUT_FAIL_ON_ERROR}" \
-level="${INPUT_LEVEL}" \
${INPUT_REVIEWDOG_FLAGS}
reviewdog_rc=$?
echo '::endgroup::'
exit $reviewdog_rc