Skip to content

Commit 0d1ace1

Browse files
author
Gauvain Pocentek
committed
[v4] Try to make the files raw() method work
1 parent 0c3fe39 commit 0d1ace1

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

gitlab/v4/objects.py

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,13 +1498,12 @@ class ProjectFile(GitlabObject):
14981498
_url = '/projects/%(project_id)s/repository/files'
14991499
canList = False
15001500
requiredUrlAttrs = ['project_id']
1501-
requiredGetAttrs = ['file_path', 'ref']
1501+
requiredGetAttrs = ['ref']
15021502
requiredCreateAttrs = ['file_path', 'branch', 'content',
15031503
'commit_message']
15041504
optionalCreateAttrs = ['encoding']
15051505
requiredDeleteAttrs = ['branch', 'commit_message', 'file_path']
15061506
shortPrintAttr = 'file_path'
1507-
getRequiresId = False
15081507

15091508
def decode(self):
15101509
"""Returns the decoded content of the file.
@@ -1518,6 +1517,34 @@ def decode(self):
15181517
class ProjectFileManager(BaseManager):
15191518
obj_cls = ProjectFile
15201519

1520+
def raw(self, filepath, ref, streamed=False, action=None, chunk_size=1024,
1521+
**kwargs):
1522+
"""Return the content of a file for a commit.
1523+
1524+
Args:
1525+
ref (str): ID of the commit
1526+
filepath (str): Path of the file to return
1527+
streamed (bool): If True the data will be processed by chunks of
1528+
`chunk_size` and each chunk is passed to `action` for
1529+
treatment.
1530+
action (callable): Callable responsible of dealing with chunk of
1531+
data.
1532+
chunk_size (int): Size of each chunk.
1533+
1534+
Returns:
1535+
str: The file content
1536+
1537+
Raises:
1538+
GitlabConnectionError: If the server cannot be reached.
1539+
GitlabGetError: If the server fails to perform the request.
1540+
"""
1541+
url = ("/projects/%s/repository/files/%s/raw" %
1542+
(self.parent.id, filepath.replace('/', '%2F')))
1543+
url += '?ref=%s' % ref
1544+
r = self.gitlab._raw_get(url, streamed=streamed, **kwargs)
1545+
raise_error_from_response(r, GitlabGetError)
1546+
return utils.response_content(r, streamed, action, chunk_size)
1547+
15211548

15221549
class ProjectPipeline(GitlabObject):
15231550
_url = '/projects/%(project_id)s/pipelines'
@@ -1861,33 +1888,6 @@ def repository_tree(self, path='', ref_name='', **kwargs):
18611888
raise_error_from_response(r, GitlabGetError)
18621889
return r.json()
18631890

1864-
def repository_blob(self, sha, filepath, streamed=False, action=None,
1865-
chunk_size=1024, **kwargs):
1866-
"""Return the content of a file for a commit.
1867-
1868-
Args:
1869-
sha (str): ID of the commit
1870-
filepath (str): Path of the file to return
1871-
streamed (bool): If True the data will be processed by chunks of
1872-
`chunk_size` and each chunk is passed to `action` for
1873-
treatment.
1874-
action (callable): Callable responsible of dealing with chunk of
1875-
data.
1876-
chunk_size (int): Size of each chunk.
1877-
1878-
Returns:
1879-
str: The file content
1880-
1881-
Raises:
1882-
GitlabConnectionError: If the server cannot be reached.
1883-
GitlabGetError: If the server fails to perform the request.
1884-
"""
1885-
url = "/projects/%s/repository/blobs/%s" % (self.id, sha)
1886-
url += '?%s' % (urllib.urlencode({'filepath': filepath}))
1887-
r = self.gitlab._raw_get(url, streamed=streamed, **kwargs)
1888-
raise_error_from_response(r, GitlabGetError)
1889-
return utils.response_content(r, streamed, action, chunk_size)
1890-
18911891
def repository_raw_blob(self, sha, streamed=False, action=None,
18921892
chunk_size=1024, **kwargs):
18931893
"""Returns the raw file contents for a blob by blob SHA.

0 commit comments

Comments
 (0)