From 3753f084373ce4ccb885524b301dcc362879313c Mon Sep 17 00:00:00 2001 From: Akihiro Ono Date: Mon, 24 May 2010 20:19:57 +0900 Subject: [PATCH] Hook script now depends on hook_config.yml --- .../plugins/redmine_le/app/models/hook_config.rb | 33 ++++++++++ .../redmine_le/lib/redmine_le/project_patch.rb | 72 +++++++++++++++------- script/hooks/subversion_hook/post_commit.rb | 20 +++--- 3 files changed, 94 insertions(+), 31 deletions(-) create mode 100644 redmine/vendor/plugins/redmine_le/app/models/hook_config.rb diff --git a/redmine/vendor/plugins/redmine_le/app/models/hook_config.rb b/redmine/vendor/plugins/redmine_le/app/models/hook_config.rb new file mode 100644 index 0000000..afd41c8 --- /dev/null +++ b/redmine/vendor/plugins/redmine_le/app/models/hook_config.rb @@ -0,0 +1,33 @@ +class HookConfig + def initialize(project) + @project = project + end + + def filename + @filename = @project.repository_path + "/hooks/hook_config.yml" + end + + def config + @config ||= (exists? ? YAML.load_file(filename) : { + "pre_commit" => nil, + "post_commit" => { + "fetch_changesets" => nil, + "perform_build" => {"jobs" => []} + } + }) + end + + def save + File.open(filename, "w") {|f| + f.print(config.to_yaml) + } + end + + def exists? + File.file?(filename) + end + + def [](key) + config[key] + end +end diff --git a/redmine/vendor/plugins/redmine_le/lib/redmine_le/project_patch.rb b/redmine/vendor/plugins/redmine_le/lib/redmine_le/project_patch.rb index b6569ac..5f73a95 100644 --- a/redmine/vendor/plugins/redmine_le/lib/redmine_le/project_patch.rb +++ b/redmine/vendor/plugins/redmine_le/lib/redmine_le/project_patch.rb @@ -1,7 +1,7 @@ require_dependency 'project' module RedmineLe - module ProjectPatch + module ProjectPatch def self.included(base) base.extend(ClassMethods) base.send(:include, InstanceMethods) @@ -17,32 +17,55 @@ module RedmineLe end module InstanceMethods + def repository_available? + module_enabled?(:repository) && + (repository.nil? || repository.url == repository_url) + end + def repository_path @repository_path ||= File.join(RedmineLe::HOME, "subversion/repos", identifier) end - def setup_integration - return unless module_enabled?(:repository) + def repository_url + @repository_url ||= + "http://localhost:#{RedmineLe::HTTP_PORT}/svn/#{identifier}" + end - unless File.exist?(repository_path) - unless system(%Q[svnadmin create "#{repository_path}"]) - raise "Cannot create repository" + def hook_config + @hook_config ||= HookConfig.new(self) + end + + def setup_repository + begin + raise unless repository_available? + unless File.directory?(repository_path) + system(%Q[svnadmin create "#{repository_path}"]) + root = "file:///" + repository_path.sub(/^\//, "") + system(%Q[svn mkdir "#{root}/trunk" "#{root}/branches" "#{root}/tags" -m "Initial repository layout"]) end - - root = "file:///" + repository_path.sub(/^\//, "") - system(%Q[svn mkdir "#{root}/trunk" "#{root}/branches" "#{root}/tags" -m "Initial repository layout"]) + rescue + raise "Cannot create repository" end - url = "http://localhost:#{RedmineLe::HTTP_PORT}/svn/#{identifier}" - ldap = AuthSourceLdap.first self.repository = Repository::Subversion.new( - :url => url, - :root_url => url, + :url => repository_url, + :root_url => repository_url, :login => RedmineLeSetting.admin_account, :password => RedmineLeSetting.admin_password ) + %w[pre-commit.bat post-commit.bat].each {|filename| + File.open(repository_path + "/hooks/#{filename}", "w") {|f| + f.print(ERB.new(File.read(RedmineLe::TEMPLATE_DIR + + "/#{filename}.erb")).result(binding)) + } + } + end + + def setup_job + ldap = AuthSourceLdap.first + url = repository_url auth_token = RedmineLe::Utils.random_string(20) data = ERB.new(File.read(RedmineLe::TEMPLATE_DIR + "/hudson_job_config.xml.erb")).result(binding) @@ -53,14 +76,19 @@ module RedmineLe raise "Cannot create Hudson job" unless response.code == "200" } - File.open(repository_path + "/hooks/post-commit.bat", "w") {|f| - f.print <<-EOT -call "#{RedmineLe::HOME}/script/setenv.bat" -set RAILS_ENV=production -start /B ruby "#{RAILS_ROOT}/script/runner" "Repository.fetch_changesets" -start /B ruby -e "require 'net/http'; Net::HTTP.get('localhost', '/hudson/job/#{identifier}/build?token=#{auth_token}', #{RedmineLe::HTTP_PORT})" - EOT - } + hook_config["post_commit"]["perform_build"]["jobs"].push({ + "name" => identifier, + "path" => "trunk" + }) + hook_config.save + end + + private + def setup_integration + return unless module_enabled?(:repository) + + setup_repository + setup_job end def cleanup_integration @@ -70,5 +98,5 @@ start /B ruby -e "require 'net/http'; Net::HTTP.get('localhost', '/hudson/job/#{ } end end - end + end end diff --git a/script/hooks/subversion_hook/post_commit.rb b/script/hooks/subversion_hook/post_commit.rb index aa9cf60..596659b 100644 --- a/script/hooks/subversion_hook/post_commit.rb +++ b/script/hooks/subversion_hook/post_commit.rb @@ -27,19 +27,21 @@ module SubversionHook end def perform_build(options) - return unless changed_files(:in => options["path"]).size > 0 - require 'rexml/document' + return unless options["jobs"] + options["jobs"].each {|job| + name = job["name"] + next unless name || changed_files(:in => job["path"]).size > 0 - name = File.basename(@repos) - hudson_xml = File.join(HOME, "hudson/home/jobs/#{name}/config.xml") - path = "/hudson/job/#{name}/build" - if File.file?(hudson_xml) + hudson_xml = File.join(HOME, "hudson/home/jobs/#{name}/config.xml") + path = "/hudson/job/#{name}/build" + next unless File.file?(hudson_xml) + + require 'rexml/document' doc = REXML::Document.new(File.new(hudson_xml)) elem = doc.elements["/project/authToken"] path += "?token=#{elem.text}" if elem - end - - run_background(%Q[ruby -e "require 'net/http'; Net::HTTP.get('localhost', '#{path}', #{http_port})"]) + run_background(%Q[ruby -e "require 'net/http'; Net::HTTP.get('localhost', '#{path}', #{http_port})"]) + } end end end -- 2.11.0