Create travis build+pull out to functions

This commit is contained in:
Dennis Eichhorn 2018-08-11 11:06:56 +02:00
parent 3cd6b6af54
commit 8443c22066
7 changed files with 330 additions and 135 deletions

View File

@ -1,12 +1,124 @@
#!/bin/bash
git diff --cached --name-only | while read FILE; do
if [[ ! -f "$FILE" ]]; then
exit 0
fi
done
. ${rootpath}/Build/Hooks/logging.sh
. ${rootpath}/Build/Hooks/syntax.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
if [[ ! -f "$FILE" ]]; then
continue
fi
# Filename
if [[ ! $(isValidFileName "$FILE") = 1 ]]; then
echo -e "\e[1;31m\tInvalid file name.\e[0m" >&2
exit 1
fi
# Logging
if [[ "$FILE" =~ ^.+(php)$ ]] && [[ $(hasPhpLogging "$FILE") = 1 ]]; then
echo -e "\e[1;33m\tWarning, the commit contains a call to var_dump() in '$FILE'. Commit was not aborted, however.\e[0m" >&2
fi
if [[ "$FILE" =~ ^.+(js)$ ]] && [[ $(hasJsLogging "$FILE") = 1 ]]; then
echo -e "\e[1;33m\tWarning, the commit contains a call to console.log() in '$1'. Commit was not aborted, however.\e[0m" >&2
fi
# Tests
if [[ "$FILE" =~ ^.+(php)$ ]] && [[ ! $(isPhanTestSuccessfull "$FILE") ]]; then
echo -e "\e[1;31m\tPhan error.\e[0m" >&2
exit 1
fi
if [[ "$FILE" =~ ^.+(php)$ ]] && [[ ! $(isPhpStanTestSuccessfull "$FILE") ]]; then
echo -e "\e[1;31m\tPhp stan error.\e[0m" >&2
exit 1
fi
# Syntax
if [[ "$FILE" =~ ^.+(php)$ ]]; then
PHP_SYNTAX = $(hasInvalidPhpSyntax "$FILE")
if [[ $PHP_SYNTAX = 1 ]]; then
echo -e "\e[1;31m\tPhp linting error.\e[0m" >&2
exit 1
fi
if [[ $PHP_SYNTAX = 2 ]]; then
echo -e "\e[1;31m\tCode Sniffer error.\e[0m" >&2
exit 1
fi
if [[ $PHP_SYNTAX = 3 ]]; then
echo -e "\e[1;31m\tMess Detector error.\e[0m" >&2
exit 1
fi
fi
if [[ "$FILE" =~ ^.+(sh)$ ]] && [[ ! $(isValidBashScript "$FILE") ]]; then
echo -e "\e[1;31m\tBash linting error.\e[0m" >&2
exit 1
fi
if [[ "$FILE" =~ ^.+(sh|js|php|json|css)$ ]]; then
GEN_SYNTAX = $(hasInvalidBasicSyntax "$FILE")
if [[ $GEN_SYNTAX = 1 ]]; then
echo -e "\e[1;31m\tFound whitespace at end of line in $1.\e[0m" >&2
grep -P ' $' $1 >&2
exit 1
fi
if [[ $GEN_SYNTAX = 2 ]]; then
echo -e "\e[1;31m\tFound tab instead of whitespace $1.\e[0m" >&2
grep -P '\t' $1 >&2
exit 1
fi
fi
if [[ "$FILE" =~ ^.+(tpl\.php|html)$ ]]; then
TPL_SYNTAX = $(hasInvalidHtmlTemplateContent "$FILE")
if [[ $TPL_SYNTAX = 1 ]]; then
echo -e "\e[1;31m\tFound missing image alt attribute.\e[0m" >&2
grep -P '(\<img)((?!.*?alt=).)*(>)' "$FILE" >&2
exit 1
fi
if [[ $TPL_SYNTAX = 2 ]]; then
echo -e "\e[1;31m\tFound missing input type attribute.\e[0m" >&2
grep -P '(<input)((?!.*?type=).)*(>)' "$FILE" >&2
exit 1
fi
if [[ $TPL_SYNTAX = 3 ]]; then
echo -e "\e[1;31m\tFound missing form element name.\e[0m" >&2
grep -P '(<input|<select|<textarea)((?!.*?name=).)*(>)' "$FILE" >&2
exit 1
fi
if [[ $TPL_SYNTAX = 4 ]]; then
echo -e "\e[1;31m\tFound missing form element action, method or id.\e[0m" >&2
grep -P '(\<form)((?!.*?(action|method|id)=).)*(>)' "$FILE" >&2
exit 1
fi
if [[ $TPL_SYNTAX = 5 ]]; then
echo -e "\e[1;31m\tFound inline styles.\e[0m" >&2
grep -P '(style=)' "$FILE" >&2
fi
if [[ $TPL_SYNTAX = 6 ]]; then
echo -e "\e[1;31m\tAttribute description should not be hard coded.\e[0m" >&2
grep -P '(value|title|alt|aria\-label)(=\")((?!\<\?).)*(>)' "$FILE" >&2
fi
if [[ $TPL_SYNTAX = 7 ]]; then
echo -e "\e[1;31m\tFound hard coded text.\e[0m" >&2
grep -P '(\<td\>|\<th\>|\<caption\>|\<label.*?(\"|l)\>)[0-9a-zA-Z\.\?]+' "$FILE" >&2
fi
fi
done
exit 0

View File

@ -1,15 +1,14 @@
#!/bin/bash
allownonascii="false"
isValidFileName() {
allownonascii="false"
git diff --cached --name-only | while read FILE; do
if [ "$allownonascii" != "true" ] &&
test $(git diff --cached --name-only --diff-filter=A -z $1 |
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
then
return 0
fi
if [ "$allownonascii" != "true" ] &&
test $(git diff --cached --name-only --diff-filter=A -z $FILE |
LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
then
echo -e "\e[1;31m\tInvalid file name.\e[0m" >&2
exit 1
fi
done || exit $?
return 1
}

View File

@ -1,20 +1,19 @@
#!/bin/bash
git diff --cached --name-only | while read FILE; do
if [[ "$FILE" =~ ^.+(php)$ ]]; then
RESULT=$(grep "var_dump(" "$FILE")
hasPhpLogging() {
RESULT=$(grep "var_dump(" "$1")
if [ ! -z $RESULT ]; then
echo -e "\e[1;33m\tWarning, the commit contains a call to var_dump() in '$FILE'. Commit was not aborted, however.\e[0m" >&2
return 1
fi
fi
done
git diff --cached --name-only | while read FILE; do
if [[ "$FILE" =~ ^.+(js)$ ]]; then
RESULT=$(grep "console.log(" "$FILE")
return 0;
}
hasJsLogging() {
RESULT=$(grep "console.log(" "$1")
if [ ! -z $RESULT ]; then
echo -e "\e[1;33m\tWarning, the commit contains a call to console.log() in '$FILE'. Commit was not aborted, however.\e[0m" >&2
return 1;
fi
fi
done || exit $?
return 0;
}

View File

@ -1,121 +1,85 @@
#!/bin/bash
git diff --cached --name-only | while read FILE; do
if [[ "$FILE" =~ ^.+(php)$ ]]; then
if [[ -f $FILE ]]; then
# php lint
php -l "$FILE"
if [ $? -ne 0 ]; then
echo -e "\e[1;31m\tPhp linting error.\e[0m" >&2
exit 1
fi
# phpcs
php -d memory_limit=4G ${rootpath}/vendor/bin/phpcs --standard="${rootpath}/Build/Config/phpcs.xml" --encoding=utf-8 -n -p $FILE
if [ $? -ne 0 ]; then
echo -e "\e[1;31m\tCode Sniffer error.\e[0m" >&2
exit 1
fi
# phpmd
php -d memory_limit=4G ${rootpath}/vendor/bin/phpmd $FILE text ${rootpath}/Build/Config/phpmd.xml --exclude *tests* --minimumpriority 1
if [ $? -ne 0 ]; then
echo -e "\e[1;31m\tMess Detector error.\e[0m" >&2
exit 1
fi
fi
fi
# Html/template checks
if [[ "$FILE" =~ ^.+(tpl\.php|html)$ ]]; then
# Invalid and empty attributes
if [[ -n $(grep -E '=\"[\#\$\%\^\&\*\(\)\\/\ ]*\"' $FILE) ]]; then
echo -e "\e[1;31m\tFound invalid attribute.\e[0m" >&2
grep -E '=\"[\#\$\%\^\&\*\(\)\\/\ ]*\"' $FILE >&2
exit 1
hasInvalidPhpSyntax() {
# php lint
php -l "$1"
if [ $? -ne 0 ]; then
return 1
fi
# Invalid class/id names
if [[ -n $(grep -E '(id|class)=\"[a-zA-Z]*[\#\$\%\^\&\*\(\)\\/\ ]+[a-zA-Z]*\"' $FILE) ]]; then
echo -e "\e[1;31m\tFound invalid class/id.\e[0m" >&2
grep -E '(id|class)=\"[a-zA-Z]*[\#\$\%\^\&\*\(\)\\/\ ]+[a-zA-Z]*\"' $FILE >&2
exit 1
# phpcs
php -d memory_limit=4G ${rootpath}/vendor/bin/phpcs --standard="${rootpath}/Build/Config/phpcs.xml" --encoding=utf-8 -n -p "$1"
if [ $? -ne 0 ]; then
return 2
fi
# phpmd
php -d memory_limit=4G ${rootpath}/vendor/bin/phpmd "$1" text ${rootpath}/Build/Config/phpmd.xml --exclude *tests* --minimumpriority 1
if [ $? -ne 0 ]; then
return 3
fi
return 0;
}
hasInvalidHtmlTemplateContent() {
# Images must have a alt= attribute *error*
if [[ -n $(grep -P '(\<img)((?!.*?alt=).)*(>)' $FILE) ]]; then
echo -e "\e[1;31m\tFound missing image alt attribute.\e[0m" >&2
grep -P '(\<img)((?!.*?alt=).)*(>)' $FILE >&2
exit 1
if [[ -n $(grep -P '(\<img)((?!.*?alt=).)*(>)' $1) ]]; then
return 1
fi
# Input elements must have a type= attribute *error*
if [[ -n $(grep -P '(<input)((?!.*?type=).)*(>)' $FILE) ]]; then
echo -e "\e[1;31m\tFound missing input type attribute.\e[0m" >&2
grep -P '(<input)((?!.*?type=).)*(>)' $FILE >&2
exit 1
if [[ -n $(grep -P '(<input)((?!.*?type=).)*(>)' $1) ]]; then
return 2
fi
# Form fields must have a name *error*
if [[ -n $(grep -P '(<input|<select|<textarea)((?!.*?name=).)*(>)' $FILE) ]]; then
echo -e "\e[1;31m\tFound missing form element name.\e[0m" >&2
grep -P '(<input|<select|<textarea)((?!.*?name=).)*(>)' $FILE >&2
exit 1
if [[ -n $(grep -P '(<input|<select|<textarea)((?!.*?name=).)*(>)' $1) ]]; then
return 3
fi
# Form must have a id, action and method *error*
if [[ -n $(grep -P '(\<form)((?!.*?(action|method|id)=).)*(>)' $FILE) ]]; then
echo -e "\e[1;31m\tFound missing form element action, method or id.\e[0m" >&2
grep -P '(\<form)((?!.*?(action|method|id)=).)*(>)' $FILE >&2
exit 1
if [[ -n $(grep -P '(\<form)((?!.*?(action|method|id)=).)*(>)' $1) ]]; then
return 4
fi
# Inline css is invalid *warning*
if [[ -n $(grep -P '(style=)' $FILE) ]]; then
echo -e "\e[1;31m\tFound inline styles.\e[0m" >&2
grep -P '(style=)' $FILE >&2
if [[ -n $(grep -P '(style=)' $1) ]]; then
return 5
fi
# Attribute descriptions should not be hard coded *warning*
if [[ -n $(grep -P '(value|title|alt|aria\-label)(=\")((?!\<\?).)*(>)' $FILE) ]]; then
echo -e "\e[1;31m\tAttribute description should not be hard coded.\e[0m" >&2
grep -P '(value|title|alt|aria\-label)(=\")((?!\<\?).)*(>)' $FILE >&2
if [[ -n $(grep -P '(value|title|alt|aria\-label)(=\")((?!\<\?).)*(>)' $1) ]]; then
return 6
fi
# Hard coded language *warning*
if [[ -n $(grep -P '(\<td\>|\<th\>|\<caption\>|\<label.*?(\"|l)\>)[0-9a-zA-Z\.\?]+' $FILE) ]]; then
echo -e "\e[1;31m\tFound hard coded text.\e[0m" >&2
grep -P '(\<td\>|\<th\>|\<caption\>|\<label.*?(\"|l)\>)[0-9a-zA-Z\.\?]+' $FILE >&2
if [[ -n $(grep -P '(\<td\>|\<th\>|\<caption\>|\<label.*?(\"|l)\>)[0-9a-zA-Z\.\?]+' $1) ]]; then
return 7
fi
fi
if [[ "$FILE" =~ ^.+(sh)$ ]]; then
if [[ -f $FILE ]]; then
# sh lint
bash -n "$FILE" 1> /dev/null
if [ $? -ne 0 ]; then
echo -e "\e[1;31m\tBash linting error.\e[0m" >&2
exit 1
fi
return 0
}
isValidBashScript() {
bash -n "$1" 1> /dev/null
if [ $? -ne 0 ]; then
return 0
fi
fi
# text files in general
if [[ "$FILE" =~ ^.+(sh|js|php|json|css)$ ]]; then
return 1
}
hasInvalidBasicSyntax() {
# Check whitespace end of line in code
if [[ -n $(grep -P ' $' $FILE) ]]; then
echo -e "\e[1;31m\tFound whitespace at end of line in $FILE.\e[0m" >&2
grep -P ' $' $FILE >&2
exit 1
if [[ -n $(grep -P ' $' $1) ]]; then
return 1
fi
# Check for tabs
if [[ -n $(grep -P '\t' $FILE) ]]; then
echo -e "\e[1;31m\tFound tab instead of whitespace $FILE.\e[0m" >&2
grep -P '\t' $FILE >&2
exit 1
if [[ -n $(grep -P '\t' $1) ]]; then
return 2
fi
fi
done || exit $?
return 0
}

View File

@ -1,23 +1,19 @@
#!/bin/bash
git diff --cached --name-only | while read FILE; do
if [[ "$FILE" =~ ^.+(php)$ ]]; then
if [[ -f $FILE ]]; then
# phan
php -d memory_limit=4G ${rootpath}/vendor/bin/phan -k ${rootpath}/Build/Config/phan.php -f $FILE
if [ $? -ne 0 ]; then
echo -e "\e[1;31m\tPhan error.\e[0m" >&2
exit 1;
fi
# phpstan
php -d memory_limit=4G ${rootpath}/vendor/bin/phpstan analyse --autoload-file=${rootpath}/phpOMS/Autoloader.php -l 7 -c ${rootpath}/Build/Config/phpstan.neon $FILE
if [ $? -ne 0 ]; then
echo -e "\e[1;31m\tPhp stan error.\e[0m" >&2
exit 1;
fi
isPhanTestSuccessfull() {
php -d memory_limit=4G ${rootpath}/vendor/bin/phan -k ${rootpath}/Build/Config/phan.php -f "$1"
if [ $? -ne 0 ]; then
return 0;
fi
fi
done || exit $?
return 1;
}
isPhpStanTestSuccessfull() {
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"
if [ $? -ne 0 ]; then
return 0;
fi
return 1;
}

124
Hooks/travis.sh Normal file
View File

@ -0,0 +1,124 @@
#!/bin/bash
. ${rootpath}/Build/Hooks/logging.sh
. ${rootpath}/Build/Hooks/syntax.sh
. ${rootpath}/Build/Hooks/filename.sh
. ${rootpath}/Build/Hooks/tests.sh
($(git diff --name-only $TRAVIS_COMMIT_RANGE)) | while read FILE; do
if [[ ! -f "$FILE" ]]; then
continue
fi
# Filename
if [[ ! $(isValidFileName "$FILE") = 1]]; then
echo -e "\e[1;31m\tInvalid file name.\e[0m" >&2
exit 1
fi
# Logging
if [[ "$FILE" =~ ^.+(php)$ ]] && [[ $(hasPhpLogging "$FILE") = 1]]; then
echo -e "\e[1;33m\tWarning, the commit contains a call to var_dump() in '$FILE'. Commit was not aborted, however.\e[0m" >&2
fi
if [[ "$FILE" =~ ^.+(js)$ ]] && [[ $(hasJsLogging "$FILE") = 1]]; then
echo -e "\e[1;33m\tWarning, the commit contains a call to console.log() in '$1'. Commit was not aborted, however.\e[0m" >&2
fi
# Tests
if [[ "$FILE" =~ ^.+(php)$ ]] && [[ ! $(isPhanTestSuccessfull "$FILE")]]; then
echo -e "\e[1;31m\tPhan error.\e[0m" >&2
exit 1
fi
if [[ "$FILE" =~ ^.+(php)$ ]] && [[ ! $(isPhpStanTestSuccessfull "$FILE")]]; then
echo -e "\e[1;31m\tPhp stan error.\e[0m" >&2
exit 1
fi
# Syntax
if [[ "$FILE" =~ ^.+(php)$ ]]; then
PHP_SYNTAX = $(hasInvalidPhpSyntax "$FILE")
if [[ $PHP_SYNTAX = 1 ]]; then
echo -e "\e[1;31m\tPhp linting error.\e[0m" >&2
exit 1
fi
if [[ $PHP_SYNTAX = 2 ]]; then
echo -e "\e[1;31m\tCode Sniffer error.\e[0m" >&2
exit 1
fi
if [[ $PHP_SYNTAX = 3 ]]; then
echo -e "\e[1;31m\tMess Detector error.\e[0m" >&2
exit 1
fi
fi
if [[ "$FILE" =~ ^.+(sh)$ ]] && [[ ! $(isValidBashScript "$FILE")]]; then
echo -e "\e[1;31m\tBash linting error.\e[0m" >&2
exit 1
fi
if [[ "$FILE" =~ ^.+(sh|js|php|json|css)$ ]]; then
GEN_SYNTAX = $(hasInvalidBasicSyntax "$FILE")
if [[ $GEN_SYNTAX = 1 ]]; then
echo -e "\e[1;31m\tFound whitespace at end of line in $1.\e[0m" >&2
grep -P ' $' $1 >&2
exit 1
fi
if [[ $GEN_SYNTAX = 2 ]]; then
echo -e "\e[1;31m\tFound tab instead of whitespace $1.\e[0m" >&2
grep -P '\t' $1 >&2
exit 1
fi
fi
if [[ "$FILE" =~ ^.+(tpl\.php|html)$ ]]; then
TPL_SYNTAX = $(hasInvalidHtmlTemplateContent "$FILE")
if [[ $TPL_SYNTAX = 1 ]]; then
echo -e "\e[1;31m\tFound missing image alt attribute.\e[0m" >&2
grep -P '(\<img)((?!.*?alt=).)*(>)' "$FILE" >&2
exit 1
fi
if [[ $TPL_SYNTAX = 2 ]]; then
echo -e "\e[1;31m\tFound missing input type attribute.\e[0m" >&2
grep -P '(<input)((?!.*?type=).)*(>)' "$FILE" >&2
exit 1
fi
if [[ $TPL_SYNTAX = 3 ]]; then
echo -e "\e[1;31m\tFound missing form element name.\e[0m" >&2
grep -P '(<input|<select|<textarea)((?!.*?name=).)*(>)' "$FILE" >&2
exit 1
fi
if [[ $TPL_SYNTAX = 4 ]]; then
echo -e "\e[1;31m\tFound missing form element action, method or id.\e[0m" >&2
grep -P '(\<form)((?!.*?(action|method|id)=).)*(>)' "$FILE" >&2
exit 1
fi
if [[ $TPL_SYNTAX = 5 ]]; then
echo -e "\e[1;31m\tFound inline styles.\e[0m" >&2
grep -P '(style=)' "$FILE" >&2
fi
if [[ $TPL_SYNTAX = 6 ]]; then
echo -e "\e[1;31m\tAttribute description should not be hard coded.\e[0m" >&2
grep -P '(value|title|alt|aria\-label)(=\")((?!\<\?).)*(>)' "$FILE" >&2
fi
if [[ $TPL_SYNTAX = 7 ]]; then
echo -e "\e[1;31m\tFound hard coded text.\e[0m" >&2
grep -P '(\<td\>|\<th\>|\<caption\>|\<label.*?(\"|l)\>)[0-9a-zA-Z\.\?]+' "$FILE" >&2
fi
fi
done
exit 0

View File

@ -5,6 +5,7 @@ apt-get update
apt-get install git php7.2 php7.2-cli php7.2-common php7.2-json php7.2-opcache php7.2-pdo php7.2-sqlite php7.2-mbstring php7.2-curl php7.2-imap php7.2-bcmath php7.2-zip php7.2-dom php7.2-xml php7.2-phar php7.2-gd php7.2-dev php-pear apache2 mysql-server
a2enmod rewrite
a2enmod headers
pecl install ast