diff --git a/docs/twilio b/docs/twilio index e3cc23d1d..e9e9da6fd 100644 --- a/docs/twilio +++ b/docs/twilio @@ -13,6 +13,7 @@ Install Notes 2. You can find your account_sid and auth_token on your account page at https://www.twilio.com/user/account 3. from_phone must be a "Twilio phone number enabled for SMS. Only phone numbers or short codes purchased from Twilio work here" 4. to_phone is the "destination phone number. Format with a '+' and country code e.g., +16175551212 (E.164 format)." +5. Check master_only if you only want to recieve updates for master Developer Notes --------------- @@ -22,6 +23,7 @@ data - auth_token - from_phone - to_phone + - master_only payload - - refer to docs/github_payload \ No newline at end of file + - refer to docs/github_payload diff --git a/services/twilio.rb b/services/twilio.rb index 1cf79c248..852008bd7 100644 --- a/services/twilio.rb +++ b/services/twilio.rb @@ -1,25 +1,38 @@ class Service::Twilio < Service string :account_sid, :from_phone, :to_phone + boolean :master_only password :auth_token - + def receive_push check_configuration_options(data) - + sms_body = "#{payload['pusher']['name']} has pushed #{payload['commits'].size} commit(s) to #{payload['repository']['name']}" - client = ::Twilio::REST::Client.new(data['account_sid'], data['auth_token']) - client.account.sms.messages.create( - :from => data['from_phone'], - :to => data['to_phone'], - :body => sms_body - ) + send_message(data, sms_body) if send_notification?(data) end - + private - + + def send_notification?(data) + notify_user = true + if data['master_only'].to_i == 1 && branch_name != 'master' + notify_user = false + end + notify_user + end + def check_configuration_options(data) raise_config_error 'Account SID must be set' if data['account_sid'].blank? raise_config_error 'Authorization token must be set' if data['auth_token'].blank? raise_config_error 'Twilio-enabled phone number or short code must be set' if data['from_phone'].blank? raise_config_error 'Destination phone number must be set' if data['to_phone'].blank? end -end \ No newline at end of file + + def send_message(data, message) + client = ::Twilio::REST::Client.new(data['account_sid'], data['auth_token']) + client.account.sms.messages.create( + :from => data['from_phone'], + :to => data['to_phone'], + :body => message + ) + end +end diff --git a/test/twilio_test.rb b/test/twilio_test.rb index cb8777553..3b07c15d4 100644 --- a/test/twilio_test.rb +++ b/test/twilio_test.rb @@ -31,7 +31,8 @@ def setup 'account_sid' => 'account_sid', 'auth_token' => 'auth_token', 'from_phone' => '+12223334444', - 'to_phone' => '+15556667777' + 'to_phone' => '+15556667777', + 'master_only' => '0' } @payload = { "after" => "a47fd41f3aa4610ea527dcc1669dfdb9c15c5425", @@ -67,17 +68,43 @@ def setup ] } end - - def test_push + + def test_push svc = service(@data, @payload) assert_equal 1, @payload['commits'].size assert_equal 'rtomayko', @payload['pusher']['name'] assert_equal 'grit', @payload['repository']['name'] - + + @stubs.post "/2010-04-01/Accounts/account_sid/SMS/Messages.json" do |env| + [200, {}, ''] + end + + twilio_response = svc.receive_push + assert twilio_response.is_a?(Twilio::REST::Message) + assert_equal 'rtomayko has pushed 1 commit(s) to grit', twilio_response.body + end + + def test_push_master_only_on_non_master + non_master_payload = @payload + non_master_payload["ref"] = "refs/heads/non-master" + + data_with_master_only = @data + data_with_master_only['master_only'] = 1 + + svc = service(data_with_master_only, non_master_payload) + twilio_response = svc.receive_push + assert_equal twilio_response, nil + end + + def test_push_master_only_on_master + data_with_master_only = @data + data_with_master_only['master_only'] = 1 + @stubs.post "/2010-04-01/Accounts/account_sid/SMS/Messages.json" do |env| [200, {}, ''] end - + + svc = service(data_with_master_only, @payload) twilio_response = svc.receive_push assert twilio_response.is_a?(Twilio::REST::Message) assert_equal 'rtomayko has pushed 1 commit(s) to grit', twilio_response.body