Skip to content

Commit

Permalink
Added support for Notifo notifications hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
knoopx committed Feb 2, 2011
1 parent 814f692 commit 5f74695
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ gem "scashin133-xmpp4r-simple", '0.8.9', :require => 'xmpp4r-simple'
# irc notification
gem "shout-bot"

# notifo notifications
gem "notifo"

# campfire notifications
gem "tinder"

Expand Down
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ GEM
ffi (0.6.3)
rake (>= 0.8.7)
haml (3.0.24)
httparty (0.4.5)
crack (>= 0.1.1)
i18n (0.4.2)
json (1.4.6)
json_pure (1.4.6)
Expand All @@ -84,6 +86,8 @@ GEM
rake
multipart-post (1.0.1)
nokogiri (1.4.4)
notifo (0.1.0)
httparty (~> 0.4.5)
open4 (1.0.1)
polyglot (0.3.1)
rack (1.2.1)
Expand Down Expand Up @@ -166,6 +170,7 @@ DEPENDENCIES
machinist
mocha
nokogiri
notifo
open4
rails (= 3.0.3)
ruby-debug
Expand Down
41 changes: 41 additions & 0 deletions extras/big_tuna/hooks/notifo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module BigTuna
class Hooks::Notifo < Hooks::Base
NAME = "notifo"

def build_fixed(build, config)
project = build.project
Delayed::Job.enqueue(Job.new(project.name, config, "Build '#{build.display_name}' in '#{project.name}' fixed", build_url(build)))
end

def build_still_fails(build, config)
project = build.project
Delayed::Job.enqueue(Job.new(project.name, config, "Build '#{build.display_name}' in '#{project.name}' still fails", build_url(build)))
end

def build_failed(build, config)
project = build.project
Delayed::Job.enqueue(Job.new(project.name, config, "Build '#{build.display_name}' in '#{project.name}' failed", build_url(build)))
end

class Job
def initialize(project_name, config, message, build_url)
@project_name = project_name
@config = config
@message = message
@build_url = build_url
end

def perform
recipients = @config["recipients"].to_s.split(",")
if recipients.size > 0
notifo = Notifo.new(@config["user"], @config["key"])
recipients.each do |recipient|
recipient.strip!
notifo.subscribe_user(recipient)
notifo.post(recipient, @message, @message, @build_url)
end
end
end
end
end
end
16 changes: 16 additions & 0 deletions extras/big_tuna/hooks/notifo/_form.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
%div
= label_tag("Send with this Notifo service")
%br
= text_field_tag("configuration[user]", @hook.configuration["user"], :size => 50)

%div
= label_tag("Identified by this API key")
%br
= password_field_tag("configuration[key]", @hook.configuration["key"], :size => 50)

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

%p.hint= "You can create a new Notifo service <a href='http://notifo.com/services' target='_blank'>here</a>".html_safe
88 changes: 88 additions & 0 deletions test/unit/notifo_hook_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
require 'test_helper'

class NotifoHookTest < 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")
super
end

test "notifo message stating that build failed is sent when build failed" do
BigTuna::Hooks::Notifo::Job.any_instance.expects(:perform).at_least_once.returns(true)

project = notifo_project_with_steps("ls invalid_file_here")
hook = project.hooks.first
assert_difference("Delayed::Job.count", +2) do # 1 job + 1 for sending the notifo message
job = project.build!
stub_notifo(hook, "Build '#{project.recent_build.display_name}' in '#{project.name}' fixed")
job.invoke_job
end
end

test "notifo message stating that build is back to normal is sent when build fixed" do
BigTuna::Hooks::Notifo::Job.any_instance.expects(:perform).at_least_once.returns(true)

project = notifo_project_with_steps("ls invalid_file_here")
hook = project.hooks.first
project.build!
run_delayed_jobs()
stub_notifo(hook, "Build '#{project.recent_build.display_name}' in '#{project.name}' fixed")
project.step_lists.first.update_attributes!(:steps => "ls .")
project.build!
jobs = run_delayed_jobs()
assert_equal 3, jobs.size # 1 project, 1 part, 1 notifo message
end

test "notifo message stating that build is still failing is sent when build still fails" do
project = notifo_project_with_steps("ls invalid_file_here")
hook = project.hooks.first
project.build!
run_delayed_jobs()
stub_notifo(hook, "Build '#{project.recent_build.display_name}' in '#{project.name}' still fails")
project.build!
jobs = run_delayed_jobs()
assert_equal 3, jobs.size # 1 project, 1 part, 1 notifo message
end

test "no notifo message sent when build is ok but was ok before" do
project = notifo_project_with_steps("ls .")
project.build!
run_delayed_jobs()
project.build!
jobs = run_delayed_jobs()
assert_equal 2, jobs.size
end

private
def notifo_project_with_steps(steps)
project = project_with_steps({
:name => "Koss",
:vcs_source => "test/files/koss",
:vcs_type => "git",
:max_builds => 2,
:hooks => {"notifo" => "notifo"},
}, steps)
hook = project.hooks.first
hook.configuration = {
"recipients" => "foo,bar",
"user" => "foo",
"key" => "secret"
}

hook.save!
project
end

def stub_notifo(hook, message)
mock = Mocha::Mock.new
mock.expects(:initialize).with(hook.configuration["user"], hook.configuration["key"]).returns(mock)
hook.configuration["recipients"].to_s.split(",").each do |recipient|
mock.expects(:deliver).with(recipient).returns(mock)
end
mock
end
end

0 comments on commit 5f74695

Please sign in to comment.