Using the GitLab REST API to Create a Git Projects
It's time to Git some REST.
Join the DZone community and get the full member experience.
Join For FreeGit is one of the most popular SCM used currently and GitLab is one of the most popular products used to manage git repositories in a centralized way — not to mention, it comes with lots of handy features.
Instead of using a flat structure, it allows the user to organize repositories into subgroups and use templates to generate repositories and apply security per group to create a group structure. When it comes to projects, we have two options, one is using the WebUI console and the other is using the REST API.
One of the aims of each IT team is using automation as much as possible for repetitive tasks such as creating groups and projects and assigning people to projects, among other things. This is when the REST API comes in handy.
Let's imagine we want to create a project called “hello-world-project-01” and we want to organize the repository under some logical structure like “my-programme -> fintech.” Then, as per the gitflow definition, the repository will have a minimum of three branches: master, develop, and feature branch.
First of all, we have to obtain the token for the user to make HTTP calls in the user account settings (right-top corner) and access the token's settings.
After filling in the web form, we will end up with the token we need for our REST calls.
Once we have the token, we can use the following APIs:
#######################################################
# Gitlab REST API Examples
#######################################################
# API Documentation https://docs.gitlab.com/ee/api/api_resources.html
export GITLAB_SERVER=http://localhost
export GITLAB_TOKEN=sR4NN_HsMxhHWgju17pn
export GITLAB_ROOTGROUP=my-programme
export GITLAB_SUBGROUP=fintech
export GITLAB_PROJECT=hello-world-project-01
export GITLAB_FEATURE=feature/my-feature-01
#
#creating root group
#
rootGroupId=$(curl --header "PRIVATE-TOKEN: $GITLAB_TOKEN" "$GITLAB_SERVER/api/v4/groups?search=$GITLAB_ROOTGROUP" | jq '.[0]["id"]' )
if [ $rootGroupId == "null" ]
then
rootGroupId=$(curl -d "name=$GITLAB_ROOTGROUP&path=$GITLAB_ROOTGROUP&visibility=private&lfs_enabled=true&description=Root group" -X POST "$GITLAB_SERVER/api/v4/groups" -H "PRIVATE-TOKEN: $GITLAB_TOKEN" | jq '.["id"]')
fi
echo "Root group Id: $rootGroupId"
#
#creating sub group
#
rootSubGroupId=$(curl --header "PRIVATE-TOKEN: $GITLAB_TOKEN" "$GITLAB_SERVER/api/v4/groups/$rootGroupId/subgroups?search=$GITLAB_SUBGROUP" | jq '.[0]["id"]' )
if [ $rootSubGroupId == "null" ]
then
rootSubGroupId=$(curl -d "name=$GITLAB_SUBGROUP&path=$GITLAB_SUBGROUP&visibility=private&lfs_enabled=true&description=$GITLAB_SUBGROUP programme&parent_id=$rootGroupId" -X POST "$GITLAB_SERVER/api/v4/groups" -H "PRIVATE-TOKEN: $GITLAB_TOKEN" | jq '.["id"]')
fi
echo "Sub group Id: $rootSubGroupId"
#
#project creation
#
projectId=$(curl "$GITLAB_SERVER/api/v4/groups/$rootSubGroupId/projects?search=$GITLAB_PROJECT" -H "PRIVATE-TOKEN: $GITLAB_TOKEN" | jq '.[0]["id"]' )
if [ $projectId == "null" ]
then
projectId=projectId=$(curl -d "path=$GITLAB_PROJECT&namespace_id=$rootSubGroupId" -X POST "$GITLAB_SERVER/api/v4/projects" -H "PRIVATE-TOKEN: $GITLAB_TOKEN" | jq '.["id"]')
fi
echo "Project Id: $projectId"
#
#Clonning git project and generating basic java maven structure
#
mvn archetype:generate -B -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DgroupId=edu.emmerson.gitlab -DartifactId=$GITLAB_PROJECT -Dversion=0.0.1
cd $GITLAB_PROJECT
git init
git remote add origin git@localhost:$GITLAB_ROOTGROUP/$GITLAB_SUBGROUP/$GITLAB_PROJECT.git
git add .
git commit -m "Initial commit"
git push -u origin master
#
# Git branches initial structure as per gitflow
#
projectId=$(curl "$GITLAB_SERVER/api/v4/groups/$rootSubGroupId/projects?search=$GITLAB_PROJECT" -H "PRIVATE-TOKEN: $GITLAB_TOKEN" | jq '.[0]["id"]' )
#create develop branch
curl -d "branch=develop&ref=master" -X POST "$GITLAB_SERVER/api/v4/projects/$projectId/repository/branches" -H "PRIVATE-TOKEN: $GITLAB_TOKEN" | jq
#create feature branch
curl -d "branch=$GITLAB_FEATURE&ref=develop" -X POST "$GITLAB_SERVER/api/v4/projects/$projectId/repository/branches" -H "PRIVATE-TOKEN: $GITLAB_TOKEN" | jq
After running the script, we can check in GitLab to see if the project and its branches have been created.
Summary
GitLab provides a good documentation on REST APIs that's easy-to-use when enabling automation tasks. You can choose different flavors on how to use that. However, in this post, I chose to use Shell scripts, but you can use any other language you are comfortable; in the end, Bob is your uncle.
Opinions expressed by DZone contributors are their own.
Comments