Repository: packit/packit
Issue: custom copr project not working

When triggering packit copr-build with project specified, it sets the project name to something else - dhodovsk/packit-cli-hello-world-master and then fails with `ERROR    Request wasn't successful, there is probably a bug in the API code.`

Packit config
```
[dhodovsk@shiny hello-world]$ cat .packit.yaml 
---
specfile_path: hello.spec
jobs:
- job: copr_build
  trigger: pull_request
  owner: dhodovsk
  project: packit
  metadata:
    targets:
    - fedora-all
```

First copr run:
```
[dhodovsk@shiny hello-world]$ packit copr-build 
Packit 0.8.1 is being used.
Input is a directory: /home/dhodovsk/repos/github.com/packit-service/hello-world
Input directory is an upstream repository.
SRPM is /home/dhodovsk/repos/github.com/packit-service/hello-world/hello-0.72-1.fc30.src.rpm
Copr project dhodovsk/packit-cli-hello-world-master not found. Creating new.
ERROR    Request wasn't successful, there is probably a bug in the API code.
Unexpected exception occurred,
please fill an issue here:
https://github.com/packit-service/packit/issues
```

Changed files:
packit/cli/copr_build.py, tests/unit/test_package_config.py, packit/config/package_config.py

diff --git a/packit/cli/copr_build.py b/packit/cli/copr_build.py
index b34490e2..7bfb6e08 100644
--- a/packit/cli/copr_build.py
+++ b/packit/cli/copr_build.py
@@ -40,7 +40,8 @@ from packit.config.aliases import get_build_targets
 @click.option(
     "--project",
     help="Project name to build in. Will be created if does not exist. "
-    "(defaults to 'packit-cli-{repo_name}-{branch/commit}')",
+    "(defaults to the first found project value in the config file or "
+    "'packit-cli-{repo_name}-{branch/commit}')",
 )
 @click.option(
     "--targets",
@@ -83,7 +84,10 @@ def copr_build(
     it defaults to the current working directory.
     """
     api = get_packit_api(config=config, local_project=path_or_url)
-    default_project_name = f"packit-cli-{path_or_url.repo_name}-{path_or_url.ref}"
+    default_project_name = api.package_config.get_copr_build_project_value()
+
+    if not default_project_name:
+        default_project_name = f"packit-cli-{path_or_url.repo_name}-{path_or_url.ref}"
 
     targets_to_build = get_build_targets(
         *targets.split(","), default="fedora-rawhide-x86_64"
diff --git a/packit/config/package_config.py b/packit/config/package_config.py
index ed9d4f56..d15ea636 100644
--- a/packit/config/package_config.py
+++ b/packit/config/package_config.py
@@ -32,7 +32,7 @@ from yaml import safe_load
 from ogr.abstract import GitProject
 from packit.actions import ActionName
 from packit.constants import CONFIG_FILE_NAMES, PROD_DISTGIT_URL
-from packit.config.job_config import JobConfig, default_jobs
+from packit.config.job_config import JobConfig, default_jobs, JobType
 from packit.config.sync_files_config import SyncFilesConfig, SyncFilesItem
 from packit.exceptions import PackitConfigException
 
@@ -189,6 +189,23 @@ class PackageConfig:
 
         return SyncFilesConfig(files)
 
+    def get_copr_build_project_value(self) -> Optional[str]:
+        projects_list = [
+            job.metadata.get("project")
+            for job in self.jobs
+            if job.type == JobType.copr_build and job.metadata.get("project")
+        ]
+        if not projects_list:
+            return None
+
+        if len(set(projects_list)) > 1:
+            logger.warning(
+                f"You have defined multiple copr projects to build in, we are going "
+                f"to pick the first one: {projects_list[0]}, reorder the job definitions"
+                f" if this is not the one you want."
+            )
+        return projects_list[0]
+
     def __eq__(self, other: object):
         if not isinstance(other, self.__class__):
             return NotImplemented
diff --git a/tests/unit/test_package_config.py b/tests/unit/test_package_config.py
index f888bfc8..7ac85b6b 100644
--- a/tests/unit/test_package_config.py
+++ b/tests/unit/test_package_config.py
@@ -1,8 +1,9 @@
 import pytest
 from flexmock import flexmock
+from packit.config import JobType, JobConfigTriggerType, JobConfig
 
 from ogr.abstract import GitProject, GitService
-from packit.config.package_config import get_specfile_path_from_repo
+from packit.config.package_config import get_specfile_path_from_repo, PackageConfig
 
 
 @pytest.mark.parametrize(
@@ -14,3 +15,57 @@ def test_get_specfile_path_from_repo(files, expected):
     gp.should_receive("get_files").and_return(files)
     git_project = GitProject(repo="", service=GitService(), namespace="")
     assert get_specfile_path_from_repo(project=git_project) == expected
+
+
+@pytest.mark.parametrize(
+    "package_config, project",
+    [
+        (
+            PackageConfig(
+                specfile_path="xxx",
+                jobs=[
+                    JobConfig(
+                        type=JobType.copr_build,
+                        trigger=JobConfigTriggerType.pull_request,
+                        metadata={},
+                    )
+                ],
+            ),
+            None,
+        ),
+        (
+            PackageConfig(
+                specfile_path="xxx",
+                jobs=[
+                    JobConfig(
+                        type=JobType.copr_build,
+                        trigger=JobConfigTriggerType.pull_request,
+                        metadata={"project": "example"},
+                    )
+                ],
+            ),
+            "example",
+        ),
+        (
+            PackageConfig(
+                specfile_path="xxx",
+                jobs=[
+                    JobConfig(
+                        type=JobType.copr_build,
+                        trigger=JobConfigTriggerType.release,
+                        metadata={"project": "example1"},
+                    ),
+                    JobConfig(
+                        type=JobType.copr_build,
+                        trigger=JobConfigTriggerType.pull_request,
+                        metadata={"project": "example2"},
+                    ),
+                ],
+            ),
+            "example1",
+        ),
+    ],
+)
+def test_project_from_copr_build_job(package_config, project):
+    config_project_value = package_config.get_copr_build_project_value()
+    assert config_project_value == project