-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathpre-commit
executable file
·428 lines (368 loc) · 13.7 KB
/
pre-commit
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#!/bin/bash
# uncomment next line to debug
# set -x
DIR=$(git rev-parse --show-toplevel)
##########
## PRO ##
##########
cd $DIR/pro
#
# To disable some checks, set DISABLED_API_PRE_COMMIT_CHECKS
# environment variable. For example:
#
# export DISABLED_PRO_PRE_COMMIT_CHECKS="lint:js,test,tsc"
#
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep '^pro/src/*' | sed 's|^pro\/||g')
if [[ ! -z "$STAGED_FILES" ]]; then
# run linter on staged files
if [[ ! ",${DISABLED_PRO_PRE_COMMIT_CHECKS}," =~ ",lint," ]]; then
echo "Looking for dead code..."
yarn lint:dead-code
echo "Running Linter..."
yarn lint:js:fix $STAGED_FILES
LINTER_EXIT_CODE=$?
# add files auto-fixed by the linter
git add -f $STAGED_FILES
# check linter exit code
if [ $LINTER_EXIT_CODE -ne 0 ]; then
echo "❌ Please fix js lint errors first "
exit 1
else
echo "✅ js lint all good"
fi
fi
# run typescript quality on staged files
if [[ ! ",${DISABLED_PRO_PRE_COMMIT_CHECKS}," =~ ",tsc," ]]; then
echo "Quality checking..."
yarn tsc -b
QUALITY_EXIT_CODE=$?
# check linter exit code
if [ $QUALITY_EXIT_CODE -ne 0 ]; then
echo "❌ Please fix ts quality errors first "
exit 1
else
echo "✅ ts quality all good"
fi
fi
# run tests related to staged files
if [[ ! ",${DISABLED_PRO_PRE_COMMIT_CHECKS}," =~ ",test," ]]; then
echo "Running Tests"
# The following command will run tests related to the vite root (`src`)
# In bash ${string//substring/replacement} replace all occurrences of a string
yarn test:unit:hook ${STAGED_FILES//src\//}
VITEST_EXIT_CODE=$?
# check vitest exit code
if [ $VITEST_EXIT_CODE -ne 0 ]; then
echo "❌ Please fix the tests"
exit 1
else
echo "✅ tests passing"
fi
fi
# run tests related to staged files with coverage
if [[ ! ",${DISABLED_PRO_PRE_COMMIT_CHECKS}," =~ ",testcoverage," ]]; then
echo "Running Tests"
# The following command will run tests related to the vite root (`src`)
# In bash ${string//substring/replacement} replace all occurrences of a string
yarn test:unit:hookcoverage ${STAGED_FILES//src\//}
VITEST_EXIT_CODE=$?
# check vitest exit code
if [ $VITEST_EXIT_CODE -ne 0 ]; then
echo "🙏 Please help us fix test coverage"
else
echo "✅ coverage 100 %"
fi
fi
fi
STAGED_SCSS_FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep '^pro/src/*' | grep 'css' | sed 's|^pro\/||g')
if [[ ! -z "$STAGED_SCSS_FILES" ]]; then
# run stylelint on staged files
if [[ ! ",${DISABLED_PRO_PRE_COMMIT_CHECKS}," =~ ",stylelint," ]]; then
echo "Running style Linter..."
yarn lint:scss:hook:fix $STAGED_SCSS_FILES
LINTER_EXIT_CODE=$?
# add files auto-fixed by the linter
git add -f $STAGED_SCSS_FILES
# check linter exit code
if [ $LINTER_EXIT_CODE -ne 0 ]; then
echo "❌ Please fix style lint errors first "
exit 1
else
echo "✅ stylelint all good"
fi
fi
fi
cd ../
##########
## END PRO ##
##########
##########
## API ##
##########
cd $DIR/api
#
# To disable some checks, set DISABLED_API_PRE_COMMIT_CHECKS
# environment variable. For example:
#
# export DISABLED_API_PRE_COMMIT_CHECKS="mypy,pylint,squawk,ruff"
#
ALEMBIC_STAGED_FILES=$(git diff --staged --name-only -- './src/pcapi/alembic/versions/*.py' | grep '^api/*' | sed 's|^api\/||g')
if [[ "$ALEMBIC_STAGED_FILES" != "" ]]; then
echo -e "\033[0;96mMigration changes detected: $ALEMBIC_STAGED_FILES \033[0m"
echo -e "\033[0;96mUpdating alembic_version_conflict_detection.txt \033[0m\n"
# Sort heads: "pre" then "post" (the output of Alembic is not stable)
alembic --config alembic.ini heads | sort --key 2 --reverse >alembic_version_conflict_detection.txt
git add alembic_version_conflict_detection.txt
if [[ ! ",${DISABLED_API_PRE_COMMIT_CHECKS}," =~ ",squawk," ]]; then
squawk_output_path=$(mktemp --tmpdir squawk-output-from-pre-commit-hook.XXXXXXXX)
squawk_lint_status_code=0
lint_alembic() {
local operation="$1"
local migration_range="$2"
env_local_file=".env.local.secret"
if [ -f "$env_local_file" ] && grep -q "^DATABASE_URL" "$env_local_file"; then
# backend is launched locally
alembic "$operation" "$migration_range" --sql 2>>"${squawk_output_path}" |
sed '/squawk:ignore-next-statement/,/;$/d' | # see alembic/CONTRIBUTING.md
squawk >>"${squawk_output_path}" || squawk_lint_status_code=1
else
# backend is launched through docker
pc alembic "$operation" "$migration_range" --sql 2>>"${squawk_output_path}" |
tail -n +2 | # ignore `pc` logging of executed bash commands
sed '/squawk:ignore-next-statement/,/;$/d' |
squawk >>"${squawk_output_path}" || squawk_lint_status_code=1
fi
}
base_commit=$(git merge-base HEAD origin/master)
base_alembic_versions=$(git --no-pager show "$base_commit":api/alembic_version_conflict_detection.txt)
base_pre=$(echo "$base_alembic_versions" | head --lines 1 | cut -f 1 -d " ")
base_post=$(echo "$base_alembic_versions" | tail --lines 1 | cut -f 1 -d " ")
lint_alembic upgrade "$base_pre":pre@head
lint_alembic upgrade "$base_post":post@head
lint_alembic downgrade post@head:"$base_post"
lint_alembic downgrade pre@head:"$base_pre"
if [ $squawk_lint_status_code -ne 0 ]; then
cat "${squawk_output_path}"
echo -e "\033[2K\r\033[0;31msquawk: Some migrations are not respecting zero-downtime best practices\033[0m"
exit $squawk_lint_status_code
else
echo -e "\033[2K\r\033[0;92msquawk: Migrations are respecting zero-downtime best practices\033[0m"
echo -e "\033[2K\r\033[0;92msquawk: However, the migrations can still time out if done on big tables such as Offer, Product, Stock or Booking\033[0m"
fi
fi
fi
counter=0
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM -- '*.py' | grep '^api/*' | sed 's|^api\/||g')
LINTED_FILES=""
MYPY_FILES=""
for FILE in $STAGED_FILES; do
LINTED_FILES+=" ${FILE}"
if [[ ! " ${FILE}" == *tests/* ]]; then
MYPY_FILES+=" ${FILE}"
fi
done
if [[ ! -z "$STAGED_FILES" ]]; then
if [[ ! ",${DISABLED_API_PRE_COMMIT_CHECKS}," =~ ",isort," ]]; then
echo -ne "\033[0;96mRunning isort to organize imports...\033[0m"
isort $LINTED_FILES --check-only --settings-file pyproject.toml 2>/dev/null
if [[ "$?" != 0 ]]; then
edited_files=$(isort $LINTED_FILES --settings-file pyproject.toml | sed -E "s/Fixing (.*)/ - \\1/g")
# Go up 1 line to overwrite "Running..." line
echo -e "\033[1F"
echo -e "\033[0;91misort: Add edited files to your commit:"
echo -e "${edited_files}\033[0m"
counter=$((counter + 1))
else
echo -e "\033[2K\r\033[0;92misort: Imports correctly sorted\033[0m"
fi
fi
if [[ ! ",${DISABLED_API_PRE_COMMIT_CHECKS}," =~ ",black," ]]; then
echo -ne "\033[0;96mRunning black to format files...\033[0m"
black $LINTED_FILES --check --quiet --config pyproject.toml
if [[ "$?" != 0 ]]; then
edited_files=$(black $LINTED_FILES --config pyproject.toml 2>&1 | grep "^reformatted " | sed -E "s/reformatted (.*)/ - \\1/g")
counter=$((counter + 1))
# Go up 1 line to overwrite "Running..." line
echo -e "\033[1F"
echo -e "\033[0;91mblack: Add edited files to your commit:"
echo -e "${edited_files}\033[0m"
else
echo -e "\033[2K\r\033[0;92mblack: Code correctly formatted\033[0m"
fi
fi
if [[ ! ",${DISABLED_API_PRE_COMMIT_CHECKS}," =~ ",mypy," ]]; then
echo -e "\033[0;96mRunning mypy for type checking...\033[0m"
if [[ ! $MYPY_FILES == "" ]]; then
mypy $MYPY_FILES --pretty
if [[ "$?" != 0 ]]; then
counter=$((counter + 1))
fi
fi
fi
if [[ ! ",${DISABLED_API_PRE_COMMIT_CHECKS}," =~ ",pylint," ]]; then
echo -e "\033[0;96mRunning pylint for code linting...\033[0m"
# parallel execution breaks plugins, use one single job !
pylint $LINTED_FILES --output-format="colorized" --score=no --jobs=1
if [[ "$?" != 0 ]]; then
counter=$((counter + 1))
fi
fi
if [[ ! ",${DISABLED_API_PRE_COMMIT_CHECKS}," =~ ",ruff," ]]; then
echo -e "\033[0;96mRunning ruff for code linting...\033[0m"
ruff check $LINTED_FILES
if [[ "$?" != 0 ]]; then
counter=$((counter + 1))
fi
fi
fi
if [[ $counter > 0 ]]; then
echo -e "\033[0;96mIf you want to bypass these checks, add --no-verify option when committing.\033[0m"
exit 1
fi
cd ../
##########
## END API ##
##########
###########
## PROXY ##
###########
cd $DIR/proxy
#
# To disable some checks, set DISABLED_API_PRE_COMMIT_CHECKS
# environment variable. For example:
#
# export DISABLED_API_PRE_COMMIT_CHECKS="mypy,pylint"
#
counter=0
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM -- '*.py' | grep '^proxy/*' | sed 's|^proxy\/||g')
LINTED_FILES=""
MYPY_FILES=""
for FILE in $STAGED_FILES; do
LINTED_FILES+=" ${FILE}"
if [[ ! " ${FILE}" == *tests/* ]]; then
MYPY_FILES+=" ${FILE}"
fi
done
if [[ ! -z "$STAGED_FILES" ]]; then
if [[ ! ",${DISABLED_API_PRE_COMMIT_CHECKS}," =~ ",isort," ]]; then
echo -ne "\033[0;96mRunning isort to organize imports...\033[0m"
isort $LINTED_FILES --check-only --settings-file pyproject.toml 2>/dev/null
if [[ "$?" != 0 ]]; then
edited_files=$(isort $LINTED_FILES --settings-file pyproject.toml | sed -E "s/Fixing (.*)/ - \\1/g")
# Go up 1 line to overwrite "Running..." line
echo -e "\033[1F"
echo -e "\033[0;91misort: Add edited files to your commit:"
echo -e "${edited_files}\033[0m"
counter=$((counter + 1))
else
echo -e "\033[2K\r\033[0;92misort: Imports correctly sorted\033[0m"
fi
fi
if [[ ! ",${DISABLED_API_PRE_COMMIT_CHECKS}," =~ ",black," ]]; then
echo -ne "\033[0;96mRunning black to format files...\033[0m"
black $LINTED_FILES --check --quiet --config pyproject.toml
if [[ "$?" != 0 ]]; then
edited_files=$(black $LINTED_FILES --config pyproject.toml 2>&1 | grep "^reformatted " | sed -E "s/reformatted (.*)/ - \\1/g")
counter=$((counter + 1))
# Go up 1 line to overwrite "Running..." line
echo -e "\033[1F"
echo -e "\033[0;91mblack: Add edited files to your commit:"
echo -e "${edited_files}\033[0m"
else
echo -e "\033[2K\r\033[0;92mblack: Code correctly formatted\033[0m"
fi
fi
if [[ ! ",${DISABLED_API_PRE_COMMIT_CHECKS}," =~ ",mypy," ]]; then
echo -e "\033[0;96mRunning mypy for type checking...\033[0m"
if [[ ! $MYPY_FILES == "" ]]; then
mypy $MYPY_FILES --pretty
if [[ "$?" != 0 ]]; then
counter=$((counter + 1))
fi
fi
fi
if [[ ! ",${DISABLED_API_PRE_COMMIT_CHECKS}," =~ ",pylint," ]]; then
echo -e "\033[0;96mRunning pylint for code linting...\033[0m"
# parallel execution breaks plugins, use one single job !
pylint $LINTED_FILES --output-format="colorized" --score=no --jobs=1
if [[ "$?" != 0 ]]; then
counter=$((counter + 1))
fi
fi
fi
if [[ $counter > 0 ]]; then
echo -e "\033[0;96mIf you want to bypass these checks, add --no-verify option when committing.\033[0m"
exit 1
fi
cd ../
###############
## END PROXY ##
###############
#######################
## API Documentation ##
#######################
cd $DIR/api/documentation
STAGED_DOCUMENTATION_FILES=$(git diff --cached --name-only | grep '^api/documentation/*' | sed 's|^api\/||g')
if [[ ! -z "$STAGED_DOCUMENTATION_FILES" ]]; then
if [[ ! ",${DISABLED_BO_PRE_COMMIT_CHECKS}," =~ ",documentation," ]]; then
echo -e "\033[0;96mCheck documentation can be built...\033[0m"
npm run build
if [[ "$?" != 0 ]]; then
echo -e "\033[0;96mIf you want to bypass these checks, add --no-verify option when committing.\033[0m"
exit 1
fi
fi
fi
###########################
## END API Documentation ##
###########################
################
## BACKOFFICE ##
################
cd $DIR/api
STAGED_HTML_FILES=$(git diff --cached --name-only --diff-filter=ACMR | grep '^api/src/pcapi/routes/backoffice/*' | grep 'html' | sed 's|^api\/||g')
if [[ ! -z "$STAGED_HTML_FILES" ]]; then
# run stylelint on staged files
if [[ ! ",${DISABLED_BO_PRE_COMMIT_CHECKS}," =~ ",djlint," ]]; then
echo "Running dj formater and linter..."
djlint $STAGED_HTML_FILES --configuration src/pcapi/routes/backoffice/.djlintrc --reformat
DJLINT_EXIT_CODE=$?
# add files auto-fixed by the linter
git add -f $STAGED_HTML_FILES
# check linter exit code
if [ $DJLINT_EXIT_CODE -ne 0 ]; then
echo "❌ Please fix djlint errors first: djlint $DIR/api/src/pcapi/routes/backoffice/ --configuration $DIR/api/src/pcapi/routes/backoffice/.djlintrc --reformat"
exit 1
else
echo "✅ djlint all good"
fi
fi
fi
cd ..
####################
## END BACKOFFICE ##
####################
#######################
## CHECK DEPENDECIES ##
#######################
cd $DIR/api
STAGED_PYPROJECT_TOML=$(git diff --cached --name-only --diff-filter=ACMR | grep -wE 'api/pyproject.toml|poetry.lock' | sed 's|^api\/||g')
if [[ ! -z "$STAGED_PYPROJECT_TOML" ]]; then
# run `poetry check` on staged files
if [[ ! ",${DISABLED_API_PRE_COMMIT_CHECKS}," =~ ",check-dependencies," ]]; then
echo "Running check-dependencies..."
poetry check
CHECK_DEPENDENCIES_EXIT_CODE=$?
# add files auto-fixed by the linter
git add -f $STAGED_PYPROJECT_TOML
# check exit code
if [ $CHECK_DEPENDENCIES_EXIT_CODE -ne 0 ]; then
echo -e "❌ Please fix check-dependencies errors first. poetry.lock and pyproject.toml are not synced.\nIf the changes are wanted, run 'poetry lock'"
exit 1
else
echo "✅ check-dependencies all good"
fi
fi
fi
# return 0-exit code
exit 0