Skip to content

Instantly share code, notes, and snippets.

@wytrych
Last active February 12, 2018 14:07
Show Gist options
  • Save wytrych/822a5e0ef89172bbaf88fe4767ef7a84 to your computer and use it in GitHub Desktop.
Save wytrych/822a5e0ef89172bbaf88fe4767ef7a84 to your computer and use it in GitHub Desktop.
A script finding merge commits between two latest tags and posting them to slack.
#!/bin/sh
HOOK_URL=<SLACK_HOOK_URL>
CHANNEL=<TARGET_CHANNEL>
REPO_NAME=`git config --get remote.origin.url | awk 'match($0,/\/([a-zA-Z1-9-]*)/) {print substr($0,RSTART+1,RLENGTH-1)}'`
REPO_URL=`git config remote.origin.url | sed 's/.*@\(.*\)\.git/\1/g' | sed 's/:/\//'`/pull
ESCAPED_REPO_URL=`echo $REPO_URL | sed 's/\//\\\\\//g'` #so many slashes!
read a b <<< $(git tag -l --sort=version:refname "v*" | tail -2)
read PULL_REQUESTS <<< $(git log --oneline $a...$b | grep "pull request" | awk 'match($0,/#[0-9]*/) {print substr($0,RSTART,RLENGTH)}')
PRS_WITH_LINKS=`echo $PULL_REQUESTS | sed -E "s/#([0-9]*)([ ]|$)/<https:\/\/$ESCAPED_REPO_URL\/\1|&> /g"`
MESSAGE="~~~ deploying $REPO_NAME $PRS_WITH_LINKS ~~~"
echo "Sending message to slack:"
echo $MESSAGE
curl -X POST --data-urlencode "payload={\"channel\": \"$CHANNEL\", \"username\": \"Pre-deploy bot\", \"text\": \"$MESSAGE\", \"icon_emoji\": \":airplane_departure:\"}" $HOOK_URL
@alexteg-advisa
Copy link

The git tags are not using natural sort by default when they contain something else than numbers, such as being prepended with v in our case. This means for example that v1.0.2 comes after v1.0.10.

To fix this issue, change git tag to git tag -l --sort=version:refname "v*" so that line becomes:

read a b <<< $(git tag -l --sort=version:refname "v*" | tail -2)

@wytrych
Copy link
Author

wytrych commented Feb 12, 2018

Thanks

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