repository: Downgrade repo version in identified branches.

Add the ability to downgrade the repo version for specifically targeted
branches, to work around the python upgrade that currently prevents
building older branches, specifically R79.

This also adds some high level debugging steps to help debugging repo
issues ongoing.

BUG=b:178532384
TEST=`run_test`

Change-Id: I7f55d8d02b261af15f43fc623bc10971b94a0d86
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/chromite/+/2660029
Commit-Queue: Mike Nichols <mikenichols@chromium.org>
Tested-by: Mike Nichols <mikenichols@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Reviewed-by: Dhanya Ganesh <dhanyaganesh@chromium.org>
diff --git a/scripts/cbuildbot_launch.py b/scripts/cbuildbot_launch.py
index 8d52d8a..690f669 100644
--- a/scripts/cbuildbot_launch.py
+++ b/scripts/cbuildbot_launch.py
@@ -333,7 +333,7 @@
 
 
 @StageDecorator
-def InitialCheckout(repo):
+def InitialCheckout(repo, options):
   """Preliminary ChromeOS checkout.
 
   Perform a complete checkout of ChromeOS on the specified branch. This does NOT
@@ -347,12 +347,14 @@
 
   Args:
     repo: repository.RepoRepository instance.
+    options: A parsed options object from a cbuildbot parser.
   """
   logging.PrintBuildbotStepText('Branch: %s' % repo.branch)
   logging.info('Bootstrap script starting initial sync on branch: %s',
                repo.branch)
   repo.PreLoad('/preload/chromeos')
-  repo.Sync(detach=True)
+  repo.Sync(detach=True,
+            downgrade_repo=_ShouldDowngradeRepo(options))
 
 
 def ShouldFixBotoCerts(options):
@@ -377,6 +379,34 @@
     return False
 
 
+def _ShouldDowngradeRepo(options):
+  """Determine which repo version to set for the branch.
+
+  Repo version is set at cache creation time, in the nightly builder,
+  which means we are typically at the latest version.  Older branches
+  are incompatible with newer version of ToT, therefore we downgrade
+  repo to a known working version.
+
+  Args:
+    options: A parsed options object from a cbuildbot parser.
+
+  Returns:
+    bool of whether to downgrade repo version based on branch.
+  """
+  try:
+    branch = options.branch or ''
+    # Only apply to "old" branches.
+    if branch.endswith('.B'):
+      branch_num = branch[:-2].split('-')[1][1:3]
+      return branch_num <= 79  # This is the newest known to be failing.
+
+    return False
+  except Exception as e:
+    logging.warning(' failed: %s', e)
+    # Conservatively continue without the fix.
+    return False
+
+
 @StageDecorator
 def Cbuildbot(buildroot, depot_tools_path, argv):
   """Start cbuildbot in specified directory with all arguments.
@@ -510,7 +540,7 @@
       # Get a checkout close enough to the branch that cbuildbot can handle it.
       if options.sync:
         with metrics.SecondsTimer(METRIC_INITIAL):
-          InitialCheckout(repo)
+          InitialCheckout(repo, options)
 
     # Run cbuildbot inside the full ChromeOS checkout, on the specified branch.
     with metrics.SecondsTimer(METRIC_CBUILDBOT), \