Skip to content

Commit

Permalink
User can enable/disable specific hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Bugno committed Dec 9, 2010
1 parent c801e85 commit a518095
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 25 deletions.
1 change: 1 addition & 0 deletions app/controllers/hooks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def configure
@hook = Hook.where(:project_id => @project.id, :hook_name => params[:name]).first
return render if request.get?
@hook.configuration = params["configuration"]
@hook.hooks_enabled = (params["hooks_enabled"] || {}).keys
@hook.save!
redirect_to(project_config_hook_path(@project, @hook.backend.class::NAME))
end
Expand Down
30 changes: 25 additions & 5 deletions app/models/hook.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
class Hook < ActiveRecord::Base
belongs_to :project
serialize :configuration, Hash
serialize :hooks_enabled, Array
AVAILABLE_HOOKS = ["build_passed", "build_fixed", "build_still_fails", "build_finished", "build_failed"]

before_create :enable_all_hooks

def backend
@backend ||= BigTuna.hooks.find { |e| e::NAME == hook_name }.new
Expand All @@ -12,34 +16,46 @@ def configuration

def build_passed(build)
invoke_with_log(build) do
self.backend.build_passed(build, self.configuration)
self.backend.build_passed(build, self.configuration) if hook_available?("build_passed")
end
end

def build_fixed(build)
invoke_with_log(build) do
self.backend.build_fixed(build, self.configuration)
self.backend.build_fixed(build, self.configuration) if hook_available?("build_fixed")
end
end

def build_still_fails(build)
invoke_with_log(build) do
self.backend.build_still_fails(build, self.configuration)
self.backend.build_still_fails(build, self.configuration) if hook_available?("build_still_fails")
end
end

def build_finished(build)
invoke_with_log(build) do
self.backend.build_finished(build, self.configuration)
self.backend.build_finished(build, self.configuration) if hook_available?("build_finished")
end
end

def build_failed(build)
invoke_with_log(build) do
self.backend.build_failed(build, self.configuration)
self.backend.build_failed(build, self.configuration) if hook_available?("build_failed")
end
end

def hook_implemented?(name)
self.backend.respond_to?(name.to_sym)
end

def hook_enabled?(name)
hooks_enabled.include?(name)
end

def hook_available?(name)
hook_implemented?(name) and hook_enabled?(name)
end

private
def invoke_with_log(build, &blk)
begin
Expand All @@ -52,4 +68,8 @@ def invoke_with_log(build, &blk)
build.save!
end
end

def enable_all_hooks
self.hooks_enabled = AVAILABLE_HOOKS.dup
end
end
11 changes: 9 additions & 2 deletions app/views/hooks/configure.html.haml
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
%h2= "#{@hook.backend.class::NAME} hook for #{@project.name}"

= form_for(@hook, :url => project_config_hook_path(@project, @hook.backend.class::NAME), :html => {:method => "PUT"}) do |f|
%div
= label_tag("Hooks enabled")
- Hook::AVAILABLE_HOOKS.each do |hook_name|
- if @hook.hook_implemented?(hook_name)
%div
= label_tag("hooks_enabled[#{hook_name}]", hook_name.humanize, :class => "hint")
= check_box_tag("hooks_enabled[#{hook_name}]", "1", @hook.hooks_enabled.include?(hook_name))
- begin
= render(:partial => "#{@hook.backend.class::NAME}/form", :locals => {:f => f})
%button{:type => "submit"} Edit
- rescue ActionView::MissingTemplate
%p This hook doesn't have any configuration.
- #pass
%button{:type => "submit"} Edit
= link_to("Back to project edit", edit_project_path(@project), :class => "black_button")
9 changes: 9 additions & 0 deletions db/migrate/20101209113828_add_hook_disabling_configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddHookDisablingConfiguration < ActiveRecord::Migration
def self.up
add_column(:hooks, :hooks_enabled, :text)
end

def self.down
remove_column(:hooks, :hooks_enabled)
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20101207163104) do
ActiveRecord::Schema.define(:version => 20101209113828) do

create_table "build_parts", :force => true do |t|
t.integer "build_id", :null => false
Expand Down Expand Up @@ -63,6 +63,7 @@
t.datetime "created_at"
t.datetime "updated_at"
t.text "configuration"
t.text "hooks_enabled"
end

create_table "projects", :force => true do |t|
Expand Down
15 changes: 0 additions & 15 deletions extras/big_tuna/hooks/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,5 @@ def self.inherited(klass)
def default_url_options
ActionMailer::Base.default_url_options
end

def build_passed(build, config)
end

def build_fixed(build, config)
end

def build_still_fails(build, config)
end

def build_finished(build, config)
end

def build_failed(build, config)
end
end
end
9 changes: 7 additions & 2 deletions test/integration/hooks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ def teardown
click_link "Configure"
end
assert page.has_content?("Recipients")
assert page.has_field?("Build still fails")
assert page.has_xpath?("//*[@name='hooks_enabled[build_still_fails]' and @checked='checked']")
uncheck "Build still fails"
click_button "Edit"
assert ! page.has_xpath?("//*[@name='hooks_enabled[build_still_fails]' and @checked='checked']")
end

test "hooks with no config print out this info to user" do
test "hooks with no config work as usually" do
with_hook_enabled(BigTuna::Hooks::NoConfig) do
project = project_with_steps({
:name => "Koss",
Expand All @@ -43,7 +48,7 @@ def teardown
within("#hook_no_config") do
click_link "Configure"
end
assert page.has_content?("This hook doesn't have any configuration.")
assert_status_code 200
end
end

Expand Down
21 changes: 21 additions & 0 deletions test/unit/hooks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,25 @@ def teardown
assert_equal Build::STATUS_HOOK_ERROR, build.status
end
end

test "if hook is not enabled it won't get executed" do
with_hook_enabled(BigTuna::Hooks::RaisingHook) do
project = project_with_steps({
:name => "Koss",
:vcs_source => "test/files/koss",
:max_builds => 2,
:hooks => {"raising_hook" => "raising_hook"},
}, "true")
hook = project.hooks.first
hook.hooks_enabled.delete("build_finished")
hook.save!
assert ! hook.hook_enabled?("build_finished")
assert hook.hook_implemented?("build_finished")

project.build!
run_delayed_jobs()
build = project.recent_build
assert_equal Build::STATUS_OK, build.status
end
end
end

0 comments on commit a518095

Please sign in to comment.