FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

repository_blob v4 usage unclear · Issue #312 · python-gitlab/python-gitlab · GitHub

Repository navigation

repository_blob v4 usage unclear #312

Description

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?

Activity

  1. gpocentek commented on Sep 19, 2017

    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!

  2. mgoodhand commented on Sep 20, 2017

    Author

    Great. Thanks!

  3. locked as resolved and limited conversation to collaborators on Nov 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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


      Back | FazBrowse Home | New Git URL