Skip to content

Commit c5d7e4e

Browse files
committed
Merge pull request github#423 from eevee/irc-hook-awesomification
Spruce up IRC notifications
2 parents c96955b + 1f0823a commit c5d7e4e

File tree

2 files changed

+136
-13
lines changed

2 files changed

+136
-13
lines changed

services/irc.rb

Lines changed: 116 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,33 @@ def receive_push
1111
return unless branch_name_matches?
1212

1313
messages = []
14-
messages << "#{summary_message}: #{url}"
15-
messages += commit_messages.first(3)
14+
messages << "#{irc_push_summary_message}: #{fmt_url url}"
15+
messages += distinct_commits.first(3).map {
16+
|commit| self.irc_format_commit_message(commit)
17+
}
1618
send_messages messages
1719
end
1820

1921
def receive_pull_request
2022
return unless opened?
2123

22-
send_messages "#{summary_message}: #{url}"
24+
send_messages "#{irc_pull_request_summary_message} #{fmt_url url}"
2325
end
2426

25-
alias receive_issues receive_pull_request
27+
def receive_issues
28+
return unless opened?
29+
30+
send_messages "#{irc_issue_summary_message} #{fmt_url url}"
31+
end
2632

2733
def send_messages(messages)
34+
messages = Array(messages)
35+
36+
if data['no_colors'].to_i == 1
37+
messages.each{|message|
38+
message.gsub!(/\002|\017|\026|\037|\003\d{0,2}(?:,\d{1,2})?/, '')}
39+
end
40+
2841
rooms = data['room'].to_s
2942
if rooms.empty?
3043
raise_config_error "No rooms: #{rooms.inspect}"
@@ -54,7 +67,7 @@ def send_messages(messages)
5467
room, pass = room.split("::")
5568
irc_puts "JOIN #{room} #{pass}" unless without_join
5669

57-
Array(messages).each do |message|
70+
messages.each do |message|
5871
irc_puts "#{command} #{room} :#{message}"
5972
end
6073

@@ -125,7 +138,85 @@ def url
125138
data['long_url'].to_i == 1 ? summary_url : shorten_url(summary_url)
126139
end
127140

128-
def format_commit_message(commit)
141+
### IRC message formatting. For reference:
142+
### \002 bold \003 color \017 reset \026 italic/reverse \037 underline
143+
### 0 white 1 black 2 dark blue 3 dark green
144+
### 4 dark red 5 brownish 6 dark purple 7 orange
145+
### 8 yellow 9 light green 10 dark teal 11 light teal
146+
### 12 light blue 13 light purple 14 dark gray 15 light gray
147+
148+
def fmt_url(s)
149+
"\00302\037#{s}\017"
150+
end
151+
152+
def fmt_repo(s)
153+
"\00313#{s}\017"
154+
end
155+
156+
def fmt_name(s)
157+
"\00315#{s}\017"
158+
end
159+
160+
def fmt_branch(s)
161+
"\00306#{s}\017"
162+
end
163+
164+
def fmt_tag(s)
165+
"\00306#{s}\017"
166+
end
167+
168+
def fmt_hash(s)
169+
"\00314#{s}\017"
170+
end
171+
172+
def irc_push_summary_message
173+
message = []
174+
message << "[#{fmt_repo repo_name}] #{fmt_name pusher_name}"
175+
176+
if created?
177+
if tag?
178+
message << "tagged #{fmt_tag tag_name} at"
179+
message << (base_ref ? fmt_branch(base_ref_name) : fmt_hash(after_sha))
180+
else
181+
message << "created #{fmt_branch branch_name}"
182+
183+
if base_ref
184+
message << "from #{fmt_branch base_ref_name}"
185+
elsif distinct_commits.empty?
186+
message << "at #{fmt_hash after_sha}"
187+
end
188+
189+
if distinct_commits.any?
190+
num = distinct_commits.size
191+
message << "(+\002#{num}\017 new commit#{num > 1 ? 's' : ''})"
192+
end
193+
end
194+
195+
elsif deleted?
196+
message << "\00304deleted\017 #{fmt_branch branch_name} at #{fmt_hash before_sha}"
197+
198+
elsif forced?
199+
message << "\00304force-pushed\017 #{fmt_branch branch_name} from #{fmt_hash before_sha} to #{fmt_hash after_sha}"
200+
201+
elsif commits.any? and distinct_commits.empty?
202+
if base_ref
203+
message << "merged #{fmt_branch base_ref_name} into #{fmt_branch branch_name}"
204+
else
205+
message << "fast-forwarded #{fmt_branch branch_name} from #{fmt_hash before_sha} to #{fmt_hash after_sha}"
206+
end
207+
208+
elsif distinct_commits.any?
209+
num = distinct_commits.size
210+
message << "pushed \002#{num}\017 new commit#{num > 1 ? 's' : ''} to #{fmt_branch branch_name}"
211+
212+
else
213+
message << "pushed nothing"
214+
end
215+
216+
message.join(' ')
217+
end
218+
219+
def irc_format_commit_message(commit)
129220
short = commit['message'].split("\n", 2).first.to_s
130221
short += '...' if short != commit['message']
131222

@@ -134,13 +225,25 @@ def format_commit_message(commit)
134225
files = Array(commit['modified'])
135226
dirs = files.map { |file| File.dirname(file) }.uniq
136227

137-
if data['no_colors'].to_i == 1
138-
"#{repo_name}: #{branch_name} #{author} * " +
139-
"#{sha1[0..6]} (#{files.size} files in #{dirs.size} dirs): #{short}"
140-
else
141-
"\002#{repo_name}:\002 \00307#{branch_name}\003 \00303#{author}\003 * " +
142-
"\002#{sha1[0..6]}\002 (#{files.size} files in #{dirs.size} dirs): #{short}"
143-
end
228+
"#{fmt_repo repo_name}/#{fmt_branch branch_name} #{fmt_hash sha1[0..6]} " +
229+
"#{fmt_name commit['author']['name']}: #{short}"
230+
end
231+
232+
def irc_issue_summary_message
233+
"[#{fmt_repo repo.name}] #{fmt_name sender.login} #{action} issue \##{issue.number}: #{issue.title}"
234+
rescue
235+
raise_config_error "Unable to build message: #{$!.to_s}"
236+
end
237+
238+
def irc_pull_request_summary_message
239+
base_ref = pull.base.label.split(':').last
240+
head_ref = pull.head.label.split(':').last
241+
head_label = head_ref != base_ref ? head_ref : pull.head.label
242+
243+
"[#{fmt_repo repo.name}] #{fmt_name sender.login} #{action} pull request " +
244+
"\##{pull.number}: #{pull.title} (#{fmt_branch base_ref}...#{fmt_branch head_ref})"
245+
rescue
246+
raise_config_error "Unable to build message: #{$!.to_s}"
144247
end
145248

146249
def branch_name_matches?

test/irc_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,26 @@ def test_overridden_port
191191
assert_equal 1234, svc.port
192192
end
193193

194+
def test_no_colors
195+
# Default should include color
196+
svc = service(:pull_request, {'room' => 'r', 'nick' => 'n'}, pull_payload)
197+
198+
svc.receive_pull_request
199+
msgs = svc.writable_io.string.split("\n")
200+
privmsg = msgs[3] # skip NICK, USER, JOIN
201+
assert_match /PRIVMSG #r.*grit/, privmsg
202+
assert_match /\003/, privmsg
203+
204+
# no_colors should strip color
205+
svc = service(:pull_request, {'room' => 'r', 'nick' => 'n', 'no_colors' => '1'}, pull_payload)
206+
207+
svc.receive_pull_request
208+
msgs = svc.writable_io.string.split("\n")
209+
privmsg = msgs[3] # skip NICK, USER, JOIN
210+
assert_match /PRIVMSG #r.*grit/, privmsg
211+
assert_no_match /\003/, privmsg
212+
end
213+
194214
def service(*args)
195215
super FakeIRC, *args
196216
end

0 commit comments

Comments
 (0)