reconquest / atlassian-external-hooks

External Hooks plugin for Atlassian Bitbucket
https://external-hooks.reconquest.io
Other
45 stars 37 forks source link

Setting up file encoding check on git push #134

Open adchatakora opened 1 year ago

adchatakora commented 1 year ago

I am having issues with the plugin where it seems to be getting stuck on the previous error code generated . Running bit bucket DC version 7.21.2 with the latest version of external-hooks My scripts looks like this ( and perhaps it is my script the issue) but i cannot seem to find what it . The idea here to reject pushes that have commits with files that are not UTF-8 or ASCII" encoded. The problem here is when i test it it works for the first run, then the subsequent runs just keep returning the previous error that was blocked.

#!/bin/bash
echo "Checking diff....."

unset precommit
#precommit=$(git log --format="%H" -n 1 --name-only  --not --remotes --diff-filter=d | grep "json\|resx\|yml\|sql\|txt"| xargs -n 1 file | grep -v "UTF-8|^ASCII") 
precommit=$(git show --format="%H" --name-only --diff-filter=d | grep "json\|resx\|yml\|sql\|txt" | xargs -n 1 file | grep -v "UTF-8\|^ASCII")
if test "$precommit" 
then 
   echo "Encoding issue detected in these files:" 
   echo $precommit 
   echo "Files should be UTF-8 or ASCII. Commit aborted!" 
   unset precommit
   exit 1 

fi 
   exit 0
kovetskiy commented 1 year ago

Hi, your code doesn't really check latest commits during the push, you should consider using BB_FROM_HASH BB_TO_HASH and BB_FROM_REF. Envs are documented here: https://external-hooks.reconquest.io/docs/environment-variables/

You can see how it can be used here: https://external-hooks.reconquest.io/docs/example.merge-check-disallow-wip-commits/