From 8c1fd09f7137244f1768c5584584dd12d08b8c85 Mon Sep 17 00:00:00 2001 From: Magnus Bergmark Date: Fri, 20 Jan 2012 08:34:38 +0100 Subject: [PATCH 1/5] Improve Talker push notifications Adds the URLs back for single commits (as long as there's more than one commit in there). It will also always show a proper summary, so no more J. Pusher pushed the following commits: when a new branch is created. Tests were also improved to excercise all code paths inside the service notification. --- docs/talker | 4 ++-- services/talker.rb | 15 ++++++++------ test/talker_test.rb | 48 ++++++++++++++++++++++++++++++++++----------- 3 files changed, 48 insertions(+), 19 deletions(-) diff --git a/docs/talker b/docs/talker index 1b51a5b03..3de184d42 100644 --- a/docs/talker +++ b/docs/talker @@ -7,7 +7,7 @@ Install Notes 1. url should be your room url, for example https://youraccount.talkerapp.com/rooms/ROOM_ID 2. token should be on your settings page 3. if digest is checked, all commits will be compressed in one message - + Note: replace https with http on the url if you're on the Free plan as it doesn't include enhanced security (SSL). Developer Notes @@ -19,4 +19,4 @@ data - digest (boolean) payload - - refer to docs/github_payload \ No newline at end of file + - refer to docs/github_payload diff --git a/services/talker.rb b/services/talker.rb index af46ff3e5..712512208 100644 --- a/services/talker.rb +++ b/services/talker.rb @@ -12,12 +12,15 @@ def receive_push http.headers["X-Talker-Token"] = token http.url_prefix = data['url'] - if data['digest'].to_i == 1 and commits.size > 1 - http_post 'messages.json', :message => "#{summary_message} – #{summary_url}" - else - http_post 'messages.json', :message => "#{pusher_name} pushed the following commits:" - commit_messages.each do |message| - http_post 'messages.json', :message => message + http_post 'messages.json', :message => "#{summary_message} – #{summary_url}" + if data['digest'].to_i == 0 + if distinct_commits.size == 1 + commit = distinct_commits.first + http_post 'messages.json', :message => format_commit_message(commit) + else + distinct_commits.each do |commit| + http_post 'messages.json', :message => "#{format_commit_message(commit)} – #{commit['url']}" + end end end end diff --git a/test/talker_test.rb b/test/talker_test.rb index ce3fb44c0..4e9a94c84 100644 --- a/test/talker_test.rb +++ b/test/talker_test.rb @@ -5,21 +5,47 @@ def setup @stubs = Faraday::Adapter::Test::Stubs.new end - def test_push - @stubs.post "/room/1/messages.json" do |env| - assert_equal 's.talkerapp.com', env[:url].host - assert_equal 't', env[:request_headers]['x-talker-token'] - data = Rack::Utils.parse_nested_query(env[:body]) - assert data.key?('message') - [200, {}, ''] - end + def test_push_with_digest_on + expect_message_posting - svc = service({'url' => 'https://s.talkerapp.com/room/1', 'token' => 't'}, payload) + svc = service({'digest' => '1'}, push_payload) svc.receive_push end - def service(*args) - super Service::Talker, *args + def test_push_with_digest_off_and_several_distinct_commits + expect_message_posting + + payload = push_payload + assert payload['commits'].size > 1 + + svc = service({'digest' => '0'}, payload) + svc.receive_push end + + def test_push_with_digest_off_and_a_single_distinct_commit + expect_message_posting + + payload = push_payload + payload['commits'] = [payload['commits'].first] + + svc = service({'digest' => '0'}, payload) + svc.receive_push + end + + def service(options, *args) + default_options = {'url' => 'https://s.talkerapp.com/room/1', 'token' => 't'} + super Service::Talker, default_options.merge(options), *args + end + + private + def expect_message_posting + @stubs.post "/room/1/messages.json" do |env| + assert_equal 's.talkerapp.com', env[:url].host + assert_equal 't', env[:request_headers]['x-talker-token'] + data = Rack::Utils.parse_nested_query(env[:body]) + assert data.key?('message') + [200, {}, ''] + end + end end From 45dd9f225ae1ebd577cbe37b6464ee0074a6823c Mon Sep 17 00:00:00 2001 From: Magnus Bergmark Date: Fri, 20 Jan 2012 08:37:15 +0100 Subject: [PATCH 2/5] Refactor common call into private method --- services/talker.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/services/talker.rb b/services/talker.rb index 712512208..60cdfbbee 100644 --- a/services/talker.rb +++ b/services/talker.rb @@ -12,16 +12,21 @@ def receive_push http.headers["X-Talker-Token"] = token http.url_prefix = data['url'] - http_post 'messages.json', :message => "#{summary_message} – #{summary_url}" + say "#{summary_message} – #{summary_url}" if data['digest'].to_i == 0 if distinct_commits.size == 1 commit = distinct_commits.first - http_post 'messages.json', :message => format_commit_message(commit) + say format_commit_message(commit) else distinct_commits.each do |commit| - http_post 'messages.json', :message => "#{format_commit_message(commit)} – #{commit['url']}" + say "#{format_commit_message(commit)} – #{commit['url']}" end end end end + + private + def say(message) + http_post 'messages.json', :message => message + end end From df6750e304a0aaba61363afc0f5f36f1ef794d9a Mon Sep 17 00:00:00 2001 From: Magnus Bergmark Date: Fri, 20 Jan 2012 08:54:37 +0100 Subject: [PATCH 3/5] Add Pull Request notifications to Talker --- services/talker.rb | 18 ++++++++++++++---- test/talker_test.rb | 16 +++++++++++----- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/services/talker.rb b/services/talker.rb index 60cdfbbee..6b63168d3 100644 --- a/services/talker.rb +++ b/services/talker.rb @@ -6,11 +6,8 @@ def receive_push repository = payload['repository']['name'] branch = branch_name commits = payload['commits'] - token = data['token'] - http.ssl[:verify] = false - http.headers["X-Talker-Token"] = token - http.url_prefix = data['url'] + prepare_http say "#{summary_message} – #{summary_url}" if data['digest'].to_i == 0 @@ -25,7 +22,20 @@ def receive_push end end + def receive_pull_request + return unless opened? + + prepare_http + say summary_message + end + private + def prepare_http + http.ssl[:verify] = false + http.headers["X-Talker-Token"] = data['token'] + http.url_prefix = data['url'] + end + def say(message) http_post 'messages.json', :message => message end diff --git a/test/talker_test.rb b/test/talker_test.rb index 4e9a94c84..c91e12613 100644 --- a/test/talker_test.rb +++ b/test/talker_test.rb @@ -8,7 +8,7 @@ def setup def test_push_with_digest_on expect_message_posting - svc = service({'digest' => '1'}, push_payload) + svc = service(:push, {'digest' => '1'}, push_payload) svc.receive_push end @@ -18,7 +18,7 @@ def test_push_with_digest_off_and_several_distinct_commits payload = push_payload assert payload['commits'].size > 1 - svc = service({'digest' => '0'}, payload) + svc = service(:push, {'digest' => '0'}, payload) svc.receive_push end @@ -28,13 +28,19 @@ def test_push_with_digest_off_and_a_single_distinct_commit payload = push_payload payload['commits'] = [payload['commits'].first] - svc = service({'digest' => '0'}, payload) + svc = service(:push, {'digest' => '0'}, payload) svc.receive_push end - def service(options, *args) + def test_pull_request + expect_message_posting + svc = service(:pull_request, {}, pull_payload) + svc.receive_pull_request + end + + def service(event, options = {}, *args) default_options = {'url' => 'https://s.talkerapp.com/room/1', 'token' => 't'} - super Service::Talker, default_options.merge(options), *args + super Service::Talker, event, default_options.merge(options), *args end private From 279362cbe9dc520e69b125a4d4a45f28db97c88f Mon Sep 17 00:00:00 2001 From: Magnus Bergmark Date: Fri, 20 Jan 2012 08:56:57 +0100 Subject: [PATCH 4/5] Rename private method to reflect actual behavior --- test/talker_test.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/talker_test.rb b/test/talker_test.rb index c91e12613..8b8418396 100644 --- a/test/talker_test.rb +++ b/test/talker_test.rb @@ -6,14 +6,14 @@ def setup end def test_push_with_digest_on - expect_message_posting + stub_message_posting svc = service(:push, {'digest' => '1'}, push_payload) svc.receive_push end def test_push_with_digest_off_and_several_distinct_commits - expect_message_posting + stub_message_posting payload = push_payload assert payload['commits'].size > 1 @@ -23,7 +23,7 @@ def test_push_with_digest_off_and_several_distinct_commits end def test_push_with_digest_off_and_a_single_distinct_commit - expect_message_posting + stub_message_posting payload = push_payload payload['commits'] = [payload['commits'].first] @@ -33,7 +33,7 @@ def test_push_with_digest_off_and_a_single_distinct_commit end def test_pull_request - expect_message_posting + stub_message_posting svc = service(:pull_request, {}, pull_payload) svc.receive_pull_request end @@ -44,7 +44,7 @@ def service(event, options = {}, *args) end private - def expect_message_posting + def stub_message_posting @stubs.post "/room/1/messages.json" do |env| assert_equal 's.talkerapp.com', env[:url].host assert_equal 't', env[:request_headers]['x-talker-token'] From f90c96061c9b6e718aa06d10455c5f439d56d42c Mon Sep 17 00:00:00 2001 From: Magnus Bergmark Date: Fri, 20 Jan 2012 10:55:59 +0100 Subject: [PATCH 5/5] Add issues notification to Talker --- services/talker.rb | 7 +++++++ test/talker_test.rb | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/services/talker.rb b/services/talker.rb index 6b63168d3..47bf37f0e 100644 --- a/services/talker.rb +++ b/services/talker.rb @@ -29,6 +29,13 @@ def receive_pull_request say summary_message end + def receive_issues + return unless opened? + + prepare_http + say summary_message + end + private def prepare_http http.ssl[:verify] = false diff --git a/test/talker_test.rb b/test/talker_test.rb index 8b8418396..729828dc6 100644 --- a/test/talker_test.rb +++ b/test/talker_test.rb @@ -38,6 +38,12 @@ def test_pull_request svc.receive_pull_request end + def test_issues + stub_message_posting + svc = service(:issues, {}, issues_payload) + svc.receive_issues + end + def service(event, options = {}, *args) default_options = {'url' => 'https://s.talkerapp.com/room/1', 'token' => 't'} super Service::Talker, event, default_options.merge(options), *args