autoland: refactor: use exceptions to signal failure (bug 1349997); r?gps draft
authorbyron jones <glob@mozilla.com>
Thu, 23 Mar 2017 14:09:16 +0800
changeset 10580 ce521a3cd1b288b2b9bd00361abb519637f9466e
parent 10579 0d0c11414c2f6f26ab70cc2f1821d175fddd116c
child 10581 bd85285478902b1aa677376d85b637e34e58fc3b
push id1595
push userbjones@mozilla.com
push dateFri, 31 Mar 2017 04:09:17 +0000
reviewersgps
bugs1349997
autoland: refactor: use exceptions to signal failure (bug 1349997); r?gps Instead of returning a tuple with (bool, result), return the result on success, and throw an Exception on failures. MozReview-Commit-ID: JvoVCAv7RT2
autoland/autoland/autoland.py
autoland/autoland/transplant.py
--- a/autoland/autoland/autoland.py
+++ b/autoland/autoland/autoland.py
@@ -114,20 +114,26 @@ def handle_pending_transplants(dbconn):
                             tree, rev, destination, attempts + 1))
 
             # TODO: We should break the transplant call into two steps, one
             #       to pull down the commits to transplant, and another
             #       one to rebase it and attempt to push so we don't
             #       duplicate work unnecessarily if we have to rebase more
             #       than once.
             os.environ['AUTOLAND_REQUEST_USER'] = requester
-            landed, result = transplant.transplant(tree, destination, rev,
-                                                   trysyntax, push_bookmark,
-                                                   commit_descriptions)
-            del os.environ['AUTOLAND_REQUEST_USER']
+            try:
+                result = transplant.transplant(tree, destination, rev,
+                                               trysyntax, push_bookmark,
+                                               commit_descriptions)
+                landed = True
+            except Exception as e:
+                result = str(e)
+                landed = False
+            finally:
+                del os.environ['AUTOLAND_REQUEST_USER']
 
             logger.info('transplant from tree: %s rev: %s attempt: %s: %s' % (
                 tree, rev, attempts + 1, result))
 
             if landed or 'abort: push creates new remote head' not in result:
                 break
 
             attempts += 1
--- a/autoland/autoland/transplant.py
+++ b/autoland/autoland/transplant.py
@@ -31,34 +31,30 @@ def transplant(tree, destination, rev, t
     # These values can appear in command arguments. Don't let unicode leak
     # into these.
     assert isinstance(tree, str)
     assert isinstance(destination, str)
     assert isinstance(rev, str)
     if push_bookmark:
         assert isinstance(push_bookmark, str)
 
-    try:
-        path = config.get('repos').get(tree,
-                                       os.path.join(os.path.sep, 'repos', tree))
-        configs = ['ui.interactive=False']
-        with hglib.open(path, encoding='utf-8', configs=configs) as hg_repo:
-            tp = Transplant(hg_repo, tree, destination, rev)
+    path = config.get('repos').get(tree,
+                                   os.path.join(os.path.sep, 'repos', tree))
+    configs = ['ui.interactive=False']
+    with hglib.open(path, encoding='utf-8', configs=configs) as hg_repo:
+        tp = Transplant(hg_repo, tree, destination, rev)
 
-            if trysyntax:
-                return True, tp.push_try(trysyntax)
+        if trysyntax:
+            return tp.push_try(trysyntax)
 
-            elif push_bookmark:
-                return True, tp.push_bookmark(commit_descriptions,
-                                              push_bookmark)
+        elif push_bookmark:
+            return tp.push_bookmark(commit_descriptions, push_bookmark)
 
-            else:
-                return True, tp.push(commit_descriptions)
-    except Exception as e:
-        return False, str(e)
+        else:
+            return tp.push(commit_descriptions)
 
 
 class Transplant:
     def __init__(self, hg_repo, tree, destination, rev):
         self.hg_repo = hg_repo
         self.tree = tree
         self.destination = destination
         self.source_rev = rev