Skip to content

Instantly share code, notes, and snippets.

@eddiemoya
Last active August 29, 2015 14:01
Show Gist options
  • Save eddiemoya/1367d0aae9e059a57ebf to your computer and use it in GitHub Desktop.
Save eddiemoya/1367d0aae9e059a57ebf to your computer and use it in GitHub Desktop.
Uses JIRA API to create releases, squash merge branches with JIRA ID, title, and priority in the commit message.
#!/bin/sh
## Installation ##
# > git clone https://gist.github.com/1367d0aae9e059a57ebf.git ~/git-jira.sh
# > git config --global alias.jira '!. ~/git-jira.sh'
##
## Commands ##
#
# General Usage:
# git jira <command> <jira_id>
# * Note: If no command is given, defaults to "lookup".
# * Note: All JIRA_ID inputs can be prefixed with PDP- or be just the number.
#
# Command: git jira lookup <jira_id>
# Description: Outputs simplified version of JIRA API output for either a single jira or a space delimited list of JIRAs
#
# Command: git jira merge <jira_id> (<jira_id> <jira_id> ...)
# Description: Takes a space delimited list of jira ID's (or single jira),
# looks it up via the API and tries to merge its branch and automatically builds a commit message.
# Rejects merge conflicts. Keeps track of rejected jiras or those without branches. Reports success/failures
# after the final jira is completed.
# * NOTE: If you've already preceeded to try to merge an issue and it turns out not to have a branch at all (in comments or in the git_branch field, just enter !!! when prompted, and answer "n" for no when asked to try again.
#
# Command: git jira release
# Takes no parameters. Looks for a list of potential JIRAs from a filter in JIRA - passes the whole list to the "git jira merge" command.
##
## TODO ##
# * Add a step in the prompts to skip a jira that doesnt have a branch after having already answered "y" to if it should be merged
# * Ensure that response from JIRA is not null. Fail gracefully.
# * Rewrite this crap in a better langange!
##
# Will be used to store info about JIRA's who's branches had conflicts
had_conflicts=();
# Will be used to store info about JIRA's which had no branches assoicated with them
no_branch=();
# Will be used to store info about JIRA's which were skipped during the process
skipped=();
# Will be used to store info about JIRA's which were successfully merged
merged=();
function set_creds {
if [ -z "$user" ]; then
read -p "/n[?] JIRA Username: " user;
echo "/n";
fi
if [ -z "$password" ]; then
read -s -p "[?] JIRA Password (${user}): " password;
echo "/n";
fi
}
##
# Checks for conflicts.
# If conflicts, back out merge, display error messege, and add to $had_conflicts array.
# If no conflicts, perform merge, add to $merged array.
##
function do_merge {
git merge --squash --quiet $branch > /dev/null;
local has_conflicts=$(git status --porcelain | grep "UU");
if [ -n "$has_conflicts" ]; then
echo ${has_conflicts} | sed 's/UU/\'$'\nUU/g'
echo "\n[!] Branch has conflicts. Resetting back to HEAD and moving to the next JIRA";
git reset --hard HEAD;
local conflicts=$(echo $has_conflicts | sed 's/UU/\'$'\n UU/g')
had_conflicts+=("* $comment$conflicts\n");
else
git commit -m "$(echo $comment)";
merged+=("* $comment\n");
fi
}
##
# Verifies that a given branch exists
#
# First checks if the branch name is set in the $jira_branch veriable.
# If its not, prompts user for a branch
#
# Check to see if branch exists locally
# If it does not, check in origin
#
# If a branch is found, it is set to the $branch_exists variable
# If not found, prompt user if we should try again.
#
# If user opts to try again, function call itself.
# If user opts not to try again, jira is added to $no_branch array
##
function check_branch_exists {
if [ "${jira_branch}" == "null" ]; then
echo "[!] No branch set in git_branch property..."
printf "[?] Branch for ${jira_id}? : "
read branch;
else
echo "[+] Branch set in git_branch property: ${jira_branch}";
branch=${jira_branch};
fi
branch_exists=$(git branch -l --list ${branch});
if [ -n "$branch_exists" ]; then
echo "[+] Branch Found1: ${branch_exists}";
echo "[.] Merging...";
else
branch="origin/${branch}";
branch_exists=$(git branch -r --list ${branch});
if [ -n "${branch_exists}" ]; then
echo "[+] Branch Found2: ${branch_exists}";
else
printf "[?] Branch [${branch}] does not exist locally, or in origin. Try again [y/n]? "
read cont;
if [[ $cont == "y" ]]; then
check_branch_exists;
else
no_branch+=("* $comment\n");
fi
fi
fi
}
function get_jira {
local jira_slug=$1;
if [[ $jira_slug =~ PDP-.* ]]; then
jira_slug="${jira_slug}";
else
jira_slug="PDP-${jira_slug}";
fi
set_creds;
echo "[.] Fetching JIRA Issue: ${jira_slug}...";
local jira_properties="{git_branch: .fields.customfield_14267, summary: .fields.summary, assignee: .fields.assignee.displayName, name: .key, priority: .fields.priority.name}";
jira=$(curl -s -u $user:$password https://obujira.searshc.com/jira/rest/api/2/issue/$jira_slug | jq "${jira_properties}");
echo ${jira_properties};
echo ${jira};
}
function build_commit_messege {
jira_name="$(echo "${jira}" | jq '.name' | tr -d '\"')";
jira_title=$(echo $jira | jq '.summary');
jira_priority=$(echo $jira | jq '.priority' | tr -d '\"');
jira_branch=$(echo $jira | jq '.git_branch' | tr -d '\"');
comment="[${jira_name}] (${jira_priority}) ${jira_title}";
}
function get_jiras {
#git fetch --all;
for jira_number in "$@"
do
# The jira number may be passed as PDP-#### or as simply ####.
if [[ $jira_number =~ PDP-.* ]]; then
jira_id="${jira_number}";
else
jira_id="PDP-${jira_number}";
fi
printf "\n[?] Look for ${jira_id} [y/n]? ";
read cont;
if [[ $cont == "y" ]]; then
get_jira $jira_id;
build_commit_messege ${jira};
echo "[+] Found ${comment}";
check_branch_exists;
if [ -n "${branch_exists}" ]; then
do_merge;
fi
else
skipped+=("* $jira_id\n");
fi
done
echo "\n[+] Merged: The following JIRA's were successfully merged.";
echo " ${merged[@]}";
echo "[.] Skipped: The following JIRA's were skipped.";
echo " ${skipped[@]}";
echo "\n[!] Conflicts: The following JIRA's encountered merge conflicts.";
echo " ${had_conflicts[@]}";
echo "[!] No Branch: The following JIRA's did not have a branch to merge.";
echo " ${no_branch[@]}";
}
function get_release_jiras
{
set_creds;
echo "[.] Fetching Release Candidates from JIRA...";
local jira_properties=".issues[].key";
local response=$(curl -s -u $user:$password https://obujira.searshc.com/jira/rest/api/2/search?jql=filter=79520 | jq "${jira_properties}" | tr -d '"');
# local issue_count=$(echo $response | jq '.issues[].key' | tr -d '"');
echo "[+] Found Release Candidates...";
echo "${response[@]}";
get_jiras ${response};
# echo $response | jq "$jira_properties";
}
case "$1" in
(merge) get_jiras "${@:2}";;
(jiras) get_jiras "${@:2}";;
(release) get_release_jiras;;
(lookup) get_jira $2;;
(*) get_jira $2;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment