You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@continuum.apache.org by ca...@apache.org on 2009/11/05 00:49:36 UTC

svn commit: r832915 - in /continuum/sandbox/continuum-ruby: README lib/ lib/continuum.rb lib/continuum/ lib/continuum/working_copy.rb lib/continuum/xml_rpc.rb

Author: carlos
Date: Wed Nov  4 23:49:36 2009
New Revision: 832915

URL: http://svn.apache.org/viewvc?rev=832915&view=rev
Log:
Add the first version of the Continuum-Ruby library

Added:
    continuum/sandbox/continuum-ruby/README   (with props)
    continuum/sandbox/continuum-ruby/lib/
    continuum/sandbox/continuum-ruby/lib/continuum/
    continuum/sandbox/continuum-ruby/lib/continuum.rb   (with props)
    continuum/sandbox/continuum-ruby/lib/continuum/working_copy.rb   (with props)
    continuum/sandbox/continuum-ruby/lib/continuum/xml_rpc.rb   (with props)

Added: continuum/sandbox/continuum-ruby/README
URL: http://svn.apache.org/viewvc/continuum/sandbox/continuum-ruby/README?rev=832915&view=auto
==============================================================================
--- continuum/sandbox/continuum-ruby/README (added)
+++ continuum/sandbox/continuum-ruby/README Wed Nov  4 23:49:36 2009
@@ -0,0 +1,32 @@
+Continuum-Ruby
+=========
+
+Ruby library to interact with Continuum, using the XML-RPC interface and enabling access
+to the working copy directories.
+
+
+Example
+=======
+
+continuum = Continuum::Continuum.new("my.continuum.host", 8080, "admin", "password", "/continuum")
+
+# xml-rpc interface
+
+xml_rpc = Continuum::XmlRpc.new(continuum)
+
+ok, result = xml_rpc.build_project(1)
+error = Continuum.parse_error(result) if !ok
+
+# getting working copy files
+
+working_copy = Continuum::WorkingCopy.new(continuum)
+test_results = working_copy.get(1, "target/surefire-reports", "emailable-report.html")
+
+files = working_copy.dir(1, "target")
+files.each do |file|
+  file_content = working_copy.get(1, "target", file)
+end
+
+
+
+Copyright (c) 2009 Carlos Sanchez, released under the Apache License, Version 2.0

Propchange: continuum/sandbox/continuum-ruby/README
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: continuum/sandbox/continuum-ruby/README
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: continuum/sandbox/continuum-ruby/lib/continuum.rb
URL: http://svn.apache.org/viewvc/continuum/sandbox/continuum-ruby/lib/continuum.rb?rev=832915&view=auto
==============================================================================
--- continuum/sandbox/continuum-ruby/lib/continuum.rb (added)
+++ continuum/sandbox/continuum-ruby/lib/continuum.rb Wed Nov  4 23:49:36 2009
@@ -0,0 +1,34 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+# 
+#   http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+require 'continuum/working_copy'
+require 'continuum/xml_rpc'
+
+module Continuum
+  class Continuum
+
+    attr_accessor :username, :password, :host, :port, :path
+
+    def initialize(host = "localhost", port = "8080", username = nil, password = nil, path = "")
+      @username = username
+      @password = password
+      @host = host
+      @port = port
+      @path = path
+    end
+  end
+end

Propchange: continuum/sandbox/continuum-ruby/lib/continuum.rb
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: continuum/sandbox/continuum-ruby/lib/continuum.rb
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: continuum/sandbox/continuum-ruby/lib/continuum/working_copy.rb
URL: http://svn.apache.org/viewvc/continuum/sandbox/continuum-ruby/lib/continuum/working_copy.rb?rev=832915&view=auto
==============================================================================
--- continuum/sandbox/continuum-ruby/lib/continuum/working_copy.rb (added)
+++ continuum/sandbox/continuum-ruby/lib/continuum/working_copy.rb Wed Nov  4 23:49:36 2009
@@ -0,0 +1,79 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+# 
+#   http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+require 'mechanize'
+
+# Allows getting files from a project working copy folder, by scraping the Continuum html
+module Continuum
+  class WorkingCopy
+
+    attr_accessor :continuum
+
+    def initialize(continuum)
+      @continuum = continuum
+      @username = continuum.username
+      @password = continuum.password
+      @host = continuum.host
+      @port = continuum.port
+      @path = continuum.path
+      @continuum_url = "http://#{@host}:#{@port}#{@path}"
+      @login_url = "#{@continuum_url}/security/login.action"
+    end
+
+    def dir(project_id, dir)
+      page = get_page working_copy_url(project_id, dir)
+      dir_contents = Nokogiri::HTML(page.body).search(".//table[@class='tableRegion']//a[contains(@href,'#{dir}')]")
+      file_names = []
+      dir_contents.each {|x| file_names << x.text}
+      file_names = file_names[1..-1] unless file_names.empty? # first element is directory name
+      return file_names
+    end
+
+    def get(project_id, dir, file)
+      page = get_page(working_copy_url(project_id, dir, file, "workingCopy")) # binary file
+      if (!page.instance_of? WWW::Mechanize::File)
+        # it's a text file
+        page = get_page(working_copy_url(project_id, dir, file, "workingCopyFileText"))
+      end
+      return page.body
+    end
+
+    private
+
+    def working_copy_url(project_id, dir, file = nil, action = "workingCopyFileText")
+      action = file.nil? ? "workingCopy" : action
+      url = "#{@continuum_url}/#{action}.action?projectId=#{project_id}&userDirectory=#{dir}"
+      url += "&file=#{file}" unless file.nil?
+      return url
+    end
+
+    def get_page(url)
+      mechanize = WWW::Mechanize.new
+      page = mechanize.get(url)
+      if page.search(".//div[@class='app']").inner_html.include? "You are not authorized to access this page"
+        page = mechanize.get(@login_url)
+        login_form = page.form_with(:name => "login")
+        login_form.username = @username
+        login_form.password = @password
+        page = login_form.click_button
+        page = mechanize.get(url)
+      end
+      return page
+    end
+
+  end
+end

Propchange: continuum/sandbox/continuum-ruby/lib/continuum/working_copy.rb
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: continuum/sandbox/continuum-ruby/lib/continuum/working_copy.rb
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: continuum/sandbox/continuum-ruby/lib/continuum/xml_rpc.rb
URL: http://svn.apache.org/viewvc/continuum/sandbox/continuum-ruby/lib/continuum/xml_rpc.rb?rev=832915&view=auto
==============================================================================
--- continuum/sandbox/continuum-ruby/lib/continuum/xml_rpc.rb (added)
+++ continuum/sandbox/continuum-ruby/lib/continuum/xml_rpc.rb Wed Nov  4 23:49:36 2009
@@ -0,0 +1,119 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+# 
+#   http://www.apache.org/licenses/LICENSE-2.0
+# 
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+require 'xmlrpc/client'
+
+OpenStruct.__send__(:define_method, :id) { @table[:id] || self.object_id }
+
+# Provides methods to interact with continuum XML-RPC interface
+module Continuum
+  class XmlRpc
+
+    attr_accessor :continuum
+
+    def initialize(continuum)
+      @continuum = continuum
+      @username = continuum.username
+      @password = continuum.password
+      @host = continuum.host
+      @port = continuum.port
+      @path = continuum.path
+    end
+
+    def self.parse_error(result)
+      (result.instance_of? XMLRPC::FaultException) ? "[#{result.faultCode}] #{result.faultString}" : ""
+    end
+
+    def build_project(project_id)
+      call_continuum("buildProject", project_id)
+    end
+
+    def get_build_results(project_id)
+      call_continuum("getBuildResultsForProjectRPC", project_id)
+    end
+
+    def get_build_output(project_id, build_id)
+      call_continuum("getBuildOutput", project_id, build_id)
+    end
+
+    def get_project_with_all_details(project_id)
+      call_continuum("getProjectWithAllDetailsRPC", project_id)
+    end
+
+    def get_all_project_groups()
+      call_continuum("getAllProjectGroupsRPC")
+    end
+    def get_all_project_groups_with_all_details()
+      call_continuum("getAllProjectGroupsWithAllDetailsRPC")
+    end
+
+    def get_projects(project_group_id)
+      call_continuum("getProjectsRPC", project_group_id)
+    end
+
+    def add_maven_two_project(url, project_group_id=nil)
+      if !project_group_id.nil?
+        call_continuum("addMavenTwoProjectRPC", url, project_group_id)
+      else
+        call_continuum("addMavenTwoProjectRPC", url)
+      end
+    end
+
+    def add_project_group(group_name, group_id, description = "")
+      call_continuum("addProjectGroupRPC", group_name, group_id, description)
+    end
+
+    def to_s
+      uri
+    end
+
+    def uri(mask_password = true)
+      if @username.nil?
+        "http://#{@host}:#{@port}#{@path}"
+      else
+        p = mask_password ? "******" : @password
+        "http://#{@username}:#{p}@#{@host}:#{@port}#{@path}"
+      end
+    end
+
+  private
+
+    def call_continuum(method, *args)
+      xml_rpc_client = XMLRPC::Client.new2("#{uri(false)}/xmlrpc")
+      ok, result = xml_rpc_client.call2("org.apache.maven.continuum.xmlrpc.ContinuumService.#{method}",*args)
+      result = parse(result) if ok
+      return ok, result
+    end
+
+    def parse(result)
+      return result.collect {|r| parse(r)} if result.instance_of?(Array)
+      return result if !is_object?(result)
+      new_hash = {}
+      result.each do |key, value|
+        parsed_value = value
+        parsed_value = parse(value) if is_object?(value) || value.instance_of?(Array)
+        new_hash[key.underscore] = parsed_value
+      end
+      OpenStruct.new new_hash
+    end
+
+    def is_object?(hash)
+      hash.instance_of?(Hash) && !hash["__class"].nil?
+    end
+
+  end
+end

Propchange: continuum/sandbox/continuum-ruby/lib/continuum/xml_rpc.rb
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: continuum/sandbox/continuum-ruby/lib/continuum/xml_rpc.rb
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision