Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #423 from eevee/irc-hook-awesomification

Spruce up IRC notifications
  • Loading branch information...
commit c5d7e4e27994e7bc99d8b8631398fc07049c7343 2 parents c96955b + 1f0823a
@technoweenie technoweenie authored
Showing with 136 additions and 13 deletions.
  1. +116 −13 services/irc.rb
  2. +20 −0 test/irc_test.rb
View
129 services/irc.rb
@@ -11,20 +11,33 @@ def receive_push
return unless branch_name_matches?
messages = []
- messages << "#{summary_message}: #{url}"
- messages += commit_messages.first(3)
+ messages << "#{irc_push_summary_message}: #{fmt_url url}"
+ messages += distinct_commits.first(3).map {
+ |commit| self.irc_format_commit_message(commit)
+ }
send_messages messages
end
def receive_pull_request
return unless opened?
- send_messages "#{summary_message}: #{url}"
+ send_messages "#{irc_pull_request_summary_message} #{fmt_url url}"
end
- alias receive_issues receive_pull_request
+ def receive_issues
+ return unless opened?
+
+ send_messages "#{irc_issue_summary_message} #{fmt_url url}"
+ end
def send_messages(messages)
+ messages = Array(messages)
+
+ if data['no_colors'].to_i == 1
+ messages.each{|message|
+ message.gsub!(/\002|\017|\026|\037|\003\d{0,2}(?:,\d{1,2})?/, '')}
+ end
+
rooms = data['room'].to_s
if rooms.empty?
raise_config_error "No rooms: #{rooms.inspect}"
@@ -54,7 +67,7 @@ def send_messages(messages)
room, pass = room.split("::")
irc_puts "JOIN #{room} #{pass}" unless without_join
- Array(messages).each do |message|
+ messages.each do |message|
irc_puts "#{command} #{room} :#{message}"
end
@@ -125,7 +138,85 @@ def url
data['long_url'].to_i == 1 ? summary_url : shorten_url(summary_url)
end
- def format_commit_message(commit)
+ ### IRC message formatting. For reference:
+ ### \002 bold \003 color \017 reset \026 italic/reverse \037 underline
+ ### 0 white 1 black 2 dark blue 3 dark green
+ ### 4 dark red 5 brownish 6 dark purple 7 orange
+ ### 8 yellow 9 light green 10 dark teal 11 light teal
+ ### 12 light blue 13 light purple 14 dark gray 15 light gray
+
+ def fmt_url(s)
+ "\00302\037#{s}\017"
+ end
+
+ def fmt_repo(s)
+ "\00313#{s}\017"
+ end
+
+ def fmt_name(s)
+ "\00315#{s}\017"
+ end
+
+ def fmt_branch(s)
+ "\00306#{s}\017"
+ end
+
+ def fmt_tag(s)
+ "\00306#{s}\017"
+ end
+
+ def fmt_hash(s)
+ "\00314#{s}\017"
+ end
+
+ def irc_push_summary_message
+ message = []
+ message << "[#{fmt_repo repo_name}] #{fmt_name pusher_name}"
+
+ if created?
+ if tag?
+ message << "tagged #{fmt_tag tag_name} at"
+ message << (base_ref ? fmt_branch(base_ref_name) : fmt_hash(after_sha))
+ else
+ message << "created #{fmt_branch branch_name}"
+
+ if base_ref
+ message << "from #{fmt_branch base_ref_name}"
+ elsif distinct_commits.empty?
+ message << "at #{fmt_hash after_sha}"
+ end
+
+ if distinct_commits.any?
+ num = distinct_commits.size
+ message << "(+\002#{num}\017 new commit#{num > 1 ? 's' : ''})"
+ end
+ end
+
+ elsif deleted?
+ message << "\00304deleted\017 #{fmt_branch branch_name} at #{fmt_hash before_sha}"
+
+ elsif forced?
+ message << "\00304force-pushed\017 #{fmt_branch branch_name} from #{fmt_hash before_sha} to #{fmt_hash after_sha}"
+
+ elsif commits.any? and distinct_commits.empty?
+ if base_ref
+ message << "merged #{fmt_branch base_ref_name} into #{fmt_branch branch_name}"
+ else
+ message << "fast-forwarded #{fmt_branch branch_name} from #{fmt_hash before_sha} to #{fmt_hash after_sha}"
+ end
+
+ elsif distinct_commits.any?
+ num = distinct_commits.size
+ message << "pushed \002#{num}\017 new commit#{num > 1 ? 's' : ''} to #{fmt_branch branch_name}"
+
+ else
+ message << "pushed nothing"
+ end
+
+ message.join(' ')
+ end
+
+ def irc_format_commit_message(commit)
short = commit['message'].split("\n", 2).first.to_s
short += '...' if short != commit['message']
@@ -134,13 +225,25 @@ def format_commit_message(commit)
files = Array(commit['modified'])
dirs = files.map { |file| File.dirname(file) }.uniq
- if data['no_colors'].to_i == 1
- "#{repo_name}: #{branch_name} #{author} * " +
- "#{sha1[0..6]} (#{files.size} files in #{dirs.size} dirs): #{short}"
- else
- "\002#{repo_name}:\002 \00307#{branch_name}\003 \00303#{author}\003 * " +
- "\002#{sha1[0..6]}\002 (#{files.size} files in #{dirs.size} dirs): #{short}"
- end
+ "#{fmt_repo repo_name}/#{fmt_branch branch_name} #{fmt_hash sha1[0..6]} " +
+ "#{fmt_name commit['author']['name']}: #{short}"
+ end
+
+ def irc_issue_summary_message
+ "[#{fmt_repo repo.name}] #{fmt_name sender.login} #{action} issue \##{issue.number}: #{issue.title}"
+ rescue
+ raise_config_error "Unable to build message: #{$!.to_s}"
+ end
+
+ def irc_pull_request_summary_message
+ base_ref = pull.base.label.split(':').last
+ head_ref = pull.head.label.split(':').last
+ head_label = head_ref != base_ref ? head_ref : pull.head.label
+
+ "[#{fmt_repo repo.name}] #{fmt_name sender.login} #{action} pull request " +
+ "\##{pull.number}: #{pull.title} (#{fmt_branch base_ref}...#{fmt_branch head_ref})"
+ rescue
+ raise_config_error "Unable to build message: #{$!.to_s}"
end
def branch_name_matches?
View
20 test/irc_test.rb
@@ -191,6 +191,26 @@ def test_overridden_port
assert_equal 1234, svc.port
end
+ def test_no_colors
+ # Default should include color
+ svc = service(:pull_request, {'room' => 'r', 'nick' => 'n'}, pull_payload)
+
+ svc.receive_pull_request
+ msgs = svc.writable_io.string.split("\n")
+ privmsg = msgs[3] # skip NICK, USER, JOIN
+ assert_match /PRIVMSG #r.*grit/, privmsg
+ assert_match /\003/, privmsg
+
+ # no_colors should strip color
+ svc = service(:pull_request, {'room' => 'r', 'nick' => 'n', 'no_colors' => '1'}, pull_payload)
+
+ svc.receive_pull_request
+ msgs = svc.writable_io.string.split("\n")
+ privmsg = msgs[3] # skip NICK, USER, JOIN
+ assert_match /PRIVMSG #r.*grit/, privmsg
+ assert_no_match /\003/, privmsg
+ end
+
def service(*args)
super FakeIRC, *args
end
Please sign in to comment.
Something went wrong with that request. Please try again.