Skip to content

Commit ac51f09

Browse files
committed
feat(api): support file format for repository archive
1 parent ae97196 commit ac51f09

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

docs/gl_objects/projects.rst

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

173+
# get the archive in a different format
174+
zip = project.repository_archive(format='zip')
175+
176+
.. note::
177+
178+
For the formats available, refer to
179+
https://docs.gitlab.com/ce/api/repositories.html#get-file-archive
180+
173181
.. warning::
174182

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

gitlab/v4/objects/repositories.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,18 @@ def repository_contributors(self, **kwargs):
158158
path = "/projects/%s/repository/contributors" % self.get_id()
159159
return self.manager.gitlab.http_list(path, **kwargs)
160160

161-
@cli.register_custom_action("Project", tuple(), ("sha",))
161+
@cli.register_custom_action("Project", tuple(), ("sha", "format"))
162162
@exc.on_http_error(exc.GitlabListError)
163163
def repository_archive(
164-
self, sha=None, streamed=False, action=None, chunk_size=1024, **kwargs
164+
self,
165+
sha=None,
166+
streamed=False,
167+
action=None,
168+
chunk_size=1024,
169+
format=None,
170+
**kwargs
165171
):
166-
"""Return a tarball of the repository.
172+
"""Return an archive of the repository.
167173
168174
Args:
169175
sha (str): ID of the commit (default branch by default)
@@ -173,6 +179,7 @@ def repository_archive(
173179
action (callable): Callable responsible of dealing with chunk of
174180
data
175181
chunk_size (int): Size of each chunk
182+
format (str): file format (tar.gz by default)
176183
**kwargs: Extra options to send to the server (e.g. sudo)
177184
178185
Raises:
@@ -183,6 +190,8 @@ def repository_archive(
183190
str: The binary data of the archive
184191
"""
185192
path = "/projects/%s/repository/archive" % self.get_id()
193+
if format:
194+
path += "." + format
186195
query_data = {}
187196
if sha:
188197
query_data["sha"] = sha

tests/functional/api/test_repository.py

Lines changed: 26 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,35 @@ 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("master")
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+
print(archive)
82+
assert assertion(BytesIO(archive))
5983

6084

6185
def test_create_commit(project):

0 commit comments

Comments
 (0)