Skip to content

Instantly share code, notes, and snippets.

@leoxlin
Last active August 25, 2022 16:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leoxlin/ac0bc7a7b4427365b9eb6334c1c43f73 to your computer and use it in GitHub Desktop.
Save leoxlin/ac0bc7a7b4427365b9eb6334c1c43f73 to your computer and use it in GitHub Desktop.

Git + JIRA Tools

  1. Install the JIRA CLI

    brew install go-jira
    

    If you cannot install this via brew or is on another OS than MacOS. Look at the install instructions on https://github.com/go-jira/jira

  2. You will need to add the following to your ~/.jira.d/config.yml

    endpoint: https://companyname.atlassian.net/
    user: user@example.com
    password-source: keyring
    

    This turn on API token authentication and you will need to create one here:

    https://id.atlassian.com/manage-profile/security/api-tokens

  3. Create the summary folder under ~/.jira.d/

    mkdir -p ~/.jira.d/summary
    
  4. Install git_show_jira_summary.sh

    curl https://gist.githubusercontent.com/leoxlin/ac0bc7a7b4427365b9eb6334c1c43f73/raw/74e06c3dc5f564984eacc4b00a1bae84a4eb4d5f/git_show_jira_summary.sh > ~/.git_show_jira_summary.sh
    chmod +x ~/.git_show_jira_summary.sh
    
  5. Add a new alias branch jira which requires a PROJECTKEY for the project you want to resolve summaries for

    git config --global alias.branchjira "! git branch | ~/.git_show_jira_summary.sh PROJECTKEY"
    
  6. For multiple project key support, just chain the pipe

    git branch | ~/.git_show_jira_summary.sh KEYONE | ~/.git_show_jira_summary.sh KEYTWO
    
#!/bin/bash
# Precommit hook to add `ISSUE-1234` to your commit message as `[ISSUE-1234] Message` if you set `ISSUE-1234`
# as your branch name. Also enforces commit to only branches with `ISSUE-1234` as branch names
# Limit the enforcement to the repos in list
ENFORCED_JIRA_COMMIT=("example1" "example2")
# Project name to watch for (i.e. ISSUE)
PROJECT_NAME="ISSUE"
REPO=$(basename `git rev-parse --show-toplevel`)
case "$REPO" in
"${ENFORCED_JIRA_COMMIT[@]}")
BRANCH=$(git rev-parse --abbrev-ref HEAD | grep $PROJECT_NAME)
if [[ "$BRANCH" == "" ]]; then
echo "Aborting commit, requires JIRA branches (eg. $PROJECT_NAME-1234)"
echo "Rerun commit with --no-verify if you want to skip this check"
exit 1
fi
;; esac
#!/bin/bash
PROJECT_NAME=$1
PRUNE_STATUS=$2
while read branch
do
if [[ $branch = $PROJECT_NAME* ]]; then
export issue=$(echo $branch | perl -n -e "/.*($PROJECT_NAME-\d+).*/ && print \"\$1\n\"")
export status=$(jira view $issue -t json | jq -r .fields.status.name)
if [[ "$status" == "$PRUNE_STATUS" ]]; then
git branch -D $branch
fi
fi
done
#!/bin/bash
PROJECT_NAME=$1
while read line
do
export issue=$(echo $line | perl -n -e "/.*($PROJECT_NAME-\d+).*/ && print \"\$1\n\"")
export branch=$(git branch --show-current)
if [[ "$line" = \** ]]; then
export prefix="* "
export line="\033[0;32m${branch}\033[0m"
else
export prefix=" "
fi
if [ -n "$issue" ]; then
if [ -f ~/.jira.d/summary/$issue ]; then
export summary=$(cat ~/.jira.d/summary/$issue)
else
export summary=$(jira view $issue --field summary --gjq='fields.summary')
echo $summary > ~/.jira.d/summary/$issue
fi
echo -e "${prefix}${line} ${summary}"
else
echo -e "${prefix}${line}"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment