Skip to content

repository_blob v4 usage unclear #312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mgoodhand opened this issue Sep 19, 2017 · 2 comments
Closed

repository_blob v4 usage unclear #312

mgoodhand opened this issue Sep 19, 2017 · 2 comments

Comments

@mgoodhand
Copy link

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?

@gpocentek
Copy link
Contributor

Hi,

You can use the files API in v4 to get a file content:

f = p.files.get('README.rst', 'master')  # retrieve the file metadata and content as base64
print(f.decode())  # print the content

# Or
print(p.files.raw('README.rst', 'master'))

The docs are probably not good enough, I'll update them. Thanks for reporting the issue!

@mgoodhand
Copy link
Author

Great. Thanks!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants