Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/flamontagne/bigtuna into …
Browse files Browse the repository at this point in the history
…flamontagne-master
  • Loading branch information
Michal Bugno committed Dec 1, 2010
2 parents 0417b04 + bf133d7 commit 9458de5
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Gemfile
Expand Up @@ -8,6 +8,9 @@ gem "stringex"
gem "open4"
gem "json"

#xmpp notifications
gem "xmpp4r-simple"

group :development, :test do
gem "capybara"
gem "launchy"
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Expand Up @@ -112,6 +112,9 @@ GEM
treetop (1.4.9)
polyglot (>= 0.3.1)
tzinfo (0.3.23)
xmpp4r (0.5)
xmpp4r-simple (0.8.8)
xmpp4r (>= 0.3.2)
xpath (0.1.2)
nokogiri (~> 1.3)

Expand All @@ -135,3 +138,4 @@ DEPENDENCIES
ruby-debug
sqlite3-ruby
stringex
xmpp4r-simple
1 change: 1 addition & 0 deletions config/initializers/big_tuna.rb
Expand Up @@ -5,6 +5,7 @@ module BigTuna

HOOKS = [
Hooks::Mailer,
Hooks::Xmpp
]

DEFAULT_CONFIG = {
Expand Down
77 changes: 77 additions & 0 deletions extras/big_tuna/hooks/xmpp.rb
@@ -0,0 +1,77 @@
#XMPP notifications
#We subclass ActionMailer
module BigTuna
class Hooks::Xmpp
NAME = "xmpp"

# def self.build_passed(build, config)
# end


def self.build_fixed(build, config)
Sender.delay.build_fixed(build,config) unless config["recipients"].split(",").empty?
end

def self.build_still_fails(build, config)
Sender.delay.build_still_fails(build,config) unless config["recipients"].split(",").empty?
end

# def self.build_finished(build, config)
# end

def self.build_failed(build, config)
Sender.delay.build_failed(build,config) unless config["recipients"].split(",").empty?
end

class Sender < ActionMailer::Base
self.append_view_path("extras/big_tuna/hooks")

def send_im(config,msg)
recipients = config["recipients"].split(",")
if recipients.size > 0
im = Jabber::Simple.new(config["sender_full_jid"], config["sender_password"])
recipients.each {|r| im.deliver(r.strip, msg)}
end
end

def build_failed(build, config)
@build = build
@project = @build.project

send_im(
config,
mail.body = render_to_string(
"xmpp/build_failed",
:locals => {:build => @build, :project => @project}
)
)
end

def build_still_fails(build, config)
@build = build
@project = @build.project

send_im(
config,
mail.body = render_to_string(
"xmpp/build_still_fails",
:locals => {:build => @build, :project => @project}
)
)
end

def build_fixed(build, config)
@build = build
@project = @build.project

send_im(
config,
mail.body = render_to_string(
"xmpp/build_fixed",
:locals => {:build => @build, :project => @project}
)
)
end
end
end
end
16 changes: 16 additions & 0 deletions extras/big_tuna/hooks/xmpp/_form.html.haml
@@ -0,0 +1,16 @@
%div
= label_tag("Send with this XMPP/GoogleTalk account")
%br
= text_field_tag("configuration[sender_full_jid]", @hook.configuration["sender_full_jid"])

%div
= label_tag("Identified by this password")
%br
= password_field_tag("configuration[sender_password]", @hook.configuration["sender_password"])

%div
= label_tag("Send to these recipients (separated by comma)")
%br
= text_area_tag("configuration[recipients]", @hook.configuration["recipients"], :cols => 50)

%p.hint= "Tip: You can enter your <a href='http://partychapp.appspot.com/' target='_blank'>partychat</a> room there".html_safe
1 change: 1 addition & 0 deletions extras/big_tuna/hooks/xmpp/build_failed.erb
@@ -0,0 +1 @@
Build '<%=@build.display_name%>' in '<%=@project.name%>' failed (<%=build_url(@build)%>)
1 change: 1 addition & 0 deletions extras/big_tuna/hooks/xmpp/build_fixed.erb
@@ -0,0 +1 @@
Build '<%=@build.display_name%>' in '<%=@project.name%>' fixed (<%=build_url(@build)%>)
1 change: 1 addition & 0 deletions extras/big_tuna/hooks/xmpp/build_still_fails.erb
@@ -0,0 +1 @@
Build '<%=@build.display_name%>' in '<%=@project.name%>' still fails (<%=build_url(@build)%>)
23 changes: 22 additions & 1 deletion test/integration/hooks_test.rb
Expand Up @@ -26,7 +26,7 @@ def teardown
end
assert page.has_content?("Recipients")
end

test "hooks with no config print out this info to user" do
with_hook_enabled(BigTuna::Hooks::NoConfig) do
project = Project.make(:steps => "ls", :name => "Koss", :vcs_source => "test/files/repo", :vcs_type => "git", :max_builds => 2, :hooks => {"no_config" => "no_config"}, :hook_update => true)
Expand All @@ -38,6 +38,27 @@ def teardown
end
end

test "xmpp hook has a valid configuration form" do
project = Project.make(
:steps => "ls",
:name => "Koss",
:vcs_source => "test/files/repo",
:vcs_type => "git",
:max_builds => 2,
:hooks => {"xmpp" => "xmpp"},
:hook_update => true
)

visit edit_project_path(project)
within("#hook_xmpp") do
click_link "Configure"
end
assert page.has_field?("configuration_sender_full_jid")
assert page.has_field?("configuration_sender_password")
assert page.has_field?("configuration_recipients")
end


private
def with_hook_enabled(hook, &blk)
old_hooks = BigTuna::HOOKS.clone
Expand Down
84 changes: 84 additions & 0 deletions test/unit/xmpp_hook_test.rb
@@ -0,0 +1,84 @@
require 'test_helper'

class XmppHookTest < ActiveSupport::TestCase
def setup
super
`cd test/files; mkdir koss; cd koss; git init; echo "my file" > file; git add file; git commit -m "my file added"`
end

def teardown
FileUtils.rm_rf("test/files/koss")
FileUtils.rm_rf("builds/koss")
super
end

test "xmpp message stating that build failed is sent when build failed" do
BigTuna::Hooks::Xmpp::Sender.any_instance.expects(:send_im).at_least_once.returns(true)

project = xmpp_project_with_steps("ls invalid_file_here")
assert_difference("Delayed::Job.count", +2) do # 1 job + 1 for sending the xmpp message
job = project.build!
job.invoke_job
end
build = project.recent_build
job = Delayed::Job.order("created_at DESC").first
msg = YAML.load(job.handler).perform
assert msg.body.include? "Build '#{build.display_name}' in '#{project.name}' failed"
end

test "xmpp message stating that build is back to normal is sent when build fixed" do
BigTuna::Hooks::Xmpp::Sender.any_instance.expects(:send_im).at_least_once.returns(true)

project = xmpp_project_with_steps("ls invalid_file_here")
job = project.build!
job.invoke_job
project.update_attributes!(:steps => "ls .")
assert_difference("Delayed::Job.count", +2) do # 1 job + 1 for sending the xmpp message
job = project.build!
job.invoke_job
end
build = project.recent_build
job = Delayed::Job.order("created_at DESC").first
msg = YAML.load(job.handler).perform
assert msg.body.include? "Build '#{build.display_name}' in '#{project.name}' fixed"
end

test "xmpp message stating that build is still failing is sent when build still fails" do
BigTuna::Hooks::Xmpp::Sender.any_instance.expects(:send_im).at_least_once.returns(true)

project = xmpp_project_with_steps("ls invalid_file_here")
job = project.build!
job.invoke_job
assert_difference("Delayed::Job.count", +2) do # 1 job + 1 for sending the xmpp message
job = project.build!
job.invoke_job
end
build = project.recent_build
job = Delayed::Job.order("created_at DESC").first
msg = YAML.load(job.handler).perform
assert msg.body.include? "Build '#{build.display_name}' in '#{project.name}' still fails"
end

test "no xmpp message sent when build is ok but was ok before" do
project = xmpp_project_with_steps("ls .")
assert_difference("Delayed::Job.count", +2) do # 2 jobs, nothing sent via xmpp
2.times do
job = project.build!
job.invoke_job
end
end
end

def xmpp_project_with_steps(steps)
project = Project.make(:steps => steps, :name => "Koss", :vcs_source => "test/files/koss", :vcs_type => "git", :max_builds => 2, :hooks => {"xmpp" => "xmpp"}, :hook_update => true)
hook = project.hooks.first
hook.configuration = {
"recipients" => "user1@example.com,user2@example.com,user3@example.com",
"sender_full_jid" => "thesender@example.com",
"sender_password" => "secret"
}

hook.save!
project
end
end

0 comments on commit 9458de5

Please sign in to comment.