Last active
August 27, 2018 21:30
-
-
Save beccaonx/000d6ccafb57fa736bd1a9d2f397ff6d to your computer and use it in GitHub Desktop.
List the stories that have been merged to master since the $last_release_tag, ask for confirmation to tag and push for release testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
### Actions | |
# - git checkout master | |
# - git pull --rebase | |
# - List the PRs that have been merged to master since the last tag (according to git describe) | |
# - Ask for confirmation to tag and push. If confirmed: | |
# - generate a release tag based on timestamp | |
# - git push --tags | |
# ### Usage: | |
# ./release_the_kraken | |
# ### Requirements | |
# Must be run in the gospotcheck project's root directory | |
# User must have permission to pull and push the gospotcheck repo | |
# GITHUB_API_TOKEN env variable must be set | |
# TRACKER_API_TOKEN env variable must be set | |
# ### Error Conditions. The script will print an error message and exit for the following reasons: | |
# - API token env variables not set | |
# - any of the git commands fail | |
# - No PRs merged to master since last release | |
# ### Limitations | |
# If more than 1 Tracker story is listed in a PR, only the first will be shown | |
###### Constants | |
RED='\033[0;31m' | |
CYAN='\033[0;36m' | |
NC='\033[0m' # No Color | |
####### Functions | |
function confirm_release { | |
echo "${RED}***** Would you like to release these PRs? (Y/N)" | |
read -r answer | |
if [ "$answer" != "${answer#[Yy]}" ] ;then | |
echo | |
else | |
echo "OK, exiting." | |
exit 0 | |
fi | |
} | |
######## Start script | |
if [ "$#" -eq 1 ]; then | |
last_release_tag=$1 | |
fi | |
if [ -z $TRACKER_API_TOKEN ];then | |
echo "${RED}You need to set the TRACKER_API_TOKEN env variable. Exiting.${NC}" | |
exit 1; | |
fi | |
if [ -z $GITHUB_API_TOKEN ];then | |
echo "${RED}You need to set the GITHUB_API_TOKEN env variable. Exiting.${NC}" | |
exit 1; | |
fi | |
echo "${CYAN}>>>Checking out the gospotcheck master branch...${NC}" | |
git checkout master | |
if [ "$?" -ne 0 ]; then | |
echo "${RED}checkout master failed. Make sure you are in the gospotcheck directory. Exiting." | |
exit 1 | |
fi | |
echo | |
echo "${CYAN}>>>Pulling down the contents of the gospotcheck master branch...${NC}" | |
git pull --rebase | |
if [ "$?" -ne 0 ]; then | |
echo "${RED}pull failed. Exiting." | |
exit 1 | |
fi | |
echo | |
if [ -z $last_release_tag ]; then | |
last_release_tag=$(git describe --abbrev=0 --tags) # gets last tag from current branch | |
fi | |
git log "$last_release_tag"..HEAD --merges --oneline | grep "pull request" > /dev/null | |
if [ "$?" -eq 1 ]; then | |
echo "${RED}No pull requests have been merged since the $last_release_tag. Exiting." | |
exit 0 | |
fi | |
echo "${CYAN}>>>The following PRs have been merged to master since the $last_release_tag:${NC}" | |
printf "%s %s %s\n" "PR" "Tracker Story Link" "Tracker Story Title" | |
git log "$last_release_tag"..HEAD --merges --oneline | grep "pull request" | grep -v "gospotcheck/master" | cut -d'#' -f2 | cut -d' ' -f1 | sort | while read pr_num; do | |
tracker_story_link=$(curl -s -H "Authorization: token $GITHUB_API_TOKEN" https://api.github.com/repos/gospotcheck/gospotcheck/pulls/$pr_num | grep body | sed s/"\["//g | sed s/"\]"//g | grep -o 'https:\/\/www.pivotaltracker.com\/story\/show\/[0-9]*' | head -1) | |
if [ -z "$tracker_story_link" ]; then | |
tracker_story_link="Tracker link not found in PR! " | |
tracker_story_title="" | |
else | |
tracker_story_number=`echo $tracker_story_link | rev | cut -d"/" -f1 | rev` | |
tracker_story_title=$(curl -s -X GET -H "X-TrackerToken: $TRACKER_API_TOKEN" https://www.pivotaltracker.com/services/v5/stories/$tracker_story_number | sed '/name/s/\\"//g' | grep -o '"name":"[^"]*' | head -1 | cut -d"\"" -f4) | |
if [ -z "$tracker_story_title" ]; then | |
tracker_story_title="NO Story title" | |
fi | |
fi | |
printf "#%s %s %s\n" "$pr_num" "$tracker_story_link" "$tracker_story_title" | |
done | |
confirm_release | |
today=$(date +%Y%m%d_%H%M%S) | |
new_release_tag="${today}_release" | |
echo "${CYAN}>>>Tagging this branch as $new_release_tag...${NC}" | |
git tag $new_release_tag | |
if [ "$?" -ne 0 ]; then | |
echo "${RED}git tag failed. Exiting." | |
exit 1 | |
fi | |
echo "${CYAN}>>>Pushing the tag...${NC}" | |
git push --tags | |
if [ "$?" -ne 0 ]; then | |
echo "${RED}Push tags failed. Deleting local tag and exiting." | |
git tag --delete "${NEW_RELEASE_TAG}" | |
exit 1 | |
fi | |
echo | |
echo "${CYAN}***** All done! *****${NC}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment