autoland: refactor: change formulate_hg_error to custom Exception (bug 1349997); r?gps draft
authorbyron jones <glob@mozilla.com>
Wed, 22 Mar 2017 22:13:37 +0800
changeset 10575 1eb9c1858c813342383904c66a4ff4c0e7b16daf
parent 10574 bc300124464a2953232b99ff78d831d6ee4bd0fa
child 10576 33d37c389de485f6011132c4a44f0a7545a74ad1
push id1595
push userbjones@mozilla.com
push dateFri, 31 Mar 2017 04:09:17 +0000
reviewersgps
bugs1349997
autoland: refactor: change formulate_hg_error to custom Exception (bug 1349997); r?gps Move error message sanitisation to a custom Exception. MozReview-Commit-ID: 3dgtdd7XALB
autoland/autoland/transplant.py
--- a/autoland/autoland/transplant.py
+++ b/autoland/autoland/transplant.py
@@ -6,16 +6,26 @@ import os
 import re
 import tempfile
 
 REPO_CONFIG = {}
 
 logger = logging.getLogger('autoland')
 
 
+class HgCommandError(Exception):
+    def __init__(self, hg_args, out):
+        # we want to strip out any sensitive --config options
+        hg_args = map(lambda x: x if not x.startswith('bugzilla') else 'xxx',
+                      hg_args)
+        message = 'hg error in cmd: hg %s: %s' % (' '.join(hg_args),
+                                                  out.getvalue())
+        super(self.__class__, self).__init__(message)
+
+
 def transplant(tree, destination, rev, trysyntax=None,
                push_bookmark=False, commit_descriptions=None):
     """Transplant a specified revision and ancestors to the specified tree.
 
     If ``trysyntax`` is specified, a Try commit will be created using the
     syntax specified.
     """
     # These values can appear in command arguments. Don't let unicode leak
@@ -75,39 +85,33 @@ def _transplant(hg_repo, tree, destinati
         return False, str(e)
 
 
 def get_repo_path(tree):
     return config.get('repos').get(tree,
                                    os.path.join(os.path.sep, 'repos', tree))
 
 
-def formulate_hg_error(cmd, output):
-    # we want to strip out any sensitive --config options
-    cmd = map(lambda x: x if not x.startswith('bugzilla') else 'xxx', cmd)
-    return 'hg error in cmd: ' + ' '.join(cmd) + ': ' + output
-
-
 def run_hg(hg_repo, rev, args):
     logger.info('rev: %s: executing: %s' % (rev, args))
     out = hglib.util.BytesIO()
     out_channels = {b'o': out.write, b'e': out.write}
     ret = hg_repo.runcommand(args, {}, out_channels)
     if ret:
         raise hglib.error.CommandError(args, ret, out, None)
     return out.getvalue()
 
 
 def run_hg_cmds(hg_repo, rev, cmds):
     last_result = ''
     for cmd in cmds:
         try:
             last_result = run_hg(hg_repo, rev, cmd)
         except hglib.error.CommandError as e:
-            raise Exception(formulate_hg_error(['hg'] + cmd, e.out.getvalue()))
+            raise HgCommandError(cmd, e.out)
     return last_result
 
 
 def strip_drafts(hg_repo, rev):
     # Strip any lingering draft changesets.
     try:
         run_hg(hg_repo, rev, ['strip', '--no-backup', '-r', 'not public()'])
     except hglib.error.CommandError:
@@ -142,18 +146,17 @@ def update_repo(hg_repo, rev, tree, remo
             output = e.out.getvalue()
             if 'no changes found' in output:
                 # we've already pulled this revision
                 continue
             elif 'abort: no rebase in progress' in output:
                 # there was no rebase in progress, nothing to see here
                 continue
             else:
-                raise Exception(formulate_hg_error(['hg'] + cmd,
-                                                   e.out.getvalue()))
+                raise HgCommandError(cmd, e.out)
 
 
 def rewrite_commit_descriptions(hg_repo, rev, commit_descriptions):
     # Rewrite commit descriptions as per the mapping provided.  Returns the
     # revision of the base commit.
     assert commit_descriptions, 'commit_descriptions requires for transplant'
 
     with tempfile.NamedTemporaryFile() as f:
@@ -179,19 +182,18 @@ def rewrite_commit_descriptions(hg_repo,
 
 
 def rebase(hg_repo, rev, base_revision, remote_tip):
     # Perform rebase if necessary.  Returns tip revision.
     cmd = ['rebase', '-s', base_revision, '-d', remote_tip]
     try:
         run_hg(hg_repo, rev, cmd)
     except hglib.error.CommandError as e:
-        output = e.out.getvalue()
-        if 'nothing to rebase' not in output:
-            raise Exception(formulate_hg_error(['hg'] + cmd, output))
+        if 'nothing to rebase' not in e.out.getvalue():
+            raise HgCommandError(cmd, e.out)
 
     return run_hg_cmds(hg_repo, rev, [
         ['log', '-r', 'tip', '-T', '{node|short}']
     ])
 
 
 def validate_descriptions(hg_repo, destination, commit_descriptions):
     # Match outgoing commit descriptions against incoming commit