Skip to content

Instantly share code, notes, and snippets.

@donnitriosa
Last active September 25, 2024 15:13
Show Gist options
  • Select an option

  • Save donnitriosa/afd70fc67458eecfdc7fd27cf6d183c2 to your computer and use it in GitHub Desktop.

Select an option

Save donnitriosa/afd70fc67458eecfdc7fd27cf6d183c2 to your computer and use it in GitHub Desktop.
This script is designed to create a new user in a GitLab instance using the GitLab API. It requires three arguments: the user's name, email, and username. The script retrieves a private token from the Git configuration to authenticate the API request.
#!/bin/bash
# Created by Donni Triosa (donni.triosa94@gmail.com)
# Description:
# This script is designed to create a new user in a GitLab instance using the GitLab API.
# It requires three arguments: the user's name, email, and username.
# The script retrieves a private token from the Git configuration to authenticate the API request.
# Prerequisites:
# 1. jq is installed (https://stedolan.github.io/jq/)
# 2. A GitLab private token is set in the Git configuration using 'git config --global credential.gitlab.api <your_token>'
# 3. The GitLab URL is set in the script (GITLAB_URL variable)
# 4. The user has the necessary permissions to create a new user in the GitLab instance.
# 5. The Token has scope 'read_api' and 'read_user' enabled. (https://gitlab.playcourt.id/help/user/profile/personal_access_tokens.md#personal-access-token-scopes)
# Usage:
# ./create_gitlab_user.sh <name> <email> <username>
# use double quotes for the name if it contains spaces
# Example:
# ./create_gitlab_user.sh "John Doe" john.doe@example.com johndoe
if [ "$#" -ne 3 ]; then
echo "Usage: $0 <name> <email> <username>"
exit 1
fi
# Retrieve Private Token from Git Config
PRIVATE_TOKEN=$(git config --global credential.gitlab.api)
# Check if PRIVATE_TOKEN is set
if [ -z "$PRIVATE_TOKEN" ]; then
echo "Error: GitLab private token is not set. Please configure it using 'git config --global credential.gitlab.api <your_token>'"
exit 1
fi
# Variables
NAME="$1"
EMAIL="$2"
USERNAME="$3"
## Change this to your GitLab URL (e.g. https://gitlab.com)
GITLAB_URL="https://your.gitlab-url.com"
# Create User
response=$(curl --silent --write-out "HTTPSTATUS:%{http_code}" --request POST "$GITLAB_URL/api/v4/users" \
--header "PRIVATE-TOKEN: $PRIVATE_TOKEN" \
--data "username=$USERNAME" \
--data "email=$EMAIL" \
--data "name=$NAME" \
--data "reset_password=true" \
--data "skip_confirmation=true")
# Extract the body and status
body=$(echo $response | sed -e 's/HTTPSTATUS\:.*//g')
status=$(echo $response | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
# Check if the request was successful
if [ $status -eq 201 ]; then
created_at=$(echo $body | jq -r '.created_at')
# Remove milliseconds and timezone for date parsing
created_at_no_ms=$(echo $created_at | sed -E 's/\.[0-9]+//; s/([+-][0-9]{2}:[0-9]{2})//')
formatted_date=$(date -j -f "%Y-%m-%dT%H:%M:%S" "$created_at_no_ms" +"%b %d, %Y %H:%M")
echo "User created successfully."
echo "Created at: $formatted_date"
else
echo "Failed to create user. Status: $status"
echo "Response: $body"
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment