-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_to_gitlab_migration.py
More file actions
132 lines (112 loc) · 4.48 KB
/
github_to_gitlab_migration.py
File metadata and controls
132 lines (112 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import os
import subprocess
import gitlab
from github import Github
from dotenv import load_dotenv
import os
load_dotenv()
# GitHub credentials
GITHUB_TOKEN = os.getenv('GITHUB_TOKEN')
GITHUB_ORG = os.getenv('GITHUB_ORG')
# GitLab credentials
GITLAB_URL = os.getenv('GITLAB_URL')
GITLAB_TOKEN = os.getenv('GITLAB_TOKEN')
GITLAB_GROUP = os.getenv('GITLAB_GROUP')
# File containing repository names
REPO_LIST_FILE = 'repositories.txt'
# Initialize GitHub and GitLab clients
github_client = Github(GITHUB_TOKEN)
gitlab_client = gitlab.Gitlab(GITLAB_URL, private_token=GITLAB_TOKEN)
def migrate_repo(github_repo, gitlab_group):
print(f"Migrating {github_repo.name}...")
try:
# Create a new project in GitLab
gitlab_project = gitlab_client.projects.create({
'name': github_repo.name,
'namespace_id': gitlab_group.id
})
# Clone the GitHub repository
clone_command = f"git clone --mirror {github_repo.clone_url} {github_repo.name}"
print(f"Cloning with command: {clone_command}")
clone_result = subprocess.run(clone_command, shell=True, capture_output=True, text=True)
print(clone_result.stdout)
if clone_result.returncode != 0:
print(f"Error cloning: {clone_result.stderr}")
return
# Change to the cloned directory
os.chdir(github_repo.name)
# Push to GitLab
push_command = f"git push --mirror {gitlab_project.http_url_to_repo}"
print(f"Pushing to GitLab with command: {push_command}")
push_result = subprocess.run(push_command, shell=True, capture_output=True, text=True)
print(push_result.stdout)
if push_result.returncode != 0:
print(f"Error pushing to GitLab: {push_result.stderr}")
else:
print(f"Successfully pushed {github_repo.name} to GitLab")
# Clean up
os.chdir('..')
os.system(f"rm -rf {github_repo.name}")
print(f"Migration of {github_repo.name} completed.")
except Exception as e:
print(f"Error migrating {github_repo.name}: {str(e)}")
finally:
# Ensure we're back in the original directory
if os.getcwd().endswith(github_repo.name):
os.chdir('..')
def main():
# Test GitLab authentication
try:
gitlab_client.auth()
print("Successfully authenticated with GitLab")
except gitlab.exceptions.GitlabAuthenticationError:
print("Failed to authenticate with GitLab. Check your token and URL.")
return
# Get GitLab user information
try:
user = gitlab_client.user
if user:
print(f"Authenticated as GitLab user: {user.name}")
else:
print("Authenticated with GitLab, but unable to retrieve user information")
except Exception as e:
print(f"Warning: Authenticated with GitLab, but encountered an error retrieving user information: {e}")
# Get GitHub organization
try:
github_org = github_client.get_organization(GITHUB_ORG)
print(f"Successfully connected to GitHub organization: {GITHUB_ORG}")
except Exception as e:
print(f"Error connecting to GitHub organization: {e}")
return
# Get GitLab group
try:
gitlab_group = gitlab_client.groups.get(GITLAB_GROUP)
print(f"Successfully got GitLab group: {gitlab_group.full_path}")
except gitlab.exceptions.GitlabGetError as e:
print(f"Error getting GitLab group: {e}")
return
# Read repository names from file
try:
with open(REPO_LIST_FILE, 'r') as file:
repo_names = [line.strip() for line in file if line.strip()]
except FileNotFoundError:
print(f"Error: File {REPO_LIST_FILE} not found.")
return
except Exception as e:
print(f"Error reading repository list file: {e}")
return
# Iterate through specified repositories in the GitHub organization
for repo_name in repo_names:
try:
repo = github_org.get_repo(repo_name)
if repo.private:
migrate_repo(repo, gitlab_group)
else:
print(f"Skipping {repo_name} as it is not a private repository.")
except Github.GithubException as e:
print(f"Error accessing GitHub repository {repo_name}: {e}")
except Exception as e:
print(f"Unexpected error processing {repo_name}: {str(e)}")
print("Migration process completed.")
if __name__ == "__main__":
main()