[monorepo] Use try build identifier from input property on try subbuilds

Bug: b/298166258
Change-Id: I08f6a5c192e9971cad914b8230ac2277f9d8bfec
Reviewed-on: https://flutter-review.googlesource.com/c/recipes/+/50400
Commit-Queue: William Hesse <whesse@google.com>
Reviewed-by: Ricardo Amador <ricardoamador@google.com>
diff --git a/recipe_modules/archives/api.py b/recipe_modules/archives/api.py
index cd4715f..3ca5847 100644
--- a/recipe_modules/archives/api.py
+++ b/recipe_modules/archives/api.py
@@ -168,7 +168,7 @@
     # Calculate prefix and commit.
     file_list = self._full_path_list(checkout, archive_config)
     if self.m.monorepo.is_monorepo_try_build:
-      commit = self.m.properties.get('try_build_identifier')
+      commit = self.m.monorepo.try_build_identifier
       bucket = MONOREPO_TRY_BUCKET
     elif self.m.monorepo.is_monorepo_ci_build:
       commit = self.m.repo_util.get_commit(checkout.join('../../monorepo'))
diff --git a/recipe_modules/monorepo/__init__.py b/recipe_modules/monorepo/__init__.py
index 1fd0b55..3944dde 100644
--- a/recipe_modules/monorepo/__init__.py
+++ b/recipe_modules/monorepo/__init__.py
@@ -1,4 +1,5 @@
 DEPS = [
     'recipe_engine/buildbucket',
+    'recipe_engine/properties',
     'recipe_engine/step',
 ]
diff --git a/recipe_modules/monorepo/api.py b/recipe_modules/monorepo/api.py
index cf84314..69f24e2 100644
--- a/recipe_modules/monorepo/api.py
+++ b/recipe_modules/monorepo/api.py
@@ -29,6 +29,10 @@
   def try_build_identifier(self):
     """Creates a unique identifier to use as an upload path for artifacts.
 
+    If this is a subbuild spawned by an engine_v2 coordinator, use
+    the try_build_identifier input property passed to this build by the
+    coordinator.
+
     There is no commit hash usable for this, because a Gerrit try job
     patches the monorepo HEAD with an uncommitted patch from the Gerrit CL.
     The flutter framework's bin/internal/engine.version can be any string,
@@ -38,8 +42,12 @@
       none
 
     Returns:
-      The buildbucket id of the engine_v2/engine_v2 coordinator build.
+      The buildbucket id of the engine_v2/engine_v2 coordinator build or
+      the try_build_identifer input property passed to this build.
 """
+    parent_identifier = self.m.properties.get('try_build_identifier')
+    if (parent_identifier):
+      return str(parent_identifier)
     buildbucket_id = self.m.buildbucket.build.id
     if buildbucket_id:
       self.m.step.empty('get buildbucket id', step_text=str(buildbucket_id))
diff --git a/recipe_modules/monorepo/tests/test_monorepo.expected/monorepo_try_build_no_builder_id.json b/recipe_modules/monorepo/tests/test_monorepo.expected/monorepo_try_build_no_builder_id.json
index ead62fc..affd5c7 100644
--- a/recipe_modules/monorepo/tests/test_monorepo.expected/monorepo_try_build_no_builder_id.json
+++ b/recipe_modules/monorepo/tests/test_monorepo.expected/monorepo_try_build_no_builder_id.json
@@ -11,9 +11,9 @@
       "The recipe has crashed at point 'Uncaught exception'!",
       "",
       "Traceback (most recent call last):",
-      "  File \"RECIPE_REPO[flutter]/recipe_modules/monorepo/tests/test_monorepo.py\", line 16, in RunSteps",
+      "  File \"RECIPE_REPO[flutter]/recipe_modules/monorepo/tests/test_monorepo.py\", line 17, in RunSteps",
       "    try_build_identifier = api.monorepo.try_build_identifier",
-      "  File \"RECIPE_REPO[flutter]/recipe_modules/monorepo/api.py\", line 47, in try_build_identifier",
+      "  File \"RECIPE_REPO[flutter]/recipe_modules/monorepo/api.py\", line 55, in try_build_identifier",
       "    self.m.step.empty(",
       "  File \"RECIPE_REPO[recipe_engine]/recipe_modules/step/api.py\", in empty",
       "    ret.presentation.status = status",
diff --git a/recipe_modules/monorepo/tests/test_monorepo.expected/monorepo_try_subbuild.json b/recipe_modules/monorepo/tests/test_monorepo.expected/monorepo_try_subbuild.json
new file mode 100644
index 0000000..fbaeb0b
--- /dev/null
+++ b/recipe_modules/monorepo/tests/test_monorepo.expected/monorepo_try_subbuild.json
@@ -0,0 +1,14 @@
+[
+  {
+    "cmd": [],
+    "name": "test",
+    "~followup_annotations": [
+      "@@@SET_BUILD_PROPERTY@is_ci_build@false@@@",
+      "@@@SET_BUILD_PROPERTY@is_try_build@true@@@",
+      "@@@SET_BUILD_PROPERTY@try_build_identifier@\"81123491\"@@@"
+    ]
+  },
+  {
+    "name": "$result"
+  }
+]
\ No newline at end of file
diff --git a/recipe_modules/monorepo/tests/test_monorepo.py b/recipe_modules/monorepo/tests/test_monorepo.py
index 433306f..4e43282 100644
--- a/recipe_modules/monorepo/tests/test_monorepo.py
+++ b/recipe_modules/monorepo/tests/test_monorepo.py
@@ -5,6 +5,7 @@
 DEPS = [
     'flutter/monorepo',
     'recipe_engine/buildbucket',
+    'recipe_engine/properties',
     'recipe_engine/step',
 ]
 
@@ -27,6 +28,12 @@
   yield api.test('monorepo_try_build', api.monorepo.try_build())
 
   yield api.test(
+      'monorepo_try_subbuild',
+      api.monorepo.try_build(),
+      api.properties(try_build_identifier='81123491'),
+  )
+
+  yield api.test(
       'monorepo_try_build_no_builder_id',
       api.monorepo.try_build(build_id=0),
       api.expect_exception('AssertionError'),
diff --git a/recipe_modules/shard_util_v2/examples/full.expected/monorepo_try_led_without_builder_id.json b/recipe_modules/shard_util_v2/examples/full.expected/monorepo_try_led_without_builder_id.json
index bd70c2d..c5dad83 100644
--- a/recipe_modules/shard_util_v2/examples/full.expected/monorepo_try_led_without_builder_id.json
+++ b/recipe_modules/shard_util_v2/examples/full.expected/monorepo_try_led_without_builder_id.json
@@ -29,7 +29,7 @@
       "    builds = self._schedule_with_led(build_list)",
       "  File \"RECIPE_REPO[flutter]/recipe_modules/shard_util_v2/api.py\", line 233, in _schedule_with_led",
       "    ] = self.m.monorepo.try_build_identifier",
-      "  File \"RECIPE_REPO[flutter]/recipe_modules/monorepo/api.py\", line 47, in try_build_identifier",
+      "  File \"RECIPE_REPO[flutter]/recipe_modules/monorepo/api.py\", line 55, in try_build_identifier",
       "    self.m.step.empty(",
       "  File \"RECIPE_REPO[recipe_engine]/recipe_modules/step/api.py\", in empty",
       "    ret.presentation.status = status",
diff --git a/recipes/engine_v2/builder.expected/monorepo_tryjob.json b/recipes/engine_v2/builder.expected/monorepo_tryjob.json
index b87e982..0e35958 100644
--- a/recipes/engine_v2/builder.expected/monorepo_tryjob.json
+++ b/recipes/engine_v2/builder.expected/monorepo_tryjob.json
@@ -395,13 +395,6 @@
   },
   {
     "cmd": [],
-    "name": "get buildbucket id",
-    "~followup_annotations": [
-      "@@@STEP_TEXT@8945511751514863184@@@"
-    ]
-  },
-  {
-    "cmd": [],
     "name": "ensure goma"
   },
   {
@@ -739,7 +732,7 @@
       "python3",
       "[CACHE]/builder/engine/src/flutter/tools/gn",
       "--ios",
-      "--gn-args=engine_version=\"8945511751514863184\""
+      "--gn-args=engine_version=\"81123491\""
     ],
     "env": {
       "ANDROID_HOME": "[CACHE]/builder/engine/src/third_party/android_tools/sdk",
@@ -784,7 +777,7 @@
         "hostname": "rdbhost"
       }
     },
-    "name": "gn --ios --gn-args=engine_version=\"8945511751514863184\""
+    "name": "gn --ios --gn-args=engine_version=\"81123491\""
   },
   {
     "cmd": [],
@@ -2494,7 +2487,7 @@
       "ensure-directory",
       "--mode",
       "0777",
-      "[CLEANUP]/tmp_tmp_3/monorepo_try/flutter_infra_release/flutter/android-x86-jit-release"
+      "[CLEANUP]/tmp_tmp_3/monorepo_try/flutter_infra_release/flutter/81123491/android-x86-jit-release"
     ],
     "cwd": "[CACHE]/builder/engine/src/flutter",
     "env": {
@@ -2538,7 +2531,7 @@
         "hostname": "rdbhost"
       }
     },
-    "name": "Ensure monorepo_try/flutter_infra_release/flutter/android-x86-jit-release"
+    "name": "Ensure monorepo_try/flutter_infra_release/flutter/81123491/android-x86-jit-release"
   },
   {
     "cmd": [
@@ -2549,7 +2542,7 @@
       "/path/to/tmp/json",
       "copy",
       "[CACHE]/builder/engine/src/out/android_jit_release_x86/zip_archives/android-x86-jit-release/artifacts.zip",
-      "[CLEANUP]/tmp_tmp_3/monorepo_try/flutter_infra_release/flutter/android-x86-jit-release"
+      "[CLEANUP]/tmp_tmp_3/monorepo_try/flutter_infra_release/flutter/81123491/android-x86-jit-release"
     ],
     "cwd": "[CACHE]/builder/engine/src/flutter",
     "env": {
@@ -2650,7 +2643,7 @@
         "hostname": "rdbhost"
       }
     },
-    "name": "gsutil Upload [CACHE]/builder/engine/src/out/android_jit_release_x86/zip_archives/android-x86-jit-release/artifacts.zip to gs://flutter_archives_v2/monorepo_try/flutter_infra_release/flutter/android-x86-jit-release/artifacts.zip",
+    "name": "gsutil Upload [CACHE]/builder/engine/src/out/android_jit_release_x86/zip_archives/android-x86-jit-release/artifacts.zip to gs://flutter_archives_v2/monorepo_try/flutter_infra_release/flutter/81123491/android-x86-jit-release/artifacts.zip",
     "~followup_annotations": [
       "@@@STEP_LINK@gsutil.upload@https://console.cloud.google.com/storage/browser/flutter_archives_v2/@@@"
     ]
diff --git a/recipes/engine_v2/builder.py b/recipes/engine_v2/builder.py
index 417c028..6127d8b 100644
--- a/recipes/engine_v2/builder.py
+++ b/recipes/engine_v2/builder.py
@@ -383,7 +383,9 @@
   )
   yield api.test(
       'monorepo_tryjob',
-      api.properties(build=build, no_goma=True),
+      api.properties(
+          build=build, no_goma=True, try_build_identifier='81123491'
+      ),
       api.monorepo.try_build(),
   )
 
diff --git a/recipes/engine_v2/tester.py b/recipes/engine_v2/tester.py
index a29b218..2cc2e93 100644
--- a/recipes/engine_v2/tester.py
+++ b/recipes/engine_v2/tester.py
@@ -55,7 +55,7 @@
   if api.monorepo.is_monorepo_try_build:
     framework_ref = 'refs/heads/main'
     artifact_url = 'https://storage.googleapis.com/flutter_archives_v2/monorepo_try'
-    engine_version = api.properties.get('try_build_identifier')
+    engine_version = api.monorepo.try_build_identifier
   elif api.monorepo.is_monorepo_ci_build:
     framework_ref = get_monorepo_framework(api)
     artifact_url = 'https://storage.googleapis.com/flutter_archives_v2/monorepo'