Skip to content

Instantly share code, notes, and snippets.

@uri
Created June 25, 2012 23:02
Show Gist options
  • Save uri/2992008 to your computer and use it in GitHub Desktop.
Save uri/2992008 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 [email protected]:#{@auth[:username]}/#{repo[:name]}.git`
Dir.chdir "./#{repo[:name]}"
create_repo repo[:name], repo[:description]
`git remote add github [email protected]:#{@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