How do you create a project in a specific group via GitLab API?

Viewed 4736

I tried to use GitLab API to create a new project. This worked, but this is in my user space:

        curl \
            --header "Authorization: Bearer ${GITLAB_API_TOKEN}" \
            --request POST \
            "https://gitlab.com/api/v4/projects/?name=$test-proj"

But I wanted to do it under a specific group with group_id <group_id> (I blanked it here). The most sensible approach that occured to me was:

        curl \
            --header "Authorization: Bearer ${GITLAB_API_TOKEN}" \
            --request POST \
            "https://gitlab.com/api/v4/groups/<group_id>/projects/?name=test-proj

But this did not work. Are there any suggestions on how I could achieve this?

I consulted the following references

The GitLab documentation mentions the path or namespace_id attribute (although I would actually be in search of a group_id attribute). I am not sure about path and how to specify that. I tried - without success - to retrieve the namespace_id via

curl --header "PRIVATE-TOKEN: ${GITLAB_API_TOKEN}" "https://gitlab.example.com/api/v4/namespaces"

It is well possible that I might not have the rights to do the required operation. Therefore a reference to an official (GitLab) documentation together with a test curl command that works would be very helpful to me - thank you!

3 Answers

For anyone looking for the direct command:

STEP 1:

Navigate as below and identify the group ID from GUI:

Admin Area -> Groups -> GroupName

enter image description here

STEP 2:

Construct the command with two parameters viz., name, namespace_id

curl --header "PRIVATE-TOKEN: <myprivatetoken>" -X POST "https://gitlab.com/api/v4/projects?name=myexpectedrepo&namespace_id=38"

Both users and groups are considered "namespaces" as far as the Gitlab API is concerned, so you can use either a group ID or a username in the namespace_id field. You can see this in use by getting a single namespace with either a Group ID (that you can see in the /groups API call, or from a Group's "profile" page) or a username:

# this will show the namespace details of the Group with ID 54
curl --header "PRIVATE-TOKEN: ${TOKEN}" "https://gitlab.com/api/v4/namespaces/54

# this will show the namespace details of the User with username my-username
curl --header "PRIVATE-TOKEN: ${TOKEN}" "https://gitlab.com/api/v4/namespace/my-username

If you have the appropriate access level, you can assign a git remote to your local repository that includes your group name. Then, the push event will trigger GitLab to create the remote repository.

$ mkdir project_name && cd $_
$ echo "# project_name" > README.md
$ git init; git add .; git commit -m initial
$ GITLAB_HOST="gitlab.com"
$ git remote add origin git@${GITLAB_HOST}:group_name/project_name.git
$ git push origin HEAD

Then, point your browser to ${GITLAB_HOST}/group_name/project_name

https://docs.gitlab.com/ee/user/group/#specify-who-can-add-projects-to-a-group

Related