From 8443c220668bc038670972a720dd1f3cd5cc6ca3 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 11 Aug 2018 11:06:56 +0200 Subject: [PATCH] Create travis build+pull out to functions --- Hooks/delegator.sh | 126 ++++++++++++++++++++++++++++++++++++++--- Hooks/filename.sh | 21 ++++--- Hooks/logging.sh | 23 ++++---- Hooks/syntax.sh | 136 +++++++++++++++++---------------------------- Hooks/tests.sh | 34 +++++------- Hooks/travis.sh | 124 +++++++++++++++++++++++++++++++++++++++++ install.sh | 1 + 7 files changed, 330 insertions(+), 135 deletions(-) create mode 100644 Hooks/travis.sh diff --git a/Hooks/delegator.sh b/Hooks/delegator.sh index ef15558..07a9c47 100644 --- a/Hooks/delegator.sh +++ b/Hooks/delegator.sh @@ -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 \ No newline at end of file +. ${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 '(\)' "$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 '()' "$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 '()' "$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 '(\)' "$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 '(\|\|\|\)[0-9a-zA-Z\.\?]+' "$FILE" >&2 + fi + fi +done + +exit 0 \ No newline at end of file diff --git a/Hooks/filename.sh b/Hooks/filename.sh index 8d3ae5c..8470026 100644 --- a/Hooks/filename.sh +++ b/Hooks/filename.sh @@ -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 $? \ No newline at end of file + return 1 +} \ No newline at end of file diff --git a/Hooks/logging.sh b/Hooks/logging.sh index 4a7dce0..d02adae 100644 --- a/Hooks/logging.sh +++ b/Hooks/logging.sh @@ -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 $? \ No newline at end of file + return 0; +} \ No newline at end of file diff --git a/Hooks/syntax.sh b/Hooks/syntax.sh index 5a67f81..e13aaa0 100644 --- a/Hooks/syntax.sh +++ b/Hooks/syntax.sh @@ -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 '(\)' $FILE) ]]; then - echo -e "\e[1;31m\tFound missing image alt attribute.\e[0m" >&2 - grep -P '(\)' $FILE >&2 - exit 1 + if [[ -n $(grep -P '(\)' $1) ]]; then + return 1 fi # Input elements must have a type= attribute *error* - if [[ -n $(grep -P '()' $FILE) ]]; then - echo -e "\e[1;31m\tFound missing input type attribute.\e[0m" >&2 - grep -P '()' $FILE >&2 - exit 1 + if [[ -n $(grep -P '()' $1) ]]; then + return 2 fi # Form fields must have a name *error* - if [[ -n $(grep -P '()' $FILE) ]]; then - echo -e "\e[1;31m\tFound missing form element name.\e[0m" >&2 - grep -P '()' $FILE >&2 - exit 1 + if [[ -n $(grep -P '()' $1) ]]; then + return 3 fi # Form must have a id, action and method *error* - if [[ -n $(grep -P '(\)' $FILE) ]]; then - echo -e "\e[1;31m\tFound missing form element action, method or id.\e[0m" >&2 - grep -P '(\)' $FILE >&2 - exit 1 + if [[ -n $(grep -P '(\)' $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 '(\|\|\|\)[0-9a-zA-Z\.\?]+' $FILE) ]]; then - echo -e "\e[1;31m\tFound hard coded text.\e[0m" >&2 - grep -P '(\|\|\|\)[0-9a-zA-Z\.\?]+' $FILE >&2 + if [[ -n $(grep -P '(\|\|\|\)[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 +} diff --git a/Hooks/tests.sh b/Hooks/tests.sh index 8cac2de..009d663 100644 --- a/Hooks/tests.sh +++ b/Hooks/tests.sh @@ -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; +} diff --git a/Hooks/travis.sh b/Hooks/travis.sh new file mode 100644 index 0000000..cde1be7 --- /dev/null +++ b/Hooks/travis.sh @@ -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 '(\)' "$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 '()' "$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 '()' "$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 '(\)' "$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 '(\|\|\|\)[0-9a-zA-Z\.\?]+' "$FILE" >&2 + fi + fi +done + +exit 0 \ No newline at end of file diff --git a/install.sh b/install.sh index dcf8a57..5cfe610 100644 --- a/install.sh +++ b/install.sh @@ -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