post-update.sh 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/bin/bash
  2. #
  3. #
  4. # This script is copyright Oz N Tiram <nahumoz@gmail.com>
  5. # It is distributed under the terms of GPLv3
  6. # To use this hook rename it to post-update
  7. # Also you need the following software packages installed
  8. # Take a look also in the companion config file
  9. # post-push.conf, you should edit it before using this hook
  10. #
  11. # * curl
  12. # * openssl
  13. #
  14. # you can control the script by editing post-update.conf
  15. # ~~~ HERE BE DRAGONS ! ~~~
  16. source ./hooks/post-update.conf
  17. TO_SUBMIT="["
  18. for HEAD in $@; do
  19. # We love bash too, but there a really fancy basename
  20. BRANCH=`basename $HEAD`
  21. REST_OF_JSON=$(git --no-pager log -n 1 --format='{"branch": "XXXBRANCHXXX", "ssh_url": "XXXSSH_URLXXX", "hashes":{"commit":"%H", "tree":"%T", "parents":"%P"},"author":{"date":"%ai","name":"%an","email":"%ae"},"committer":{"date":"%ci","name":"%cn","email":"%ce"},"repository":{"name":"XXXREPO_NAMEXXX"}}')
  22. REST_OF_JSON=${REST_OF_JSON/XXXBRANCHXXX/${BRANCH}}
  23. REST_OF_JSON=${REST_OF_JSON/XXXSSH_URLXXX/${SSH_URL}}
  24. REST_OF_JSON=${REST_OF_JSON/XXXREPO_NAMEXXX/${REPO_NAME}}
  25. JSON_TO_SUBMIT=$REST_OF_JSON
  26. TO_SUBMIT=$TO_SUBMIT$JSON_TO_SUBMIT","
  27. JSON_TO_SUBMIT=""
  28. done
  29. # BASH sorcery to remove the last character
  30. TO_SUBMIT=${TO_SUBMIT%?}"]"
  31. all=`mktemp`
  32. echo $TO_SUBMIT > $all
  33. SECRET=$(echo -n $TO_SUBMIT | openssl dgst -sha1 -hmac $SECRET_KEY)
  34. # Why does this produce a different digest?
  35. # SECRET1=$(openssl dgst -sha1 -hmac $SECRET_KEY $all)
  36. SECRET=`echo $SECRET | cut -d'=' -f2`
  37. curl -H "Content-Type: application/json" -H "${SECRET_KEY_NAME}:${SECRET}" \
  38. -X POST http://${SUBMIT_HOST}:${SUBMIT_PORT}/ --data @$all
  39. if [ $? -eq 0 ]; then
  40. rm $all
  41. fi