summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Ludwig <mail@danjou.de>2021-07-31 22:26:26 +0200
committerJohn Villalovos <john@sodarock.com>2021-12-21 09:04:22 -0800
commit83dcabf3b04af63318c981317778f74857279909 (patch)
treecd1501961fafcf609835436fa8b93c20bdddaf22
parenteef8059d63f4c882fca6390ae18e3002e86c90d9 (diff)
downloadgitlab-83dcabf3b04af63318c981317778f74857279909.tar.gz
feat(api): support file format for repository archive
-rw-r--r--docs/gl_objects/projects.rst8
-rw-r--r--gitlab/v4/objects/repositories.py8
-rw-r--r--tests/functional/api/test_repository.py27
3 files changed, 39 insertions, 4 deletions
diff --git a/docs/gl_objects/projects.rst b/docs/gl_objects/projects.rst
index 3ff7241..4bae083 100644
--- a/docs/gl_objects/projects.rst
+++ b/docs/gl_objects/projects.rst
@@ -180,6 +180,14 @@ Get the repository archive::
# get the archive for a branch/tag/commit
tgz = project.repository_archive(sha='4567abc')
+ # get the archive in a different format
+ zip = project.repository_archive(format='zip')
+
+.. note::
+
+ For the formats available, refer to
+ https://docs.gitlab.com/ce/api/repositories.html#get-file-archive
+
.. warning::
Archives are entirely stored in memory unless you use the streaming feature.
diff --git a/gitlab/v4/objects/repositories.py b/gitlab/v4/objects/repositories.py
index e7e434d..b520ab7 100644
--- a/gitlab/v4/objects/repositories.py
+++ b/gitlab/v4/objects/repositories.py
@@ -186,7 +186,7 @@ class RepositoryMixin(_RestObjectBase):
path = f"/projects/{self.get_id()}/repository/contributors"
return self.manager.gitlab.http_list(path, **kwargs)
- @cli.register_custom_action("Project", tuple(), ("sha",))
+ @cli.register_custom_action("Project", tuple(), ("sha", "format"))
@exc.on_http_error(exc.GitlabListError)
def repository_archive(
self,
@@ -194,9 +194,10 @@ class RepositoryMixin(_RestObjectBase):
streamed: bool = False,
action: Optional[Callable[..., Any]] = None,
chunk_size: int = 1024,
+ format: Optional[str] = None,
**kwargs: Any,
) -> Optional[bytes]:
- """Return a tarball of the repository.
+ """Return an archive of the repository.
Args:
sha: ID of the commit (default branch by default)
@@ -206,6 +207,7 @@ class RepositoryMixin(_RestObjectBase):
action: Callable responsible of dealing with chunk of
data
chunk_size: Size of each chunk
+ format: file format (tar.gz by default)
**kwargs: Extra options to send to the server (e.g. sudo)
Raises:
@@ -216,6 +218,8 @@ class RepositoryMixin(_RestObjectBase):
The binary data of the archive
"""
path = f"/projects/{self.get_id()}/repository/archive"
+ if format:
+ path += "." + format
query_data = {}
if sha:
query_data["sha"] = sha
diff --git a/tests/functional/api/test_repository.py b/tests/functional/api/test_repository.py
index 06d4297..ecef1f1 100644
--- a/tests/functional/api/test_repository.py
+++ b/tests/functional/api/test_repository.py
@@ -1,5 +1,8 @@
import base64
+import tarfile
import time
+import zipfile
+from io import BytesIO
import pytest
@@ -48,14 +51,34 @@ def test_repository_tree(project):
blob = project.repository_raw_blob(blob_id)
assert blob.decode() == "Initial content"
+ snapshot = project.snapshot()
+ assert isinstance(snapshot, bytes)
+
+
+def test_repository_archive(project):
archive = project.repository_archive()
assert isinstance(archive, bytes)
archive2 = project.repository_archive("main")
assert archive == archive2
- snapshot = project.snapshot()
- assert isinstance(snapshot, bytes)
+
+@pytest.mark.parametrize(
+ "format,assertion",
+ [
+ ("tbz", tarfile.is_tarfile),
+ ("tbz2", tarfile.is_tarfile),
+ ("tb2", tarfile.is_tarfile),
+ ("bz2", tarfile.is_tarfile),
+ ("tar", tarfile.is_tarfile),
+ ("tar.gz", tarfile.is_tarfile),
+ ("tar.bz2", tarfile.is_tarfile),
+ ("zip", zipfile.is_zipfile),
+ ],
+)
+def test_repository_archive_formats(project, format, assertion):
+ archive = project.repository_archive(format=format)
+ assert assertion(BytesIO(archive))
def test_create_commit(project):