Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gitlab API for all projects under group

I want to get a list of all the projects which are under a particular group in Gitlab. Here is the example scenario:

Group A (id: 1) has 3 Project

Group A / Project 1

Group A / Project 2

Group A / Project 3

Group B (id: 2) has 5 Projects

Group B / Project 1

Group B / Project 2

Group B / Project 3

Group B / Project 4

Group B / Project 5

Now if I hit the rest api GET /groups it will give me only the list of groups. If i hit the rest api GET /projects/all, it will give me a list of all the projects.

What I am looking for, is an operation something like GET /groups/:groupid/projects/all

That is: all the projects for that particular group. Like if I say GET /groups/1/projects/all it will give me Project 1, Project 2 and Project 3.

The only way I can think of is to get a list of all the projects and loop over them to see if it matches my group name, but this will be lot of unnecessary parsing.

How can I achieve this in a better way?

I am working on Gitlab CE 7.2.1. I am referring the Gitlab API documententation

like image 860
Raghuveer Avatar asked Jul 19 '15 06:07

Raghuveer


People also ask

What is group and subgroup in GitLab?

With GitLab Groups you can assemble related projects together and grant members access to several projects at once. A subgroup, also known as nested group or hierarchical group, is essentially a group within a group. It has the same features a top-level group has, with some limitations.

Can a GitLab project have multiple repositories?

One Solution: Gitlab supports the creation of groups of projects/repos, which can be managed as a project consisting of multiple repos.

What is GitLab REST API?

The GitLab API allows you to perform many of the actions you typically do when using the user interface. Using the API allows us to automate the process and eliminate any user interaction.


4 Answers

This is fairly handy if you use curl:

curl --header "PRIVATE-TOKEN: xxxxxxxxxxxxxxx" http://gitlab.your_namespace.com/api/v4/groups/your_group/projects
like image 139
Dante Avatar answered Oct 20 '22 18:10

Dante


You can also use the recently released Gitlab GraphQL API to query groups by name :

{
  group(fullPath: "your_group_here") {
    projects {
      nodes {
        name
        description
        httpUrlToRepo
        nameWithNamespace
        starCount
      }
    }
  }
}

You can go to the following URL : https://[your_gitlab_host]/-/graphql-explorer and past the above query

The Graphql endpoint is a POST on "https://$gitlab_url/api/graphql" An example using curl and jq:

gitlab_url=<your gitlab host>
access_token=<your access token>
group_name=<your group>

curl -s -H "Authorization: Bearer $access_token" \
     -H "Content-Type:application/json" \
     -d '{ 
          "query": "{ group(fullPath: \"'$group_name'\") { projects {nodes { name description httpUrlToRepo nameWithNamespace starCount}}}}"
      }' "https://$gitlab_url/api/graphql" | jq '.'
like image 29
Bertrand Martel Avatar answered Oct 20 '22 19:10

Bertrand Martel


Adding to @Dante's answer,

This gives first 20 projects in the group.

curl --header "PRIVATE-TOKEN: xxxxxxxxxxxxxxx" https://gitlab.your_namespace.com/api/v4/groups/your_group_id/projects

To get more projects we should add 'page' and 'per_page' parameter.

The below request will fetch you up to 100 projects under requested group.

 curl --header "PRIVATE-TOKEN: xxxxxxxxxxxxxxx" https://gitlab.your_namespace.com/api/v4/groups/your_group_id/projects?&per_page=100" .  

If you now want all projects, you have to loop through the pages. Change the page parameter.

Add json_pp to your request to get a nicely formatted output.

curl --header "PRIVATE-TOKEN: xxxxxxxxxxxxxxx" https://gitlab.your_namespace.com/api/v4/groups/your_group_id/projects | json_pp
like image 8
nandeesh Avatar answered Oct 20 '22 19:10

nandeesh


I tested on Gitlab 8.0. Its group API can provide the project list under a specific group. Just send the GET request to http://gitlab.example.com/api/v3/groups/[group_id]?private_token=xxxxxxxxxxxx with your private token.

For example: http://gitlab.example.com/api/v3/groups/3?private_token=xxxxxxxxxxxxx.

In the JSON response, the list is an array under projects key.

like image 5
Jack Yu Avatar answered Oct 20 '22 18:10

Jack Yu