-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaction.yml
More file actions
204 lines (189 loc) · 7.69 KB
/
action.yml
File metadata and controls
204 lines (189 loc) · 7.69 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
---
# SPDX-FileCopyrightText: 2021 ale5000
# SPDX-License-Identifier: LGPL-3.0-or-later
name: "ShellChecker"
author: "ale5000"
description: "GitHub action to execute a lint check of all shell scripts using ShellCheck."
branding:
icon: "check-circle"
color: "green"
inputs:
shellcheck-version:
description: "Version of ShellCheck to use. Values: [stable, latest, v0.8.0, ...]"
required: false
default: "stable"
severity:
description: "Minimum severity of issues to display. Values: [style, info, warning, error]"
required: false
enable-all-opt-checks:
description: "Enable all optional checks. Values: [true, false]"
required: false
default: "false"
ignore-files:
description: "List of files to ignore, the separator is: |"
required: false
default: "gradlew"
verbose-logging:
description: "Enable detailed logging."
required: false
default: "false"
outputs:
NumFilesWthIssues:
description: "Number of files with issues"
value: "${{ steps.shellcheck-scan.outputs.numfileswithissues }}"
ListFilesWithIssues:
description: "A list of files with issues"
value: "${{ steps.shellcheck-scan.outputs.listfileswithissues }}"
runs:
using: "composite"
steps:
- name: "Set up matchers"
shell: bash
env:
ACTION_PATH: "${{ github.action_path }}"
run: |
# Setting up matchers...
matcher_file="${ACTION_PATH:?}/.github/matchers/tty/shellcheck-matcher-legacy.json"
if test -f "${matcher_file:?}"; then
echo "::add-matcher::${matcher_file}"
echo 'Matcher 1 configured.'
fi
matcher_file="${ACTION_PATH:?}/.github/matchers/tty/shellcheck-matcher.json"
if test -f "${matcher_file:?}"; then
echo "::add-matcher::${matcher_file}"
echo 'Matcher 2 configured.'
fi
- name: "Download ShellCheck"
shell: bash
env:
RUNNER_OS: "${{ runner.os }}"
ACTION_PATH: "${{ github.action_path }}"
SHELLCHECK_VER: "${{ inputs.shellcheck-version }}"
run: |
# Downloading ShellCheck...
BASE_LINE='62'
annotate_error()
{
LINE="$(("${BASH_LINENO[0]}" + "${BASE_LINE}" - 2))"
echo 1>&2 "::error::[ShellChecker] Error ${1} in action.yml at line ${LINE}"
exit "${1}"
}
case "${RUNNER_OS:?}" in 'Linux') current_os='linux' ;; 'macOS') current_os='darwin' ;; 'Windows') current_os='win' ;; *) annotate_error 1 ;; esac
base_url="https://github.com/koalaman/shellcheck/releases/download/${SHELLCHECK_VER:?}/"
if test "${current_os:?}" = 'win'; then archive_name="shellcheck-${SHELLCHECK_VER:?}.zip"; else archive_name="shellcheck-${SHELLCHECK_VER:?}.${current_os:?}.x86_64.tar.xz"; fi
## Installing wget (only on Windows)...
if test "${current_os:?}" = 'win'; then choco install --no-progress --yes wget 1>&- || annotate_error 2; fi
download_dir="${ACTION_PATH:?}/our_downloads"
mkdir -p "${download_dir:?}" || annotate_error 3
cd "${download_dir:?}" || annotate_error 4
wget -q -O "${archive_name:?}" "${base_url:?}${archive_name:?}" || annotate_error 5
archive_name="${download_dir:?}/${archive_name:?}"
if test "${current_os:?}" = 'win'; then
unzip -ojq "${archive_name:?}" '*' -d "${download_dir:?}/shellcheck-${SHELLCHECK_VER:?}/" || annotate_error 6
else
tar -xf "${archive_name:?}" || annotate_error 6
fi
mv -f "${download_dir:?}/shellcheck-${SHELLCHECK_VER:?}/shellcheck"* "${ACTION_PATH:?}/" || annotate_error 7
cd "${ACTION_PATH:?}" || annotate_error 8
rm -rf "${download_dir:?}" || annotate_error 9
- name: "Execute ShellCheck scan"
id: "shellcheck-scan"
shell: bash
env:
RUNNER_OS: "${{ runner.os }}"
ACTION_PATH: "${{ github.action_path }}"
EVENT_NAME: "${{ github.event_name }}"
VERBOSE: "${{ inputs.verbose-logging }}"
SEVERITY: "${{ inputs.severity }}"
ENABLE_ALL_CHECKS: "${{ inputs.enable-all-opt-checks }}"
IGNORED_FILES: "${{ inputs.ignore-files }}"
run: |
# Executing ShellCheck scan...
set -e
# shellcheck disable=SC3040
set -o pipefail || true
PATH="${ACTION_PATH:?}:${PATH:?}"
command -v shellcheck
shellcheck --version
SHELLCHECK_OPTS='-C'
if test -n "${SEVERITY?}"; then
SHELLCHECK_OPTS+=" -S${SEVERITY:?}"
fi
if test "${EVENT_NAME:?}" = 'pull_request'; then
SHELLCHECK_OPTS+=' -e1091'
else
SHELLCHECK_OPTS+=' -x'
if test "${ENABLE_ALL_CHECKS:?}" != 'false'; then
SHELLCHECK_OPTS+=' -oall'
fi
fi
export SHELLCHECK_OPTS
ignore_list=()
if test -n "${IGNORED_FILES?}"; then
OLDIFS="${IFS-}"
IFS='|'
read -ra ignore_list <<< "${IGNORED_FILES:?}"
IFS="${OLDIFS?}"
fi
is_shell_script()
{
# These aren't supported: csh tcsh mksh oksh
# This is supported only by old versions (for example v0.3.5): zsh
test '#!' = "$(head -qc 2 -- "${1:?}")" || return "${?}"
head -qn 1 -- "${1:?}" | grep -qw -m 1 -e 'sh\|ash\|dash\|bash\|bats\|ksh\|zsh' -- || return "${?}"
}
scan_files()
{
count='0'
list=()
escaped_csi="$(printf '\033\[')"
regexp_1="s/^\(${escaped_csi}0m\)/\t\t\1/"
regexp_2="s/^\(${escaped_csi}[2-9][0-9]\?m\)/ \t\1/"
while read -r FILE; do
FILE_LOWER="$(echo "${FILE}" | tr '[:upper:]' '[:lower:]')"
to_ignore=false
for ELEM in "${ignore_list[@]}"; do
if [[ "${FILE}" == *"/${ELEM}" ]]; then
to_ignore=true
break
fi
done
if "${to_ignore:?}"; then
echo "Ignored: ${FILE?}"
elif expr "${FILE_LOWER?}" : '^.*\.\(sh\|ash\|dash\|ksh\|bash\|bats\)$' 1> /dev/null || is_shell_script "${FILE:?}"; then
echo "Currently scanning: ${FILE?}"
STATUS=0
if test "${RUNNER_OS:?}" = 'Windows'; then
shellcheck "${FILE?}" | tr -d '\r' | sed -e "${regexp_1}" | sed -e "${regexp_2}" || STATUS="$?"
else
shellcheck "${FILE?}" | sed -e "${regexp_1}" | sed -e "${regexp_2}" || STATUS="$?"
fi
if test "${STATUS}" -ne 0; then
count="$((count + 1))"
list+=("${FILE?}")
fi
elif test "${VERBOSE:?}" = 'false'; then
continue
else
echo "Skipped: ${FILE?}"
fi
echo '################################################################################'
done
list_string="$(printf '\"%s\" ' "${list[@]}")"
unset list
printf 'numfileswithissues=%s\n' "${count:?}" >> "${GITHUB_OUTPUT:?}"
printf 'listfileswithissues=%s\n' "(${list_string?})" >> "${GITHUB_OUTPUT:?}"
unset list_string
return "${count}"
}
printf '################################################################################\n'
num_files_with_issues=0
find '.' -type f \! -path './.git/*' | LC_ALL=C sort | scan_files || num_files_with_issues="$?"
printf '\n'
if test "${num_files_with_issues:?}" -gt 0; then
printf '::notice::%s\n' "[ShellChecker] NUMBER OF FILES WITH ISSUES: ${num_files_with_issues:?} 🐛"
exit 1
else
printf '\033[32m%s\033[0m\n' '[ShellChecker] NUMBER OF FILES WITH ISSUES: 0 ✅'
printf '\033[32m%s\033[0m\n' 'All is good!'
fi