Rakefiles: support interactive push in `rake workflow:push`
authorJan Vrany <jan.vrany@labware.com>
Thu, 04 Nov 2021 16:23:15 +0000
changeset 322 d31ea885c8fa
parent 321 c868ba75e2da
child 323 1836a0e14ddb
Rakefiles: support interactive push in `rake workflow:push` When invoking `rake workflow:push` from an interactive terminal, for each repository ask user whether or not push changes.
rakelib/support.rb
rakelib/workflow.rake
--- a/rakelib/support.rb	Wed Nov 03 22:09:33 2021 +0000
+++ b/rakelib/support.rb	Thu Nov 04 16:23:15 2021 +0000
@@ -43,6 +43,26 @@
   (user == 'jv') or (user == 'vranyj1') ? true : false
 end
 
+# When on interactive terminal, ask user a yes/no question and return
+# user's answer. If terminal is not interactive, return false.
+def yesNO(question)
+  if STDIN.tty? then
+    puts "#{question} [y/N]?"
+    answer = STDIN.gets.chomp
+    case answer
+    when ''
+      return false
+    when /[nN]([oO])?/
+      return false
+    when /[yY]([eE][sS])?/
+      return true
+    end
+  else
+    return false
+  end
+end
+
+
 # ArtifactRepository module provides API for downloading (binary) files from
 # some sort of - well - artifact repository. A repository contains multiple
 # builds (currently the API provides only access to `latestBuild()`), build
--- a/rakelib/workflow.rake	Wed Nov 03 22:09:33 2021 +0000
+++ b/rakelib/workflow.rake	Thu Nov 04 16:23:15 2021 +0000
@@ -48,29 +48,34 @@
           puts''
         end
       else
-        if review_only
+        has_changes_in_this_repo = false
+        if review_only or STDIN.tty?
           opts[:rev] = '.'
           remote_url = hg.paths[remote_used] || remote_used
           bookmark = hg.bookmark
           hg.outgoing(remote_used, **opts) do | status, stdout |
             case status.exitstatus
-            when 0          
+            when 0
               puts ''
               puts "== changes going to #{remote_url} =="
               STDOUT.print stdout
               puts "Will update bookmark '#{bookmark}" if push_bookmark and bookmark
               puts '===================================='
               has_changes = true
+              has_changes_in_this_repo = true
             when 1
               # nothing
-            else        
+            else
               raise Exception.new("Failed to do 'outgoing' from #{remote} (exit code #{status.exitstatus})")
             end
           end
-        else
-          (push_bookmark && bookmark) ? (opts[:bookmarks] = ['.']) : (opts[:rev] = '.') # bookmark can be uninitialized
-          hg.push(remote_used, **opts)
-          has_changes = true
+        end
+        if not review_only
+          if not STDIN.tty? or (has_changes_in_this_repo and yesNO("Push to #{remote_url}")) then
+            (push_bookmark && bookmark) ? (opts[:bookmarks] = ['.']) : (opts[:rev] = '.') # bookmark can be uninitialized
+            hg.push(remote_used, **opts)
+            has_changes = true
+          end
         end
       end
     end