7

In GitLab you can create subgroups within a group and projects within those subgroups. The GitLab documentation allows you to list the subgroups of a group and the projects of a group, but I cannot find anything about listing projects of a subgroup. Does anyone know the curl command for this, or at least know that it's not supported yet?

I've tried:
curl -s -H 'PRIVATE-TOKEN: xxxxx' https://gitlab.com/api/v4/groups/mygroup/subgroups/mysubgroup/projects
curl -s -H 'PRIVATE-TOKEN: xxxxx' https://gitlab.com/api/v4/subgroups/mysubgroup/projects
and other similar variations with no luck so far.

chacd
  • 131
  • 1
  • 4

5 Answers5

12

Use %2f to specify full path to subgroup:

curl -s https://gitlab.com/api/v4/groups/gitlab-org%2fgitter/projects

See this issue.

giraffes
  • 121
  • 1
  • 4
11

To get all the projects of group and subgroups call the api like this

curl --silent --header "Private-Token: YOUR_GITLAB_TOKEN" https://gitlab.example.com/api/v4/groups/<group_name>/projects?include_subgroups=true

or using group id

<your_gitlab-url>/api/v4/groups/<group_ID>/projects?include_subgroups=true
Dashrath Mundkar
  • 7,956
  • 2
  • 28
  • 42
1

You should be able to list the projects of any group (subgroup or not) through said group ID:

See "List a group's projects"

GET /groups/:id/projects

So first get a list of the subgroups in order to get their IDs, then query their projects.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    Thanks for the response. That works with the numerical id, but not the actual name of the group, but I will use that as a final resort. – chacd Aug 24 '18 at 10:13
1

To get all soubgrups of group call the v4 gitlab API like this

GET /groups/:id/subgroups
rafapc2
  • 406
  • 4
  • 7
1

1. Get by groups API (Recommend)

Get All pages of the subproject:

GITLAB_API="https://<SITE>/api/v4/groups/<GROUP_NAME>/projects?include_subgroups=true&per_page=100"
PRIVATE_TOKEN="<your token>"

for ((page=1; ; page+=1)); do

  # Use cURL to get all projects
  response=$(curl --silent --header "PRIVATE-TOKEN: $PRIVATE_TOKEN" "$GITLAB_API&page=${page}")
  if [ $(jq length <<< "$response") -eq 0 ]; then
      break
  fi

  # Use jq to parse JSON response, extract project names, write as csv
  names=$(echo "$response" | jq -r '.[] | [ .path_with_namespace] | @csv')

  # Print project names to console
  echo "$names"

done

Gitlab docs is here.

2. Get by project API

Get all project and filter them.

GITLAB_API="https://<SITE>/api/v4/projects?per_page=100"
PRIVATE_TOKEN="<your token>"

for ((page=1; ; page+=1)); do

  # Use cURL to get all projects
  response=$(curl --silent --header "PRIVATE-TOKEN: $PRIVATE_TOKEN" "$GITLAB_API&page=${page}")
  if [ $(jq length <<< "$response") -eq 0 ]; then
      break
  fi

  # Use jq to parse JSON response, extract project names, write as csv
  names=$(echo "$response" | jq -r '.[] | select(.path_with_namespace | startswith("<GROUP_NAME>")) | [ .path_with_namespace] | @csv')

  # Print project names to console
  echo "$names"

done
Jess Chen
  • 3,136
  • 1
  • 26
  • 35