Class: ConventionalCommits::CommitMessageGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/commit/commit_message_generator.rb

Instance Method Summary collapse

Instance Method Details

#prepare_message_template_for_type(type: "", cfg_path: Configuration::DEFAULT_CONFIGURATION_PATH, msg_file_path: Configuration::DEFAULT_COMMIT_MSG_PATH) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/commit/commit_message_generator.rb', line 30

def prepare_message_template_for_type(type: "",
                                      cfg_path: Configuration::DEFAULT_CONFIGURATION_PATH,
                                      msg_file_path: Configuration::DEFAULT_COMMIT_MSG_PATH)
  prepare_template_message(cfg_path:) unless should_try_to_parse_msg_from_file(source: type)

  parser = CommitMessageParser.new
  components = parser.message_components(commit_msg_path: msg_file_path, cfg_path:)
  prepare_template_message(custom_body: components[:body], cfg_path:)
rescue StandardError
  data = File.read_file(msg_file_path).to_s
  prepare_template_message(custom_body: data, cfg_path:)
end

#prepare_template_message(custom_body: "", cfg_path: Configuration::DEFAULT_CONFIGURATION_PATH) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/commit/commit_message_generator.rb', line 6

def prepare_template_message(custom_body: "", cfg_path: Configuration::DEFAULT_CONFIGURATION_PATH)
  branch_name = Git.new.current_branch_name
  main_config = Configuration::MainConfigurationReader.new.get_configuration(path: cfg_path)
  configuration = main_config.branch
  components = BranchNameGenerator.new.branch_name_components(branch_name, path: cfg_path)
  scope = components[:scope] ? "(#{components[:scope].downcase})" : ""
  type = (components[:type] || "").downcase
  ticket_number = components[:ticket_number] || ""
  description = components[:description] || ""
  description = sanitize(description)

  unless main_config.type.is_allowed(type)
    raise ConventionalCommits::GenericError,
          "The type #{type} is not allowed. Allowed types #{main_config.type.all_types}"
  end

  type = "#{main_config.type.main_type(type)}#{scope}: #{description}".strip

  ticket_ref = "#{configuration.ticket_prefix}#{configuration.ticket}#{configuration.ticket_separator}#{ticket_number}"
  body = custom_body.strip.empty? ? Configuration::DEFAULT_COMMIT_BODY_TEMPLATE : custom_body.strip
  footer = "Ref: #{ticket_ref}".strip
  "#{type}\n\n#{body}\n\n#{footer}"
end