-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
205 lines (160 loc) · 4.03 KB
/
main.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import asyncio
import os
import httpx
import aiofiles.tempfile
organization = "AmanoTeam"
class Repository:
def __init__(self, id, name, description):
self.id = id
self.name = name
self.description = description
def __eq__(self, other):
return self.name == other.name
async def gitlab_create_repository(client, name, description = None):
gitlab_token = os.getenv(key = "GL_TOKEN")
response = await client.post(
url = "https://gitlab.com/api/v4/projects",
json = {
"default_branch": "master",
"description": "" if description is None else description,
"initialize_with_readme": "false",
"name": name,
"path": name,
"visibility": "public",
"tag_list": []
},
headers = {
"Authorization": "Bearer %s" % gitlab_token
}
)
data = response.json()
return response.status_code == 200
async def gitlab_unprotect_branches(client, repository):
gitlab_token = os.getenv(key = "GL_TOKEN")
response = await client.get(
url = "https://gitlab.com/api/v4/projects/%i/protected_branches" % (repository),
headers = {
"Authorization": "Bearer %s" % gitlab_token
}
)
data = response.json()
for branch in data:
name = branch["name"]
print("- Deleting branch protections for branch '%s'" % (name))
response = await client.delete(
url = "https://gitlab.com/api/v4/projects/%i/protected_branches/%s" % (repository, name),
headers = {
"Authorization": "Bearer %s" % gitlab_token
}
)
return response.status_code == 200
async def mirror(client):
github_token = os.getenv(key = "GH_TOKEN")
gitlab_token = os.getenv(key = "GL_TOKEN")
github_repositories = []
gitlab_repositories = []
response = await client.get(
url = "https://api.github.com/users/%s/repos" % (organization),
params = {
"per_page": 100
},
headers = {
"Authorization": "Bearer %s" % github_token
}
)
data = response.json()
for repository in data:
(full_name, private, description) = (
repository["full_name"],
repository["private"],
repository["description"],
)
if private:
continue
name = (
full_name
.split(sep = "/", maxsplit = 1)
.pop(-1)
)
repo = Repository(
id = None,
name = name,
description = description
)
github_repositories.append(repo)
response = await client.get(
url = "https://gitlab.com/api/v4/projects",
params = {
"owned": "true",
"per_page": 100
},
headers = {
"Authorization": "Bearer %s" % gitlab_token
}
)
data = response.json()
for repository in data:
(id, name) = (
repository["id"],
repository["name"]
)
repo = Repository(
id = id,
name = name,
description = None
)
await gitlab_unprotect_branches(
client = client,
repository = repo.id
)
gitlab_repositories.append(repo)
for repository in github_repositories:
status = True
if repository not in gitlab_repositories:
status = await gitlab_create_repository(
client = client,
name = repository.name,
description = repository.description
)
if not status:
continue
async with aiofiles.tempfile.TemporaryDirectory() as temporary_directory:
directory = temporary_directory
url = "https://github.com/%s/%s.git" % (
organization,
repository.name
)
print("- Cloning GitHub repository from '%s' to '%s'" % (url, directory))
process = await asyncio.create_subprocess_exec(
*(
"git",
"clone",
"--quiet",
"--mirror",
url,
directory
)
)
await process.communicate()
url = "https://glab:%s@gitlab.com/%s-mirror/%s.git" % (
gitlab_token,
organization,
repository.name
)
print("- Mirroring GitHub repository from '%s' to '%s'" % (directory, url))
process = await asyncio.create_subprocess_exec(
*(
"git",
"-C", directory,
"push",
"--force",
"--quiet",
"--mirror",
url
)
)
await process.communicate()
async def main():
async with httpx.AsyncClient(http2 = True, timeout = None) as client:
await mirror(client = client)
asyncio.run(main())