-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathconventional_commit.rb
97 lines (82 loc) · 2.33 KB
/
conventional_commit.rb
1
2
3
4
5
6
7
8
9
10
11
12
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
module Vector
class ConventionalCommit
class << self
def parse(message)
hash = parse_commit_message(message)
new(hash)
end
def parse!(message)
hash = parse_commit_message!(message)
new(hash)
end
private
def parse_commit_message(message)
begin
parse_commit_message!(message)
rescue Exception => e
if message.include?("Use `namespace` field in metric sources")
raise e
end
{
"breaking_change" => nil,
"description" => message,
"pr_number" => nil,
"scopes" => [],
"type" => nil
}
end
end
def parse_commit_message!(message)
match = message.match(/^(?<type>[a-z]*)(\((?<scope>[a-z0-9_, ]*)\))?(?<breaking_change>!)?: (?<description>.*?)( \(#(?<pr_number>[0-9]*)\))?$/)
if match.nil?
raise <<~EOF
Commit message does not conform to the conventional commit format.
Unable to parse at all!
#{message}
Please correct in the release /.meta file and retry.
EOF
end
attributes =
{
"type" => match[:type],
"breaking_change" => !match[:breaking_change].nil?,
"description" => match[:description]
}
attributes["scopes"] =
if match[:scope]
match[:scope].split(",").collect(&:strip)
else
[]
end
attributes["pr_number"] =
if match[:pr_number]
match[:pr_number].to_i
else
nil
end
attributes
end
end
attr_reader :breaking_change,
:description,
:pr_number,
:type,
:scopes
def initialize(hash)
@breaking_change = hash.fetch("breaking_change")
@description = hash.fetch("description")
@pr_number = hash.fetch("pr_number")
@type = hash.fetch("type")
@scopes = hash.fetch("scopes")
end
def to_h
{
"breaking_change" => breaking_change,
"description" => description,
"pr_number" => pr_number,
"type" => type,
"scopes" => scopes
}
end
end
end