Created
July 24, 2013 19:52
-
-
Save VizirAdmin/6073927 to your computer and use it in GitHub Desktop.
Script to import git repositories stored in Amazon S3 to your Bitbucket account.
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 'rubygems' | |
require 'aws/s3' | |
require 'bitbucket_rest_api' | |
DESTINATION_FOLDER = "codeplane" | |
S3_BUCKET = "S3_BUCKET_NAME" | |
S3_ACCESS_KEY_ID = "S3_ACCESS_KEY_ID" | |
S3_SECRET_ACCESS_KEY = "S3_SECRET_ACCESS_KEY" | |
BITBUCKET_USER_EMAIL = "BITBUCKET_USER_EMAIL" | |
BITBUCKET_PASSWORD = "BITBUCKET_PASSWORD" | |
BITBUCKET_URL = "BITBUCKET_URL" | |
def get_repositories_names | |
repositories_names = [] | |
AWS::S3::Bucket.find(S3_BUCKET).each do |object| | |
repositories_names << object.path.split("/").last | |
end | |
return repositories_names | |
end | |
def download_repositories(repositories_names) | |
repositories_names.each do |repo_name| | |
puts '*' * 100 | |
puts "DOWNLOADING #{repo_name}" | |
open("#{DESTINATION_FOLDER}/#{repo_name.downcase}", 'w') do |file| | |
AWS::S3::S3Object.stream(repo_name, S3_BUCKET) do |chunk| | |
file.write(chunk) | |
end | |
end | |
puts "DOWNLOADED #{repo_name}" | |
puts '*' * 100 | |
end | |
end | |
def upload_repositories_to_bitbucket(repositories_names, bitbucket) | |
repositories_names.each do |repo_name| | |
bitbucket_repo_name = repo_name.downcase.gsub('.tar.gz','') | |
puts '*' * 100 | |
puts "UPLOADING #{bitbucket_repo_name}" | |
system "mkdir #{DESTINATION_FOLDER}/#{bitbucket_repo_name}" | |
system "tar -xvzf #{DESTINATION_FOLDER}/#{repo_name} -C #{DESTINATION_FOLDER}/#{bitbucket_repo_name}" | |
begin | |
bitbucket.repos.create(:name => bitbucket_repo_name, :is_private => true) | |
rescue Exception => e | |
puts bitbucket_repo_name | |
puts e | |
next | |
end | |
system "cd #{DESTINATION_FOLDER}/#{bitbucket_repo_name}; git remote add bitbucket #{BITBUCKET_URL}/#{bitbucket_repo_name}.git; git push -u bitbucket --all;" | |
puts "UPLOADED #{bitbucket_repo_name}" | |
puts '*' * 100 | |
end | |
end | |
connection = AWS::S3::Base.establish_connection!( | |
access_key_id: S3_ACCESS_KEY_ID, | |
secret_access_key: S3_SECRET_ACCESS_KEY | |
) | |
bitbucket = BitBucket.new(basic_auth: "#{BITBUCKET_USER_EMAIL}:#{BITBUCKET_PASSWORD}") | |
system "mkdir #{DESTINATION_FOLDER}" | |
repositories_names = get_repositories_names | |
download_repositories = download_repositories(repositories_names) | |
upload_repositories_to_bitbucket(repositories_names, bitbucket) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment