Module: Kettle::Dev::CommitMsg

Defined in:
lib/kettle/dev/commit_msg.rb,
sig/kettle/dev.rbs

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) ⇒ void

This method returns an undefined value.

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



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 = `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 = "      \#{commit_msg.strip}\n      [\#{match_data[:story_type]}][\#{match_data[:story_id]}]\n    EOS\n    File.open(path, \"w\") { |file| file.print(commit_msg) }\n  end\nend\n"