Upload metrics from test runner

DeviceLab test metrics have been uploaded to metrics center by calling cocoon from test runner. Since metrics center migration has been finished, this CL starts uploading metrics from test runner. Related test runner PR: https://github.com/flutter/flutter/pull/88447

Change-Id: Idf2de0052420fef4844222d7232ebe3d523fdb76
Bug: https://github.com/flutter/flutter/issues/88296
Reviewed-on: https://flutter-review.googlesource.com/c/recipes/+/17200
Commit-Queue: Keyong Han <keyonghan@google.com>
Reviewed-by: Casey Hillers <chillers@google.com>
diff --git a/recipes/devicelab/devicelab_drone.expected/basic.json b/recipes/devicelab/devicelab_drone.expected/basic.json
index 0116e98..d96a541 100644
--- a/recipes/devicelab/devicelab_drone.expected/basic.json
+++ b/recipes/devicelab/devicelab_drone.expected/basic.json
@@ -110,6 +110,17 @@
     ]
   },
   {
+    "cmd": [
+      "git",
+      "log",
+      "--pretty=format:%ct",
+      "-n",
+      "1"
+    ],
+    "cwd": "[CLEANUP]/tmp_tmp_1/flutter sdk",
+    "name": "git commit time"
+  },
+  {
     "cmd": [],
     "name": "Initialize logs"
   },
diff --git a/recipes/devicelab/devicelab_drone.expected/local-engine.json b/recipes/devicelab/devicelab_drone.expected/local-engine.json
index 9e45a30..29ba684 100644
--- a/recipes/devicelab/devicelab_drone.expected/local-engine.json
+++ b/recipes/devicelab/devicelab_drone.expected/local-engine.json
@@ -110,6 +110,17 @@
     ]
   },
   {
+    "cmd": [
+      "git",
+      "log",
+      "--pretty=format:%ct",
+      "-n",
+      "1"
+    ],
+    "cwd": "[CLEANUP]/tmp_tmp_1/flutter sdk",
+    "name": "git commit time"
+  },
+  {
     "cmd": [],
     "name": "Initialize logs"
   },
@@ -751,6 +762,8 @@
     "cmd": [
       "luci-auth",
       "token",
+      "-scopes",
+      "https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/datastore",
       "-lifetime",
       "3m"
     ],
@@ -840,19 +853,25 @@
       "DEPOT_TOOLS": "RECIPE_REPO[depot_tools]",
       "FLUTTER_LOGS_DIR": "[CLEANUP]/flutter_logs_dir",
       "FLUTTER_TEST_OUTPUTS_DIR": "[CLEANUP]/flutter_logs_dir",
+      "GCP_PROJECT": "flutter-infra",
       "LOCAL_ENGINE": "[CLEANUP]/builder/src/out/host-release",
       "LUCI_BRANCH": "",
       "LUCI_CI": "True",
       "LUCI_PR": "",
       "OS": "linux",
       "PUB_CACHE": "[START_DIR]/.pub-cache",
-      "SDK_CHECKOUT_PATH": "[CLEANUP]/tmp_tmp_1/flutter sdk"
+      "SDK_CHECKOUT_PATH": "[CLEANUP]/tmp_tmp_1/flutter sdk",
+      "TOKEN_PATH": "[CLEANUP]/tmp_tmp_3"
     },
     "env_prefixes": {
       "PATH": [
         "[CLEANUP]/builder/src/out/host-release/dart-sdk/bin",
         "[CLEANUP]/tmp_tmp_1/flutter sdk/bin",
         "[CLEANUP]/tmp_tmp_1/flutter sdk/bin/cache/dart-sdk/bin",
+        "[CLEANUP]/tmp_tmp_2/vpython",
+        "[CLEANUP]/builder/src/out/host-release/dart-sdk/bin",
+        "[CLEANUP]/tmp_tmp_1/flutter sdk/bin",
+        "[CLEANUP]/tmp_tmp_1/flutter sdk/bin/cache/dart-sdk/bin",
         "[CLEANUP]/tmp_tmp_2/vpython"
       ]
     },
diff --git a/recipes/devicelab/devicelab_drone.expected/no-task-name.json b/recipes/devicelab/devicelab_drone.expected/no-task-name.json
index 286b5f6..7c022e7 100644
--- a/recipes/devicelab/devicelab_drone.expected/no-task-name.json
+++ b/recipes/devicelab/devicelab_drone.expected/no-task-name.json
@@ -7,7 +7,7 @@
       "The recipe has crashed at point 'Uncaught exception'!",
       "",
       "Traceback (most recent call last):",
-      "  File \"RECIPE_REPO[flutter]/recipes/devicelab/devicelab_drone.py\", line 38, in RunSteps",
+      "  File \"RECIPE_REPO[flutter]/recipes/devicelab/devicelab_drone.py\", line 39, in RunSteps",
       "    raise ValueError('A task_name property is required')",
       "ValueError: A task_name property is required"
     ]
diff --git a/recipes/devicelab/devicelab_drone.expected/post-submit.json b/recipes/devicelab/devicelab_drone.expected/post-submit.json
index 4f91cd8..e0faedf 100644
--- a/recipes/devicelab/devicelab_drone.expected/post-submit.json
+++ b/recipes/devicelab/devicelab_drone.expected/post-submit.json
@@ -118,6 +118,17 @@
     ]
   },
   {
+    "cmd": [
+      "git",
+      "log",
+      "--pretty=format:%ct",
+      "-n",
+      "1"
+    ],
+    "cwd": "[CLEANUP]\\tmp_tmp_1\\flutter sdk",
+    "name": "git commit time"
+  },
+  {
     "cmd": [],
     "name": "Initialize logs"
   },
@@ -639,6 +650,8 @@
     "cmd": [
       "luci-auth",
       "token",
+      "-scopes",
+      "https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/datastore",
       "-lifetime",
       "3m"
     ],
@@ -712,6 +725,8 @@
       "True",
       "--results-file",
       "[CLEANUP]\\results_tmp_1\\results",
+      "--commit-time",
+      "",
       "--service-account-token-file",
       "[CLEANUP]\\tmp_tmp_3"
     ],
@@ -720,17 +735,22 @@
       "DEPOT_TOOLS": "RECIPE_REPO[depot_tools]",
       "FLUTTER_LOGS_DIR": "[CLEANUP]\\flutter_logs_dir",
       "FLUTTER_TEST_OUTPUTS_DIR": "[CLEANUP]\\flutter_logs_dir",
+      "GCP_PROJECT": "flutter-infra",
       "LUCI_BRANCH": "",
       "LUCI_CI": "True",
       "LUCI_PR": "",
       "OS": "win",
       "PUB_CACHE": "[START_DIR]\\.pub-cache",
-      "SDK_CHECKOUT_PATH": "[CLEANUP]\\tmp_tmp_1\\flutter sdk"
+      "SDK_CHECKOUT_PATH": "[CLEANUP]\\tmp_tmp_1\\flutter sdk",
+      "TOKEN_PATH": "[CLEANUP]\\tmp_tmp_3"
     },
     "env_prefixes": {
       "PATH": [
         "[CLEANUP]\\tmp_tmp_1\\flutter sdk\\bin",
         "[CLEANUP]\\tmp_tmp_1\\flutter sdk\\bin\\cache\\dart-sdk\\bin",
+        "[CLEANUP]\\tmp_tmp_2\\vpython",
+        "[CLEANUP]\\tmp_tmp_1\\flutter sdk\\bin",
+        "[CLEANUP]\\tmp_tmp_1\\flutter sdk\\bin\\cache\\dart-sdk\\bin",
         "[CLEANUP]\\tmp_tmp_2\\vpython"
       ]
     },
diff --git a/recipes/devicelab/devicelab_drone.expected/upload-metrics-mac.json b/recipes/devicelab/devicelab_drone.expected/upload-metrics-mac.json
index c2411ca..cfed665 100644
--- a/recipes/devicelab/devicelab_drone.expected/upload-metrics-mac.json
+++ b/recipes/devicelab/devicelab_drone.expected/upload-metrics-mac.json
@@ -110,6 +110,17 @@
     ]
   },
   {
+    "cmd": [
+      "git",
+      "log",
+      "--pretty=format:%ct",
+      "-n",
+      "1"
+    ],
+    "cwd": "[CLEANUP]/tmp_tmp_1/flutter sdk",
+    "name": "git commit time"
+  },
+  {
     "cmd": [],
     "name": "Initialize logs"
   },
@@ -649,6 +660,8 @@
     "cmd": [
       "luci-auth",
       "token",
+      "-scopes",
+      "https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/datastore",
       "-lifetime",
       "3m"
     ],
@@ -722,6 +735,8 @@
       "False",
       "--results-file",
       "[CLEANUP]/results_tmp_1/results",
+      "--commit-time",
+      "",
       "--service-account-token-file",
       "[CLEANUP]/tmp_tmp_3"
     ],
@@ -730,17 +745,22 @@
       "DEPOT_TOOLS": "RECIPE_REPO[depot_tools]",
       "FLUTTER_LOGS_DIR": "[CLEANUP]/flutter_logs_dir",
       "FLUTTER_TEST_OUTPUTS_DIR": "[CLEANUP]/flutter_logs_dir",
+      "GCP_PROJECT": "flutter-infra",
       "LUCI_BRANCH": "",
       "LUCI_CI": "True",
       "LUCI_PR": "",
       "OS": "linux",
       "PUB_CACHE": "[START_DIR]/.pub-cache",
-      "SDK_CHECKOUT_PATH": "[CLEANUP]/tmp_tmp_1/flutter sdk"
+      "SDK_CHECKOUT_PATH": "[CLEANUP]/tmp_tmp_1/flutter sdk",
+      "TOKEN_PATH": "[CLEANUP]/tmp_tmp_3"
     },
     "env_prefixes": {
       "PATH": [
         "[CLEANUP]/tmp_tmp_1/flutter sdk/bin",
         "[CLEANUP]/tmp_tmp_1/flutter sdk/bin/cache/dart-sdk/bin",
+        "[CLEANUP]/tmp_tmp_2/vpython",
+        "[CLEANUP]/tmp_tmp_1/flutter sdk/bin",
+        "[CLEANUP]/tmp_tmp_1/flutter sdk/bin/cache/dart-sdk/bin",
         "[CLEANUP]/tmp_tmp_2/vpython"
       ]
     },
diff --git a/recipes/devicelab/devicelab_drone.expected/xcode-chromium-mac.json b/recipes/devicelab/devicelab_drone.expected/xcode-chromium-mac.json
index b5f1d8d..427587d 100644
--- a/recipes/devicelab/devicelab_drone.expected/xcode-chromium-mac.json
+++ b/recipes/devicelab/devicelab_drone.expected/xcode-chromium-mac.json
@@ -110,6 +110,17 @@
     ]
   },
   {
+    "cmd": [
+      "git",
+      "log",
+      "--pretty=format:%ct",
+      "-n",
+      "1"
+    ],
+    "cwd": "[CLEANUP]/tmp_tmp_1/flutter sdk",
+    "name": "git commit time"
+  },
+  {
     "cmd": [],
     "name": "Initialize logs"
   },
@@ -649,6 +660,8 @@
     "cmd": [
       "luci-auth",
       "token",
+      "-scopes",
+      "https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/datastore",
       "-lifetime",
       "3m"
     ],
@@ -734,17 +747,22 @@
       "DEPOT_TOOLS": "RECIPE_REPO[depot_tools]",
       "FLUTTER_LOGS_DIR": "[CLEANUP]/flutter_logs_dir",
       "FLUTTER_TEST_OUTPUTS_DIR": "[CLEANUP]/flutter_logs_dir",
+      "GCP_PROJECT": "flutter-infra",
       "LUCI_BRANCH": "",
       "LUCI_CI": "True",
       "LUCI_PR": "",
       "OS": "linux",
       "PUB_CACHE": "[START_DIR]/.pub-cache",
-      "SDK_CHECKOUT_PATH": "[CLEANUP]/tmp_tmp_1/flutter sdk"
+      "SDK_CHECKOUT_PATH": "[CLEANUP]/tmp_tmp_1/flutter sdk",
+      "TOKEN_PATH": "[CLEANUP]/tmp_tmp_3"
     },
     "env_prefixes": {
       "PATH": [
         "[CLEANUP]/tmp_tmp_1/flutter sdk/bin",
         "[CLEANUP]/tmp_tmp_1/flutter sdk/bin/cache/dart-sdk/bin",
+        "[CLEANUP]/tmp_tmp_2/vpython",
+        "[CLEANUP]/tmp_tmp_1/flutter sdk/bin",
+        "[CLEANUP]/tmp_tmp_1/flutter sdk/bin/cache/dart-sdk/bin",
         "[CLEANUP]/tmp_tmp_2/vpython"
       ]
     },
diff --git a/recipes/devicelab/devicelab_drone.expected/xcode-devicelab.json b/recipes/devicelab/devicelab_drone.expected/xcode-devicelab.json
index 4da7978..f823354 100644
--- a/recipes/devicelab/devicelab_drone.expected/xcode-devicelab.json
+++ b/recipes/devicelab/devicelab_drone.expected/xcode-devicelab.json
@@ -110,6 +110,17 @@
     ]
   },
   {
+    "cmd": [
+      "git",
+      "log",
+      "--pretty=format:%ct",
+      "-n",
+      "1"
+    ],
+    "cwd": "[CLEANUP]/tmp_tmp_1/flutter sdk",
+    "name": "git commit time"
+  },
+  {
     "cmd": [],
     "name": "Initialize logs"
   },
@@ -681,6 +692,8 @@
     "cmd": [
       "luci-auth",
       "token",
+      "-scopes",
+      "https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/datastore",
       "-lifetime",
       "3m"
     ],
@@ -766,17 +779,22 @@
       "DEPOT_TOOLS": "RECIPE_REPO[depot_tools]",
       "FLUTTER_LOGS_DIR": "[CLEANUP]/flutter_logs_dir",
       "FLUTTER_TEST_OUTPUTS_DIR": "[CLEANUP]/flutter_logs_dir",
+      "GCP_PROJECT": "flutter-infra",
       "LUCI_BRANCH": "",
       "LUCI_CI": "True",
       "LUCI_PR": "",
       "OS": "linux",
       "PUB_CACHE": "[START_DIR]/.pub-cache",
-      "SDK_CHECKOUT_PATH": "[CLEANUP]/tmp_tmp_1/flutter sdk"
+      "SDK_CHECKOUT_PATH": "[CLEANUP]/tmp_tmp_1/flutter sdk",
+      "TOKEN_PATH": "[CLEANUP]/tmp_tmp_3"
     },
     "env_prefixes": {
       "PATH": [
         "[CLEANUP]/tmp_tmp_1/flutter sdk/bin",
         "[CLEANUP]/tmp_tmp_1/flutter sdk/bin/cache/dart-sdk/bin",
+        "[CLEANUP]/tmp_tmp_2/vpython",
+        "[CLEANUP]/tmp_tmp_1/flutter sdk/bin",
+        "[CLEANUP]/tmp_tmp_1/flutter sdk/bin/cache/dart-sdk/bin",
         "[CLEANUP]/tmp_tmp_2/vpython"
       ]
     },
diff --git a/recipes/devicelab/devicelab_drone.py b/recipes/devicelab/devicelab_drone.py
index fb6147c..e654c5a 100644
--- a/recipes/devicelab/devicelab_drone.py
+++ b/recipes/devicelab/devicelab_drone.py
@@ -5,6 +5,7 @@
 from recipe_engine.recipe_api import Property
 
 DEPS = [
+    'fuchsia/git',
     'flutter/bucket_util',
     'flutter/devicelab_osx_sdk',
     'flutter/flutter_deps',
@@ -46,6 +47,15 @@
       api.properties.get('git_url'),
       api.properties.get('git_ref'),
   )
+  with api.context(cwd=flutter_path):
+    commit_time = api.git(
+        'git commit time',
+        'log',
+        '--pretty=format:%ct',
+        '-n',
+        '1',
+        stdout=api.raw_io.output()
+    ).stdout.rstrip()
   env, env_prefixes = api.repo_util.flutter_environment(flutter_path)
   api.logs_util.initialize_logs_collection(env)
   with api.step.nest('Dependencies'):
@@ -117,8 +127,8 @@
           check_flaky(api)
   with api.context(env=env, env_prefixes=env_prefixes, cwd=devicelab_path):
     uploadResults(
-        api, results_path, test_status == 'flaky', git_branch,
-        api.properties.get('buildername')
+        api, env, env_prefixes, results_path, test_status == 'flaky',
+        git_branch, api.properties.get('buildername'), commit_time
     )
     uploadMetricsToCas(api, results_path)
 
@@ -178,7 +188,8 @@
   """
   supported_branches = ['master']
   if api.runtime.is_experimental or api.properties.get(
-      'git_url') or 'staging' in builder_name or git_branch not in supported_branches:
+      'git_url'
+  ) or 'staging' in builder_name or git_branch not in supported_branches:
     return True
   else:
     return False
@@ -186,11 +197,14 @@
 
 def uploadResults(
     api,
+    env,
+    env_prefixes,
     results_path,
     is_test_flaky,
     git_branch,
     builder_name,
-    test_status='Succeeded'
+    commit_time,
+    test_status='Succeeded',
 ):
   """Upload DeviceLab test results to Cocoon.
 
