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
set -x
. ${rootpath}/Build/Hooks/logging.sh
. ${rootpath}/Build/Hooks/syntax.sh
. ${rootpath}/Build/Hooks/filename.sh
. ${rootpath}/Build/Hooks/tests.sh
git diff --cached --name-only | while read FILE; do
echo $FILE
if [[ ! -f "$FILE" ]]; then
continue
fi
# Filename
if [[ $(isValidFileName "$FILE") = 1 ]]; then
if [[ $(isValidFileName "$FILE") = 0 ]]; then
echo -e "\e[1;31m\tInvalid file name '$FILE'.\e[0m" >&2
exit 1
fi
@ -38,8 +42,7 @@ git diff --cached --name-only | while read FILE; do
# Syntax
if [[ "$FILE" =~ ^.+(php)$ ]]; then
$(hasInvalidPhpSyntax "$FILE")
PHP_SYNTAX=$?
PHP_SYNTAX=$(hasInvalidPhpSyntax "$FILE")
if [[ $PHP_SYNTAX = 1 ]]; then
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
if [[ "$FILE" =~ ^.+(sh|js|php|json|css)$ ]]; then
$(hasInvalidBasicSyntax "$FILE")
GEN_SYNTAX=$?
GEN_SYNTAX=$(hasInvalidBasicSyntax "$FILE")
if [[ $GEN_SYNTAX = 1 ]]; then
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
if [[ "$FILE" =~ ^.+(tpl\.php|html)$ ]]; then
$(hasInvalidHtmlTemplateContent "$FILE")
TPL_SYNTAX=$?
TPL_SYNTAX=$(hasInvalidHtmlTemplateContent "$FILE")
if [[ $TPL_SYNTAX = 1 ]]; then
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" |
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
then
echo 0
return 0
fi
echo 1
return 1
}

View File

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

View File

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

View File

@ -3,17 +3,21 @@
isPhanTestSuccessful() {
php -d memory_limit=4G ${rootpath}/vendor/bin/phan -k ${rootpath}/Build/Config/phan.php -f "$1" >&2
if [ $? -ne 0 ]; then
echo 0
return 0
fi
echo 1
return 1
}
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
if [ $? -ne 0 ]; then
echo 0
return 0
fi
echo 1
return 1
}

View File

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