Skip to content

Commit 83dcabf

Browse files
dAnjouJohnVillalovos
authored andcommitted
feat(api): support file format for repository archive
1 parent eef8059 commit 83dcabf

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

docs/gl_objects/projects.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ Get the repository archive::
180180
# get the archive for a branch/tag/commit
181181
tgz = project.repository_archive(sha='4567abc')
182182

183+
# get the archive in a different format
184+
zip = project.repository_archive(format='zip')
185+
186+
.. note::
187+
188+
For the formats available, refer to
189+
https://docs.gitlab.com/ce/api/repositories.html#get-file-archive
190+
183191
.. warning::
184192

185193
Archives are entirely stored in memory unless you use the streaming feature.

gitlab/v4/objects/repositories.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,17 +186,18 @@ def repository_contributors(
186186
path = f"/projects/{self.get_id()}/repository/contributors"
187187
return self.manager.gitlab.http_list(path, **kwargs)
188188

189-
@cli.register_custom_action("Project", tuple(), ("sha",))
189+
@cli.register_custom_action("Project", tuple(), ("sha", "format"))
190190
@exc.on_http_error(exc.GitlabListError)
191191
def repository_archive(
192192
self,
193193
sha: str = None,
194194
streamed: bool = False,
195195
action: Optional[Callable[..., Any]] = None,
196196
chunk_size: int = 1024,
197+
format: Optional[str] = None,
197198
**kwargs: Any,
198199
) -> Optional[bytes]:
199-
"""Return a tarball of the repository.
200+
"""Return an archive of the repository.
200201
201202
Args:
202203
sha: ID of the commit (default branch by default)
@@ -206,6 +207,7 @@ def repository_archive(
206207
action: Callable responsible of dealing with chunk of
207208
data
208209
chunk_size: Size of each chunk
210+
format: file format (tar.gz by default)
209211
**kwargs: Extra options to send to the server (e.g. sudo)
210212
211213
Raises:
@@ -216,6 +218,8 @@ def repository_archive(
216218
The binary data of the archive
217219
"""
218220
path = f"/projects/{self.get_id()}/repository/archive"
221+
if format:
222+
path += "." + format
219223
query_data = {}
220224
if sha:
221225
query_data["sha"] = sha

tests/functional/api/test_repository.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import base64
2+
import tarfile
23
import time
4+
import zipfile
5+
from io import BytesIO
36

47
import pytest
58

@@ -48,14 +51,34 @@ def test_repository_tree(project):
4851
blob = project.repository_raw_blob(blob_id)
4952
assert blob.decode() == "Initial content"
5053

54+
snapshot = project.snapshot()
55+
assert isinstance(snapshot, bytes)
56+
57+
58+
def test_repository_archive(project):
5159
archive = project.repository_archive()
5260
assert isinstance(archive, bytes)
5361

5462
archive2 = project.repository_archive("main")
5563
assert archive == archive2
5664

57-
snapshot = project.snapshot()
58-
assert isinstance(snapshot, bytes)
65+
66+
@pytest.mark.parametrize(
67+
"format,assertion",
68+
[
69+
("tbz", tarfile.is_tarfile),
70+
("tbz2", tarfile.is_tarfile),
71+
("tb2", tarfile.is_tarfile),
72+
("bz2", tarfile.is_tarfile),
73+
("tar", tarfile.is_tarfile),
74+
("tar.gz", tarfile.is_tarfile),
75+
("tar.bz2", tarfile.is_tarfile),
76+
("zip", zipfile.is_zipfile),
77+
],
78+
)
79+
def test_repository_archive_formats(project, format, assertion):
80+
archive = project.repository_archive(format=format)
81+
assert assertion(BytesIO(archive))
5982

6083

6184
def test_create_commit(project):

0 commit comments

Comments
 (0)