Last active
February 22, 2019 20:21
-
-
Save cseeger/169c36ca707aab151831b0a20880fd9e to your computer and use it in GitHub Desktop.
Git Hook to automate inclusion of JIRA (`[ABC-123]`) formatted ticket ids in your commit message
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# Install via: | |
# `curl GIST_URL > .git/hooks/commit-msg && chmod u+x .git/hooks/commit-msg` | |
COMMIT_MESSAGE = File.read(ARGV[0]) | |
def check_commit_message | |
message_regex = /\[[A-Z]+-[0-9]+\]/ | |
unless message_regex.match(COMMIT_MESSAGE) | |
attempt_extract_from_branch_name | |
end | |
end | |
def attempt_extract_from_branch_name | |
branch_regex = /[A-Z]+-[0-9]+/ | |
branch_name = `git symbolic-ref --short HEAD` | |
branch_match = branch_regex.match(branch_name) | |
unless branch_match.nil? | |
puts "Found: #{branch_match[0]}" | |
File.open(ARGV[0], 'w') do |new_message| | |
new_message.write("[#{branch_match[0]}] #{COMMIT_MESSAGE}") | |
end | |
exit 0 | |
end | |
puts "[POLICY] Your commit message is not formatted correctly" | |
puts " Please include your JIRA id: '[ABC-123] Hello world!'" | |
exit 1 | |
end | |
check_commit_message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment