scheduler_stages: Fix release waterfall builder names.

If a release waterfall builder is run with a different branch (such as
a stabilize branch), the wrong builder name is generated for the slave
builders when scheduling them.

BUG=chromium:793447
TEST=run_tests

Change-Id: If19f5f0db2b457ed92aca5aa97573e8789eae162
Reviewed-on: https://chromium-review.googlesource.com/818307
Tested-by: Don Garrett <dgarrett@chromium.org>
Trybot-Ready: Don Garrett <dgarrett@chromium.org>
Reviewed-by: Paul Hobbs <phobbs@google.com>
(cherry picked from commit 3043e79ef8e089b7627b1baa1b7df678701f1ee4)
Reviewed-on: https://chromium-review.googlesource.com/818401
Reviewed-by: Don Garrett <dgarrett@chromium.org>
(cherry picked from commit 8d03155c6a3f3d17405c992ed3d78e8dc6511bac)
Reviewed-on: https://chromium-review.googlesource.com/820655
Commit-Queue: Grace Kihumba <gkihumba@chromium.org>
Tested-by: Grace Kihumba <gkihumba@chromium.org>
Reviewed-by: Grace Kihumba <gkihumba@chromium.org>
diff --git a/cbuildbot/stages/scheduler_stages.py b/cbuildbot/stages/scheduler_stages.py
index b1f2e02..0bac991 100644
--- a/cbuildbot/stages/scheduler_stages.py
+++ b/cbuildbot/stages/scheduler_stages.py
@@ -8,6 +8,7 @@
 from __future__ import print_function
 
 import json
+import os
 import time
 
 from chromite.cbuildbot.stages import generic_stages
@@ -19,13 +20,13 @@
 from chromite.lib.const import waterfall
 
 
-def BuilderName(build_name, active_waterfall, branch):
+def BuilderName(build_config, active_waterfall, current_builder):
   """Gets the corresponding builder name of the build.
 
   Args:
-    build_name: build config (string) of the build.
+    build_config: build config (string) of the build.
     active_waterfall: active waterfall to run the build.
-    branch: branch to run the build.
+    current_builder: buildbot builder name of the current builder, or None.
 
   Returns:
     Builder name to run the build on.
@@ -33,9 +34,12 @@
   # The builder name is configured differently for release builds in
   # chromeos and chromeos_release waterfalls. (see crbug.com/755276)
   if active_waterfall == waterfall.WATERFALL_RELEASE:
-    return '%s %s' % (build_name, branch)
+    assert current_builder
+    # Example: master-release release-R64-10176.B
+    named_branch = current_builder.split()[1]
+    return '%s %s' % (build_config, named_branch)
   else:
-    return build_name
+    return build_config
 
 
 class ScheduleSlavesStage(generic_stages.BuilderStage):
@@ -78,8 +82,9 @@
                     More context: crbug.com/661689
       dryrun: Whether a dryrun.
     """
+    current_buildername = os.environ.get('BUILDBOT_BUILDERNAME', None)
     builder_name = BuilderName(
-        build_name, build_config.active_waterfall, self._run.manifest_branch)
+        build_name, build_config.active_waterfall, current_buildername)
 
     tags = ['buildset:%s' % buildset_tag,
             'build_type:%s' % build_config.build_type,
diff --git a/cbuildbot/stages/scheduler_stages_unittest.py b/cbuildbot/stages/scheduler_stages_unittest.py
index 8013724..712519d 100644
--- a/cbuildbot/stages/scheduler_stages_unittest.py
+++ b/cbuildbot/stages/scheduler_stages_unittest.py
@@ -28,11 +28,12 @@
   def testBuilderName(self):
     """Test BuilderName."""
     builder_name = scheduler_stages.BuilderName(
-        'parrot-release', waterfall.WATERFALL_INTERNAL, 'master')
+        'parrot-release', waterfall.WATERFALL_INTERNAL, 'master-release')
     self.assertEqual(builder_name, 'parrot-release')
 
     builder_name = scheduler_stages.BuilderName(
-        'parrot-release', waterfall.WATERFALL_RELEASE, 'release-R62-9901.B')
+        'parrot-release', waterfall.WATERFALL_RELEASE,
+        'master-release release-R62-9901.B')
     self.assertEqual(builder_name, 'parrot-release release-R62-9901.B')