Skip to content

Instantly share code, notes, and snippets.

@kalpeshsingh
Last active February 15, 2024 02:00
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save kalpeshsingh/e7682478b8927700c714f12e37f0837e to your computer and use it in GitHub Desktop.
Save kalpeshsingh/e7682478b8927700c714f12e37f0837e to your computer and use it in GitHub Desktop.
A pre-push git hook that notify Amazon Chime group
#!/bin/sh
branch="$(git rev-parse --abbrev-ref HEAD)"
# get computer name to append in Chime message
username=$USER
# a branch name where you want to prevent git push. In this case, it's "master"
if [ "$branch" = "master" ]; then
echo "You can't commit directly to '"${branch}"' branch" # webstorm or intellij will show this as popup
curl -X POST "<Chime-Webhook-URL>" -H "Content-Type:application/json" --data '{"Content": "@All 🚧 '@"${username}"' trying to push code to '"${branch}"'. \n Please note that commit is not blocked but it is discouraged to push code directly to '"${branch}"'."}'
exit 1 # if you remove this line then it won't block push but send message to group and on command line
fi
# Things you need to do -
# 1. Copy above code
# 2. Go to your project/.git/hooks
# 3. Copy pre-push.sample and rename it to pre-push
# 4. Paste the above code
# 5. Make this file executable - $ chmod +x .git/hooks/pre-commit
# 6. Voila! Now you will see your push is failing in "master" branch
# 7. If you use Amazon Chime then create a webhook URL and replace the in cURL or add code which sends message to Slack or
# your preferred communication channel
# ---------------------AMAZON CHIME DOCS----------------------- #
# Amazon Chime Webhook documentation
# https://docs.aws.amazon.com/chime/latest/ug/webhooks.html
@looopTools
Copy link

Thanks a lot for this 👍

@jairoFernandez
Copy link

👍

@rohit679
Copy link

Thanks a lot, this really helps me in sorting out my mistakes

@praful-dhabekar
Copy link

Hi, thanks for this gist.
In your hook, you're getting branch but I wanted to also check which tags are available. Is there any way to achieve this?
Thanks in advance!

@TimHal
Copy link

TimHal commented Oct 12, 2022

I would suggest replacing the hardcoded master branch name with the default branch. Reason being that many repos made the switch to rename master to main or something else completely.

#!/bin/sh

protected_branch="$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')"
branch="$(git rev-parse --abbrev-ref HEAD)"

# get computer name to append in Chime message
username=$USER

# a branch name where you want to prevent git push. In this case, it's "master"
if [ "$branch" = "$protected_branch" ]; then
  echo "You can't commit directly to '"${branch}"' branch" # webstorm or intellij will show this as popup
  curl -X POST "<Chime-Webhook-URL>" -H "Content-Type:application/json" --data '{"Content": "@All 🚧  '@"${username}"' trying to push code to '"${branch}"'. \n Please note that commit is not blocked but it is discouraged to push code directly to '"${branch}"'."}'
  exit 1 # if you remove this line then it won't block push but send message to group and on command line
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment