Repository: packit/packit-service
Issue: Improve getting of the Testing Farm composes

When getting a TF compose, we sanitize the target in [distro2compose method](https://github.com/packit/packit-service/blob/dad5a6d1c5eea1feb15a95652804cfc84083c0ca/packit_service/worker/helpers/testing_farm.py#L336). This breaks some usecases, e.g. having `RHEL-7.8-ZStream`  in the configuration results in `RHEL-7.8-Zstream` compose  (because of `title()`)  that is not in the list of available composes and therefore the test run fails ([example](https://github.com/r0x0d/leapp-packit-poc/pull/3/checks?check_run_id=8308160405)).

We could firstly check the raw distro (the target without arch) and if it is in the list of available composes, don't sanitize it. Or we could try to improve the sanitization itself, but we need to make sure we don't break the other usecases.

Changed files:
tests/integration/test_pr_comment.py, tests/unit/test_testing_farm.py, packit_service/worker/helpers/testing_farm.py

diff --git a/packit_service/worker/helpers/testing_farm.py b/packit_service/worker/helpers/testing_farm.py
index 631b5bdc..b4ee2928 100644
--- a/packit_service/worker/helpers/testing_farm.py
+++ b/packit_service/worker/helpers/testing_farm.py
@@ -231,6 +231,25 @@ class TestingFarmJobHelper(CoprBuildJobHelper):
             self._copr_builds_from_other_pr = self.get_copr_builds_from_other_pr()
         return self._copr_builds_from_other_pr
 
+    @property
+    def available_composes(self) -> Optional[Set[str]]:
+        """
+        Fetches available composes from the Testing Farm endpoint.
+
+        Returns:
+            Set of all available composes or `None` if error occurs.
+        """
+        endpoint = (
+            f"composes/{'redhat' if self.job_config.use_internal_tf else 'public' }"
+        )
+
+        response = self.send_testing_farm_request(endpoint=endpoint)
+        if response.status_code != 200:
+            return None
+
+        # {'composes': [{'name': 'CentOS-Stream-8'}, {'name': 'Fedora-Rawhide'}]}
+        return {c["name"] for c in response.json()["composes"]}
+
     @staticmethod
     def _artifact(
         chroot: str, build_id: Optional[int], built_packages: Optional[List[Dict]]
@@ -451,6 +470,20 @@ class TestingFarmJobHelper(CoprBuildJobHelper):
             compose if we were able to map the distro to compose present
             in the list of available composes, otherwise None
         """
+        composes = self.available_composes
+        if composes is None:
+            msg = "We were not able to get the available TF composes."
+            logger.error(msg)
+            self.report_status_to_test_for_test_target(
+                state=BaseCommitStatus.error,
+                description=msg,
+                target=target,
+            )
+            return None
+
+        if target in composes:
+            return target
+
         distro, arch = target.rsplit("-", 1)
         compose = (
             distro.title()
@@ -466,24 +499,6 @@ class TestingFarmJobHelper(CoprBuildJobHelper):
             # TF has separate composes for aarch64 architecture
             compose += "-aarch64"
 
-        endpoint = (
-            f"composes/{'redhat' if self.job_config.use_internal_tf else 'public'}"
-        )
-        response = self.send_testing_farm_request(endpoint=endpoint)
-
-        if response.status_code != 200:
-            msg = "We were not able to get the available TF composes."
-            logger.error(msg)
-            self.report_status_to_test_for_test_target(
-                state=BaseCommitStatus.error,
-                description=msg,
-                target=target,
-            )
-            return None
-
-        # {'composes': [{'name': 'CentOS-Stream-8'}, {'name': 'Fedora-Rawhide'}]}
-        composes = [c["name"] for c in response.json()["composes"]]
-
         if self.job_config.use_internal_tf:
             if compose in composes:
                 return compose
diff --git a/tests/integration/test_pr_comment.py b/tests/integration/test_pr_comment.py
index 312cb39b..68d0c20b 100644
--- a/tests/integration/test_pr_comment.py
+++ b/tests/integration/test_pr_comment.py
@@ -1381,7 +1381,7 @@ def test_pr_test_command_handler_compose_not_present(
         url="",
         markdown_content="The compose Fedora-Rawhide (from target fedora-rawhide) is not in "
         "the list of available composes:\\n"
-        "['some-other-compose']. Please, check the targets defined in your test job configuration. "
+        "{'some-other-compose'}. Please, check the targets defined in your test job configuration. "
         "If you think your configuration is correct, get "
         "in touch with [us](https://packit.dev/#contact).",
     ).once()
diff --git a/tests/unit/test_testing_farm.py b/tests/unit/test_testing_farm.py
index 2f62ddf0..7de278e4 100644
--- a/tests/unit/test_testing_farm.py
+++ b/tests/unit/test_testing_farm.py
@@ -172,6 +172,9 @@ def test_testing_farm_response(
         ("centos-8-latest-x86_64", "CentOS-8-latest", True),
         ("centos-8-Latest-x86_64", "CentOS-8-latest", True),
         ("centos-8.4-x86_64", "CentOS-8.4", True),
+        # If target is present in the available composes, just return it
+        ("RHEL-7.8-ZStream", "RHEL-7.8-ZStream", True),
+        ("RHEL-7.9-rhui", "RHEL-7.9-rhui", True),
     ],
 )
 def test_distro2compose(target, compose, use_internal_tf):