OSDN Git Service

Hook script now depends on hook_config.yml
authorAkihiro Ono <a-ono@users.sourceforge.jp>
Mon, 24 May 2010 11:19:57 +0000 (20:19 +0900)
committerAkihiro Ono <a-ono@users.sourceforge.jp>
Mon, 24 May 2010 11:19:57 +0000 (20:19 +0900)
redmine/vendor/plugins/redmine_le/app/models/hook_config.rb [new file with mode: 0644]
redmine/vendor/plugins/redmine_le/lib/redmine_le/project_patch.rb
script/hooks/subversion_hook/post_commit.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 (file)
index 0000000..afd41c8
--- /dev/null
@@ -0,0 +1,33 @@
+class HookConfig\r
+       def initialize(project)\r
+               @project = project\r
+       end\r
+\r
+       def filename\r
+               @filename = @project.repository_path + "/hooks/hook_config.yml"\r
+       end\r
+\r
+       def config\r
+               @config ||= (exists? ? YAML.load_file(filename) : {\r
+                       "pre_commit" => nil,\r
+                       "post_commit" => {\r
+                               "fetch_changesets" => nil,\r
+                               "perform_build" => {"jobs" => []}\r
+                       }\r
+               })\r
+       end\r
+\r
+       def save\r
+               File.open(filename, "w") {|f|\r
+                       f.print(config.to_yaml)\r
+               }\r
+       end\r
+\r
+       def exists?\r
+               File.file?(filename)\r
+       end\r
+\r
+       def [](key)\r
+               config[key]\r
+       end\r
+end\r
index b6569ac..5f73a95 100644 (file)
@@ -1,7 +1,7 @@
 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
@@ -17,32 +17,55 @@ module RedmineLe
     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
@@ -53,14 +76,19 @@ module RedmineLe
           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
@@ -70,5 +98,5 @@ start /B ruby -e "require 'net/http'; Net::HTTP.get('localhost', '/hudson/job/#{
         }\r
       end\r
     end\r
-       end\r
+  end\r
 end\r
index aa9cf60..596659b 100644 (file)
@@ -27,19 +27,21 @@ module SubversionHook
                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