Skip to content

repository_blob v4 usage unclear #312

@mgoodhand

Description

@mgoodhand

I'm using python-gitlab (1.0.0) to grab the contents of a text file in a repository.

The introductory sections recommend the use of the v4 API, but the Projects examples still appear to use v3:

file_content = p.repository_blob('master', 'README.rst')

The help for gitlab.v4.objects.Project.repository_blob says:

 |  repository_blob(*args, **kwargs)
 |      Return a blob by blob SHA.
 |
 |      Args:
 |          sha(str): ID of the blob
 |          **kwargs: Extra options to send to the server (e.g. sudo)
 |
 |      Raises:
 |          GitlabAuthenticationError: If authentication is not correct
 |          GitlabGetError: If the server failed to perform the request
 |
 |      Returns:
 |          str: The blob metadata

When I invoke this method, I get back a dict rather than a string:

>>> p.repository_blob('584b989ef38c8dadc5378508254e61dd0553e9a6')
{u'content': u'VGhpcyBwcm9qZWN0IHN0b3JlcyB0aGUgZGF0YSB0aGF0IGZlZWRzIHRoZSB0aHVyc2RheUAgcmVwb3J0cyBhbmQgZGFzaGJvYXJkLg==', u'sha': u'584b989ef38c8dadc5378508254e61dd0553e9a6', u'encoding': u'base64', u'size': 76}

Is there a method in v4 comparable to the v3 repository_blob?

My current code to grab the contents of a file in v4 is verbose and inefficient:

p = gl.projects.get(project_name)
items = p.repository_tree(dir_name)
match = [m for m in items if m['name']==file_name]
if len(match) == 1:
    m = match[0]
    content = p.repository_blob(m['id'])['content']

Is there a better way?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions