Created
December 16, 2013 09:37
-
-
Save yasuoza/7984489 to your computer and use it in GitHub Desktop.
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
require 'net/http' | |
require 'json' | |
GITLAB_URL = 'http://source.yourhost.com' | |
GITLAB_TOKEN = '>>>>' | |
BITBUCKET_OWNER = '>>>>' | |
BITBUCKET_PASSWORD = '>>>>' | |
def create_bitbucket_repo(owner, password, repo_slug) | |
uri = URI("https://bitbucket.org/api/2.0/repositories/#{owner}/#{repo_slug}") | |
req = Net::HTTP::Post.new(uri) | |
req.basic_auth(owner, password) | |
req.set_form_data({is_private: true, forking_policy: 'no_forks'}) | |
res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| | |
http.request(req) | |
end | |
res.code | |
end | |
def get_gitlab_projects_from(url, token) | |
uri = URI.join(url, '/api/v3/projects') | |
req = Net::HTTP::Get.new(uri) | |
req['PRIVATE-TOKEN'] = token | |
res = Net::HTTP.start(uri.host, uri.port) do |http| | |
http.request(req) | |
end | |
JSON.parse(res.body) | |
end | |
def main | |
repos = get_gitlab_projects_from(GITLAB_URL, GITLAB_TOKEN) | |
pwd = Dir.pwd | |
repos.each do |repo| | |
repo_name = repo['name'] | |
p "Excuting #{repo_name}" | |
repo_url = repo['ssh_url_to_repo'] | |
system "git clone #{repo_url}" | |
repo_dir = repo_url.match(/\/(?<repo_dir>.*)\.git/)['repo_dir'] | |
Dir.chdir(repo_dir) | |
p create_bitbucket_repo(BITBUCKET_OWNER, BITBUCKET_PASSWORD, repo_dir) | |
system "git remote rm origin" | |
system "git remote add origin [email protected]:#{owner}/#{repo_dir.downcase}.git" | |
system "git push -u origin master" | |
Dir.chdir(pwd) | |
p "Done #{repo_name}!" | |
end | |
end | |
main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment