Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@ftonato

This comment has been minimized.

Copy link

ftonato commented Oct 22, 2019

Hey @kalpeshsingh,

Thank you for sharing this hook, is incredibly useful!

I would like to suggest a fix for a typo on line #21, where you wrote $ chmod +x .git/hooks/pre-commit should be $ chmod +x .git/hooks/pre-push (based on line #19).

@devyani22

This comment has been minimized.

Copy link

devyani22 commented Dec 17, 2019

Great! How do I allow this to a specific user?

@kalpeshsingh

This comment has been minimized.

Copy link
Owner Author

kalpeshsingh commented Dec 18, 2019

Great! How do I allow this to a specific user?

You can use username variable and put a if condition to match that user.
It will then work for that specific user only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.