Skip to content

feat: add support for select="package_file" in package upload #2581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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