Try fixing hook script

This commit is contained in:
Dennis Eichhorn 2018-09-22 14:38:30 +02:00
parent 616f7769c8
commit c72fbad90d
6 changed files with 39 additions and 14 deletions

View File

@ -1,17 +1,21 @@
#!/bin/bash #!/bin/bash
set -x
. ${rootpath}/Build/Hooks/logging.sh . ${rootpath}/Build/Hooks/logging.sh
. ${rootpath}/Build/Hooks/syntax.sh . ${rootpath}/Build/Hooks/syntax.sh
. ${rootpath}/Build/Hooks/filename.sh . ${rootpath}/Build/Hooks/filename.sh
. ${rootpath}/Build/Hooks/tests.sh . ${rootpath}/Build/Hooks/tests.sh
git diff --cached --name-only | while read FILE; do git diff --cached --name-only | while read FILE; do
echo $FILE
if [[ ! -f "$FILE" ]]; then if [[ ! -f "$FILE" ]]; then
continue continue
fi fi
# Filename # Filename
if [[ $(isValidFileName "$FILE") = 1 ]]; then if [[ $(isValidFileName "$FILE") = 0 ]]; then
echo -e "\e[1;31m\tInvalid file name '$FILE'.\e[0m" >&2 echo -e "\e[1;31m\tInvalid file name '$FILE'.\e[0m" >&2
exit 1 exit 1
fi fi
@ -38,8 +42,7 @@ git diff --cached --name-only | while read FILE; do
# Syntax # Syntax
if [[ "$FILE" =~ ^.+(php)$ ]]; then if [[ "$FILE" =~ ^.+(php)$ ]]; then
$(hasInvalidPhpSyntax "$FILE") PHP_SYNTAX=$(hasInvalidPhpSyntax "$FILE")
PHP_SYNTAX=$?
if [[ $PHP_SYNTAX = 1 ]]; then if [[ $PHP_SYNTAX = 1 ]]; then
echo -e "\e[1;31m\tPhp linting error.\e[0m" >&2 echo -e "\e[1;31m\tPhp linting error.\e[0m" >&2
@ -63,8 +66,7 @@ git diff --cached --name-only | while read FILE; do
fi fi
if [[ "$FILE" =~ ^.+(sh|js|php|json|css)$ ]]; then if [[ "$FILE" =~ ^.+(sh|js|php|json|css)$ ]]; then
$(hasInvalidBasicSyntax "$FILE") GEN_SYNTAX=$(hasInvalidBasicSyntax "$FILE")
GEN_SYNTAX=$?
if [[ $GEN_SYNTAX = 1 ]]; then if [[ $GEN_SYNTAX = 1 ]]; then
echo -e "\e[1;31m\tFound whitespace at end of line in $FILE.\e[0m" >&2 echo -e "\e[1;31m\tFound whitespace at end of line in $FILE.\e[0m" >&2
@ -80,8 +82,7 @@ git diff --cached --name-only | while read FILE; do
fi fi
if [[ "$FILE" =~ ^.+(tpl\.php|html)$ ]]; then if [[ "$FILE" =~ ^.+(tpl\.php|html)$ ]]; then
$(hasInvalidHtmlTemplateContent "$FILE") TPL_SYNTAX=$(hasInvalidHtmlTemplateContent "$FILE")
TPL_SYNTAX=$?
if [[ $TPL_SYNTAX = 1 ]]; then if [[ $TPL_SYNTAX = 1 ]]; then
echo -e "\e[1;31m\tFound missing image alt attribute.\e[0m" >&2 echo -e "\e[1;31m\tFound missing image alt attribute.\e[0m" >&2

View File

