1

I am trying to list the project list by names not by ID#. This goes the same with listing the groups by name not by ID#.

I tried running some python-gitlab codes with python 2.7 version but I only get the ID#.

projects = gl.projects.list()
groups = gl.groups.list()

These two lines only gives the ID# of the projects and the groups but that's not what I want. What I want to know if it's possible to only get the name listed in groups and projects. Is that possible?

2 Answers 2

2

There is an attribute called "attributes". Try the following:

for project in gl.projects.list():
    # print everything
    print(project.attributes)
    # or a single attribute
    print(project.attributes['name'])

The same for groups:

for group in gl.groups.list():
    # print everything
    print(group.attributes)
    # or a single attribute
    print(group.attributes['name'])
Sign up to request clarification or add additional context in comments.

3 Comments

This works great! How would you return the list as the print statement prints the list of project names and group names? I tried the return statement but I cannot get the list as print statement would do it. I tried with append syntax as well but I am not getting it right. I get [u'pro1', u'pro2', ..] I just one something like... pro1 and pro2 like a list.
It is not clear to me what your current problem is. You already tried to append to a list within the for loop? Initializing a list before the loop and then doing myList.append(group.attributes['name']) in the loop should work.
I am coming back to this. Why does this even print the subgroups as well? I only want to print the groups not the subgroups from git site.
0

Use pagination if you have more than 20 projects in the group

    gl = gitlab.Gitlab('https://gitlab.com', private_token='Token')
    group = gl.groups.get(group_id, lazy=True)
    project_lst=group.projects.list(as_list=False)  #pagination
    for item in project_lst:
        project_id = gl.projects.get(item.attributes['id'])
        ......................................
        ......................................

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.