Repository: SFDO-Tooling/CumulusCI
Pull request #3723 (2023-12-18 12:38:33)

Retrieve pre-flight checks
[W-14540405](https://gus.lightning.force.com/lightning/r/ADM_Work__c/a07EE00001f2ZmgYAE/view)

Tasks under a particular category are retrieved by taking the group name as an option. So, existing preflight checks can be retrieved using 'Salesforce Preflight Checks' as group name 


Patch:
diff --git a/cumulusci/cumulusci.yml b/cumulusci/cumulusci.yml
index bcdd32f69..9b13e8174 100644
--- a/cumulusci/cumulusci.yml
+++ b/cumulusci/cumulusci.yml
@@ -125,6 +125,9 @@ tasks:
             settings_type: EnhancedNotesSettings
             settings_field: IsEnhancedNotesEnabled
             value: True
+    retrieve_tasks:
+        description: Retrieves the tasks under the particular category or group
+        class_path: cumulusci.tasks.preflight.retrieve_tasks.RetrieveTasks
     custom_settings_value_wait:
         description: Waits for a specific field value on the specified custom settings object and field
         class_path: cumulusci.tasks.salesforce.custom_settings_wait.CustomSettingValueWait
diff --git a/cumulusci/tasks/preflight/retrieve_tasks.py b/cumulusci/tasks/preflight/retrieve_tasks.py
new file mode 100644
index 000000000..b868c97e9
--- /dev/null
+++ b/cumulusci/tasks/preflight/retrieve_tasks.py
@@ -0,0 +1,30 @@
+from typing import List
+
+from cumulusci.cli.runtime import CliRuntime
+from cumulusci.cli.utils import group_items
+from cumulusci.core.exceptions import CumulusCIException
+from cumulusci.core.tasks import BaseTask
+
+
+class RetrieveTasks(BaseTask):
+    task_options = {
+        "group_name": {
+            "description": "Name of the category or Group",
+            "required": True,
+        },
+    }
+
+    def _run_task(self):
+        runtime = CliRuntime(load_keychain=True)
+        tasks = runtime.get_available_tasks()
+        task_groups = group_items(tasks)
+        task_groups = task_groups[self.options["group_name"]]
+        self.return_values: List[str] = []
+        for task_name, description in task_groups:
+            self.return_values.append(task_name)
+        if self.return_values:
+            self.return_values.sort()
+        else:
+            raise CumulusCIException("No tasks in the specified group")
+
+        self.logger.info(self.return_values)
diff --git a/cumulusci/tasks/salesforce/DescribeMetadataTypes.py b/cumulusci/tasks/salesforce/DescribeMetadataTypes.py
index 49c605c37..0b2d4f4e1 100644
--- a/cumulusci/tasks/salesforce/DescribeMetadataTypes.py
+++ b/cumulusci/tasks/salesforce/DescribeMetadataTypes.py
@@ -22,5 +22,5 @@ class DescribeMetadataTypes(BaseRetrieveMetadata):
 
     def _run_task(self):
         api_object = self._get_api()
-        metadata_list = api_object()
-        self.logger.info("Metadata Types supported by org:\n" + str(metadata_list))
+        self.return_values = api_object()
+        self.logger.info("Metadata Types supported by org:\n" + str(self.return_values))


Test patch:
diff --git a/cumulusci/tasks/preflight/tests/test_retrieve_tasks.py b/cumulusci/tasks/preflight/tests/test_retrieve_tasks.py
new file mode 100644
index 000000000..50b739ab0
--- /dev/null
+++ b/cumulusci/tasks/preflight/tests/test_retrieve_tasks.py
@@ -0,0 +1,62 @@
+from unittest import mock
+
+import pytest
+
+from cumulusci.cli.runtime import CliRuntime
+from cumulusci.core.exceptions import CumulusCIException
+from cumulusci.tasks.preflight.retrieve_tasks import RetrieveTasks
+from cumulusci.tasks.salesforce.tests.util import create_task
+
+
+class TestRetrieveTasks:
+    @pytest.mark.parametrize(
+        "available_tasks, group_name, expected_output",
+        [
+            (
+                [
+                    {
+                        "name": "test_task1",
+                        "description": "Test Task",
+                        "group": "Group",
+                    },
+                    {
+                        "name": "test_task2",
+                        "description": "Test Task",
+                        "group": "Group",
+                    },
+                    {
+                        "name": "test_task3",
+                        "description": "Test Task",
+                        "group": "Test Group",
+                    },
+                ],
+                "Group",
+                ["test_task1", "test_task2"],
+            ),
+            (
+                [
+                    {
+                        "name": "test_task1",
+                        "description": "Test Task",
+                        "group": "Group",
+                    },
+                ],
+                "Tests",
+                None,
+            ),
+        ],
+    )
+    def test_run_task(self, available_tasks, group_name, expected_output):
+        task = create_task(RetrieveTasks, options={"group_name": group_name})
+
+        with mock.patch.object(
+            CliRuntime, "get_available_tasks", return_value=available_tasks
+        ):
+            if expected_output is not None:
+                output = task()
+                assert output == expected_output
+            else:
+                with pytest.raises(
+                    CumulusCIException, match="No tasks in the specified group"
+                ):
+                    task()