Repository: SFDO-Tooling/CumulusCI
Pull request #3616 (2023-07-07 16:26:27)

Add preflight check for PSL assignments


Patch:
diff --git a/cumulusci/cumulusci.yml b/cumulusci/cumulusci.yml
index 7ab9af534..57cd1e022 100644
--- a/cumulusci/cumulusci.yml
+++ b/cumulusci/cumulusci.yml
@@ -311,9 +311,13 @@ tasks:
         class_path: cumulusci.tasks.preflight.licenses.GetAvailablePermissionSetLicenses
         group: Salesforce Preflight Checks
     get_assigned_permission_sets:
-        description: Retrieves a list of the names of any permission sets assigned to the running user.
+        description: Retrieves a list of the developer names of any permission sets assigned to the running user.
         class_path: cumulusci.tasks.preflight.permsets.GetPermissionSetAssignments
         group: Salesforce Preflight Checks
+    get_assigned_permission_set_licenses:
+        description: Retrieves a list of the developer names of any Permission Set Licenses assigned to the running user.
+        class_path: cumulusci.tasks.preflight.licenses.GetPermissionLicenseSetAssignments
+        group: Salesforce Preflight Checks
     get_available_permission_sets:
         description: Retrieves a list of the currently available Permission Sets
         class_path: cumulusci.tasks.preflight.licenses.GetAvailablePermissionSets
diff --git a/cumulusci/tasks/preflight/licenses.py b/cumulusci/tasks/preflight/licenses.py
index 3bf6853dc..cc40ed159 100644
--- a/cumulusci/tasks/preflight/licenses.py
+++ b/cumulusci/tasks/preflight/licenses.py
@@ -25,6 +25,17 @@ class GetAvailablePermissionSetLicenses(BaseSalesforceApiTask):
         self.logger.info(f"Found permission set licenses:\n{licenses}")
 
 
+class GetPermissionLicenseSetAssignments(BaseSalesforceApiTask):
+    def _run_task(self):
+        query = f"SELECT PermissionSetLicense.DeveloperName FROM PermissionSetLicenseAssign WHERE AssigneeId = '{self.org_config.user_id}'"
+        self.return_values = [
+            result["PermissionSetLicense"]["DeveloperName"]
+            for result in self.sf.query_all(query)["records"]
+        ]
+        permsets = "\n".join(self.return_values)
+        self.logger.info(f"Found permission licenses sets assigned:\n{permsets}")
+
+
 class GetAvailablePermissionSets(BaseSalesforceApiTask):
     def _run_task(self):
         self.return_values = [


Test patch:
diff --git a/cumulusci/tasks/preflight/tests/test_licenses.py b/cumulusci/tasks/preflight/tests/test_licenses.py
index 413cac9f8..f2cf117ad 100644
--- a/cumulusci/tasks/preflight/tests/test_licenses.py
+++ b/cumulusci/tasks/preflight/tests/test_licenses.py
@@ -4,6 +4,7 @@ from cumulusci.tasks.preflight.licenses import (
     GetAvailableLicenses,
     GetAvailablePermissionSetLicenses,
     GetAvailablePermissionSets,
+    GetPermissionLicenseSetAssignments,
 )
 from cumulusci.tasks.salesforce.tests.util import create_task
 
@@ -44,6 +45,37 @@ class TestLicensePreflights:
         )
         assert task.return_values == ["TEST1", "TEST2"]
 
+    def test_assigned_permsetlicense_preflight(self):
+        task = create_task(GetPermissionLicenseSetAssignments, {})
+        task._init_api = Mock()
+        task._init_api.return_value.query_all.return_value = {
+            "totalSize": 2,
+            "done": True,
+            "records": [
+                {
+                    "PermissionSetLicense": {
+                        "MasterLabel": "Document Checklist",
+                        "DeveloperName": "DocumentChecklist",
+                    },
+                },
+                {
+                    "PermissionSetLicense": {
+                        "MasterLabel": "Einstein Analytics Plus Admin",
+                        "DeveloperName": "EinsteinAnalyticsPlusAdmin",
+                    },
+                },
+            ],
+        }
+        task()
+
+        task._init_api.return_value.query_all.assert_called_once_with(
+            "SELECT PermissionSetLicense.DeveloperName FROM PermissionSetLicenseAssign WHERE AssigneeId = 'USER_ID'"
+        )
+        assert task.return_values == [
+            "DocumentChecklist",
+            "EinsteinAnalyticsPlusAdmin",
+        ]
+
     def test_permsets_preflight(self):
         task = create_task(GetAvailablePermissionSets, {})
         task._init_api = Mock()