Repository: packit/packit-service
Issue: environment.artifacts.repository (same for environment.artifacts.repository-file) are not merged

### What happened? What is the problem?

With [the following configuration ](https://github.com/oamg/leapp/pull/827/commits/05abcfe77e9a3d5776049fe4df262ad0b704e02b#diff-2b4300f6d69654fd71c0cdd329b5a5f13fa8fe7eb709c76276bdfd3ea1dde578R60) the package defined in packit config is not installed along with build from PR.

With [the following configuration](https://github.com/oamg/leapp/pull/827/commits/eeaa2c66cfc9a0f40bdc8daab4e53f5865e1dfa4#diff-2b4300f6d69654fd71c0cdd329b5a5f13fa8fe7eb709c76276bdfd3ea1dde578R61) the package from the PR is not installed at all.

### What did you expect to happen?

Both the package from PR and the package defined in the packit config are installed in the test env

### Examples of failed tests

repository - https://api.dev.testing-farm.io/v0.1/requests/4558e0b0-edfd-4aee-91a6-282f24b2a343
repository-file - https://api.dev.testing-farm.io/v0.1/requests/6563b980-f147-4a86-8421-f2f0f1015a32

### What is the impacted category (job)?

Testing Farm tests

### Workaround

- [ ] There is an existing workaround that can be used until this issue is fixed.

### Participation

- [ ] I am willing to submit a pull request for this issue. (Packit team is happy to help!)

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

diff --git a/packit_service/constants.py b/packit_service/constants.py
index fa963747..013d6c32 100644
--- a/packit_service/constants.py
+++ b/packit_service/constants.py
@@ -32,6 +32,7 @@ TESTING_FARM_EXTRA_PARAM_MERGED_SUBTREES = (
     "test",
     "settings",
 )
+TESTING_FARM_ARTIFACTS_KEY = "artifacts"
 
 MSG_DOWNSTREAM_JOB_ERROR_HEADER = (
     "Packit failed on creating {object} in dist-git "
diff --git a/packit_service/worker/helpers/testing_farm.py b/packit_service/worker/helpers/testing_farm.py
index 1c2564e5..deb2ecd4 100644
--- a/packit_service/worker/helpers/testing_farm.py
+++ b/packit_service/worker/helpers/testing_farm.py
@@ -20,6 +20,7 @@ from packit_service.constants import (
     BASE_RETRY_INTERVAL_IN_MINUTES_FOR_OUTAGES,
     PUBLIC_TF_ARCHITECTURE_LIST,
     INTERNAL_TF_ARCHITECTURE_LIST,
+    TESTING_FARM_ARTIFACTS_KEY,
 )
 from packit_service.models import (
     CoprBuildTargetModel,
@@ -348,12 +349,31 @@ class TestingFarmJobHelper(CoprBuildJobHelper):
                 if key not in payload or is_final(value):
                     payload[key] = value
                 elif not is_final(value):
+                    if key == TESTING_FARM_ARTIFACTS_KEY:
+                        cls._handle_extra_artifacts(
+                            payload, params[TESTING_FARM_ARTIFACTS_KEY]
+                        )
+                        continue
                     cls._merge_payload_with_extra_params(payload[key], params[key])
 
         elif isinstance(params, list):
             for payload_el, params_el in zip(payload, params):
                 cls._merge_payload_with_extra_params(payload_el, params_el)
 
+    @classmethod
+    def _handle_extra_artifacts(cls, payload: Any, extra_params_artifacts: Any):
+        """
+        We treat `artifacts` specially since we do not want to overwrite
+        the artifacts defined by us, but combine them with the one in `tf_extra_params`.
+        """
+        if isinstance(extra_params_artifacts, list):
+            payload[TESTING_FARM_ARTIFACTS_KEY] += extra_params_artifacts
+        else:
+            logger.info(
+                "Type of artifacts in the tf_extra_params is not a list, "
+                "not adding them to payload."
+            )
+
     def _payload(
         self,
         target: str,
diff --git a/tests/unit/test_testing_farm.py b/tests/unit/test_testing_farm.py
index 2721c120..da21b97a 100644
--- a/tests/unit/test_testing_farm.py
+++ b/tests/unit/test_testing_farm.py
@@ -773,6 +773,58 @@ def test_payload(
             ],
             [{"tmt": {"context": {"how": "full"}}}],
         ),
+        (
+            {
+                "environments": [
+                    {
+                        "arch": "x86_64",
+                        "artifacts": [
+                            {
+                                "id": "123:fedora-37",
+                                "type": "fedora-copr-build",
+                            }
+                        ],
+                    }
+                ]
+            },
+            {
+                "environments": [
+                    {
+                        "artifacts": [
+                            {
+                                "type": "repository",
+                                "id": "123:fedora-37",
+                                "packages": "some-nvr",
+                            }
+                        ],
+                        "settings": {
+                            "provisioning": {"tags": {"BusinessUnit": "sst_upgrades"}}
+                        },
+                    }
+                ]
+            },
+            {
+                "environments": [
+                    {
+                        "arch": "x86_64",
+                        "artifacts": [
+                            {
+                                "id": "123:fedora-37",
+                                "type": "fedora-copr-build",
+                            },
+                            {
+                                "type": "repository",
+                                "id": "123:fedora-37",
+                                "packages": "some-nvr",
+                            },
+                        ],
+                        "settings": {
+                            "provisioning": {"tags": {"BusinessUnit": "sst_upgrades"}}
+                        },
+                    }
+                ]
+            },
+        ),
     ],
 )
 def test_merge_payload_with_extra_params(payload, params, result):