Skip to content

Commit

Permalink
feat: add support for select="package_file" in package upload
Browse files Browse the repository at this point in the history
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
  • Loading branch information
JohnVillalovos committed May 28, 2023
1 parent 86eba06 commit 244a825
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
25 changes: 14 additions & 11 deletions gitlab/v4/objects/packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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",
Expand Down
17 changes: 17 additions & 0 deletions tests/functional/api/test_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"


Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 244a825

Please sign in to comment.