Last active
August 29, 2015 14:25
-
-
Save macowie/974e8c362360db175d9c to your computer and use it in GitHub Desktop.
Quick 'n Dirty Gitlab Import script
This file contains hidden or 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
#!/usr/bin/env ruby | |
require 'httparty' | |
require 'json' | |
LEGACY_GIT = '[email protected]' | |
GITLAB_URL = "hostname.yourdomain.com" | |
TOKEN = "YOUR_GITLAB_API_TOKEN" | |
DEFAULT_HTTP_OPTIONS = { headers: { 'Content-Type' => 'application/json' } } | |
def create_project(path) | |
name = path.split("/").last | |
group_name = path.split("/").first | |
options = { body: { name: name, namespace_id: find_group_id(group_name), private_token: TOKEN }.to_json }.merge(DEFAULT_HTTP_OPTIONS) | |
HTTParty.post("https://#{GITLAB_URL}/api/v3/projects", options) | |
end | |
def find_group_id(group_name) | |
req = HTTParty.get("https://#{GITLAB_URL}/api/v3/groups", { search: group_name, private_token: TOKEN }.merge(DEFAULT_HTTP_OPTIONS)) | |
req.parsed_response.first['id'] | |
end | |
def clone_old(repo_name) | |
`git clone --mirror #{LEGACY_GIT}:#{repo_name}` | |
end | |
def push_new(repo_name, new_path) | |
Dir.chdir "#{repo_name}.git" do | |
`git remote add gitlab git@#{GITLAB_URL}:#{new_path}.git` | |
`git push -f --tags gitlab refs/heads/*:refs/heads/*` | |
end | |
end | |
File.open('mapping.txt').each do |line| | |
old_name = line.split(' ').first | |
new_path = line.split(' ').last | |
p "#{old_name} to #{new_path}..." | |
create_project(new_path) | |
clone_old(old_name) | |
push_new(old_name, new_path) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment