Skip to content

Instantly share code, notes, and snippets.

@dmitriy-kiriyenko
Last active September 14, 2018 15:48
Show Gist options
  • Save dmitriy-kiriyenko/187a0357827c6867feed9005138590b8 to your computer and use it in GitHub Desktop.
Save dmitriy-kiriyenko/187a0357827c6867feed9005138590b8 to your computer and use it in GitHub Desktop.
HOWTO: add ticket number from git branch to commits and PRs

The problem: you want JIRA ticket number in commit descriptions and possibly in the PRs. The solution: automate it.

Setup and usage:

  1. In the repository place the prepare-commit-message.sh file from this gist at .git/hooks/prepare-commit-message (without .sh extension). chmod +x it.
  2. Name branches like ECA-333/remove-all-shit-and-add-good-code.
  3. Run git config --global commit.template=~/.gitmessage. Place gitmessage from this gist at ~/.gitmessage (don't forget the starting dot).
  4. Use git commit and wait for the editor, don't git commit -m or gitx shennanigans to utilize it.
  5. This way ECA-333 will be grabbed out and used: (A) in the commit header and (B) below the commit description, formatted markdown-ready as a link to our JIRA. Example commit message:
  6. Frequently a PR is just one commit. Pay some efforts to prepare a good commit message. Then PR and enjoy title of your commit grabbed as PR title, and body (and a markdown link to JIRA!) grabbed to PR description.

We need to go deeper:

  1. Now you possibly want to go further and don't manually copy it across the projects. I have a word to you.
  2. Run git config --global init.templatedir=~/.git-templates.
  3. Place prepare-commit-message file from this gist at ~/.git-templates/hooks/prepare-commit-message (without .sh extension). chmod +x it.
  4. All new repos will now have it.
  5. In an existing repo run git init. Don't worry, it won't crash anything, just reinitialize the hooks and other things.
What was done (no period at the end)
Why and how it was done.
#!/bin/bash
# Include any branches for which you wish to disable this script
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop staging test release hotfix)
fi
# Get the current branch name and check if it is excluded
BRANCH_NAME=$(git symbolic-ref --short HEAD)
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
# Trim it down to get the parts we're interested in
TRIMMED=$(echo $BRANCH_NAME | sed -e 's:^\([^/]*\)\/.*:\1:' -e \
'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/')
LINK="[$TRIMMED](https://everfi.atlassian.net/browse/$TRIMMED)"
BRANCH_ALREADY_THERE=$(grep -c "atlassian" $1)
# If it isn't excluded, preprend the trimmed branch identifier to the given message
if [ -n "$BRANCH_NAME" ] && ! [ "$BRANCH_ALREADY_THERE" -ge 1 ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]]; then
sed -i.bak -e "1s/^/$TRIMMED /" $1
awk -v "LINK=$LINK" '/^#/ && !x {print LINK; print; x=1} 1' $1 > tmptmptmp && mv tmptmptmp $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment