6967: Move source_version detection code from config yaml to lib/app_version.rb.
authorTom Clegg <tom@curoverse.com>
Wed, 7 Oct 2015 15:11:14 +0000 (11:11 -0400)
committerTom Clegg <tom@curoverse.com>
Thu, 8 Oct 2015 19:24:14 +0000 (15:24 -0400)
apps/workbench/app/helpers/version_helper.rb
apps/workbench/app/views/application/_report_issue_popup.html.erb
apps/workbench/config/application.default.yml
apps/workbench/config/application.rb
apps/workbench/lib/app_version.rb [new file with mode: 0644]

index 6cae78f53efc495f4a54797b39dc6e6e889492a3..5c15986601e1b9b69d772d5391b88146fbec33c9 100644 (file)
@@ -1,30 +1,12 @@
 module VersionHelper
-  # api_version returns the git commit hash for the API server's
-  # current version.  It is extracted from api_version_text, which
-  # returns the source_version provided by the discovery document and
-  # may have the word "-modified" appended to it (if the API server is
-  # running from a locally modified repository).
-
-  def api_version
-    api_version_text.sub(/[^[:xdigit:]].*/, '')
-  end
-
-  def api_version_text
+  # Get the source_version given in the API server's discovery
+  # document.
+  def api_source_version
     arvados_api_client.discovery[:source_version]
   end
 
-  # wb_version and wb_version_text provide the same strings for the
-  # code version that this Workbench is currently running.
-
-  def wb_version
-    Rails.configuration.source_version
-  end
-
-  def wb_version_text
-    wb_version + (Rails.configuration.local_modified or '')
-  end
-
+  # URL for browsing source code for the given version.
   def version_link_target version
-    "https://arvados.org/projects/arvados/repository/changes?rev=#{version}"
+    "https://arvados.org/projects/arvados/repository/changes?rev=#{version.sub(/-.*/, "")}"
   end
 end
index 1c964abfe0491b8ad1c77c02767aaf845927093e..1f66146e2626cbfc705a94ea510ba6c062715e84 100644 (file)
@@ -3,18 +3,15 @@
   arvados_base = Rails.configuration.arvados_v1_base
   support_email = Rails.configuration.support_email_address
 
-  api_version_link = link_to api_version_text, version_link_target(api_version)
-  wb_version_link = link_to wb_version_text, version_link_target(wb_version)
-
   additional_info = {}
   additional_info['Current location'] = params[:current_location]
   additional_info['User UUID'] = current_user.uuid if current_user
 
   additional_info_str = additional_info.map {|k,v| "#{k}=#{v}"}.join("\n")
 
-  additional_info['api_version'] = api_version_text
+  additional_info['api_source_version'] = api_source_version
   additional_info['generated_at'] = generated_at
-  additional_info['workbench_version'] = wb_version_text
+  additional_info['workbench_version'] = AppVersion.hash
   additional_info['arvados_base'] = arvados_base
   additional_info['support_email'] = support_email
   additional_info['error_message'] = params[:error_message] if params[:error_message]
         <div class="form-group">
           <label for="wb_version" class="col-sm-4 control-label"> Workbench version </label>
           <div class="col-sm-8">
-            <p class="form-control-static" name="wb_version"><%= wb_version_link %></p>
+            <p class="form-control-static" name="wb_version">
+              <%= link_to AppVersion.hash, version_link_target(AppVersion.hash) %>
+            </p>
           </div>
         </div>
 
         <div class="form-group">
           <label for="server_version" class="col-sm-4 control-label"> API version </label>
           <div class="col-sm-8">
-            <p class="form-control-static" name="server_version"><%= api_version_link %></p>
+            <p class="form-control-static" name="server_version">
+              <%= link_to api_source_version, version_link_target(api_source_version) %>
+            </p>
           </div>
         </div>
 
index 1650e12f4dc08da930509eb1352e7f81f88e4937..00959bbb3bea30568e62ddb82c9f0d7f733fb44d 100644 (file)
@@ -1,45 +1,6 @@
 # Do not use this file for site configuration. Create application.yml
 # instead (see application.yml.example).
 
-<%
-# If you change any of the code in this block, you'll probably also want
-# to update it in API server's application.default.yml.
-def info_cmd(*args, &block)
-  IO.popen(args, "r", chdir: Rails.root, err: "/dev/null", &block)
-end
-
-source_version = ""
-local_modified = false
-if Rails.env == "production"
-  # Read the version from our package's git-commit.version file, if available.
-  begin
-    source_version = IO.read(Rails.root.join("git-commit.version")).strip
-  rescue Errno::ENOENT
-  end
-end
-
-if source_version.empty?
-  begin
-    status_output = false
-    info_cmd("git", "status", "-s") do |git_pipe|
-      git_pipe.each_line do |_|
-        status_output = true
-        # Continue reading the pipe so git doesn't get SIGPIPE.
-      end
-    end
-    if $?.success?
-      info_cmd("git", "log", "-n1", "--format=%H") do |git_pipe|
-        git_pipe.each_line do |line|
-          source_version = line.chomp
-        end
-      end
-      local_modified = status_output
-    end
-  rescue SystemCallError
-  end
-end
-%>
-
 # Below is a sample setting for diagnostics testing.
 # Configure workbench URL as "arvados_workbench_url"
 # Configure test user tokens as "user_tokens".
@@ -84,7 +45,6 @@ development:
   assets.debug: true
   profiling_enabled: true
   site_name: Arvados Workbench (dev)
-  local_modified: "<%= local_modified ? '-modified' : '' %>"
 
   # API server configuration
   arvados_login_base: ~
@@ -222,9 +182,11 @@ common:
   # the profile page.
   user_profile_form_message: Welcome to Arvados. All <span style="color:red">required fields</span> must be completed before you can proceed.
 
-  # source_version
-  source_version: "<%= source_version[0...8] %>"
-  local_modified: false
+  # Override the automatic version string. With the default value of
+  # false, the version string is read from git-commit.version in
+  # Rails.root (included in vendor packages) or determined by invoking
+  # "git log".
+  source_version: false
 
   # report notification to and from addresses
   issue_reporter_email_from: arvados@example.com
index 4ac68198e8bd6fa6404512ad5479397223de1bc8..d1c7934ab3ba16b57f61425edcc48a57b81df1e3 100644 (file)
@@ -12,6 +12,7 @@ module ArvadosWorkbench
 
     # Custom directories with classes and modules you want to be autoloadable.
     # config.autoload_paths += %W(#{config.root}/extras)
+    config.autoload_paths += %W(#{config.root}/lib)
 
     # Only load the plugins named here, in the order given (default is alphabetical).
     # :all can be used as a placeholder for all plugins not explicitly named.
diff --git a/apps/workbench/lib/app_version.rb b/apps/workbench/lib/app_version.rb
new file mode 100644 (file)
index 0000000..05b9f26
--- /dev/null
@@ -0,0 +1,53 @@
+# If you change this file, you'll probably also want to make the same
+# changes in apps/workbench/lib/app_version.rb.
+
+class AppVersion
+  def self.git(*args, &block)
+    IO.popen(["git", "--git-dir", ".git"] + args, "r",
+             chdir: Rails.root.join('../..'),
+             err: "/dev/null",
+             &block)
+  end
+
+  def self.forget
+    @hash = nil
+  end
+
+  # Return abbrev commit hash for current code version: "abc1234", or
+  # "abc1234-modified" if there are uncommitted changes. If present,
+  # return contents of {root}/git-commit.version instead.
+  def self.hash
+    if (cached = Rails.configuration.source_version || @hash)
+      return cached
+    end
+
+    # Read the version from our package's git-commit.version file, if available.
+    begin
+      @hash = IO.read(Rails.root.join("git-commit.version")).strip
+    rescue Errno::ENOENT
+    end
+
+    if @hash.nil? or @hash.empty?
+      begin
+        local_modified = false
+        git("status", "-s") do |git_pipe|
+          git_pipe.each_line do |_|
+            STDERR.puts _
+            local_modified = true
+            # Continue reading the pipe so git doesn't get SIGPIPE.
+          end
+        end
+        if $?.success?
+          git("log", "-n1", "--format=%H") do |git_pipe|
+            git_pipe.each_line do |line|
+              @hash = line.chomp[0...8] + (local_modified ? '-modified' : '')
+            end
+          end
+        end
+      rescue SystemCallError
+      end
+    end
+
+    @hash || "unknown"
+  end
+end