require_dependency 'project'\r
\r
module RedmineLe\r
- module ProjectPatch\r
+ module ProjectPatch\r
def self.included(base)\r
base.extend(ClassMethods)\r
base.send(:include, InstanceMethods)\r
end\r
\r
module InstanceMethods\r
+ def repository_available?\r
+ module_enabled?(:repository) &&\r
+ (repository.nil? || repository.url == repository_url)\r
+ end\r
+\r
def repository_path\r
@repository_path ||=\r
File.join(RedmineLe::HOME, "subversion/repos", identifier)\r
end\r
\r
- def setup_integration\r
- return unless module_enabled?(:repository)\r
+ def repository_url\r
+ @repository_url ||=\r
+ "http://localhost:#{RedmineLe::HTTP_PORT}/svn/#{identifier}"\r
+ end\r
\r
- unless File.exist?(repository_path)\r
- unless system(%Q[svnadmin create "#{repository_path}"])\r
- raise "Cannot create repository"\r
+ def hook_config\r
+ @hook_config ||= HookConfig.new(self)\r
+ end\r
+\r
+ def setup_repository\r
+ begin\r
+ raise unless repository_available?\r
+ unless File.directory?(repository_path)\r
+ system(%Q[svnadmin create "#{repository_path}"])\r
+ root = "file:///" + repository_path.sub(/^\//, "")\r
+ system(%Q[svn mkdir "#{root}/trunk" "#{root}/branches" "#{root}/tags" -m "Initial repository layout"])\r
end\r
- \r
- root = "file:///" + repository_path.sub(/^\//, "")\r
- system(%Q[svn mkdir "#{root}/trunk" "#{root}/branches" "#{root}/tags" -m "Initial repository layout"])\r
+ rescue\r
+ raise "Cannot create repository" \r
end\r
\r
- url = "http://localhost:#{RedmineLe::HTTP_PORT}/svn/#{identifier}"\r
- ldap = AuthSourceLdap.first\r
self.repository = Repository::Subversion.new(\r
- :url => url,\r
- :root_url => url,\r
+ :url => repository_url,\r
+ :root_url => repository_url,\r
:login => RedmineLeSetting.admin_account,\r
:password => RedmineLeSetting.admin_password\r
)\r
\r
+ %w[pre-commit.bat post-commit.bat].each {|filename|\r
+ File.open(repository_path + "/hooks/#{filename}", "w") {|f|\r
+ f.print(ERB.new(File.read(RedmineLe::TEMPLATE_DIR +\r
+ "/#{filename}.erb")).result(binding))\r
+ }\r
+ }\r
+ end\r
+\r
+ def setup_job\r
+ ldap = AuthSourceLdap.first\r
+ url = repository_url\r
auth_token = RedmineLe::Utils.random_string(20)\r
data = ERB.new(File.read(RedmineLe::TEMPLATE_DIR + "/hudson_job_config.xml.erb")).result(binding)\r
\r
raise "Cannot create Hudson job" unless response.code == "200"\r
}\r
\r
- File.open(repository_path + "/hooks/post-commit.bat", "w") {|f|\r
- f.print <<-EOT\r
-call "#{RedmineLe::HOME}/script/setenv.bat"\r
-set RAILS_ENV=production\r
-start /B ruby "#{RAILS_ROOT}/script/runner" "Repository.fetch_changesets"\r
-start /B ruby -e "require 'net/http'; Net::HTTP.get('localhost', '/hudson/job/#{identifier}/build?token=#{auth_token}', #{RedmineLe::HTTP_PORT})"\r
- EOT\r
- }\r
+ hook_config["post_commit"]["perform_build"]["jobs"].push({\r
+ "name" => identifier,\r
+ "path" => "trunk"\r
+ })\r
+ hook_config.save\r
+ end\r
+\r
+ private\r
+ def setup_integration\r
+ return unless module_enabled?(:repository)\r
+\r
+ setup_repository\r
+ setup_job\r
end\r
\r
def cleanup_integration\r
}\r
end\r
end\r
- end\r
+ end\r
end\r
end\r
\r
def perform_build(options)\r
- return unless changed_files(:in => options["path"]).size > 0\r
- require 'rexml/document'\r
+ return unless options["jobs"]\r
+ options["jobs"].each {|job|\r
+ name = job["name"]\r
+ next unless name || changed_files(:in => job["path"]).size > 0\r
\r
- name = File.basename(@repos)\r
- hudson_xml = File.join(HOME, "hudson/home/jobs/#{name}/config.xml")\r
- path = "/hudson/job/#{name}/build"\r
- if File.file?(hudson_xml)\r
+ hudson_xml = File.join(HOME, "hudson/home/jobs/#{name}/config.xml")\r
+ path = "/hudson/job/#{name}/build"\r
+ next unless File.file?(hudson_xml)\r
+\r
+ require 'rexml/document'\r
doc = REXML::Document.new(File.new(hudson_xml))\r
elem = doc.elements["/project/authToken"]\r
path += "?token=#{elem.text}" if elem\r
- end\r
-\r
- run_background(%Q[ruby -e "require 'net/http'; Net::HTTP.get('localhost', '#{path}', #{http_port})"])\r
+ run_background(%Q[ruby -e "require 'net/http'; Net::HTTP.get('localhost', '#{path}', #{http_port})"])\r
+ }\r
end\r
end\r
end\r