Skip to content
Browse files

Merge branch 'master' of github.com:travis-ci/travis-core

  • Loading branch information...
2 parents 2b19752 + 4aa36dd commit 6c2c012182b0c80fa1836ac5274f7fb1ab87625d @svenfuchs svenfuchs committed Dec 10, 2011
View
8 lib/travis/model/build/notifications.rb
@@ -15,6 +15,10 @@ def send_webhook_notifications?
webhooks.any? && send_notifications_for?(:webhooks)
end
+ def send_campfire_notifications?
+ campfire_channels.any? && send_notifications_for?(:campfire)
+ end
+
def send_irc_notifications?
irc_channels.any? && send_notifications_for?(:irc)
end
@@ -49,6 +53,10 @@ def webhooks
@webhooks ||= notification_values(:webhooks, :urls).map {|webhook| webhook.split(' ') }.flatten.map(&:strip).reject(&:blank?)
end
+ def campfire_channels
+ @campfire_channels ||= notification_values(:campfire, :channels).map {|channel| channel.split(' ')}.flatten.map(&:strip).reject(&:blank?)
+ end
+
def irc_channels
@irc_channels ||= notification_values(:irc, :channels).inject(Hash.new([])) do |servers, url|
# TODO parsing irc urls should probably happen in the client class
View
19 lib/travis/notifications/handler/campfire.rb
@@ -3,28 +3,41 @@ module Notifications
module Handler
class Campfire < Webhook
+ def notify(event, object, *args)
+ send_campfire(object.campfire_channels, object) if object.send_campfire_notifications?
+ rescue Exception => e
+ log_exception(e)
+ end
+
protected
- def send_webhook_notifications(targets, build)
+ def send_campfire(targets, build)
+ message = build_message(build)
+
targets.each do |webhook|
data = extract_data(webhook)
url = extract_url(data)
self.class.http_client.post(url) do |req|
- req.body = { :message => { :body => build_message(build) }}
+ req.body = { :message => { :body => build }}
req.headers['Authorization'] = data[:token]
end
end
end
def build_message(build)
commit = build.commit
+ build_url = self.build_url(build)
["[travis-ci] #{build.repository.slug}##{build.number} (#{commit.branch} - #{commit.commit[0, 7]} : #{commit.author_name}): the build has #{build.passed? ? 'passed' : 'failed' }",
"[travis-ci] Change view : #{commit.compare_url}",
"[travis-ci] Build details : #{build_url}"].join("\n")
end
- def build_url(data)
+ def build_url(build)
+ [Travis.config.host, build.repository.owner_name, build.repository.name, 'builds', build.id].join('/')
+ end
+
+ def extract_url(data)
"https://#{data[:subdomain]}.campfirenow.com/room/#{data[:room]}/speak.json"
end
View
25 spec/support/matchers.rb
@@ -96,14 +96,31 @@ def authorization_for(object)
end
end
-RSpec::Matchers.define :post_campfire_on do |event, object, options|
+RSpec::Matchers.define :post_campfire_on do |http, event, object, options|
match do |dispatch|
- options[:to].map! do |scheme|
+ options[:to].each do |scheme|
data = Travis::Notifications::Handler::Campfire.new.send(:extract_data, scheme)
- Travis::Notifications::Handler::Campfire.new.send(:build_url, data)
+ url = Travis::Notifications::Handler::Campfire.new.send(:extract_url, data)
+
+ expect_request(url, data, object)
+ end
+ dispatch.call(event, object)
+ end
+
+ def expect_request(url, data, object)
+ uri = URI.parse(url)
+ http.post uri.path do |env|
+ env[:url].host.should == uri.host
+ env[:url].path.should == uri.path
+ env[:request_headers]['Authorization'].should == data[:token]
+
+ message = Travis::Notifications::Handler::Campfire.new.send(:build_message, object)
+ payload_from(env).should == message
end
+ end
- post_webhooks_on(event, object, options)
+ def payload_from(env)
+ Rack::Utils.parse_query(env[:body])['message[body]']
end
end
View
40 spec/travis/model/build/notifications_spec.rb
@@ -127,6 +127,19 @@
end
end
+ describe :send_campfire_notifications? do
+ it 'returns true if the build configuration specifies campfire channels' do
+ channels = %w(travis:apitoken@42)
+ stubs(:config => { :notifications => { :campfire => channels } })
+ send_campfire_notifications?.should be_true
+ end
+
+ it 'returns false if the build configuration does not specify any webhooks' do
+ stubs(:config => {})
+ send_campfire_notifications?.should be_false
+ end
+ end
+
describe :webhooks do
it 'returns an array of urls when given a string' do
webhooks = 'http://evome.fr/notifications'
@@ -159,6 +172,33 @@
end
end
+ describe :campfire_channels do
+ it 'returns an array of urls when given a string' do
+ channels = 'travis:apitoken@42'
+ stubs(:config => { :notifications => { :campfire => channels } })
+ self.campfire_channels.should == [channels]
+ end
+
+ it 'returns an array of urls when given an array' do
+ channels = ['travis:apitoken@42']
+ stubs(:config => { :notifications => { :campfire => channels } })
+ self.campfire_channels.should == channels
+ end
+
+ it 'returns an array of multiple urls when given a comma separated string' do
+ channels = 'travis:apitoken@42,evome:apitoken@44'
+ stubs(:config => { :notifications => { :campfire => channels } })
+ self.campfire_channels.should == channels.split(' ').map(&:strip)
+ end
+
+ it 'returns an array of values if the build configuration specifies an array of urls within a config hash' do
+ channels = { :channels => %w(travis:apitoken&42), :on_success => 'change' }
+ stubs(:config => { :notifications => { :campfire => channels } })
+ self.campfire_channels.should == channels[:channels]
+ end
+ end
+
+
describe :irc_channels do
it 'returns an array of urls when given a string' do
channels = 'irc.freenode.net#travis'
View
16 spec/travis/notifications/handler/campfire_spec.rb
@@ -7,18 +7,30 @@
stub_http
end
+ let(:http) { Faraday::Adapter::Test::Stubs.new }
let(:dispatch) { lambda { |event, object| Travis::Notifications.dispatch(event, object) } }
+ let(:io) { StringIO.new }
+
+ before do
+ Travis.logger = Logger.new(io)
+ Travis.config.notifications = [:campfire]
+
+ Travis::Notifications::Handler::Campfire.http_client = Faraday.new do |f|
+ f.request :url_encoded
+ f.adapter :test, http
+ end
+ end
it "sends campfire notifications to the rooms given as an array" do
targets = ['evome:apitoken@42', 'rails:sometoken@69']
build = Factory(:build, :config => { 'notifications' => { 'campfire' => targets } })
- dispatch.should post_campfire_on('build:finished', build, :to => targets)
+ dispatch.should post_campfire_on(http, 'build:finished', build, :to => targets)
end
it "sends campfire notifications to the room given as a string" do
target = 'evome:apitoken@42'
build = Factory(:build, :config => { 'notifications' => { 'campfire' => target } })
- dispatch.should post_campfire_on('build:finished', build, :to => [target])
+ dispatch.should post_campfire_on(http, 'build:finished', build, :to => [target])
end
it "sends no campfire notification if the given url is blank" do

0 comments on commit 6c2c012

Please sign in to comment.
Something went wrong with that request. Please try again.