Skip to content

Commit 7fbf27a

Browse files
committed
tidy: Use yq via venv
1 parent 29d1926 commit 7fbf27a

File tree

2 files changed

+53
-24
lines changed

2 files changed

+53
-24
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
target
22
Cargo.lock
3+
.venv
34

45
# For platform and editor specific settings, it is recommended to add to
56
# a global .gitignore file.

tools/tidy.sh

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ trap 's=$?; echo >&2 "$0: error on line "${LINENO}": ${BASH_COMMAND}"; exit ${s}
1515
# - shfmt
1616
# - shellcheck
1717
# - npm
18-
# - jq and yq
18+
# - jq
19+
# - python
1920
# - rustup (if Rust code exists)
2021
# - clang-format (if C/C++ code exists)
2122
#
@@ -53,6 +54,11 @@ error() {
5354
fi
5455
should_fail=1
5556
}
57+
venv() {
58+
local bin="$1"
59+
shift
60+
"${venv_bin}/${bin}${exe}" "$@"
61+
}
5662

5763
if [[ $# -gt 0 ]]; then
5864
cat <<EOF
@@ -204,32 +210,54 @@ if [[ -n "$(git ls-files '*.yml' '*.js' '*.json')" ]]; then
204210
# Check GitHub workflows.
205211
if [[ -d .github/workflows ]]; then
206212
info "checking GitHub workflows"
207-
if type -P jq &>/dev/null && type -P yq &>/dev/null; then
208-
for workflow in .github/workflows/*.yml; do
209-
# The top-level permissions must be weak as they are referenced by all jobs.
210-
permissions=$(yq -c '.permissions' "${workflow}")
211-
case "${permissions}" in
212-
'{"contents":"read"}' | '{"contents":"none"}') ;;
213-
null) error "${workflow}: top level permissions not found; it must be 'contents: read' or weaker permissions" ;;
214-
*) error "${workflow}: only 'contents: read' and weaker permissions are allowed at top level; if you want to use stronger permissions, please set job-level permissions" ;;
213+
if type -P jq &>/dev/null; then
214+
if type -P python3 &>/dev/null || type -P python &>/dev/null; then
215+
py_suffix=''
216+
if type -P python3 &>/dev/null; then
217+
py_suffix='3'
218+
fi
219+
exe=''
220+
venv_bin='.venv/bin'
221+
case "$(uname -s)" in
222+
MINGW* | MSYS* | CYGWIN* | Windows_NT)
223+
exe='.exe'
224+
venv_bin='.venv/Scripts'
225+
;;
215226
esac
216-
# Make sure the 'needs' section is not out of date.
217-
if grep -q '# tidy:needs' "${workflow}" && ! grep -Eq '# *needs: \[' "${workflow}"; then
218-
# shellcheck disable=SC2207
219-
jobs_actual=($(yq '.jobs' "${workflow}" | jq -r 'keys_unsorted[]'))
220-
unset 'jobs_actual[${#jobs_actual[@]}-1]'
221-
# shellcheck disable=SC2207
222-
jobs_expected=($(yq -r '.jobs."ci-success".needs[]' "${workflow}"))
223-
if [[ "${jobs_actual[*]}" != "${jobs_expected[*]+"${jobs_expected[*]}"}" ]]; then
224-
printf -v jobs '%s, ' "${jobs_actual[@]}"
225-
sed -i "s/needs: \[.*\] # tidy:needs/needs: [${jobs%, }] # tidy:needs/" "${workflow}"
226-
check_diff "${workflow}"
227-
error "${workflow}: please update 'needs' section in 'ci-success' job"
228-
fi
227+
if [[ ! -d .venv ]]; then
228+
"python${py_suffix}" -m venv .venv
229229
fi
230-
done
230+
if [[ ! -e "${venv_bin}/yq${exe}" ]]; then
231+
venv "pip${py_suffix}" install yq
232+
fi
233+
for workflow in .github/workflows/*.yml; do
234+
# The top-level permissions must be weak as they are referenced by all jobs.
235+
permissions=$(venv yq -c '.permissions' "${workflow}")
236+
case "${permissions}" in
237+
'{"contents":"read"}' | '{"contents":"none"}') ;;
238+
null) error "${workflow}: top level permissions not found; it must be 'contents: read' or weaker permissions" ;;
239+
*) error "${workflow}: only 'contents: read' and weaker permissions are allowed at top level; if you want to use stronger permissions, please set job-level permissions" ;;
240+
esac
241+
# Make sure the 'needs' section is not out of date.
242+
if grep -q '# tidy:needs' "${workflow}" && ! grep -Eq '# *needs: \[' "${workflow}"; then
243+
# shellcheck disable=SC2207
244+
jobs_actual=($(venv yq '.jobs' "${workflow}" | jq -r 'keys_unsorted[]'))
245+
unset 'jobs_actual[${#jobs_actual[@]}-1]'
246+
# shellcheck disable=SC2207
247+
jobs_expected=($(venv yq -r '.jobs."ci-success".needs[]' "${workflow}"))
248+
if [[ "${jobs_actual[*]}" != "${jobs_expected[*]+"${jobs_expected[*]}"}" ]]; then
249+
printf -v jobs '%s, ' "${jobs_actual[@]}"
250+
sed -i "s/needs: \[.*\] # tidy:needs/needs: [${jobs%, }] # tidy:needs/" "${workflow}"
251+
check_diff "${workflow}"
252+
error "${workflow}: please update 'needs' section in 'ci-success' job"
253+
fi
254+
fi
255+
done
256+
else
257+
warn "'python3' is not installed; skipped GitHub workflow check"
258+
fi
231259
else
232-
warn "'jq' or 'yq' is not installed; skipped GitHub workflow check"
260+
warn "'jq' is not installed; skipped GitHub workflow check"
233261
fi
234262
fi
235263
fi

0 commit comments

Comments
 (0)