1#!/bin/sh
2# Create a link to this file at .git/hooks/pre-commit to
3# force PEP8 validation prior to committing
4#
5# Ignored violations:
6#
7#   W504: Line break after binary operator
8#   E501: Line too long
9
10exec 1>&2
11
12EXIT=0
13RED='\033[0;31m'
14YELLOW='\033[0;33m'
15NOCOLOR='\033[0m'
16
17if [ -d ./venv/ ]; then
18    VENV="$PWD/venv"
19    if [ -e $VENV/bin/python ]; then
20        PATH=$VENV/bin:$PATH
21    elif [ -e $VENV/Scripts/python.exe ]; then
22        PATH=$VENV/Scripts:$PATH
23    fi
24fi
25
26if [ ${NOVALIDATE} ]; then
27  echo "${YELLOW}Skipping validation checks${NOCOLOR}"
28  exit $EXIT
29fi
30
31echo "Validating PEP8 compliance..."
32pycodestyle --ignore=W504,E501 --exclude=node_modules netbox/
33if [ $? != 0 ]; then
34	EXIT=1
35fi
36
37echo "Checking for missing migrations..."
38python netbox/manage.py makemigrations --dry-run --check
39if [ $? != 0 ]; then
40	EXIT=1
41fi
42
43echo "Checking UI ESLint, TypeScript, and Prettier compliance..."
44yarn --cwd "$PWD/netbox/project-static" validate
45if [ $? != 0 ]; then
46	EXIT=1
47fi
48
49if [ $EXIT != 0 ]; then
50  printf "${RED}COMMIT FAILED${NOCOLOR}\n"
51fi
52
53exit $EXIT
54