Class: ConventionalCommits::CommitMessageParser

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

Instance Method Summary collapse

Instance Method Details

#message_components(commit_msg_path: Configuration::DEFAULT_COMMIT_MSG_PATH, cfg_path: Configuration::DEFAULT_CONFIGURATION_PATH) ⇒ Object

Raises:



5
6
7
8
9
10
11
# File 'lib/commit/commit_message_parser.rb', line 5

def message_components(commit_msg_path: Configuration::DEFAULT_COMMIT_MSG_PATH,
                       cfg_path: Configuration::DEFAULT_CONFIGURATION_PATH)
  raise GenericError, "Commit Message File is not created" unless File.exist?(commit_msg_path)

  data = File.read_file(commit_msg_path)
  message_components_from_string(commit_msg: data.to_s, cfg_path:)
end

#message_components_from_string(commit_msg: "", cfg_path: Configuration::DEFAULT_CONFIGURATION_PATH) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/commit/commit_message_parser.rb', line 13

def message_components_from_string(commit_msg: "",
                                   cfg_path: Configuration::DEFAULT_CONFIGURATION_PATH)
  msg_components = commit_msg.split "\n"
  if msg_components.length < 5
    raise GenericError, "Commit Message Doesnt respect the spec, expect to have subject, body and footer"
  end

  main_config = Configuration::MainConfigurationReader.new.get_configuration(path: cfg_path)

  subject_components = msg_components[0].split(":").map(&:strip)
  if subject_components.length < 2
    raise raise GenericError,
                "Subject doesnt respect the format"
  end
  scope_split = subject_components[0].split(/\(([^)]+)\)/)
  components = { scope: nil, type: "", title: "", body: "" }
  components[:scope] = scope_split.length > 1 ? scope_split[1] : nil
  components[:type] = scope_split[0]
  components[:title] = subject_components[1]
  components[:body] = msg_components[2]

  unless main_config.type.is_allowed(components[:type])
    raise raise GenericError,
                "Commit Type is not in the config yaml"
  end

  components
end