Skip to content

Commit 1dcc563

Browse files
author
Michael Buchar
committed
ci(shfmt): add shfmt code formatting checks (#734)
1 parent c36c461 commit 1dcc563

File tree

2 files changed

+57
-42
lines changed

2 files changed

+57
-42
lines changed

.github/workflows/ci.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,17 @@ jobs:
124124
npm install jsonlint --global
125125
./run-tests.sh --check-jsonlint
126126
127+
format-shfmt:
128+
runs-on: ubuntu-24.04
129+
steps:
130+
- name: Checkout
131+
uses: actions/checkout@v4
132+
133+
- name: Check shell script code formatting
134+
run: |
135+
sudo apt-get install shfmt
136+
./run-tests.sh --check-shfmt
137+
127138
docs-sphinx:
128139
runs-on: ubuntu-24.04
129140
steps:

run-tests.sh

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,42 @@ set -o nounset
1212
export REANA_SQLALCHEMY_DATABASE_URI=postgresql+psycopg2://postgres:mysecretpassword@localhost/postgres
1313

1414
# Verify that db container is running before continuing
15-
_check_ready () {
15+
_check_ready() {
1616
RETRIES=40
17-
while ! $2
18-
do
17+
while ! $2; do
1918
echo "==> [INFO] Waiting for $1, $((RETRIES--)) remaining attempts..."
2019
sleep 2
21-
if [ $RETRIES -eq 0 ]
22-
then
20+
if [ $RETRIES -eq 0 ]; then
2321
echo "==> [ERROR] Couldn't reach $1"
2422
exit 1
2523
fi
2624
done
2725
}
2826

29-
_db_check () {
30-
docker exec --user postgres postgres__reana-server bash -c "pg_isready" &>/dev/null;
27+
_db_check() {
28+
docker exec --user postgres postgres__reana-server bash -c "pg_isready" &>/dev/null
3129
}
3230

33-
clean_old_db_container () {
31+
clean_old_db_container() {
3432
OLD="$(docker ps --all --quiet --filter=name=postgres__reana-server)"
3533
if [ -n "$OLD" ]; then
3634
echo '==> [INFO] Cleaning old DB container...'
3735
docker stop postgres__reana-server
3836
fi
3937
}
4038

41-
start_db_container () {
39+
start_db_container() {
4240
echo '==> [INFO] Starting DB container...'
4341
docker run --rm --name postgres__reana-server -p 5432:5432 -e POSTGRES_PASSWORD=mysecretpassword -d docker.io/library/postgres:14.10
4442
_check_ready "Postgres" _db_check
4543
}
4644

47-
stop_db_container () {
45+
stop_db_container() {
4846
echo '==> [INFO] Stopping DB container...'
4947
docker stop postgres__reana-server
5048
}
5149

52-
check_commitlint () {
50+
check_commitlint() {
5351
from=${2:-master}
5452
to=${3:-HEAD}
5553
pr=${4:-[0-9]+}
@@ -60,18 +58,18 @@ check_commitlint () {
6058
commit_title=$(echo "$line" | cut -d ' ' -f 2-)
6159
commit_number_of_parents=$(git rev-list --parents "$commit_hash" -n1 | awk '{print NF-1}')
6260
# (i) skip checking release commits generated by Release Please
63-
if [ "$commit_number_of_parents" -le 1 ] && echo "$commit_title" | grep -qE "^chore\(.*\): release"; then
61+
if [ "$commit_number_of_parents" -le 1 ] && echo "$commit_title" | grep -qP "^chore\(.*\): release"; then
6462
continue
6563
fi
6664
# (ii) check presence of PR number
67-
if ! echo "$commit_title" | grep -qE "\(#$pr\)$"; then
65+
if ! echo "$commit_title" | grep -qP "\(\#$pr\)$"; then
6866
echo "✖ Headline does not end by '(#$pr)' PR number: $commit_title"
6967
found=1
7068
fi
7169
# (iii) check absence of merge commits in feature branches
7270
if [ "$commit_number_of_parents" -gt 1 ]; then
73-
if echo "$commit_title" | grep -qE "^chore\(.*\): merge "; then
74-
break # skip checking maint-to-master merge commits
71+
if echo "$commit_title" | grep -qP "^chore\(.*\): merge "; then
72+
break # skip checking maint-to-master merge commits
7573
else
7674
echo "✖ Merge commits are not allowed in feature branches: $commit_title"
7775
found=1
@@ -83,33 +81,33 @@ check_commitlint () {
8381
fi
8482
}
8583

86-
check_shellcheck () {
84+
check_shellcheck() {
8785
find . -name "*.sh" -exec shellcheck {} \+
8886
}
8987

90-
check_pydocstyle () {
88+
check_pydocstyle() {
9189
pydocstyle reana_server
9290
}
9391

94-
check_black () {
92+
check_black() {
9593
black --check .
9694
}
9795

98-
check_flake8 () {
96+
check_flake8() {
9997
flake8 .
10098
}
10199

102-
check_openapi_spec () {
100+
check_openapi_spec() {
103101
FLASK_APP=reana_server/app.py python ./scripts/generate_openapi_spec.py
104102
diff -q -w temp_openapi.json docs/openapi.json
105103
rm temp_openapi.json
106104
}
107105

108-
check_manifest () {
106+
check_manifest() {
109107
check-manifest
110108
}
111109

112-
check_sphinx () {
110+
check_sphinx() {
113111
sphinx-build -qnNW docs docs/_build/html
114112
}
115113

@@ -121,23 +119,27 @@ check_yamllint() {
121119
yamllint .
122120
}
123121

124-
check_pytest () {
122+
check_pytest() {
125123
clean_old_db_container
126124
start_db_container
127125
trap clean_old_db_container SIGINT SIGTERM SIGSEGV ERR
128126
pytest
129127
stop_db_container
130128
}
131129

132-
check_dockerfile () {
133-
docker run -i --rm docker.io/hadolint/hadolint:v2.12.0 < Dockerfile
130+
check_dockerfile() {
131+
docker run -i --rm docker.io/hadolint/hadolint:v2.12.0 <Dockerfile
134132
}
135133

136-
check_docker_build () {
134+
check_docker_build() {
137135
docker build -t docker.io/reanahub/reana-server .
138136
}
139137

140-
check_all () {
138+
check_shfmt() {
139+
shfmt -d .
140+
}
141+
142+
check_all() {
141143
check_commitlint
142144
check_shellcheck
143145
check_pydocstyle
@@ -151,6 +153,7 @@ check_all () {
151153
check_docker_build
152154
check_jsonlint
153155
check_yamllint
156+
check_shfmt
154157
}
155158

156159
if [ $# -eq 0 ]; then
@@ -160,19 +163,20 @@ fi
160163

161164
arg="$1"
162165
case $arg in
163-
--check-commitlint) check_commitlint "$@";;
164-
--check-shellcheck) check_shellcheck;;
165-
--check-pydocstyle) check_pydocstyle;;
166-
--check-black) check_black;;
167-
--check-flake8) check_flake8;;
168-
--check-openapi-spec) check_openapi_spec;;
169-
--check-manifest) check_manifest;;
170-
--check-sphinx) check_sphinx;;
171-
--check-pytest) check_pytest;;
172-
--check-all) check_all;;
173-
--check-dockerfile) check_dockerfile;;
174-
--check-docker-build) check_docker_build;;
175-
--check-jsonlint) check_jsonlint ;;
176-
--check-yamllint) check_yamllint ;;
177-
*) echo "[ERROR] Invalid argument '$arg'. Exiting." && exit 1;;
166+
--check-commitlint) check_commitlint "$@" ;;
167+
--check-shellcheck) check_shellcheck ;;
168+
--check-pydocstyle) check_pydocstyle ;;
169+
--check-black) check_black ;;
170+
--check-flake8) check_flake8 ;;
171+
--check-openapi-spec) check_openapi_spec ;;
172+
--check-manifest) check_manifest ;;
173+
--check-sphinx) check_sphinx ;;
174+
--check-pytest) check_pytest ;;
175+
--check-all) check_all ;;
176+
--check-dockerfile) check_dockerfile ;;
177+
--check-docker-build) check_docker_build ;;
178+
--check-jsonlint) check_jsonlint ;;
179+
--check-yamllint) check_yamllint ;;
180+
--check-shfmt) check_shfmt ;;
181+
*) echo "[ERROR] Invalid argument '$arg'. Exiting." && exit 1 ;;
178182
esac

0 commit comments

Comments
 (0)