From f829ac85d82b0cf1bde7e2b5708d41d18091b632 Mon Sep 17 00:00:00 2001 From: "John L. Villalovos" Date: Sun, 28 May 2023 04:15:28 -0700 Subject: [PATCH] feat: add support for `select="package_file"` in package upload Add ability to use `select="package_file"` when uploading a generic package as described in: https://docs.gitlab.com/ee/user/packages/generic_packages/index.html Closes: #2557 --- gitlab/v4/objects/packages.py | 25 ++++++++++++++----------- tests/functional/api/test_packages.py | 17 +++++++++++++++++ 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/gitlab/v4/objects/packages.py b/gitlab/v4/objects/packages.py index 9ae2fd52a..fc5b91075 100644 --- a/gitlab/v4/objects/packages.py +++ b/gitlab/v4/objects/packages.py @@ -47,6 +47,7 @@ def upload( package_version: str, file_name: str, path: Union[str, Path], + select: Optional[str] = None, **kwargs: Any, ) -> GenericPackage: """Upload a file as a generic package. @@ -58,6 +59,7 @@ def upload( version regex rules file_name: The name of the file as uploaded in the registry path: The path to a local file to upload + select: GitLab API accepts a value of 'package_file' Raises: GitlabConnectionError: If the server cannot be reached @@ -77,20 +79,21 @@ def upload( raise exc.GitlabUploadError(f"Failed to read package file {path}") from e url = f"{self._computed_path}/{package_name}/{package_version}/{file_name}" - server_data = self.gitlab.http_put(url, post_data=file_data, raw=True, **kwargs) + query_data = {} if select is None else {"select": select} + server_data = self.gitlab.http_put( + url, query_data=query_data, post_data=file_data, raw=True, **kwargs + ) if TYPE_CHECKING: assert isinstance(server_data, dict) - return self._obj_cls( - self, - attrs={ - "package_name": package_name, - "package_version": package_version, - "file_name": file_name, - "path": path, - "message": server_data["message"], - }, - ) + attrs = { + "package_name": package_name, + "package_version": package_version, + "file_name": file_name, + "path": path, + } + attrs.update(server_data) + return self._obj_cls(self, attrs=attrs) @cli.register_custom_action( "GenericPackageManager", diff --git a/tests/functional/api/test_packages.py b/tests/functional/api/test_packages.py index 9f06439b4..e83d6b94c 100644 --- a/tests/functional/api/test_packages.py +++ b/tests/functional/api/test_packages.py @@ -10,6 +10,7 @@ package_name = "hello-world" package_version = "v1.0.0" file_name = "hello.tar.gz" +file_name2 = "hello2.tar.gz" file_content = "package content" @@ -37,6 +38,22 @@ def test_upload_generic_package(tmp_path, project): assert package.message == "201 Created" +def test_upload_generic_package_select(tmp_path, project): + path = tmp_path / file_name2 + path.write_text(file_content) + package = project.generic_packages.upload( + package_name=package_name, + package_version=package_version, + file_name=file_name2, + path=path, + select="package_file", + ) + + assert isinstance(package, GenericPackage) + assert package.file_name == file_name2 + assert package.size == path.stat().st_size + + def test_download_generic_package(project): package = project.generic_packages.download( package_name=package_name,