Skip to content

Instantly share code, notes, and snippets.

@scott-ad-riley
Last active September 5, 2017 14:08
Show Gist options
  • Save scott-ad-riley/0d774a70452bd5bb118638b7dbed0176 to your computer and use it in GitHub Desktop.
Save scott-ad-riley/0d774a70452bd5bb118638b7dbed0176 to your computer and use it in GitHub Desktop.
Prepend the jira-ticket-id into your commits
  1. cd into a deliveroo repo directory
  2. run:
curl -s https://gist.githubusercontent.com/scott-ad-riley/0d774a70452bd5bb118638b7dbed0176/raw/dde71f9489f2d01a6f048ec58d6f7f4cf93f63ce/prepare-commit-msg.rb > ./.git/hooks/prepare-commit-msg
  1. That downloads the script in this gist (I promise) and puts it in a ./git/hooks/prepare-commit-msg file
  2. You'll need to run this for each repo

N.B. It currently throws and prevents you from committing if your commit message starts with a different ticket number to the branch you're on (i.e. if you pass it PAY-1234: some message and you're on a branch that doesn't start PAY-1234/...)

N.B.B It shouldn't care if you want to use xxx as your commit message, aslong as it's at the beginning of your branch too.

If you can think of anything else that's worth adding (or if you don't like the guard against committing with the wrong ticket number let me know.

#!/usr/bin/env ruby
# This is whatever your commit template currently is, plus whatever message is passed in
@original_message = File.read(ARGV[0])
# Get your current jira ticket number from your current branch
ticket_name = `git rev-parse --abbrev-ref HEAD`.match(/^(.*)\//)[1]
def prepend_message(msg)
File.open(ARGV[0], 'w') { |f| f.write msg + @original_message }
end
if ARGV[1] == 'message'
# if original message does not have the ticket number in it
unless @original_message.match?(/\S*-\S*:/)
# put the ticket name on the start of the message
prepend_message(ticket_name + ": ")
else
raise 'Looks like you are committing on the wrong branch' if !(@original_message.match(/^(.*):/)[1] == ticket_name)
end
else
prepend_message(ticket_name + ": ")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment