Repository: packit/packit-service
Issue: TypeError: run_testing_farm_handler() missing 2 required positional arguments: 'chroot' and 'build_id'

Sentry Issue: [RED-HAT-0P-32S](https://sentry.io/organizations/red-hat-0p/issues/2153527786/?referrer=github_integration)

```
TypeError: run_testing_farm_handler() missing 2 required positional arguments: 'chroot' and 'build_id'
(2 additional frame(s) were not displayed)
...
  File "packit_service/worker/jobs.py", line 330, in process_jobs
    group(signatures).apply_async()
  File "celery/canvas.py", line 1007, in apply_async
    results = list(self._apply_tasks(tasks, producer, app, p,
  File "celery/canvas.py", line 1089, in _apply_tasks
    sig.apply_async(producer=producer, add_to_parent=False,
  File "celery/canvas.py", line 229, in apply_async
    return _apply(args, kwargs, **options)
  File "celery/app/task.py", line 532, in apply_async
    check_arguments(*(args or ()), **(kwargs or {}))
```

Changed files:
packit_service/worker/handlers/github_handlers.py, packit_service/worker/tasks.py, tests/integration/test_pr_comment.py

diff --git a/packit_service/worker/handlers/github_handlers.py b/packit_service/worker/handlers/github_handlers.py
index 10637ced..4d9db63c 100644
--- a/packit_service/worker/handlers/github_handlers.py
+++ b/packit_service/worker/handlers/github_handlers.py
@@ -413,8 +413,8 @@ class TestingFarmHandler(JobHandler):
         package_config: PackageConfig,
         job_config: JobConfig,
         data: EventData,
-        chroot: str,
-        build_id: int,
+        chroot: Optional[str] = None,
+        build_id: Optional[int] = None,
     ):
         super().__init__(
             package_config=package_config,
@@ -428,8 +428,13 @@ class TestingFarmHandler(JobHandler):
     @property
     def db_trigger(self) -> Optional[AbstractTriggerDbType]:
         if not self._db_trigger:
-            build = CoprBuildModel.get_by_id(self.build_id)
-            self._db_trigger = build.job_trigger.get_trigger_object()
+            # copr build end
+            if self.build_id:
+                build = CoprBuildModel.get_by_id(self.build_id)
+                self._db_trigger = build.job_trigger.get_trigger_object()
+            # '/packit test' comment
+            else:
+                self._db_trigger = self.data.db_trigger
         return self._db_trigger
 
     def run(self) -> TaskResults:
@@ -443,5 +448,14 @@ class TestingFarmHandler(JobHandler):
             db_trigger=self.db_trigger,
             job_config=self.job_config,
         )
-        logger.info("Running testing farm.")
+
+        if self.data.event_type in (
+            PullRequestCommentGithubEvent.__name__,
+            MergeRequestCommentGitlabEvent.__name__,
+            PullRequestCommentPagureEvent.__name__,
+        ):
+            logger.debug(f"Test job config: {testing_farm_helper.job_tests}")
+            return testing_farm_helper.run_testing_farm_on_all()
+
+        logger.info(f"Running testing farm for {self.chroot}.")
         return testing_farm_helper.run_testing_farm(chroot=self.chroot)
diff --git a/packit_service/worker/tasks.py b/packit_service/worker/tasks.py
index 7ddb0885..c4a4997d 100644
--- a/packit_service/worker/tasks.py
+++ b/packit_service/worker/tasks.py
@@ -20,7 +20,7 @@
 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 # SOFTWARE.
 import logging
-from typing import List
+from typing import List, Optional
 
 from packit_service.celerizer import celery_app
 from packit_service.constants import RETRY_LIMIT
@@ -147,7 +147,11 @@ def run_installation_handler(event: dict, package_config: dict, job_config: dict
 
 @celery_app.task(name=TaskName.testing_farm)
 def run_testing_farm_handler(
-    event: dict, package_config: dict, job_config: dict, chroot: str, build_id: int
+    event: dict,
+    package_config: dict,
+    job_config: dict,
+    chroot: Optional[str] = None,
+    build_id: Optional[int] = None,
 ):
     handler = TestingFarmHandler(
         package_config=load_package_config(package_config),
diff --git a/tests/integration/test_pr_comment.py b/tests/integration/test_pr_comment.py
index 143e9e11..43a439db 100644
--- a/tests/integration/test_pr_comment.py
+++ b/tests/integration/test_pr_comment.py
@@ -38,7 +38,8 @@ from packit_service.service.db_triggers import AddPullRequestDbTrigger
 from packit_service.worker.build.copr_build import CoprBuildJobHelper
 from packit_service.worker.jobs import SteveJobs, get_packit_commands_from_comment
 from packit_service.worker.result import TaskResults
-from packit_service.worker.tasks import run_copr_build_handler
+from packit_service.worker.tasks import run_copr_build_handler, run_testing_farm_handler
+from packit_service.worker.testing_farm import TestingFarmJobHelper
 from packit_service.worker.whitelist import Whitelist
 from tests.spellbook import DATA_DIR, first_dict_value, get_parameters_from_results
 
@@ -339,3 +340,73 @@ def test_pr_comment_wrong_packit_command_handler(
 
     results = SteveJobs().process_message(pr_wrong_packit_comment_event)
     assert results == []
+
+
+def test_pr_test_command_handler(pr_embedded_command_comment_event):
+    jobs = [
+        {
+            "trigger": "pull_request",
+            "job": "tests",
+            "metadata": {"targets": "fedora-rawhide-x86_64"},
+        }
+    ]
+    packit_yaml = (
+        "{'specfile_path': 'the-specfile.spec', 'synced_files': [], 'jobs': "
+        + str(jobs)
+        + "}"
+    )
+    flexmock(
+        GithubProject,
+        full_repo_name="packit-service/hello-world",
+        get_file_content=lambda path, ref: packit_yaml,
+        get_files=lambda ref, filter_regex: ["the-specfile.spec"],
+        get_web_url=lambda: "https://github.com/the-namespace/the-repo",
+        get_pr=lambda pr_id: flexmock(head_commit="12345"),
+    )
+    flexmock(Github, get_repo=lambda full_name_or_id: None)
+
+    config = ServiceConfig()
+    config.command_handler_work_dir = SANDCASTLE_WORK_DIR
+    flexmock(ServiceConfig).should_receive("get_service_config").and_return(config)
+    trigger = flexmock(
+        job_config_trigger_type=JobConfigTriggerType.pull_request, id=123
+    )
+    flexmock(AddPullRequestDbTrigger).should_receive("db_trigger").and_return(trigger)
+    flexmock(PullRequestModel).should_receive("get_by_id").with_args(123).and_return(
+        trigger
+    )
+    flexmock(LocalProject, refresh_the_arguments=lambda: None)
+    flexmock(Whitelist, check_and_report=True)
+    flexmock(PullRequestModel).should_receive("get_or_create").with_args(
+        pr_id=9,
+        namespace="packit-service",
+        repo_name="hello-world",
+        project_url="https://github.com/packit-service/hello-world",
+    ).and_return(
+        flexmock(id=9, job_config_trigger_type=JobConfigTriggerType.pull_request)
+    )
+    pr_embedded_command_comment_event["comment"]["body"] = "/packit test"
+    (
+        flexmock(GithubProject)
+        .should_receive("can_merge_pr")
+        .with_args("phracek")
+        .and_return(True)
+        .once()
+    )
+    flexmock(GithubProject, get_files="foo.spec")
+    flexmock(GithubProject).should_receive("is_private").and_return(False)
+    flexmock(Signature).should_receive("apply_async").once()
+    flexmock(TestingFarmJobHelper).should_receive("run_testing_farm_on_all").and_return(
+        TaskResults(success=True, details={})
+    )
+
+    processing_results = SteveJobs().process_message(pr_embedded_command_comment_event)
+    event_dict, job, job_config, package_config = get_parameters_from_results(
+        processing_results
+    )
+
+    run_testing_farm_handler(
+        package_config=package_config,
+        event=event_dict,
+        job_config=job_config,
+    )