DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • GraphQL vs REST API: Which Is Better for Your Project in 2025?
  • Update User Details in API Test Client Using REST Assured [Video]
  • Create User API Test Client With REST Assured [Video]
  • How to Make a REST API Call in Angular

Trending

  • How to Migrate From LangChainGo to Genkit Go
  • Moving 900+ GB of Prometheus Without a Cutover Weekend
  • Building IoT Time-Series Applications With Java and Apache IoTDB
  • How to Format Articles for DZone

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Using the GitLab REST API to Create a Git Projects

Using the GitLab REST API to Create a Git Projects

It's time to Git some REST.

By 
Emmerson Miranda
·
Oct. 04, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
29.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Git some REST

Git some REST with the GitLab REST API.

Git 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.

GitLab user account settings

After filling in the web form, we will end up with the token we need for our REST calls.

Personal Access Tokens

Once we have the token, we can use the following APIs:

  • Groups 

  • Projects 

  • Branches 

#######################################################
# 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.

Check project in GitLab

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.

REST Web Protocols API GitLab Git

Opinions expressed by DZone contributors are their own.

Related

  • GraphQL vs REST API: Which Is Better for Your Project in 2025?
  • Update User Details in API Test Client Using REST Assured [Video]
  • Create User API Test Client With REST Assured [Video]
  • How to Make a REST API Call in Angular

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook