jira_check_lib.sh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/bin/bash
  2. function get_sed () {
  3. OS=$(uname -s)
  4. if [ "${OS}" == "Linux" ]; then
  5. SED=$(command -v sed)
  6. else
  7. SED=$(command -v gsed)
  8. fi
  9. if [ -z "${SED}" ]; then
  10. echo ">> GNU 'sed' was not found on the system."
  11. echo ">> Please install it"
  12. exit 1
  13. fi
  14. echo "${SED}"
  15. }
  16. function not_include_ticket () {
  17. set -o pipefail
  18. if $(cat "$*" | grep -q "^JIRA: "); then
  19. return 1
  20. fi
  21. return 0
  22. }
  23. function get_branch_name () {
  24. echo "$(git branch --show-current)"
  25. }
  26. # Function to extract the ticket number:
  27. # Ex: AVXSRE-12324-toto -> AVXSRE-1234
  28. # AVXSRE-12134/titi -> AVXSRE-1234
  29. function clean_branch_name () {
  30. echo "$1" | grep -E -o '[A-Z][A-Z0-9]+-[0-9]+'
  31. }
  32. function add_jira_ref () {
  33. # hook arguments
  34. COMMIT_MSG_FILE=$1
  35. # Possible values are none (git commit), message (git commit -m <msg>), template, merge, squash, or commit
  36. COMMIT_SOURCE=$2
  37. SED="$(get_sed)"
  38. BRANCH="$(get_branch_name)"
  39. CLEAN_BRANCH="$(clean_branch_name $BRANCH)"
  40. # check branch name isn’t empty (typical e.g. during rebase)
  41. if not_include_ticket "${COMMIT_MSG_FILE}" && [ -n "${CLEAN_BRANCH}" ]; then
  42. # check that this is a "message": if a -m or -F option was given
  43. if [ "${COMMIT_SOURCE}" == "message" ]; then
  44. echo >> "${COMMIT_MSG_FILE}"
  45. echo "JIRA: ${CLEAN_BRANCH}" >> "${COMMIT_MSG_FILE}"
  46. return 0
  47. fi
  48. # check that this is a "commit"
  49. if [ -z "${COMMIT_SOURCE}" ]; then
  50. ${SED} -i "1s@^@\n\nJIRA: ${CLEAN_BRANCH}@" "${COMMIT_MSG_FILE}"
  51. return 0
  52. fi
  53. # check that this is an "amend"
  54. if [ "${COMMIT_SOURCE}" == "commit" ]; then
  55. ${SED} -i "2s@^@\n\nJIRA: ${CLEAN_BRANCH}\n@" "${COMMIT_MSG_FILE}"
  56. return 0
  57. fi
  58. fi
  59. }