Created
January 5, 2024 23:26
-
-
Save euberdeveloper/3aae1b0f459e0c60b23d403603d25952 to your computer and use it in GitHub Desktop.
A gist to clone groups and subgroups of gitlab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Syntax: [./git_groups_deep_clone.sh <gitlab_host> <gitlab_private_token> <root_group_id> | |
gitlab_host=$1 | |
gitlab_private_token=$2 | |
root_group_id=$3 | |
git_shallow=" --depth 1 " | |
log() | |
{ | |
local timestamp=$(date +%Y-%m-%d\ %H:%M:%S) | |
local log_prefix="[$timestamp] >>>>>>>>>>> " | |
echo "$log_prefix $1" | |
} | |
get_group_name() | |
{ | |
local gitlab_host=$1 | |
local gitlab_private_token=$2 | |
local group_id=$3 | |
local curl_result=$(curl -s --header "PRIVATE-TOKEN: $gitlab_private_token" https://$gitlab_host/api/v4/groups/$group_id) | |
local group_name=$(echo $curl_result | jq -r ".name") | |
echo $group_name | |
} | |
get_projects_urls() | |
{ | |
local gitlab_host=$1 | |
local gitlab_private_token=$2 | |
local group_id=$3 | |
local curl_result=$(curl -s --header "PRIVATE-TOKEN: $gitlab_private_token" https://$gitlab_host/api/v4/groups/$group_id) | |
local projects_urls=$(echo $curl_result | jq -r ".projects[].http_url_to_repo ") | |
echo $projects_urls | |
} | |
get_subgroups_ids() | |
{ | |
local gitlab_host=$1 | |
local gitlab_private_token=$2 | |
local group_id=$3 | |
local curl_result=$(curl -s --header "PRIVATE-TOKEN: $gitlab_private_token" https://$gitlab_host/api/v4/groups/$group_id/subgroups) | |
local subgroups_ids=$(echo $curl_result | jq -r ".[].id") | |
echo $subgroups_ids | |
} | |
clone_group() | |
{ | |
local gitlab_host=$1 | |
local gitlab_private_token=$2 | |
local group_id=$3 | |
local shallow_args=$(test "$4" == "true" && echo $git_shallow || echo '') | |
local group_name=$(get_group_name $gitlab_host $gitlab_private_token $group_id) | |
local projects_urls=$(get_projects_urls $gitlab_host $gitlab_private_token $group_id) | |
mkdir "$group_name" | |
cd "$group_name" | |
for project_url in $projects_urls | |
do | |
git clone $shallow_args $project_url | |
done | |
} | |
log "Cloning group $root_group_id..." | |
clone_group $gitlab_host $gitlab_private_token $root_group_id true | |
log "Cloned" | |
log "Getting subgroups..." | |
subgroups_ids=$(get_subgroups_ids $gitlab_host $gitlab_private_token $root_group_id) | |
log "Gotten" | |
mkdir subgroups | |
cd subgroups | |
for subgroup_id in $subgroups_ids | |
do | |
log "Cloning group $subgroup_id..." | |
clone_group $gitlab_host $gitlab_private_token $subgroup_id true | |
cd .. | |
log "Cloned" | |
done | |
log "Done!!!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment