Skip to content

Instantly share code, notes, and snippets.

@beccaonx
Created August 27, 2018 21:18
Show Gist options
  • Save beccaonx/0b7462ccb9b42c45bf28a90764144ce9 to your computer and use it in GitHub Desktop.
Save beccaonx/0b7462ccb9b42c45bf28a90764144ce9 to your computer and use it in GitHub Desktop.
#!/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)
# ### Usage:
# ./PRs_since_last_release.sh
# ### 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
######## Start script
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
last_release_tag=$(git describe --abbrev=0 --tags) # gets last tag from current branch
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment