Inject correct git_ref and git_url to devicelab_test_drone

This fixes: https://ci.chromium.org/ui/p/flutter/builders/prod/Linux_build_test%20flutter_gallery__transition_perf/10/overview

Change-Id: Ic18b15bdc405f2017d287f1c6fbdad52f4ae18e6
Bug: https://github.com/flutter/flutter/issues/103542
Reviewed-on: https://flutter-review.googlesource.com/c/recipes/+/33300
Reviewed-by: Casey Hillers <chillers@google.com>
Commit-Queue: Keyong Han <keyonghan@google.com>
diff --git a/recipe_modules/repo_util/api.py b/recipe_modules/repo_util/api.py
index 7a2946e..7eaf9fc 100644
--- a/recipe_modules/repo_util/api.py
+++ b/recipe_modules/repo_util/api.py
@@ -260,13 +260,20 @@
         timeout=300,
     )
 
-  def get_env_commit(self):
-    '''Get the commit sha of the current repo from env.'''
+  def get_env_ref(self):
+    '''Get the ref of the current build from env.'''
     gitiles_commit = self.m.buildbucket.gitiles_commit.id
     if gitiles_commit:
       return gitiles_commit
     return self.m.properties.get('git_ref', 'led')
 
+  def get_env_url(self, repo):
+    '''Get the url of the current build from env.'''
+    git_url = REPOS[repo]
+    if 'git_url' in self.m.properties:
+      git_url = self.m.properties['git_url']
+    return git_url
+
   def current_commit_branches(self, checkout_path):
     """Gets the list of branches for the current commit."""
     with self.m.step.nest('Identify branches'):
diff --git a/recipe_modules/repo_util/examples/full.expected/monorepo_wrong_host.json b/recipe_modules/repo_util/examples/full.expected/monorepo_wrong_host.json
index c5a0665..2720218 100644
--- a/recipe_modules/repo_util/examples/full.expected/monorepo_wrong_host.json
+++ b/recipe_modules/repo_util/examples/full.expected/monorepo_wrong_host.json
@@ -1024,7 +1024,7 @@
       "The recipe has crashed at point 'Uncaught exception'!",
       "",
       "Traceback (most recent call last):",
-      "  File \"RECIPE_REPO[flutter]/recipe_modules/repo_util/examples/full.py\", line 42, in RunSteps",
+      "  File \"RECIPE_REPO[flutter]/recipe_modules/repo_util/examples/full.py\", line 43, in RunSteps",
       "    api.repo_util.monorepo_checkout(api.path['start_dir'], {}, {})",
       "  File \"RECIPE_REPO[flutter]/recipe_modules/repo_util/api.py\", line 127, in monorepo_checkout",
       "    raise ValueError(",
diff --git a/recipe_modules/repo_util/examples/full.py b/recipe_modules/repo_util/examples/full.py
index b993058..4241f31 100644
--- a/recipe_modules/repo_util/examples/full.py
+++ b/recipe_modules/repo_util/examples/full.py
@@ -33,7 +33,8 @@
       'flutter', flutter_checkout_path, ref='refs/heads/beta'
   )
   api.repo_util.run_flutter_doctor()
-  api.repo_util.get_env_commit()
+  api.repo_util.get_env_ref()
+  api.repo_util.get_env_url('flutter')
 
   env, env_paths = api.repo_util.engine_environment(flutter_checkout_path)
   env, env_paths = api.repo_util.flutter_environment(flutter_checkout_path)
diff --git a/recipes/devicelab/devicelab_drone_build_test.expected/artifact does not exist.json b/recipes/devicelab/devicelab_drone_build_test.expected/artifact does not exist.json
index 7f863ac..3e72b2c 100644
--- a/recipes/devicelab/devicelab_drone_build_test.expected/artifact does not exist.json
+++ b/recipes/devicelab/devicelab_drone_build_test.expected/artifact does not exist.json
@@ -809,7 +809,7 @@
       }
     },
     "name": "buildbucket.schedule",
-    "stdin": "{\"requests\": [{\"scheduleBuild\": {\"builder\": {\"bucket\": \"ci\", \"builder\": \"Linux Devicelab Test Drone\", \"project\": \"test\"}, \"exe\": {\"cipdVersion\": \"refs/heads/master\"}, \"experimental\": \"NO\", \"experiments\": {\"luci.buildbucket.parent_tracking\": false}, \"fields\": \"builder,createTime,createdBy,critical,endTime,id,infra,input,number,output,startTime,status,updateTime\", \"gitilesCommit\": {\"host\": \"git.example.com\", \"id\": \"2d72510e447ab60a9728aeea2362d8be2cbd7789\", \"project\": \"test/repo\", \"ref\": \"refs/heads/main\"}, \"priority\": 25, \"properties\": {\"artifact\": \"def\", \"dependencies\": [], \"git_ref\": \"refs/pull/1/head\", \"git_url\": null, \"task_name\": \"abc\"}, \"requestId\": \"8945511751514863184-00000000-0000-0000-0000-000000001337\", \"swarming\": {\"parentRunId\": \"fake-task-id\"}, \"tags\": [{\"key\": \"parent_buildbucket_id\", \"value\": \"8945511751514863184\"}, {\"key\": \"user_agent\", \"value\": \"recipe\"}]}}]}",
+    "stdin": "{\"requests\": [{\"scheduleBuild\": {\"builder\": {\"bucket\": \"ci\", \"builder\": \"Linux Devicelab Test Drone\", \"project\": \"test\"}, \"exe\": {\"cipdVersion\": \"refs/heads/master\"}, \"experimental\": \"NO\", \"experiments\": {\"luci.buildbucket.parent_tracking\": false}, \"fields\": \"builder,createTime,createdBy,critical,endTime,id,infra,input,number,output,startTime,status,updateTime\", \"gitilesCommit\": {\"host\": \"git.example.com\", \"id\": \"2d72510e447ab60a9728aeea2362d8be2cbd7789\", \"project\": \"test/repo\", \"ref\": \"refs/heads/main\"}, \"priority\": 25, \"properties\": {\"artifact\": \"def\", \"dependencies\": [], \"git_ref\": \"2d72510e447ab60a9728aeea2362d8be2cbd7789\", \"git_url\": \"https://flutter.googlesource.com/mirrors/flutter\", \"task_name\": \"abc\"}, \"requestId\": \"8945511751514863184-00000000-0000-0000-0000-000000001337\", \"swarming\": {\"parentRunId\": \"fake-task-id\"}, \"tags\": [{\"key\": \"parent_buildbucket_id\", \"value\": \"8945511751514863184\"}, {\"key\": \"user_agent\", \"value\": \"recipe\"}]}}]}",
     "~followup_annotations": [
       "@@@STEP_LOG_LINE@json.output@{@@@",
       "@@@STEP_LOG_LINE@json.output@  \"responses\": [@@@",
@@ -853,8 +853,8 @@
       "@@@STEP_LOG_LINE@request@        \"properties\": {@@@",
       "@@@STEP_LOG_LINE@request@          \"artifact\": \"def\", @@@",
       "@@@STEP_LOG_LINE@request@          \"dependencies\": [], @@@",
-      "@@@STEP_LOG_LINE@request@          \"git_ref\": \"refs/pull/1/head\", @@@",
-      "@@@STEP_LOG_LINE@request@          \"git_url\": null, @@@",
+      "@@@STEP_LOG_LINE@request@          \"git_ref\": \"2d72510e447ab60a9728aeea2362d8be2cbd7789\", @@@",
+      "@@@STEP_LOG_LINE@request@          \"git_url\": \"https://flutter.googlesource.com/mirrors/flutter\", @@@",
       "@@@STEP_LOG_LINE@request@          \"task_name\": \"abc\"@@@",
       "@@@STEP_LOG_LINE@request@        }, @@@",
       "@@@STEP_LOG_LINE@request@        \"requestId\": \"8945511751514863184-00000000-0000-0000-0000-000000001337\", @@@",
diff --git a/recipes/devicelab/devicelab_drone_build_test.expected/artifact exists.json b/recipes/devicelab/devicelab_drone_build_test.expected/artifact exists.json
index 988c94e..eedfce6 100644
--- a/recipes/devicelab/devicelab_drone_build_test.expected/artifact exists.json
+++ b/recipes/devicelab/devicelab_drone_build_test.expected/artifact exists.json
@@ -34,7 +34,7 @@
     ],
     "infra_step": true,
     "name": "buildbucket.schedule",
-    "stdin": "{\"requests\": [{\"scheduleBuild\": {\"builder\": {\"builder\": \"Linux Devicelab Test Drone\"}, \"exe\": {\"cipdVersion\": \"refs/heads/master\"}, \"experimental\": \"NO\", \"experiments\": {\"luci.buildbucket.parent_tracking\": false}, \"fields\": \"builder,createTime,createdBy,critical,endTime,id,infra,input,number,output,startTime,status,updateTime\", \"priority\": 25, \"properties\": {\"artifact\": \"def\", \"dependencies\": [], \"git_ref\": \"refs/pull/1/head\", \"git_url\": null, \"task_name\": \"abc\"}, \"requestId\": \"0-00000000-0000-0000-0000-000000001337\", \"swarming\": {\"parentRunId\": \"fake-task-id\"}, \"tags\": [{\"key\": \"parent_buildbucket_id\", \"value\": \"0\"}, {\"key\": \"user_agent\", \"value\": \"recipe\"}]}}]}",
+    "stdin": "{\"requests\": [{\"scheduleBuild\": {\"builder\": {\"builder\": \"Linux Devicelab Test Drone\"}, \"exe\": {\"cipdVersion\": \"refs/heads/master\"}, \"experimental\": \"NO\", \"experiments\": {\"luci.buildbucket.parent_tracking\": false}, \"fields\": \"builder,createTime,createdBy,critical,endTime,id,infra,input,number,output,startTime,status,updateTime\", \"priority\": 25, \"properties\": {\"artifact\": \"def\", \"dependencies\": [], \"git_ref\": \"refs/pull/1/head\", \"git_url\": \"test/repo\", \"task_name\": \"abc\"}, \"requestId\": \"0-00000000-0000-0000-0000-000000001337\", \"swarming\": {\"parentRunId\": \"fake-task-id\"}, \"tags\": [{\"key\": \"parent_buildbucket_id\", \"value\": \"0\"}, {\"key\": \"user_agent\", \"value\": \"recipe\"}]}}]}",
     "~followup_annotations": [
       "@@@STEP_LOG_LINE@json.output@{@@@",
       "@@@STEP_LOG_LINE@json.output@  \"responses\": [@@@",
@@ -69,7 +69,7 @@
       "@@@STEP_LOG_LINE@request@          \"artifact\": \"def\", @@@",
       "@@@STEP_LOG_LINE@request@          \"dependencies\": [], @@@",
       "@@@STEP_LOG_LINE@request@          \"git_ref\": \"refs/pull/1/head\", @@@",
-      "@@@STEP_LOG_LINE@request@          \"git_url\": null, @@@",
+      "@@@STEP_LOG_LINE@request@          \"git_url\": \"test/repo\", @@@",
       "@@@STEP_LOG_LINE@request@          \"task_name\": \"abc\"@@@",
       "@@@STEP_LOG_LINE@request@        }, @@@",
       "@@@STEP_LOG_LINE@request@        \"requestId\": \"0-00000000-0000-0000-0000-000000001337\", @@@",
diff --git a/recipes/devicelab/devicelab_drone_build_test.expected/local-engine.json b/recipes/devicelab/devicelab_drone_build_test.expected/local-engine.json
index 73e6645..6606796 100644
--- a/recipes/devicelab/devicelab_drone_build_test.expected/local-engine.json
+++ b/recipes/devicelab/devicelab_drone_build_test.expected/local-engine.json
@@ -980,7 +980,7 @@
       }
     },
     "name": "buildbucket.schedule",
-    "stdin": "{\"requests\": [{\"scheduleBuild\": {\"builder\": {\"bucket\": \"ci\", \"builder\": \"Linux Devicelab Test Drone\", \"project\": \"test\"}, \"exe\": {\"cipdVersion\": \"refs/heads/master\"}, \"experimental\": \"NO\", \"experiments\": {\"luci.buildbucket.parent_tracking\": false}, \"fields\": \"builder,createTime,createdBy,critical,endTime,id,infra,input,number,output,startTime,status,updateTime\", \"gitilesCommit\": {\"host\": \"git.example.com\", \"id\": \"2d72510e447ab60a9728aeea2362d8be2cbd7789\", \"project\": \"test/repo\", \"ref\": \"refs/heads/master\"}, \"priority\": 25, \"properties\": {\"artifact\": \"def\", \"dependencies\": [], \"git_ref\": null, \"git_url\": null, \"task_name\": \"abc\"}, \"requestId\": \"8945511751514863184-00000000-0000-0000-0000-000000001337\", \"swarming\": {\"parentRunId\": \"fake-task-id\"}, \"tags\": [{\"key\": \"parent_buildbucket_id\", \"value\": \"8945511751514863184\"}, {\"key\": \"user_agent\", \"value\": \"recipe\"}]}}]}",
+    "stdin": "{\"requests\": [{\"scheduleBuild\": {\"builder\": {\"bucket\": \"ci\", \"builder\": \"Linux Devicelab Test Drone\", \"project\": \"test\"}, \"exe\": {\"cipdVersion\": \"refs/heads/master\"}, \"experimental\": \"NO\", \"experiments\": {\"luci.buildbucket.parent_tracking\": false}, \"fields\": \"builder,createTime,createdBy,critical,endTime,id,infra,input,number,output,startTime,status,updateTime\", \"gitilesCommit\": {\"host\": \"git.example.com\", \"id\": \"2d72510e447ab60a9728aeea2362d8be2cbd7789\", \"project\": \"test/repo\", \"ref\": \"refs/heads/master\"}, \"priority\": 25, \"properties\": {\"artifact\": \"def\", \"dependencies\": [], \"git_ref\": \"2d72510e447ab60a9728aeea2362d8be2cbd7789\", \"git_url\": \"https://flutter.googlesource.com/mirrors/flutter\", \"task_name\": \"abc\"}, \"requestId\": \"8945511751514863184-00000000-0000-0000-0000-000000001337\", \"swarming\": {\"parentRunId\": \"fake-task-id\"}, \"tags\": [{\"key\": \"parent_buildbucket_id\", \"value\": \"8945511751514863184\"}, {\"key\": \"user_agent\", \"value\": \"recipe\"}]}}]}",
     "~followup_annotations": [
       "@@@STEP_LOG_LINE@json.output@{@@@",
       "@@@STEP_LOG_LINE@json.output@  \"responses\": [@@@",
@@ -1024,8 +1024,8 @@
       "@@@STEP_LOG_LINE@request@        \"properties\": {@@@",
       "@@@STEP_LOG_LINE@request@          \"artifact\": \"def\", @@@",
       "@@@STEP_LOG_LINE@request@          \"dependencies\": [], @@@",
-      "@@@STEP_LOG_LINE@request@          \"git_ref\": null, @@@",
-      "@@@STEP_LOG_LINE@request@          \"git_url\": null, @@@",
+      "@@@STEP_LOG_LINE@request@          \"git_ref\": \"2d72510e447ab60a9728aeea2362d8be2cbd7789\", @@@",
+      "@@@STEP_LOG_LINE@request@          \"git_url\": \"https://flutter.googlesource.com/mirrors/flutter\", @@@",
       "@@@STEP_LOG_LINE@request@          \"task_name\": \"abc\"@@@",
       "@@@STEP_LOG_LINE@request@        }, @@@",
       "@@@STEP_LOG_LINE@request@        \"requestId\": \"8945511751514863184-00000000-0000-0000-0000-000000001337\", @@@",
diff --git a/recipes/devicelab/devicelab_drone_build_test.py b/recipes/devicelab/devicelab_drone_build_test.py
index 40fa388..4d64b79 100644
--- a/recipes/devicelab/devicelab_drone_build_test.py
+++ b/recipes/devicelab/devicelab_drone_build_test.py
@@ -61,7 +61,7 @@
   if not task_name:
     raise ValueError('A task_name property is required')
 
-  commit_sha = api.repo_util.get_env_commit()
+  commit_sha = api.repo_util.get_env_ref()
   artifact = api.properties.get('artifact', None)
   if not artifact:
     raise ValueError('An artifact property is required')
@@ -99,8 +99,8 @@
       'dependencies': [api.shard_util_v2.unfreeze_dict(dep) for dep in deps],
       'task_name': task_name,
       'artifact': artifact,
-      'git_ref': api.properties.get('git_ref'),
-      'git_url': api.properties.get('git_url'),
+      'git_ref': api.repo_util.get_env_ref(),
+      'git_url': api.repo_util.get_env_url('flutter'),
   }
 
   req = api.buildbucket.schedule_request(
@@ -214,7 +214,8 @@
           task_name='abc',
           git_branch='master',
           artifact='def',
-          git_ref='refs/pull/1/head'
+          git_ref='refs/pull/1/head',
+          git_url='test/repo'
       ),
       api.repo_util.flutter_environment_data(checkout_dir=checkout_path),
       api.step_data(
diff --git a/recipes/devicelab/devicelab_test_drone.py b/recipes/devicelab/devicelab_test_drone.py
index deaeba5..93607f1 100644
--- a/recipes/devicelab/devicelab_test_drone.py
+++ b/recipes/devicelab/devicelab_test_drone.py
@@ -137,7 +137,7 @@
 
 def download_artifact(api, artifact, artifact_destination_dir):
   '''Download pre-build artifact.'''
-  commit_sha = api.repo_util.get_env_commit()
+  commit_sha = api.repo_util.get_env_ref()
   bucket = api.buildbucket.build.builder.bucket
   artifact_gcs_dir = 'flutter/%s/%s' % (bucket, commit_sha)
   artifact_gcs_path = '%s/%s' % (artifact_gcs_dir, artifact)