Skip to content

Instantly share code, notes, and snippets.

@jtanguy
Created January 16, 2023 13:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jtanguy/56b4efd31fe312da1d7bb0729d23b577 to your computer and use it in GitHub Desktop.
Save jtanguy/56b4efd31fe312da1d7bb0729d23b577 to your computer and use it in GitHub Desktop.
Create a branch name from assigned jira tickets
#!/usr/bin/env bash
#
# External programs: jq fzf elinks curl sed awk cut
JIRA_URL="https://<jira>.atlassian.net"
USER_EMAIL="<user-email>"
USER_KEY="<api-key>"
JIRA_QUERY='resolution=unresolved and statusCategory = "In Progress" and assignee = currentUser() order by updated desc'
function create-branch(){
local jq_template query branch_name
jq_template='"'\
'\(.key). \(.fields.summary)'\
'\t'\
'<h1>\(.key). \(.fields.summary)</h1>'\
'<ul>'\
'<li>Reporter: \(.fields.reporter.displayName)</li>'\
'<li>Created: \(.fields.created)</li>'\
'<li>Updated: \(.fields.updated)</li>'\
'</ul>'\
'<div>\(.renderedFields.description | gsub("[\\n]"; "") )</div>'\
'"'
branch_name=$(
curl \
--data-urlencode "jql=$query" \
--get \
--user "$USER_EMAIL:$USER_EMAIL" \
--silent \
--compressed \
"$JIRA_URL/rest/api/3/search?fields=summary,reporter,created,updated,description&expand=renderedFields&maxResults=10" |
jq ".issues[] | $jq_template" |
sed -e 's/"\(.*\)"/\1/' -e 's/\\t/\t/' |
fzf \
--with-nth=1 \
--delimiter='\t' \
--preview='echo {2} | elinks -dump 1 -dump-color-mode 1' \
--preview-window=top,80%:wrap |
cut -f1 |
sed -e 's/\. /\t/' -e 's/[^a-zA-Z0-9\t]/-/g' |
awk '{printf "%s", $1, tolower($2)}'
)
if [ -n "$branch_name" ]; then
echo "Proposed branch name: $branch_name"
read -r -e -i "$branch_name-" branch_name
if [ -n "$branch_name" ]; then
git checkout -b "$branch_name"
fi;
fi
}
create-branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment