Repository: packit-service/packit
Pull request #624 (2019-12-05 14:22:00)

Add defaults for upstream_package_name, downstream_package_name
If there is no upstream_package_name/downstream_package_name given in .packit.yaml, they default to the name of the Github repo.

softwarefactory-project-zuul[bot]: Build succeeded.

- [packit-pre-commit ](https://softwarefactory-project.io/zuul/t/local/build/48b850f9e2e34a868771f93c07d0bdbc) : SUCCESS in 1m 44s
- [packit-tests-rpm ](https://softwarefactory-project.io/zuul/t/local/build/eb414765ba9e4b8790384ef4162e0a94) : SUCCESS in 4m 01s
- [packit-tests-pip-deps ](https://softwarefactory-project.io/zuul/t/local/build/373bb0856e9444d889638b6f6c171f46) : SUCCESS in 4m 27s
- [packit-tests-git-master ](https://softwarefactory-project.io/zuul/t/local/build/2c28980aa35143c68d9d3bad413d641d) : SUCCESS in 4m 19s
- [packit-tests-rpm-sess-rec ](https://softwarefactory-project.io/zuul/t/local/build/dedc59e5ef2446a2b1604366dc9c7e38) : SUCCESS in 2m 22s
- [packit-tests-pip-deps-sess-rec ](https://softwarefactory-project.io/zuul/t/local/build/d8b1048590f54309b27436a6b4c519ce) : SUCCESS in 2m 31s
- [packit-tests-git-master-sess-rec ](https://softwarefactory-project.io/zuul/t/local/build/3f530f58898c4d4eb5bcddf5ccb03ff6) : SUCCESS in 4m 31s

packit-as-a-service[bot]: Congratulations! The build [has finished](https://copr-be.cloud.fedoraproject.org/results/packit/packit-service-packit-624/fedora-rawhide-x86_64/01124489-packit/builder-live.log.gz) successfully. :champagne:

You can install the built RPMs by following these steps:

* `sudo yum install -y dnf-plugins-core` on RHEL 8
* `sudo dnf install -y dnf-plugins-core` on Fedora
* `dnf copr enable packit/packit-service-packit-624`
* And now you can install the packages.

Please note that the RPMs should be used only in a testing environment.

Patch:
diff --git a/packit/cli/utils.py b/packit/cli/utils.py
index 8021cfd4..b78c8202 100644
--- a/packit/cli/utils.py
+++ b/packit/cli/utils.py
@@ -109,7 +109,9 @@ def get_packit_api(
     Load the package config, set other options and return the PackitAPI
     """
     package_config = get_local_package_config(
-        local_project.working_dir, try_local_dir_last=True
+        local_project.working_dir,
+        repo_name=local_project.repo_name,
+        try_local_dir_last=True,
     )
 
     if dist_git_path:
diff --git a/packit/config/package_config.py b/packit/config/package_config.py
index cb171757..be600318 100644
--- a/packit/config/package_config.py
+++ b/packit/config/package_config.py
@@ -76,7 +76,7 @@ class PackageConfig(BaseConfig):
         self.upstream_project_url: Optional[str] = upstream_project_url
         self.upstream_package_name: Optional[str] = upstream_package_name
         # this is generated by us
-        self._downstream_package_name: Optional[str] = downstream_package_name
+        self.downstream_package_name: Optional[str] = downstream_package_name
         self.dist_git_base_url: str = dist_git_base_url or PROD_DISTGIT_URL
         self._downstream_project_url: str = downstream_project_url
         # path to a local git clone of the dist-git repo; None means to clone in a tmpdir
@@ -101,12 +101,6 @@ class PackageConfig(BaseConfig):
         # template to create an upstream tag name (upstream may use different tagging scheme)
         self.upstream_tag_template = upstream_tag_template
 
-    @property
-    def downstream_package_name(self) -> str:
-        if not self._downstream_package_name:
-            raise PackitConfigException("downstream_package_name is not set")
-        return self._downstream_package_name
-
     @property
     def downstream_project_url(self) -> str:
         if not self._downstream_project_url:
@@ -146,7 +140,11 @@ class PackageConfig(BaseConfig):
 
     @classmethod
     def get_from_dict(
-        cls, raw_dict: dict, config_file_path: str = None, validate=True
+        cls,
+        raw_dict: dict,
+        config_file_path: str = None,
+        repo_name: str = None,
+        validate=True,
     ) -> "PackageConfig":
         if validate:
             cls.validate(raw_dict)
@@ -157,9 +155,16 @@ class PackageConfig(BaseConfig):
         create_tarball_command = raw_dict.get("create_tarball_command", None)
         current_version_command = raw_dict.get("current_version_command", None)
 
-        upstream_package_name = cls.get_deprecated_key(
-            raw_dict, "upstream_package_name", "upstream_project_name"
-        ) or cls.get_deprecated_key(raw_dict, "upstream_package_name", "upstream_name")
+        upstream_package_name = (
+            cls.get_deprecated_key(
+                raw_dict, "upstream_package_name", "upstream_project_name"
+            )
+            or cls.get_deprecated_key(
+                raw_dict, "upstream_package_name", "upstream_name"
+            )
+            or repo_name
+        )
+
         upstream_project_url = raw_dict.get("upstream_project_url", None)
 
         if raw_dict.get("dist_git_url", None):
@@ -167,9 +172,11 @@ class PackageConfig(BaseConfig):
                 "dist_git_url is no longer being processed, "
                 "it is generated from dist_git_base_url and downstream_package_name"
             )
-        downstream_package_name = cls.get_deprecated_key(
-            raw_dict, "downstream_package_name", "package_name"
+        downstream_package_name = (
+            cls.get_deprecated_key(raw_dict, "downstream_package_name", "package_name")
+            or repo_name
         )
+
         specfile_path = raw_dict.get("specfile_path", None)
         if not specfile_path:
             if downstream_package_name:
@@ -254,7 +261,10 @@ class PackageConfig(BaseConfig):
 
 
 def get_local_package_config(
-    *directory, try_local_dir_first=False, try_local_dir_last=False
+    *directory,
+    repo_name: str = None,
+    try_local_dir_first=False,
+    try_local_dir_last=False,
 ) -> PackageConfig:
     """
     :return: local PackageConfig if present
@@ -280,7 +290,9 @@ def get_local_package_config(
                     )
                     raise Exception(f"Cannot load package config: {ex}.")
                 return parse_loaded_config(
-                    loaded_config=loaded_config, config_file_path=str(config_file_name)
+                    loaded_config=loaded_config,
+                    config_file_path=str(config_file_name),
+                    repo_name=repo_name,
                 )
 
             logger.debug(f"The local config file '{config_file_name_full}' not found.")
@@ -315,7 +327,9 @@ def get_package_config_from_repo(
             logger.error(f"Cannot load package config '{config_file_name}'.")
             raise PackitException(f"Cannot load package config: {ex}.")
         return parse_loaded_config(
-            loaded_config=loaded_config, config_file_path=config_file_name
+            loaded_config=loaded_config,
+            config_file_path=config_file_name,
+            repo_name=sourcegit_project.repo,
         )
 
     logger.warning(
@@ -326,14 +340,17 @@ def get_package_config_from_repo(
 
 
 def parse_loaded_config(
-    loaded_config: dict, config_file_path: str = None
+    loaded_config: dict, config_file_path: str = None, repo_name: str = None
 ) -> PackageConfig:
     """Tries to parse the config to PackageConfig."""
     logger.debug(f"Package config:\n{json.dumps(loaded_config, indent=4)}")
 
     try:
         package_config = PackageConfig.get_from_dict(
-            raw_dict=loaded_config, config_file_path=config_file_path, validate=True
+            raw_dict=loaded_config,
+            config_file_path=config_file_path,
+            repo_name=repo_name,
+            validate=True,
         )
         return package_config
     except Exception as ex:
diff --git a/packit/distgit.py b/packit/distgit.py
index cdcc4abf..ebc78116 100644
--- a/packit/distgit.py
+++ b/packit/distgit.py
@@ -118,7 +118,8 @@ class DistGit(PackitRepositoryBase):
         if not self._downstream_config:
             try:
                 self._downstream_config = get_local_package_config(
-                    self.local_project.working_dir
+                    self.local_project.working_dir,
+                    repo_name=self.local_project.repo_name,
                 )
             except PackitConfigException:
                 return None


Test patch:
diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py
index b7f441d3..a6ef8b75 100644
--- a/tests/unit/test_config.py
+++ b/tests/unit/test_config.py
@@ -452,6 +452,35 @@ def test_package_config_parse(raw, expected):
     assert package_config == expected
 
 
+@pytest.mark.parametrize(
+    "raw,expected",
+    [
+        (
+            {
+                "specfile_path": "fedora/package.spec",
+                "synced_files": ["fedora/package.spec"],
+            },
+            PackageConfig(
+                specfile_path="fedora/package.spec",
+                synced_files=SyncFilesConfig(
+                    files_to_sync=[
+                        SyncFilesItem(
+                            src="fedora/package.spec", dest="fedora/package.spec"
+                        )
+                    ]
+                ),
+                downstream_package_name="package",
+                upstream_package_name="package",
+            ),
+        )
+    ],
+)
+def test_package_config_upstream_and_downstream_package_names(raw, expected):
+    package_config = PackageConfig.get_from_dict(raw_dict=raw, repo_name="package")
+    assert package_config
+    assert package_config == expected
+
+
 def test_dist_git_package_url():
     di = {
         "dist_git_base_url": "https://packit.dev/",