@ -4,8 +4,10 @@ isValidFileName() {
if test $(git diff --cached --name-only --diff-filter=A -z "$1" | if test $(git diff --cached --name-only --diff-filter=A -z "$1" |
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
then then
echo 0
return 0 return 0
fi fi
echo 1
return 1 return 1
} }

View File

@ -3,17 +3,21 @@
hasPhpLogging() { hasPhpLogging() {
RESULT=$(grep "var_dump(" "$1") RESULT=$(grep "var_dump(" "$1")
if [ ! -z $RESULT ]; then if [ ! -z $RESULT ]; then
echo 1
return 1 return 1
fi fi
echo 0
return 0 return 0
} }
hasJsLogging() { hasJsLogging() {
RESULT=$(grep " console.log(" "$1") RESULT=$(grep " console.log(" "$1")
if [ ! -z $RESULT ]; then if [ ! -z $RESULT ]; then
echo 1
return 1 return 1
fi fi
echo 0
return 0 return 0
} }

View File

@ -4,82 +4,99 @@ hasInvalidPhpSyntax() {
# php lint # php lint
$(php -l "$1" > /dev/null) $(php -l "$1" > /dev/null)
if [[ $? != 0 ]]; then if [[ $? != 0 ]]; then
echo 1
return 1 return 1
fi fi
# phpcs # phpcs
$(php -d memory_limit=4G ${rootpath}/vendor/bin/phpcs --standard="${rootpath}/Build/Config/phpcs.xml" --encoding=utf-8 -n -p "$1" > /dev/null) $(php -d memory_limit=4G ${rootpath}/vendor/bin/phpcs --standard="${rootpath}/Build/Config/phpcs.xml" --encoding=utf-8 -n -p "$1" > /dev/null)
if [[ $? != 0 ]]; then if [[ $? != 0 ]]; then
echo 2
return 2 return 2
fi fi
# phpmd # phpmd
$(php -d memory_limit=4G ${rootpath}/vendor/bin/phpmd "$1" text ${rootpath}/Build/Config/phpmd.xml --exclude *tests* --minimumpriority 1 > /dev/null) $(php -d memory_limit=4G ${rootpath}/vendor/bin/phpmd "$1" text ${rootpath}/Build/Config/phpmd.xml --exclude *tests* --minimumpriority 1 > /dev/null)
if [[ $? != 0 ]]; then if [[ $? != 0 ]]; then
echo 3
return 3 return 3
fi fi
echo 0
return 0 return 0
} }
hasInvalidHtmlTemplateContent() { hasInvalidHtmlTemplateContent() {
# Images must have a alt= attribute *error* # Images must have a alt= attribute *error*
if [[ -n $(grep -P '(\<img)((?!.*?alt=).)*(>)' "$1") ]]; then if [[ -n $(grep -P '(\<img)((?!.*?alt=).)*(>)' "$1") ]]; then
echo 1
return 1 return 1
fi fi
# Input elements must have a type= attribute *error* # Input elements must have a type= attribute *error*
if [[ -n $(grep -P '(<input)((?!.*?type=).)*(>)' "$1") ]]; then if [[ -n $(grep -P '(<input)((?!.*?type=).)*(>)' "$1") ]]; then
echo 2
return 2 return 2
fi fi
# Form fields must have a name *error* # Form fields must have a name *error*
if [[ -n $(grep -P '(<input|<select|<textarea)((?!.*?name=).)*(>)' "$1") ]]; then if [[ -n $(grep -P '(<input|<select|<textarea)((?!.*?name=).)*(>)' "$1") ]]; then
echo 3
return 3 return 3
fi fi
# Form must have a id, action and method *error* # Form must have a id, action and method *error*
if [[ -n $(grep -P '(\<form)((?!.*?(name|id)=).)*(>)' "$1") ]]; then if [[ -n $(grep -P '(\<form)((?!.*?(name|id)=).)*(>)' "$1") ]]; then
echo 4
return 4 return 4
fi fi
# Inline css is invalid *warning* # Inline css is invalid *warning*
if [[ -n $(grep -P '(style=)' "$1") ]]; then if [[ -n $(grep -P '(style=)' "$1") ]]; then
echo 5
return 5 return 5
fi fi
# Attribute descriptions should not be hard coded *warning* # Attribute descriptions should not be hard coded *warning*
if [[ -n $(grep -P '(value|title|alt|aria\-label)(=\")((?!\<\?).)*(>)' "$1") ]]; then if [[ -n $(grep -P '(value|title|alt|aria\-label)(=\")((?!\<\?).)*(>)' "$1") ]]; then
echo 6
return 6 return 6
fi fi
# Hard coded language *warning* # Hard coded language *warning*
if [[ -n $(grep -P '(\<td\>|\<th\>|\<caption\>|\<label.*?(\"|l)\>)[0-9a-zA-Z\.\?]+' "$1") ]]; then if [[ -n $(grep -P '(\<td\>|\<th\>|\<caption\>|\<label.*?(\"|l)\>)[0-9a-zA-Z\.\?]+' "$1") ]]; then
echo 7
return 7 return 7
fi fi
echo 0
return 0 return 0
} }
isValidBashScript() { isValidBashScript() {
bash -n "$1" 1> /dev/null bash -n "$1" 1> /dev/null
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo 0
return 0 return 0
fi fi
echo 1
return 1 return 1
} }
hasInvalidBasicSyntax() { hasInvalidBasicSyntax() {
# Check whitespace end of line in code # Check whitespace end of line in code
if [[ -n $(grep -P ' $' "$1") ]]; then if [[ -n $(grep -P ' $' "$1") ]]; then
echo 1
return 1 return 1
fi fi
# Check for tabs # Check for tabs
if [[ -n $(grep -P '\t' "$1") ]]; then if [[ -n $(grep -P '\t' "$1") ]]; then
echo 2
return 2 return 2
fi fi
echo 0
return 0 return 0
} }

