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..47bf37f0e 100644 --- a/services/talker.rb +++ b/services/talker.rb @@ -6,19 +6,44 @@ 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'] - - 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 + + prepare_http + + say "#{summary_message} – #{summary_url}" + if data['digest'].to_i == 0 + if distinct_commits.size == 1 + commit = distinct_commits.first + say format_commit_message(commit) + else + distinct_commits.each do |commit| + say "#{format_commit_message(commit)} – #{commit['url']}" + end end end end + + def receive_pull_request + return unless opened? + + prepare_http + say summary_message + end + + def receive_issues + 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 end diff --git a/test/talker_test.rb b/test/talker_test.rb index ce3fb44c0..729828dc6 100644 --- a/test/talker_test.rb +++ b/test/talker_test.rb @@ -5,21 +5,59 @@ 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 + stub_message_posting - svc = service({'url' => 'https://s.talkerapp.com/room/1', 'token' => 't'}, payload) + svc = service(:push, {'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 + stub_message_posting + + payload = push_payload + assert payload['commits'].size > 1 + + svc = service(:push, {'digest' => '0'}, payload) + svc.receive_push + end + + def test_push_with_digest_off_and_a_single_distinct_commit + stub_message_posting + + payload = push_payload + payload['commits'] = [payload['commits'].first] + + svc = service(:push, {'digest' => '0'}, payload) + svc.receive_push end + + def test_pull_request + stub_message_posting + svc = service(:pull_request, {}, pull_payload) + 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 + end + + private + 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'] + data = Rack::Utils.parse_nested_query(env[:body]) + assert data.key?('message') + [200, {}, ''] + end + end end