Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all the projects and all the groups

Tags:

gitlab

What is the easiest method to list all the projects and groups in GitLab using my private token?

like image 798
Bond Avatar asked Jul 12 '17 23:07

Bond


People also ask

What are groups in GitLab?

In GitLab, you use groups to manage one or more related projects at the same time. You can use groups to manage permissions for your projects. If someone has access to the group, they get access to all the projects in the group.

How do I find my GitLab Project ID?

On the Edit Project page there is a Project ID field in the top right corner. (You can also see the ID on the CI/CD pipelines page, in the exameple code of the Triggers section.) In older versions, you can see it on the Triggers page, in the URLs of the example code.

Where are GitLab files stored?

GitLab stores repositories on repository storage. Repository storage is either: A gitaly_address , which points to a Gitaly node. A path , which points directly to the directory where the repositories are stored.


2 Answers

If only your private token is available, you can only use the API:

PROJECTS

Use the following command to request projects:

curl "https://<host>/api/v4/projects?private_token=<your private token>" 

This will return you the first 20 entries. To get more you can add the paramater per_page

curl "https://<host>/api/v4/projects?private_token=<your private token>&per_page=100" 

with this parameter you can request between 20and 100 entries. https://docs.gitlab.com/ce/api/README.html#pagination

If you now want all projects, you have to loop through the pages. To get to another page add the parameter page.

curl "https://<host>/api/v4/projects?private_token=<your private token>&per_page=100&page=<page_number>" 

Now you may want to know how many pages there are. For that, add the curl parameter --head. This will not return the payload, but the header.

The result should look like this:

HTTP/1.1 200 OK Server: nginx Date: Thu, 13 Jul 2017 17:43:24 GMT Content-Type: application/json Content-Length: 29428 Cache-Control: no-cache Link: <request link> Vary: Origin X-Frame-Options: SAMEORIGIN X-Next-Page: 2 X-Page: 1 X-Per-Page: 20 X-Prev-Page: X-Request-Id: 80ecc167-4f3f-4c99-b09d-261e240e7fe9 X-Runtime: 4.117558 X-Total: 312257 X-Total-Pages: 15613 Strict-Transport-Security: max-age=31536000 

The two interesting parts here are X-Totaland X-Total-Pages. The first is the count of available entries and the second the count of total pages.

I suggest to use python or some other kind of script to handle the requests and concat the results at the end.

If you want to refine the search, consult this wiki page: https://docs.gitlab.com/ce/api/projects.html#projects-api

GROUPS

For groups simply replace projects with groups in the curls. https://docs.gitlab.com/ce/api/groups.html#list-groups


UPDATE:

Here is the official list of Gitlab API clients/wrappers: https://about.gitlab.com/partners/technology-partners/#api-clients
I highly recommend using one of these.

like image 191
secustor Avatar answered Sep 20 '22 15:09

secustor


Using the python-gitlab module we can get all groups and projects inside it. But if you have more than 20 groups or 20 projects inside of any group use pagination.

import gitlab gl = gitlab.Gitlab('https://gitlab.com', private_token='Token') groups = gl.groups.list() for each in groups:     group = gl.groups.get(each, lazy=True)     project_lst=group.projects.list(as_list=False)  #pagination     for item in project_lst:     project_id = gl.projects.get(item.attributes['id'])         ......................................         ...................................... 
like image 43
Ramesh Reddy Avatar answered Sep 16 '22 15:09

Ramesh Reddy