Guest User

Untitled

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