Module: Kettle::Dev::CommitMsg

Defined in:
lib/kettle/dev/commit_msg.rb

Constant Summary collapse

BRANCH_RULES =
{
  "jira" => /^(?<story_type>(hotfix)|(bug)|(feature)|(candy))\/(?<story_id>\d{8,})-.+\Z/,
}.freeze

Class Method Summary collapse

Class Method Details

.enforce_branch_rule!(path) ⇒ Object

Enforce branch rule by appending [type] to the commit message when missing.

Parameters:

  • path (String)

    path to commit message file (ARGV from git)



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kettle/dev/commit_msg.rb', line 17

def enforce_branch_rule!(path)
  validate = ENV.fetch("GIT_HOOK_BRANCH_VALIDATE", "false")
  branch_rule_type = (!validate.casecmp("false").zero? && validate) || nil
  return unless branch_rule_type

  branch_rule = BRANCH_RULES[branch_rule_type]
  return unless branch_rule

  branch = %x(git branch 2> /dev/null | grep -e ^* | awk '{print $2}')
  match_data = branch.match(branch_rule)
  return unless match_data

  commit_msg = File.read(path)
  unless commit_msg.include?(match_data[:story_id])
    commit_msg = <<~EOS
      #{commit_msg.strip}
      [#{match_data[:story_type]}][#{match_data[:story_id]}]
    EOS
    File.open(path, "w") { |file| file.print(commit_msg) }
  end
end