Skip to content

Instantly share code, notes, and snippets.

@lucca65
Forked from uri/bitbucket-import.rb
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucca65/76833948bdf16a4d3e1d to your computer and use it in GitHub Desktop.
Save lucca65/76833948bdf16a4d3e1d to your computer and use it in GitHub Desktop.
Import public repos from Bitbucket to Github
require 'httparty'
class Github
include HTTParty
base_uri "https://api.github.com"
BaseDirectory = "/Users/ugorelik/programming/repos"
def initialize(u, p)
@auth = { username: u, password: p}
load_bitbucket_repos
# self.class.get "/", @auth
end
def import
Dir.chdir BaseDirectory
@bitbucket_repos.each do |repo|
`git clone git@bitbucket.org:#{@auth[:username]}/#{repo[:name]}.git`
Dir.chdir "./#{repo[:name]}"
create_repo repo[:name], repo[:description]
`git remote add github git@github.com:#{@auth[:username]}/#{repo[:name]}.git`
`git push github master`
# Clean up
Dir.chdir ".."
`rm -rf #{repo[:name]}`
end
end
def create_repo name, description=""
options = {:body => {:name => name, :description => description}.to_json}
options.merge!({:basic_auth => @auth})
response = self.class.post("/user/repos", options)
end
def delete_repo name
self.class.delete("/repos/#{@auth[:username]}/#{name}", :basic_auth => @auth)
end
def test_request
reponse = self.class.get("/users/#{@auth[:username]}/repos")
end
def save_repo_slugs_to_file
open('repo-slugs.txt', 'w') do |f|
@bitbucket_repos.each do |repo|
f.puts repo[:name]
end
end
end
private
def load_bitbucket_repos
response = HTTParty.get "https://api.bitbucket.org/1.0/users/#{@auth[:username]}/" # My username is the same for bitbucket and github
@bitbucket_repos = response['repositories'].map { |repo| {:name => repo["slug"], :description => repo['description']}}
end
end
github = Github.new "username", "password"
github.import
# github.save_repo_slugs_to_file
# github.delete_repo "the-blob"
# github.delete_repo "prototype-x"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment