Skip to content

Instantly share code, notes, and snippets.

@ArturT
Forked from marioizquierdo/git-commit-msg-jira-trello.rb
Last active October 31, 2017 15:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ArturT/1cc0fb4e60e2f17621c7 to your computer and use it in GitHub Desktop.
Save ArturT/1cc0fb4e60e2f17621c7 to your computer and use it in GitHub Desktop.
git hook prepare commit message for JIRA. It adds story id and story url in commit message.
#!/usr/bin/env ruby
#
# Git prepare-commit-msg hook for JIRA project.
#
# Name your feature brach like: my-feature-STORY_ID for instance feature-LL-123
#
# Install:
# Create a file with this content in `yourproject/.githooks/prepare-commit-msg`
# or put this file somewhere, for example `~/.githooks/prepare-commit-msg`, and symlink it:
#
# $ cd yourproject
# $ ln -s ~/.githooks/prepare-commit-msg .git/hooks/commit-msg
#
# START configuration:
project_id = 'LL'
jira_project_url = 'https://jira.example.com/browse/'
# END of configuration
branchname = `git branch --no-color 2> /dev/null`[/^\* (.+)/, 1].to_s # get current branch name
commit_message_file = ARGV[0] # argument given by git when calling this hook
original_commit_message = File.read(commit_message_file).strip
# JIRA
jira_pattern = /(#{project_id}-\d+)/i # match branch names like my-feature-project_id-1234
if m = branchname.match(jira_pattern)
jira_number = m.captures.first
# Add jira_number to commit message
message = "\n#{original_commit_message}\n\n[##{jira_number}]\nStory url: #{jira_project_url}#{jira_number}"
File.open(commit_message_file, 'w') {|f| f.write message }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment