Skip to content

Instantly share code, notes, and snippets.

@freshp
Last active April 6, 2020 11:57
Show Gist options
  • Save freshp/977f4b5bc67bd2426450680adb172d54 to your computer and use it in GitHub Desktop.
Save freshp/977f4b5bc67bd2426450680adb172d54 to your computer and use it in GitHub Desktop.
Run php SCA as pre-push-hook installed by composer as script

install the pre-push-hook with composer

  • example composer.json part
...
    "scripts": {
        "post-install-cmd": [
            "@install-pre-push-hook"
        ],
        "post-update-cmd": [
            "@install-pre-push-hook"
        ],
        "install-pre-push-hook": [
            "./pre-push-hook-installer.sh"
        ],
        "quickcheck": [
            "/usr/local/bin/php ./vendor/bin/phpunit",
            "/usr/local/bin/php ./vendor/bin/phpstan analyse",
            "/usr/local/bin/php ./vendor/bin/phpcs"
        ]
    },
...
#!/usr/bin/env bash
# possibility to bypass hook
if [[ -f "./NO_PRE_PUSH_HOOK_EXECUTION" ]]; then
echo "Pre push was not executed. Please delete the 'NO_PRE_PUSH_HOOK_EXECUTION' from the root directory."
exit
fi
# run quickcheck script from composer as an example
echo 'run quickcheck'
/usr/local/bin/php /usr/local/bin/composer quickcheck
#!/usr/bin/env bash
outputPrefix="PRE-PUSH-INSTALLER:"
redColor="\033[0;31m"
greenColor="\033[0;32m"
noColor="\033[0m"
osRegex="^(darwin|linux|bsd).*$"
if [[ ! "$OSTYPE" =~ $osRegex ]]; then
echo -e "${redColor}$outputPrefix current operating system ('$OSTYPE') is not supported for pre-push-installer${noColor}"
exit
fi
composerPath="/usr/local/bin/composer"
if [[ ! -x "$composerPath" ]]; then
echo -e "${redColor}$outputPrefix could not find a global installation of composer under $composerPath${noColor}"
exit
fi
phpPath="/usr/local/bin/php"
if [[ ! -x "$phpPath" ]]; then
echo -e "${redColor}$outputPrefix could not find a global version of php under $phpPath${noColor}"
exit
fi
gitHookPath=".git/hooks"
if [[ ! -d "$gitHookPath" ]]; then
mkdir -p ${gitHookPath}
echo -e "${greenColor}$outputPrefix folder for git hooks generated${noColor}"
fi
gitHookPath=".git/hooks"
prePushHook="$gitHookPath/pre-push"
softLinkSource="../../pre-push-hook.sh"
if [[ ! -f ${prePushHook} ]]; then
ln -snf ${softLinkSource} ${prePushHook}
echo -e "${greenColor}$outputPrefix symlink to '$prePushHook' was generated${noColor}"
else
if [[ ! "$(readlink ${gitHookPath}/pre-push)" = "$softLinkSource" ]]; then
echo -e "${redColor}$outputPrefix another pre-push hook already exists.${noColor}"
echo -e "$outputPrefix delete the '$prePushHook' hook and rerun the installer '$0'"
exit
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment