Skip to content

Instantly share code, notes, and snippets.

@thegauraw
Last active May 28, 2020 20:14
Show Gist options
  • Save thegauraw/da2a3429f19f603cf1c9b3b09553728b to your computer and use it in GitHub Desktop.
Save thegauraw/da2a3429f19f603cf1c9b3b09553728b to your computer and use it in GitHub Desktop.
How to clone all the repositories or projects from gitlab?
#!/usr/bin/env ruby
require 'rubygems'
require 'bundler/setup'
# TWO WAYS TO RUN THIS SCRIPT:
# ----------------------------
# --->>> THE FIRST WAY> Run this script as executable: `./clone_gitlab_projects.rb`
# You need to make the script executable: `chmod +x gitlab_clone_all_repos.rb`
# ---- ANYTHING ABOVE THIS LINE IS ONLY REQUIRED IF YOU WANT TO RUN THE SCRIPT WITH SINGLE COMMAND `./clone_gitlab_projects.rb` ---- #
# --->>> THE SECOND WAY> Run the script as regular ruby script: `ruby clone_gitlab_projects.rb`
# You need to make sure that you have installed 'httparty' gem with: gem install httparty
# if not, check the top section for setup to run script as executable `./clone_gitlab_projects.rb`
# ABOUT THIS SCRIPT:
# ==================
# This script clones the repo from gitlab
# You can
# either: run the script and enter values in run time (DEFAULT)
# or: change values of: ORGN_API_URL & PRIVATE_TOKEN at the top first and update (comment/uncomment) part in: def self.url_with_token(resource_url)
# ******* SCRIPT BEGINS HERE ********
require 'httparty'
# # if you want to define constant
# ORGN_API_URL = 'https://gitlab.example.com/api/v3/'
# PRIVATE_TOKEN = 'QALWKQFAGZDWQYDGHADS' # as query string
# visit - https://gitlab.example.com/profile/account
class MyGitlab
include HTTParty
def self.get_all resource
get(url_with_token(resource), verify: false).parsed_response
end
def self.url_with_token(resource_url)
# ONE WAY
# Enter values in runtime
print "Enter organization url for gitlab: "
orgn_api_url = gets.chomp
print "Enter private token for gitlab: "
private_token = gets.chomp
url = URI.join(orgn_api_url, resource_url)
"#{url}?private_token=#{private_token}"
###########################################
# # ANOTHER WAY
# # if you have defined constant
# url = URI.join(ORGN_API_URL, resource_url)
# "#{url}?private_token=#{PRIVATE_TOKEN}"
end
end
def main
projects = MyGitlab.get_all('projects')
# keys just for useful reference
# keys = ['name', 'description', 'ssh_url_to_repo', 'web_url', 'path_with_namespace']
count = projects.count
projects.each_with_index do |project, idx|
puts "\n#{(100.0 * idx / count).round(2)}% done"
puts "clonning project #{idx+1} of #{projects.count}..."
`git clone #{project['ssh_url_to_repo']}`
end
puts "\n\nClonning complete.\n\n"
end
if __FILE__ == $0
puts "Clones all the repo from gitlab"
puts "You must have: organization url:\n\te.g. https://gitlab.example.com/api/v3/"
puts "You must have: private token from your account (https://gitlab.example.com/profile/account):\n\te.g. QALWKQFAGZDWQYDGHADS"
main
end
@nathanbrauer
Copy link

nathanbrauer commented Feb 21, 2017

@albireo - care to share your modification? :)

@arasevic
Copy link

arasevic commented Dec 9, 2017

You just need to make the return string in line 36 to be the following:
"#{url}?private_token=#{PRIVATE_TOKEN}&per_page=100"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment