[run_script] Move CIPD steps to a helper function

Small refactor of the script to isolate CIPD logic, in preparation for
introducing new functionality into this script and avoiding clutter in
the top-level RunSteps function.

Bug: 75278
Change-Id: I0108eeff2c5b723d057d814075a2b34c49ee9861
Reviewed-on: https://fuchsia-review.googlesource.com/c/infra/recipes/+/519844
Fuchsia-Auto-Submit: Anthony Fandrianto <atyfto@google.com>
Reviewed-by: Oliver Newman <olivernewman@google.com>
Commit-Queue: Anthony Fandrianto <atyfto@google.com>
diff --git a/recipes/run_script.py b/recipes/run_script.py
index 8248ebc..09075ed 100644
--- a/recipes/run_script.py
+++ b/recipes/run_script.py
@@ -24,7 +24,6 @@
 
 
 def RunSteps(api, props):
-    bb_input = api.buildbucket.build.input
     checkout_dir = api.path["start_dir"].join("checkout")
     with api.step.nest("checkout"), api.context(infra_steps=True):
         if props.checkout_with_repo:
@@ -50,7 +49,9 @@
             # Script must emit a newline-separated txt file of CIPD yaml paths,
             # e.g. "/path/to/foo.yaml\n/path/to/bar.yaml" relative to the
             # working dir i.e. the checkout dir.
-            assert bb_input.gitiles_commit.project, "we should only be uploading in CI"
+            assert (
+                api.buildbucket.build.input.gitiles_commit.project
+            ), "we should only be uploading in CI"
             cipd_yaml_manifest = api.path.mkstemp("cipd_manifest")
             script_args += ["--cipd-yaml-manifest", cipd_yaml_manifest]
 
@@ -61,28 +62,42 @@
             api.python(step_name, props.script, script_args, venv=True)
         else:
             api.step(step_name, [props.script] + script_args)
-
         if props.upload_to_cipd:
-            # Create package(s) from the script's emitted CIPD yaml(s).
-            cipd_yaml_paths = (
-                api.file.read_text(
-                    "read CIPD yaml manifest",
-                    cipd_yaml_manifest,
-                    test_data="path/to/foo.yaml\npath/to/bar.yaml\n",
-                )
-                .rstrip()
-                .split("\n")
+            upload_to_cipd(
+                api,
+                checkout_dir,
+                cipd_yaml_manifest,
+                set_repo_tags=props.set_repo_tags,
             )
-            if props.set_repo_tags:
-                tags = api.repo.snapshot("repo snapshot", path=checkout_dir)
-            else:
-                tags = {"git_revision": bb_input.gitiles_commit.id}
-            for cipd_yaml_path in cipd_yaml_paths:
-                api.cipd.create_from_yaml(
-                    checkout_dir.join(cipd_yaml_path),
-                    refs=["latest"],
-                    tags=tags,
-                )
+
+
+def upload_to_cipd(api, checkout_dir, cipd_yaml_manifest, set_repo_tags=False):
+    """Upload package(s) to CIPD from the script-generated CIPD .yaml manifest.
+
+    Args:
+        cipd_yaml_manifest (Path): Path to CIPD .yaml manifest.
+        set_repo_tags (bool): If True, set CIPD tags based on a repo snapshot.
+          Otherwise, set CIPD tags based on the input gitiles commit.
+    """
+    cipd_yaml_paths = (
+        api.file.read_text(
+            "read CIPD yaml manifest",
+            cipd_yaml_manifest,
+            test_data="path/to/foo.yaml\npath/to/bar.yaml\n",
+        )
+        .rstrip()
+        .split("\n")
+    )
+    if set_repo_tags:
+        tags = api.repo.snapshot("repo snapshot", path=checkout_dir)
+    else:
+        tags = {"git_revision": api.buildbucket.build.input.gitiles_commit.id}
+    for cipd_yaml_path in cipd_yaml_paths:
+        api.cipd.create_from_yaml(
+            checkout_dir.join(cipd_yaml_path),
+            refs=["latest"],
+            tags=tags,
+        )
 
 
 def GenTests(api):