forked from appelier/bigtuna
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for Notifo notifications hook.
- Loading branch information
Showing
5 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |