Repository: SFDO-Tooling/CumulusCI
Pull request #3684 (2023-10-18 06:24:47)

metadeploy_publish presents clear errors when plans are not available


Patch:
diff --git a/cumulusci/tasks/metadeploy.py b/cumulusci/tasks/metadeploy.py
index e55154422..53f87c387 100644
--- a/cumulusci/tasks/metadeploy.py
+++ b/cumulusci/tasks/metadeploy.py
@@ -83,12 +83,21 @@ class Publish(BaseMetaDeployTask):
         Path(self.labels_path).mkdir(parents=True, exist_ok=True)
 
         if plan_name := self.options.get("plan"):
+            if self.project_config.lookup(f"plans__{plan_name}") is None:
+                raise TaskOptionsError(
+                    f"Plan {plan_name} not found in project configuration"
+                )
             plan_configs = {
                 plan_name: self.project_config.lookup(f"plans__{plan_name}")
             }
             self.plan_configs = plan_configs
         else:
             self.plan_configs = self.project_config.plans
+        # Handled exception for no plan
+        if self.plan_configs is None or len(self.plan_configs) == 0:
+            raise CumulusCIException(
+                "No plan found to publish in project configuration"
+            )
 
         self._load_labels()
 
@@ -107,7 +116,6 @@ class Publish(BaseMetaDeployTask):
             raise CumulusCIException(
                 f"No slug found in MetaDeploy for product {product} from {repo_url}"
             )
-
         if not self.dry_run:
             version = self._find_or_create_version(product)
             if self.labels_path and "slug" in product:
@@ -138,7 +146,6 @@ class Publish(BaseMetaDeployTask):
             )
             project_config.set_keychain(self.project_config.keychain)
 
-            # Create each plan
             for plan_name, plan_config in self.plan_configs.items():
                 self._add_plan_labels(
                     plan_name=plan_name,


Test patch:
diff --git a/cumulusci/tasks/tests/test_metadeploy.py b/cumulusci/tasks/tests/test_metadeploy.py
index 785dba131..bd39b62a3 100644
--- a/cumulusci/tasks/tests/test_metadeploy.py
+++ b/cumulusci/tasks/tests/test_metadeploy.py
@@ -439,6 +439,14 @@ class TestPublish(GithubApiTestMixin):
         )
         project_config = create_project_config()
         project_config.config["project"]["git"]["repo_url"] = "EXISTING_REPO"
+        project_config.config["plans"] = {
+            "install": {
+                "title": "Test Install",
+                "slug": "install",
+                "tier": "primary",
+                "steps": {1: {"flow": "install_prod"}},
+            }
+        }
         project_config.keychain.set_service(
             "metadeploy",
             "test_alias",
@@ -485,6 +493,37 @@ class TestPublish(GithubApiTestMixin):
         task._init_task()
         assert expected_plans == task.plan_configs
 
+    @pytest.mark.parametrize(
+        "options, errortype,errormsg",
+        [
+            (
+                {"tag": "release/1.0"},
+                CumulusCIException,
+                "No plan found to publish in project configuration",
+            ),
+            (
+                {"tag": "release/1.0", "plan": "install"},
+                TaskOptionsError,
+                "Plan install not found in project configuration",
+            ),
+        ],
+    )
+    def test_init_task_no_plan(self, options, errortype, errormsg):
+        project_config = create_project_config()
+        project_config.config["project"]["git"]["repo_url"] = "EXISTING_REPO"
+        project_config.keychain.set_service(
+            "metadeploy",
+            "test_alias",
+            ServiceConfig({"url": "https://metadeploy", "token": "TOKEN"}),
+        )
+        task_config = TaskConfig({"options": options})
+        task = Publish(project_config, task_config)
+        with pytest.raises(
+            errortype,
+            match=errormsg,
+        ):
+            task._init_task()
+
     @responses.activate
     def test_find_or_create_plan_template__not_found(self):
         responses.add(
@@ -540,6 +579,9 @@ class TestPublish(GithubApiTestMixin):
             "tier": "primary",
             "steps": {1: {"task": "None"}},
         }
+        project_config.config["plans"] = {
+            "Test Install": plan_config,
+        }
         task_config = TaskConfig({"options": {"tag": "release/1.0"}})
         task = Publish(project_config, task_config)
         task._init_task()