View File

@ -3,17 +3,21 @@
isPhanTestSuccessful() { isPhanTestSuccessful() {
php -d memory_limit=4G ${rootpath}/vendor/bin/phan -k ${rootpath}/Build/Config/phan.php -f "$1" >&2 php -d memory_limit=4G ${rootpath}/vendor/bin/phan -k ${rootpath}/Build/Config/phan.php -f "$1" >&2
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo 0
return 0 return 0
fi fi
echo 1
return 1 return 1
} }
isPhpStanTestSuccessful() { isPhpStanTestSuccessful() {
php -d memory_limit=4G ${rootpath}/vendor/bin/phpstan analyse --autoload-file=${rootpath}/phpOMS/Autoloader.php -l 7 -c ${rootpath}/Build/Config/phpstan.neon "$1" >&2 php -d memory_limit=4G ${rootpath}/vendor/bin/phpstan analyse --autoload-file=${rootpath}/phpOMS/Autoloader.php -l 7 -c ${rootpath}/Build/Config/phpstan.neon "$1" >&2
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
echo 0
return 0 return 0
fi fi
echo 1
return 1 return 1
} }

View File

@ -13,7 +13,7 @@ git diff --name-only $TRAVIS_COMMIT_RANGE | while read FILE; do
fi fi
# Filename # Filename
if [[ $(isValidFileName "$FILE") = 1 ]]; then if [[ $(isValidFileName "$FILE") = 0 ]]; then
echo -e "\e[1;31m\tInvalid file name '$FILE'.\e[0m" >&2 echo -e "\e[1;31m\tInvalid file name '$FILE'.\e[0m" >&2
exit 1 exit 1
fi fi
@ -40,8 +40,7 @@ git diff --name-only $TRAVIS_COMMIT_RANGE | while read FILE; do
# Syntax # Syntax
if [[ "$FILE" =~ ^.+(php)$ ]]; then if [[ "$FILE" =~ ^.+(php)$ ]]; then
$(hasInvalidPhpSyntax "$FILE") PHP_SYNTAX=$(hasInvalidPhpSyntax "$FILE")
PHP_SYNTAX=$?
if [[ $PHP_SYNTAX = 1 ]]; then if [[ $PHP_SYNTAX = 1 ]]; then
echo -e "\e[1;31m\tPhp linting error.\e[0m" >&2 echo -e "\e[1;31m\tPhp linting error.\e[0m" >&2
@ -65,8 +64,7 @@ git diff --name-only $TRAVIS_COMMIT_RANGE | while read FILE; do
fi fi
if [[ "$FILE" =~ ^.+(sh|js|php|json|css)$ ]]; then if [[ "$FILE" =~ ^.+(sh|js|php|json|css)$ ]]; then
$(hasInvalidBasicSyntax "$FILE") GEN_SYNTAX=$(hasInvalidBasicSyntax "$FILE")
GEN_SYNTAX=$?
if [[ $GEN_SYNTAX = 1 ]]; then if [[ $GEN_SYNTAX = 1 ]]; then
echo -e "\e[1;31m\tFound whitespace at end of line in $FILE.\e[0m" >&2 echo -e "\e[1;31m\tFound whitespace at end of line in $FILE.\e[0m" >&2
@ -82,8 +80,7 @@ git diff --name-only $TRAVIS_COMMIT_RANGE | while read FILE; do
fi fi
if [[ "$FILE" =~ ^.+(tpl\.php|html)$ ]]; then if [[ "$FILE" =~ ^.+(tpl\.php|html)$ ]]; then
$(hasInvalidHtmlTemplateContent "$FILE") TPL_SYNTAX=$(hasInvalidHtmlTemplateContent "$FILE")
TPL_SYNTAX=$?
if [[ $TPL_SYNTAX = 1 ]]; then if [[ $TPL_SYNTAX = 1 ]]; then
echo -e "\e[1;31m\tFound missing image alt attribute.\e[0m" >&2 echo -e "\e[1;31m\tFound missing image alt attribute.\e[0m" >&2