Class: Codebot::Formatters::Push
Overview
This class formats push events.
Instance Attribute Summary
#payload
Instance Method Summary
collapse
#abbreviate, #action, #ary_to_sentence, #closed?, #extract, #format_branch, #format_dangerous, #format_event, #format_hash, #format_number, #format_repository, #format_url, #format_user, #gitlab_action, #gitlab_closed?, #gitlab_opened?, #gitlab_repository_url, #gitlab_url, #initialize, #opened?, #prettify, #repository_name, #repository_url, #sanitize, #sender_name, #shorten_url, #url
Instance Method Details
#after_sha ⇒ Object
157
158
159
|
# File 'lib/codebot/formatters/push.rb', line 157
def after_sha
(:after).to_s
end
|
#after_sha_url ⇒ Object
161
162
163
|
# File 'lib/codebot/formatters/push.rb', line 161
def after_sha_url
"#{repository_url}/commits/#{after_sha}"
end
|
#base_ref ⇒ Object
133
134
135
|
# File 'lib/codebot/formatters/push.rb', line 133
def base_ref
(:base_ref)
end
|
#base_ref_name ⇒ Object
137
138
139
|
# File 'lib/codebot/formatters/push.rb', line 137
def base_ref_name
base_ref.sub(%r{\Arefs/(heads|tags)/}, '')
end
|
#before_sha ⇒ Object
149
150
151
|
# File 'lib/codebot/formatters/push.rb', line 149
def before_sha
(:before).to_s
end
|
#before_sha_url ⇒ Object
153
154
155
|
# File 'lib/codebot/formatters/push.rb', line 153
def before_sha_url
"#{repository_url}/commits/#{before_sha}"
end
|
#branch_url ⇒ Object
141
142
143
|
# File 'lib/codebot/formatters/push.rb', line 141
def branch_url
"#{repository_url}/commits/#{branch_name}"
end
|
#commits ⇒ Object
114
115
116
|
# File 'lib/codebot/formatters/push.rb', line 114
def commits
(:commits)
end
|
#compare_url ⇒ Object
145
146
147
|
# File 'lib/codebot/formatters/push.rb', line 145
def compare_url
(:compare).to_s
end
|
#created? ⇒ Boolean
98
99
100
|
# File 'lib/codebot/formatters/push.rb', line 98
def created?
/\A0{40}\z/ =~ (:before)
end
|
79
80
81
|
# File 'lib/codebot/formatters/push.rb', line 79
def default_format
'%<repository>s/%<branch>s %<hash>s %<author>s: %<title>s'
end
|
#deleted? ⇒ Boolean
102
103
104
|
# File 'lib/codebot/formatters/push.rb', line 102
def deleted?
/\A0{40}\z/ =~ (:after)
end
|
#distinct_commits ⇒ Object
165
166
167
168
169
|
# File 'lib/codebot/formatters/push.rb', line 165
def distinct_commits
(:distinct_commits) || commits.select do |commit|
commit['distinct'] && !commit['message'].to_s.strip.empty?
end
end
|
#forced? ⇒ Boolean
106
107
108
|
# File 'lib/codebot/formatters/push.rb', line 106
def forced?
(:forced)
end
|
Formats IRC messages for a push event.
13
14
15
16
17
|
# File 'lib/codebot/formatters/push.rb', line 13
def format
["#{summary}: #{format_url url}"] + distinct_commits.map do |commit|
format_commit_message(commit)
end
end
|
rubocop:disable Metrics/AbcSize
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/codebot/formatters/push.rb', line 68
def format_commit_message(commit) author = commit['author']['name'] if commit['author'].is_a? Hash
default_format % {
repository: format_repository(repository_name),
branch: format_branch(branch_name),
hash: format_hash(commit['id']),
author: format_user(author),
title: prettify(full_commit_message(commit))
}
end
|
#full_commit_message(commit) ⇒ Object
83
84
85
|
# File 'lib/codebot/formatters/push.rb', line 83
def full_commit_message(commit)
(commit.is_a?(Hash) ? commit['message'] : nil).to_s
end
|
#pusher_name ⇒ Object
118
119
120
|
# File 'lib/codebot/formatters/push.rb', line 118
def pusher_name
(:pusher, :name) || 'somebody'
end
|
#ref ⇒ Object
122
123
124
|
# File 'lib/codebot/formatters/push.rb', line 122
def ref
(:ref).to_s
end
|
#ref_name ⇒ Object
Also known as:
tag_name, branch_name
126
127
128
|
# File 'lib/codebot/formatters/push.rb', line 126
def ref_name
ref.sub(%r{\Arefs/(heads|tags)/}, '')
end
|
#summary ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/LineLength
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
|
# File 'lib/codebot/formatters/push.rb', line 19
def summary msg = "[#{format_repository repository_name}]"
msg << " #{format_user(pusher_name)}"
if created?
if tag?
msg << " tagged #{format_branch tag_name} at "
msg << if base_ref
format_branch(base_ref_name)
else
format_hash(after_sha)
end
else
msg << " created #{format_branch branch_name}"
msg << if base_ref
" from #{format_branch base_ref_name}"
else
" at #{format_hash after_sha}"
end
len = distinct_commits.length
msg << " (+#{format_number len, 'new commit', 'new commits'})"
end
elsif deleted?
msg << " #{format_dangerous 'deleted'}"
msg << " #{format_branch branch_name}"
msg << " at #{format_hash before_sha}"
elsif forced?
msg << " #{format_dangerous 'force-pushed'}"
msg << " #{format_branch branch_name}"
msg << " from #{format_hash before_sha}"
msg << " to #{format_hash after_sha}"
elsif !commits.empty? && distinct_commits.empty?
if base_ref
msg << " merged #{format_branch base_ref_name}"
msg << " into #{format_branch branch_name}"
else
msg << " fast-forwarded #{format_branch branch_name}"
msg << " from #{format_hash before_sha}"
msg << " to #{format_hash after_sha}"
end
else
len = distinct_commits.length
msg << " pushed #{format_number len, 'new commit', 'new commits'}"
msg << " to #{format_branch branch_name}"
end
msg
end
|
#summary_url ⇒ Object
rubocop:disable Metrics/AbcSize
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/codebot/formatters/push.rb', line 87
def summary_url if created? then distinct_commits.empty? ? branch_url : compare_url
elsif deleted? then before_sha_url
elsif forced? then branch_url
elsif distinct_commits.length == 1
distinct_commits.first['url'].to_s
else
compare_url
end
end
|
#tag? ⇒ Boolean
110
111
112
|
# File 'lib/codebot/formatters/push.rb', line 110
def tag?
%r{\Arefs/tags/} =~ ref
end
|