Guest User

Untitled

a guest
Oct 24th, 2017
1,010
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. import os
  2. import re
  3. import subprocess
  4.  
  5. import requests
  6.  
  7.  
  8. GITLAB_ENDPOINT = os.environ["GITLAB_ENDPOINT"]
  9. GITLAB_TOKEN = os.environ["GITLAB_TOKEN"]
  10.  
  11.  
  12. BITBUCKET_ENDPOINT = os.environ["BITBUCKET_ENDPOINT"]
  13. BITBUCKET_TEAM = os.environ["BITBUCKET_TEAM"]
  14. BITBUCKET_USERNAME = os.environ["BITBUCKET_USERNAME"]
  15. BITBUCKET_PASSWORD = os.environ["BITBUCKET_PASSWORD"]
  16.  
  17.  
  18.  
  19. bitbucket = requests.Session()
  20. bitbucket.auth = (BITBUCKET_USERNAME, BITBUCKET_PASSWORD)
  21.  
  22.  
  23. def list_gitlab_repositories():
  24. repositories = []
  25. page = 1
  26. while True:
  27. params = {"page": page, "per_page": 100, "private_token": GITLAB_TOKEN}
  28. url = os.path.join(GITLAB_ENDPOINT, "projects")
  29. res = requests.get(url, params=params)
  30. repositories += res.json()
  31. if page >= int(res.headers["x-total-pages"]):
  32. break
  33. page += 1
  34. return repositories
  35.  
  36.  
  37. def list_bitbucket_projects():
  38. url = os.path.join(BITBUCKET_ENDPOINT, "teams", BITBUCKET_TEAM, "projects/")
  39. projects = []
  40. while url:
  41. res = bitbucket.get(url)
  42. payload = res.json()
  43. projects += payload["values"]
  44. url = payload.get("next", None)
  45. return projects
  46.  
  47.  
  48. def list_bitbucket_repositories():
  49. url = os.path.join(BITBUCKET_ENDPOINT, "repositories", BITBUCKET_TEAM)
  50. repositories = []
  51. while url:
  52. res = bitbucket.get(url)
  53. payload = res.json()
  54. repositories += payload["values"]
  55. url = payload.get("next", None)
  56. return repositories
  57.  
  58.  
  59. def generate_key(name):
  60. splitted = re.split("[- _]", name)
  61. chars = 2 if len(splitted) > 1 else 4
  62. return ''.join(n[:chars].upper() for n in splitted)
  63.  
  64.  
  65. def create_bitbucket_project(name):
  66. payload = {
  67. "name": name,
  68. "key": generate_key(name),
  69. "is_private": True
  70. }
  71. url = os.path.join(BITBUCKET_ENDPOINT, "teams", BITBUCKET_TEAM, "projects/")
  72. res = bitbucket.post(url, json=payload)
  73. if not 200 <= res.status_code < 300:
  74. raise ValueError("could not create project {0}: {1}".format(name, res.text))
  75.  
  76.  
  77. def create_bitbucket_repository(name, project):
  78. payload = {"scm": "git", "is_private": True, "project": {"key": generate_key(project)}}
  79. url = os.path.join(BITBUCKET_ENDPOINT, "repositories", BITBUCKET_TEAM, name)
  80. res = bitbucket.post(url, json=payload)
  81. if not 200 <= res.status_code < 300:
  82. if not "Repository with this Slug and Owner already exists." in res.text:
  83. raise ValueError("could not create repository {0}: {1}".format(name, res.text))
  84.  
  85.  
  86. def clone_repository(repository):
  87. project_dir = os.path.join("/tmp", repository["namespace"]["name"], repository["path"])
  88. if os.path.exists(project_dir) and os.listdir(project_dir):
  89. return False
  90. os.makedirs(project_dir, exist_ok=True)
  91. subprocess.run(["git", "clone", "--mirror", repository["ssh_url_to_repo"], project_dir])
  92. return project_dir
  93.  
  94. def upload_repository(name, project):
  95. project_dir = os.path.join("/tmp", project, name)
  96. remote = "git@bitbucket.org:{0}/{1}.git".format(BITBUCKET_TEAM, name)
  97. subprocess.run(["git", "remote", "add", "bitbucket", remote], cwd=project_dir)
  98. subprocess.run(["git", "push", "--all", "bitbucket"], cwd=project_dir)
  99. subprocess.run(["git", "push", "--tags", "bitbucket"], cwd=project_dir)
  100.  
  101. class Migrator:
  102. def __init__(self):
  103. self.repositories = list_gitlab_repositories()
  104. self.projects = set(project["name"] for project in list_bitbucket_projects())
  105.  
  106. def migrate_repositories(self):
  107. for repository in self.repositories:
  108. self.migrate_repository(repository)
  109.  
  110. def ensure_project_exists(self, project):
  111. if project not in self.projects:
  112. create_bitbucket_project(project)
  113. self.projects.add(project)
  114.  
  115. def migrate_repository(self, repository):
  116. project = repository["namespace"]["name"]
  117. self.ensure_project_exists(project)
  118. project_dir = clone_repository(repository)
  119. if not project_dir:
  120. return
  121. create_bitbucket_repository(repository["path"], project)
  122. upload_repository(repository["path"], project)
  123.  
  124.  
  125. def main():
  126. migrator = Migrator()
  127. migrator.migrate_repositories()
  128.  
  129.  
  130. if __name__ == '__main__':
  131. main()
Add Comment
Please, Sign In to add comment