@@ -210,18 +224,29 @@
         '--test-status', test_status
     ])
   else:
-    runner_params.extend(['--results-file', results_path])
+    runner_params.extend([
+        '--results-file', results_path, '--commit-time', commit_time
+    ])
+
   with api.step.nest('Upload metrics'):
     service_account = api.service_account.default()
-    access_token = service_account.get_access_token()
+    access_token = service_account.get_access_token(
+        scopes=[
+            'https://www.googleapis.com/auth/cloud-platform',
+            'https://www.googleapis.com/auth/datastore'
+        ]
+    )
     access_token_path = api.path.mkstemp()
     api.file.write_text(
         "write token", access_token_path, access_token, include_log=False
     )
+    env['TOKEN_PATH'] = access_token_path
+    env['GCP_PROJECT'] = 'flutter-infra'
     runner_params.extend(['--service-account-token-file', access_token_path])
     upload_command = ['dart', 'bin/test_runner.dart', 'upload-metrics']
     upload_command.extend(runner_params)
-    api.step('upload results', upload_command, infra_step=True)
+    with api.context(env=env, env_prefixes=env_prefixes):
+      api.step('upload results', upload_command, infra_step=True)
 
 
 def uploadMetricsToCas(api, results_path):
@@ -267,9 +292,7 @@
           task_name='abc',
           dependencies=[{'dependency': 'xcode'}]
       ), api.repo_util.flutter_environment_data(checkout_dir=checkout_path),
-      api.buildbucket.ci_build(
-          git_ref='refs/heads/master',
-      ),
+      api.buildbucket.ci_build(git_ref='refs/heads/master',),
       api.step_data(
           'run abc',
           stdout=api.raw_io.output_text('#flaky\nthis is a flaky\nflaky: true'),
@@ -283,9 +306,7 @@
           task_name='abc',
           dependencies=[{'dependency': 'xcode'}]
       ),
-      api.buildbucket.ci_build(
-          git_ref='refs/heads/master',
-      ),
+      api.buildbucket.ci_build(git_ref='refs/heads/master',),
       api.repo_util.flutter_environment_data(checkout_dir=checkout_path),
   )
   yield api.test(
@@ -299,9 +320,7 @@
           stdout=api.raw_io.output_text('#flaky\nthis is a flaky\nflaky: true'),
           retcode=0
       ),
-      api.buildbucket.ci_build(
-          git_ref='refs/heads/master',
-      ),
+      api.buildbucket.ci_build(git_ref='refs/heads/master',),
       api.platform.name('win'),
   )
   yield api.test(
@@ -313,9 +332,7 @@
           upload_metrics=True,
           upload_metrics_to_cas=True,
       ), api.repo_util.flutter_environment_data(checkout_dir=checkout_path),
-      api.buildbucket.ci_build(
-          git_ref='refs/heads/master',
-      )
+      api.buildbucket.ci_build(git_ref='refs/heads/master',)
   )
   yield api.test(
       "local-engine",
diff --git a/recipes/engine/engine_metrics.expected/basic.json b/recipes/engine/engine_metrics.expected/basic.json
index 26c461d..711a4f4 100644
--- a/recipes/engine/engine_metrics.expected/basic.json
+++ b/recipes/engine/engine_metrics.expected/basic.json
@@ -715,7 +715,7 @@
     "env": {
       "ANDROID_HOME": "[CACHE]/builder/src/third_party/android_tools/sdk",
       "FLUTTER_PREBUILT_DART_SDK": "True",
-      "GCP_PROJECT": "flutter-cirrus",
+      "GCP_PROJECT": "flutter-infra",
       "TOKEN_PATH": "[CLEANUP]/tmp_tmp_1"
     },
     "env_prefixes": {
diff --git a/recipes/engine/engine_metrics.py b/recipes/engine/engine_metrics.py
index 494155f..16cb8a4 100644
--- a/recipes/engine/engine_metrics.py
+++ b/recipes/engine/engine_metrics.py
@@ -73,7 +73,7 @@
       'write token', access_token_path, access_token, include_log=False
   )
   env['TOKEN_PATH'] = access_token_path
-  env['GCP_PROJECT'] = 'flutter-cirrus'
+  env['GCP_PROJECT'] = 'flutter-infra'
   with api.context(env=env, env_prefixes=env_prefixes, cwd=benchmark_path):
     api.step('Upload metrics', ['bash', script_path])