Skip to content

Instantly share code, notes, and snippets.

@thrgamon
Last active December 6, 2018 21:36
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 thrgamon/0437b19d1959cf191f5d3c26e0fbfd28 to your computer and use it in GitHub Desktop.
Save thrgamon/0437b19d1959cf191f5d3c26e0fbfd28 to your computer and use it in GitHub Desktop.
Takes the ticket prefix from the branch name and adds it to the commit message
#!/usr/bin/env ruby
BRANCHES_TO_SKIP = ['master', 'develop'].freeze
# Grabs 1-4 letters, a dash and 1-4 numbers after a leading slash.
# For example, with the string 'feature/DOT-38-update-gems' it would match 'DOT-38'
PREFIX_REGEX = /(?<=\/)[a-zA-Z]{1,4}-\d{1,4}/.freeze
message_file = ARGV[0]
branch_name = `git symbolic-ref --short HEAD`.strip
return if BRANCHES_TO_SKIP.include?(branch_name)
return unless branch_name =~ PREFIX_REGEX
jira_ticket_prefix = branch_name.scan(PREFIX_REGEX).first
message_text = File.read(message_file)
return if message_text.include?(jira_ticket_prefix)
new_message_text = [jira_ticket_prefix, ': ', message_text].join
File.write(message_file, new_message_text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment