Pass git branch to devicelab test runner

The git checkout in LUCI is in a detached state,
so the test runner must be told the git branch.

Bug: https://github.com/flutter/flutter/issues/71749
Test: Added branch to upload_metrics test
Change-Id: I83e8e6544e37c3070ffb016bb23cf8c12cc6e3f6
Reviewed-on: https://flutter-review.googlesource.com/c/recipes/+/9160
Reviewed-by: Keyong Han <keyonghan@google.com>
Commit-Queue: Casey Hillers <chillers@google.com>
diff --git a/recipes/devicelab/devicelab_drone.expected/post-submit.json b/recipes/devicelab/devicelab_drone.expected/post-submit.json
index 3ae8f08..c1c7d4c 100644
--- a/recipes/devicelab/devicelab_drone.expected/post-submit.json
+++ b/recipes/devicelab/devicelab_drone.expected/post-submit.json
@@ -34,7 +34,7 @@
       "git",
       "fetch",
       "origin",
-      "master",
+      "refs/heads/master",
       "--recurse-submodules",
       "--progress",
       "--tags"
@@ -220,7 +220,13 @@
       "dart",
       "bin/run.dart",
       "-t",
-      "abc"
+      "abc",
+      "--service-account-token-file",
+      "[CLEANUP]/tmp_tmp_1",
+      "--luci-builder",
+      "Linux abc",
+      "--git-branch",
+      "master"
     ],
     "cwd": "[START_DIR]/flutter/dev/devicelab",
     "env": {
diff --git a/recipes/devicelab/devicelab_drone.py b/recipes/devicelab/devicelab_drone.py
index b7441cc..b7fe5cc 100644
--- a/recipes/devicelab/devicelab_drone.py
+++ b/recipes/devicelab/devicelab_drone.py
@@ -40,24 +40,24 @@
   deps = api.properties.get('dependencies', [])
   api.flutter_deps.required_deps(env, env_prefixes, deps)
   devicelab_path = flutter_path.join('dev', 'devicelab')
+  # Run test
   test_runner_command = ['dart', 'bin/run.dart', '-t', task_name]
   # Create service account for post submit tests.
-  service_account_args = []
   if api.properties.get('upload_metrics'):
     service_account = api.service_account.default()
     access_token = service_account.get_access_token()
     access_token_path = api.path.mkstemp()
+    git_branch = api.properties.get('branch').replace('refs/heads/', '')
     api.file.write_text(
         "write token", access_token_path, access_token, include_log=False
     )
     test_runner_command.extend([
-        '--service-account-token-file', access_token_path, '--luci-builder',
-        api.properties.get('buildername')
+        '--service-account-token-file', access_token_path,
+        '--luci-builder', api.properties.get('buildername'),
+        # LUCI git checkouts end up in a detached HEAD state, so branch must
+        # be passed from gitiles -> test runner -> Cocoon.
+        '--git-branch', git_branch
     ])
-  # Run test
-  test_runner_command = ['dart', 'bin/run.dart', '-t', task_name]
-  test_runner_command.extend(service_account_args)
-
   # Gems are installed differently new and old versions of xcode. Pre xcode 12
   # the ruby sdk included in xcode was able to compile the gems correctly but
   # with xcode 12 gems building from inside a xcode context fails. We use the
@@ -134,6 +134,7 @@
   yield api.test(
       "post-submit",
       api.properties(
+          branch='refs/heads/master',
           buildername='Linux abc',
           pool='flutter.luci.prod',
           task_name='abc',