Commits

Kamil Trzciński committed a1b8df3 Draft

Added GitHub APIv3 support using Pygithub3.
Added GIT support for BitBucket's repositories.

  • Participants
  • Parent commits 0199378

Comments (0)

Files changed (5)

bitbucket2github/bitbucket.py

             "name":repo.get('slug'),
             "description":repo.get('description', ''),
             "homepage":repo.get('website', ''),
+			"scm":repo.get('scm', 'hg')
         } for repo in response.get('repositories')
     ]
 
     if not repo_exists(reponame, username):
         print "Creating " + reponame + " in BitBucket"
         # Somehow BitBucket authentication with urllib2 is not working. So using this ugly approach.
-        cmd = 'curl -d "name={reponame}" -d "description={description}" -d "website={url}" -u {username}:{password} {base_url}/repositories/'
+        cmd = 'curl -d "name={reponame}" -d "description={description}" -d "website={url}" -d "scm={scm}" -u {username}:{password} {base_url}/repositories/'
         cmd = cmd.format(reponame=reponame,
                          username=username,
                          password=password,
                          base_url=base_url,
+						 scm="git",
                          description=repo['description'],
                          url=repo['homepage'])
 

bitbucket2github/bitbucket2github.py

 
 bitbucket_username = vault.get('bitbucket.org', 'username')
 github_username = vault.get('github.com', 'username')
-github_api_token = vault.get('github.com', github_username)
+github_password = vault.get('github.com', github_username)
 
-bitbucket_url = 'http://bitbucket.org/{0}/{1}'
+bitbucket_url = 'https://bitbucket.org/{0}/{1}'
+bitbucket_git_url = 'https://bitbucket.org/{0}/{1}.git'
 github_url = 'git+ssh://git@github.com/{0}/{1}.git'
 tmp_dir = tempfile.gettempdir()
 
-
 def backup(repo):
     print "Syncing %s from BitBucket to GitHub" % repo['name']
-    github.create_repo(repo, github_username, github_api_token)
+    github.create_repo(repo, github_username, github_password)
 
-    bitbucket_repo = bitbucket_url.format(bitbucket_username, repo['name'])
     github_repo = github_url.format(github_username, repo['name'])
     local_repo = os.path.join(tmp_dir, repo['name'])
-
-    if os.path.exists(local_repo):
-        sh('hg pull {0} -R {1}'.format(bitbucket_repo, local_repo))
+    
+    if repo['scm'] == "git":
+        bitbucket_repo = bitbucket_git_url.format(bitbucket_username, repo['name'])
+		
+        if os.path.exists(local_repo):
+            sh('cd {1}; git fetch {0}'.format(bitbucket_repo, local_repo))
+        else:
+            sh('git clone --mirror {0} {1}'.format(bitbucket_repo, local_repo))
+        
+        sh('cd {1}; git push --mirror {0}'.format(github_repo, local_repo))		
     else:
-        sh('hg clone {0} {1}'.format(bitbucket_repo, local_repo))
-
-    sh('hg bookmark master -f -R {0}'.format(local_repo))
-    sh('hg push {0} -R {1}'.format(github_repo, local_repo))
+        bitbucket_repo = bitbucket_url.format(bitbucket_username, repo['name'])
+		
+        if os.path.exists(local_repo):
+            sh('hg pull {0} -R {1}'.format(bitbucket_repo, local_repo))
+        else:
+            sh('hg clone {0} {1}'.format(bitbucket_repo, local_repo))
+        
+        sh('hg bookmark master -f -R {0}'.format(local_repo))
+        sh('hg push {0} -R {1}'.format(github_repo, local_repo))
 
 
 def main():

bitbucket2github/github.py

 #!/usr/bin/env python
 
-from github2.client import Github
+from pygithub3 import Github
 
 def repos(username):
     """ Returns the list of public repos owned by user """
     return [{
             "name":repo.name,
             "description":repo.description or '',
-            "homepage":repo.homepage or ''
+            "homepage":repo.homepage or '',
+			"scm":"git",
+			"clone_url":repo.clone_url
         }
-        for repo in github.repos.list(username)]
+        for repo in github.repos.list(username).iterator()]
 
 
 def repo_exists(reponame, username):
 
     github = Github()
     try:
-        repo = github.repos.show(username + '/' + reponame)
-    except RuntimeError:
+        repo = github.repos.get(user=username, repo=reponame)
+    except:
         return False
     else:
         return True
 
 
-def create_repo(repo, username, api_token):
+def create_repo(repo, username, password):
     """ Creates a public repository with the given credentials """
 
     reponame = repo['name']
-    github = Github(username=username, api_token=api_token)
+    github = Github(login=username, password=password)
+	
+    try:
+        repo = github.repos.get(user=username, repo=reponame)
+    except:
+        print "Creating " + reponame + " in GitHub"
+        github.repos.create(dict(name=reponame, 
+                        description=repo['description'], 
+                        homepage=repo['homepage']))
 
-    if not repo_exists(reponame, username):
-        print "Creating " + reponame + " in GitHub"
-        github.repos.create(reponame, 
-                        description=repo['description'], 
-                        homepage=repo['homepage'])
-

bitbucket2github/github2bitbucket.py

 bitbucket_username = vault.get('bitbucket.org', 'username')
 bitbucket_password = vault.get('bitbucket.org', bitbucket_username)
 
-bitbucket_url = 'ssh://hg@bitbucket.org/{0}/{1}'
-github_url = 'git+ssh://git@github.com/{0}/{1}.git'
+bitbucket_url = 'ssh://git@bitbucket.org/{0}/{1}.git'
 tmp_dir = tempfile.gettempdir()
 
-
 def backup(repo):
     print "Syncing %s from GitHub to BitBucket" % repo['name']
     bitbucket.create_repo(repo, bitbucket_username, bitbucket_password)
 
     bitbucket_repo = bitbucket_url.format(bitbucket_username, repo['name'])
-    github_repo = github_url.format(github_username, repo['name'])
     local_repo = os.path.join(tmp_dir, repo['name'])
-
+	
     if os.path.exists(local_repo):
-        sh('hg fetch {0} -R {1}'.format(github_repo, local_repo))
+    	sh('cd {1}; git fetch {0}'.format(repo['clone_url'], local_repo))
     else:
-        sh('hg clone {0} {1}'.format(github_repo, local_repo))
-
-    sh('hg bookmark master -f -R {0}'.format(local_repo))
-    sh('hg push {0} -R {1}'.format(bitbucket_repo, local_repo))
+    	sh('git clone --mirror {0} {1}'.format(repo['clone_url'], local_repo))
+    
+    sh('cd {1}; git push --mirror {0}'.format(bitbucket_repo, local_repo))
 
 
 def main():
 #!/usr/bin/env python
 
 project = 'bitbucket2github'
-version = 0.1
+version = 0.2
 
 # Bootstrap installation of Distribute
 import distribute_setup
 
 install_requires = [
     # -*- Extra requirements: -*-
-    'github2',
+    'pygithub3',
     'hg-git',
     'scriptine',
     'vault',