|
| 1 | +import os |
| 2 | +import requests |
| 3 | +import json |
| 4 | +import subprocess |
| 5 | +import shutil |
| 6 | + |
| 7 | +# Access stored environmental variables for username and password |
| 8 | +Bitbucket_User = os.environ['BITBUCKET_USER'] |
| 9 | +Bitbucket_Pass = os.environ['BITBUCKET_PASS'] |
| 10 | +Git_User = os.environ['GIT_USER'] |
| 11 | +Git_Token = os.environ['GIT_TOKEN'] |
| 12 | + |
| 13 | +team_id = 0 |
| 14 | +empty_git_repo_list = [] |
| 15 | +github_existing_team_list = [] |
| 16 | +bitbucket_git_repo_list = [] |
| 17 | +repo_project_mapping = {} |
| 18 | +bitbucket_project_list = [] |
| 19 | +bitbucket_nongit_repo_list = [] |
| 20 | +git_repo_team_mapping = {} |
| 21 | + |
| 22 | +# Create a list of existing bitbucket repo names |
| 23 | + |
| 24 | +bitbucket_url = 'https://api.bitbucket.org/2.0/repositories/edgenuity?pagelen=100' |
| 25 | +headers = {'Content-Type':'application/json'} |
| 26 | +response = requests.get(url=bitbucket_url,headers=headers,auth=(Bitbucket_User,Bitbucket_Pass)) |
| 27 | +response_json = json.loads(response.text) |
| 28 | +for repo in response_json['values']: |
| 29 | + if 'project' in repo: |
| 30 | + repo_project_mapping[repo['slug'].encode("utf-8")] = repo['project']['name'].encode("utf-8") |
| 31 | + if repo['project']['name'] not in bitbucket_project_list: |
| 32 | + bitbucket_project_list.append(repo['project']['name']) |
| 33 | + if repo['scm']!= "git": |
| 34 | + bitbucket_nongit_repo_list.append(repo['slug'].encode("utf-8")) |
| 35 | + else: |
| 36 | + bitbucket_git_repo_list.append(repo['slug'].encode("utf-8")) |
| 37 | + |
| 38 | +while True: |
| 39 | + if "next" in response_json: |
| 40 | + print "More repos exist" |
| 41 | + bitbucket_url = response_json["next"] |
| 42 | + headers = {'Content-Type':'application/json'} |
| 43 | + response = requests.get(url=bitbucket_url,headers=headers,auth=(Bitbucket_User,Bitbucket_Pass)) |
| 44 | + response_json = json.loads(response.text) |
| 45 | + for repo in response_json['values']: |
| 46 | + if 'project' in repo: |
| 47 | + repo_project_mapping[repo['slug'].encode("utf-8")] = repo['project']['name'].encode("utf-8") |
| 48 | + if repo['project']['name'] not in bitbucket_project_list: |
| 49 | + bitbucket_project_list.append(repo['project']['name']) |
| 50 | + if repo['scm']!= "git": |
| 51 | + bitbucket_nongit_repo_list.append(repo['slug'].encode("utf-8")) |
| 52 | + else: |
| 53 | + bitbucket_git_repo_list.append(repo['slug'].encode("utf-8")) |
| 54 | + else: |
| 55 | + print "All repos covered" |
| 56 | + break |
| 57 | + |
| 58 | +print bitbucket_project_list |
| 59 | +#print repo_project_mapping |
| 60 | +#print bitbucket_git_repo_list |
| 61 | +print bitbucket_nongit_repo_list |
| 62 | +print len(bitbucket_project_list) |
| 63 | +print len(bitbucket_nongit_repo_list) |
| 64 | +print len(bitbucket_git_repo_list) |
| 65 | + |
| 66 | +git_repo_url = 'https://api.github.com/orgs/edgenuity/repos' |
| 67 | +git_team_url = 'https://api.github.com/orgs/edgenuity/teams' |
| 68 | +git_add_repo_to_team_url = 'https://api.github.com/teams' |
| 69 | +git_headers = {'Content-Type':'application/json','Authorization':'token '+ Git_Token} |
| 70 | +git_team_repo_mapping_headers = {'Content-Type':'application/json','Authorization':'token '+ Git_Token,'Accept':'application/vnd.github.hellcat-preview+json'} |
| 71 | + |
| 72 | +for repo_name in bitbucket_git_repo_list: |
| 73 | + create = True |
| 74 | + if repo_name in repo_project_mapping: |
| 75 | + print repo_name |
| 76 | + team_name = repo_project_mapping[repo_name] |
| 77 | + print team_name |
| 78 | + # Check if team exists on github, if not, create it |
| 79 | + response = requests.get(url=git_team_url+'?per_page=100',headers=git_headers) |
| 80 | + response_json = json.loads(response.text) |
| 81 | + print len(response_json) |
| 82 | + for team in response_json: |
| 83 | + if team['name'] not in github_existing_team_list: |
| 84 | + github_existing_team_list.append(team['name'].encode("utf-8")) |
| 85 | + if team_name == team['name']: |
| 86 | + team_id = team['id'] |
| 87 | + print team_id |
| 88 | + create = False |
| 89 | + break |
| 90 | + #print github_existing_team_list |
| 91 | + #print len(github_existing_team_list) |
| 92 | + if create == True: |
| 93 | + print "Team not present, creating it" |
| 94 | + try: |
| 95 | + team_create_data = {'name': team_name,'description': 'This is '+team_name+' team','privacy': 'secret'} |
| 96 | + # Create team on github having same name as that of bitbucket project |
| 97 | + response = requests.post(url=git_team_url,headers=git_headers,data=json.dumps(team_create_data)) |
| 98 | + print response.text |
| 99 | + response_json = json.loads(response.text) |
| 100 | + if 'errors' in response_json: |
| 101 | + if response_json['errors'][0]['message'] == "Name has already been taken": |
| 102 | + raise ValueError("Duplicate team name") |
| 103 | + else: |
| 104 | + # Store team id for future purposes |
| 105 | + global team_id |
| 106 | + team_id = response_json['id'] |
| 107 | + print team_id |
| 108 | + |
| 109 | + except ValueError as e: |
| 110 | + print str(e) |
| 111 | + continue |
| 112 | + try: |
| 113 | + repo_create_data = {'name': repo_name,'description': 'This is '+repo_name+' repository','homepage': 'https://github.com/edgenuity/'+repo_name,'private': True,'has_issues': True,'has_projects': True,'has_wiki': True} |
| 114 | + # Create an empty Git repo having same name as the bitbucket repo |
| 115 | + response = requests.post(url=git_repo_url,headers=git_headers,data=json.dumps(repo_create_data)) |
| 116 | + print response.text |
| 117 | + # If repo is already present on Github, there is every chance that code will also be present so skpping next steps to speed up execution |
| 118 | + if int(response.status_code)!= 422: |
| 119 | + # Clone existing bitbucket repo |
| 120 | + clone_cmd = "git clone --mirror https://"+Bitbucket_User+":"+Bitbucket_Pass+"@bitbucket.org/edgenuity/"+repo_name+".git" |
| 121 | + subprocess.call(clone_cmd,shell=True) |
| 122 | + working_dir = os.getcwd() |
| 123 | + os.chdir(repo_name+".git") |
| 124 | + # Rename existing Git remote connection to bitbucket |
| 125 | + rename_origin_cmd = "git remote rename origin bitbucket" |
| 126 | + subprocess.call(rename_origin_cmd,shell=True) |
| 127 | + # Add remote origin as Git's newly created repo |
| 128 | + add_git_origin_cmd = "git remote add origin https://"+Git_User+":"+Git_Token+"@github.com/edgenuity/"+repo_name+".git" |
| 129 | + subprocess.call(add_git_origin_cmd,shell=True) |
| 130 | + # Set remote git url |
| 131 | + set_git_url_cmd = "git remote set-url --push origin https://"+Git_User+":"+Git_Token+"@github.com/edgenuity/"+repo_name+".git" |
| 132 | + subprocess.call(set_git_url_cmd,shell=True) |
| 133 | + # Push all branches in the repo |
| 134 | + git_push_branches_cmd = "git push --mirror" |
| 135 | + subprocess.call(git_push_branches_cmd,shell=True) |
| 136 | + # Remove remote bitbucket origin |
| 137 | + remove_bitbucket_origin_cmd = "git remote rm bitbucket" |
| 138 | + subprocess.call(remove_bitbucket_origin_cmd,shell=True) |
| 139 | + os.chdir(working_dir) |
| 140 | + # Remove cloned bitbucket repo after pushing code to free up space |
| 141 | + shutil.rmtree(repo_name+".git") |
| 142 | + # Add github repository to team |
| 143 | + response = requests.put(url=git_add_repo_to_team_url+"/"+str(team_id)+"/repos/edgenuity/"+repo_name,headers=git_team_repo_mapping_headers) |
| 144 | + print response.text |
| 145 | + if int(response.status_code) == 204: |
| 146 | + print "Repo successfully added to team" |
| 147 | + else: |
| 148 | + raise ("Repo could not be added to the team") |
| 149 | + except Exception as e: |
| 150 | + print str(e) |
| 151 | + |
| 152 | +print "Teams on Github are: " +str(github_existing_team_list) |
| 153 | +print "Number of teams on Github are: " +str(len(github_existing_team_list)) |
| 154 | + |
| 155 | +# Get a list of almost empty Git repos which are almost empty due to either code not getting pushed into it or source repo did not have any code |
| 156 | +# Iterating for 7 times as repo number is between 600 to 700 |
| 157 | +for i in range (1,8): |
| 158 | + print i |
| 159 | + response = requests.get(url=git_repo_url+'?page='+str(i)+'&per_page=100',headers=git_headers) |
| 160 | + print response.text |
| 161 | + response_json = json.loads(response.text) |
| 162 | + print len(response_json) |
| 163 | + for repo in response_json: |
| 164 | + if int(repo['size']) == 0: |
| 165 | + empty_git_repo_list.append(repo['name'].encode("utf-8")) |
| 166 | + |
| 167 | +print "Almost empty Github repo names are:" +str(empty_git_repo_list) |
| 168 | +print "Number of almost empty Github repo names are:" +str(len(empty_git_repo_list)) |
| 169 | + |
0 commit comments