-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
executable file
·347 lines (271 loc) · 11.2 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env python3
import os
import re
import sh
import time
from pathlib import Path
from github import Github
from dingtalkchatbot.chatbot import DingtalkChatbot
from sh import git
dingtalk_access_token = os.environ["INPUT_DINGTALK_ACCESS_TOKEN"]
dingtalk_secret = os.environ["INPUT_DINGTALK_SECRET"]
enable_dingtalk_notification = len(dingtalk_access_token) > 0 and len(dingtalk_secret) > 0
dingtalk_bot = DingtalkChatbot(
webhook=f"https://oapi.dingtalk.com/robot/send?access_token={dingtalk_access_token}",
secret=dingtalk_secret,
)
gh_url = "https://github.com"
token = os.environ['INPUT_REPO_TOKEN']
gh = Github(token)
prog = re.compile(r"(.*)\(#(\d+)\)(?:$|\n).*")
title_re = re.compile(r"(.*)(?:$|\n).*")
class Commit:
def __init__(self, commit = None):
self.commit = commit
self.title = None
self.pr_num = -1
self.extract_pr_num_and_title(commit)
def author(self):
assert self.is_valid()
return self.commit.commit.author
def login(self):
assert self.is_valid()
return self.commit.author.login
def is_valid(self):
return self.commit is not None and (self.pr_num >= 0 or self.title is not None)
def has_same_title(self, ci):
return self.title.lower() == ci.title.lower()
def extract_pr_num_and_title(self, commit):
if commit is None:
return
msg = prog.match(commit.commit.message)
if msg:
self.pr_num = int(msg.group(2))
while msg:
self.title = msg.group(1).strip()
msg = prog.match(self.title)
else:
msg = title_re.match(commit.commit.message)
if msg:
self.title = msg.group(1).strip()
def get_org_members(org_name):
print(">>> Get org members")
org = gh.get_organization(org_name)
return [m.login for m in org.get_members()]
def must_create_dir(filename):
dirname = os.path.dirname(filename)
if len(dirname) > 0 and not os.path.exists(dirname):
sh.mkdir('-p', dirname)
def overwrite_conflict_files(ci):
print(">>> Overwrite PR conflict files")
for f in ci.files:
if f.status == "removed" and os.path.exists(f.filename):
git.rm('-rf', f.filename)
else:
must_create_dir(f.filename)
sh.curl("-fsSL", f.raw_url, "-o", f.filename)
print(f" {f.filename}")
def commit_changes(ci: Commit):
author = ci.author()
print(f">>> Commit changes by <{author.email}>")
git.add(".")
git.commit("-nam", ci.title, "--author", f"{author.name} <{author.email}>")
def conflict_file_list(lines):
prefix = "CONFLICT (content): Merge conflict in "
return [l[len(prefix):] for l in lines if l.startswith(prefix)]
def apply_patch(branch, comm_ci):
print(f">>> Apply patch file to {branch}")
stopped = False
author = comm_ci.author()
git.config("--local", "user.name", author.name)
git.config("--local", "user.email", author.email)
git.clean("-f")
git.fetch("origin", "master")
git.checkout("-b", branch, "origin/master")
git_commit = comm_ci.commit
conflict_files = []
try:
git('cherry-pick', git_commit.sha)
except sh.ErrorReturnCode as e:
err = str(e)
if err.find('git commit --allow-empty') >= 0:
git('commit', '--allow-empty', '--allow-empty-message', '--no-edit')
else:
print(">>> Fail to apply the patch to branch {}, cause: {}".format(branch, err))
if err.find('more, please see e.stdout') >= 0:
err = e.stdout.decode()
conflict_files = conflict_file_list(err.splitlines())
# git('cherry-pick', '--abort')
# overwrite_conflict_files(git_commit)
commit_changes(comm_ci)
stopped = True
try:
git.push("-u", "origin", branch)
except sh.ErrorReturnCode as e:
print(">>> Fail to push branch({}) to origin, caused by {}".format(branch, e))
return (stopped, conflict_files)
def find_latest_community_commit_in_ent_repo(ent_commit: Commit, community_commits):
assert ent_commit.is_valid()
for ci in community_commits:
assert ci.is_valid()
if ent_commit.has_same_title(ci):
user = gh.get_user().login
if ent_commit.login() == user:
return ci
else:
print(">>> [WARN] the commit has been checkin by {} rather than {}: {}".format(ent_commit.login(), user, ent_commit.title))
return Commit()
def generate_latest_100_commits(repo):
commits = []
for i, ci in enumerate(repo.get_commits()):
if i > 100:
break
commit = Commit(repo.get_commit(ci.sha))
if commit.is_valid():
commits.append(commit)
return commits
def find_unmerged_community_commits_in_ent_repo(community_repo, ent_repo):
ent_commits = generate_latest_100_commits(ent_repo)
community_commits = generate_latest_100_commits(community_repo)
for ent_commit in ent_commits:
ci = find_latest_community_commit_in_ent_repo(ent_commit, community_commits)
if ci.is_valid():
return community_commits[:community_commits.index(ci)]
return []
def pr_ref(repo, pr):
pr_num = pr if isinstance(pr, int) else pr.number
if pr_num >= 0:
return "{}#{}".format(repo.full_name, pr_num)
return repo.full_name
def pr_link(repo, pr):
pr_num = pr if isinstance(pr, int) else pr.number
return "[{}]({}/{}/pull/{})".format(pr_ref(repo, pr_num), gh_url, repo.full_name, pr_num)
def co_authored_by(author):
return "Co-authored-by: {} <{}>".format(author.name, author.email)
def append_migration_in_msg(repo, ci, pr):
body = pr.body if pr.body else ""
coauthor = co_authored_by(ci.author())
return "{}\n\nMigrated from {}\n\n{}\n".format(body, pr_ref(repo, pr), coauthor)
def notify_author_by_comment(ent_repo, comm_repo, comm_ci, issue_num, comm_pr_num, org_members, conflict_files):
comment = ""
if comm_ci.login() in org_members:
comment += f"@{comm_ci.login()}\n"
print(f">>> Notify the author by comment: {comm_ci.login()}")
else:
print(f">>> The author {comm_ci.login()} is not in the orgnization, need not to notify him")
comment += """This PR will cause conflicts when applying patch.
Please carefully compare all the changes in this PR to avoid overwriting legal codes.
If you need to make changes, please make the commits on current branch.
You can use following commands to resolve the conflicts locally:
```shell
$ git clone [email protected]:{}.git
$ cd {}
$ git remote -vv
$ git fetch origin pull/{}/head:pr-{}
$ git checkout pr-{}
# resolve the conflicts
$ git push -f origin pr-{}
```
CONFLICT FILES:
```text
{}
```
"""
issue = ent_repo.get_issue(issue_num)
issue.create_comment(comment.format(ent_repo.full_name,
ent_repo.name,
issue_num,
comm_pr_num,
comm_pr_num,
comm_pr_num,
'\n'.join(conflict_files)))
def create_pr(comm_repo, ent_repo, comm_ci, org_members):
try:
merged_pr = comm_repo.get_pull(comm_ci.pr_num)
branch = "pr-{}".format(merged_pr.number)
stopped, conflict_files = apply_patch(branch, comm_ci)
body = append_migration_in_msg(comm_repo, comm_ci, merged_pr)
new_pr = ent_repo.create_pull(title=comm_ci.title, body=body, head=branch, base="master")
print(f">>> Create PR: {pr_ref(ent_repo, new_pr)}")
time.sleep(2)
new_pr = ent_repo.get_pull(new_pr.number)
new_pr.add_to_labels('auto-sync')
merged_pr_lables = merged_pr.get_labels()
labels = [label.name for label in merged_pr_lables if label.name.find('cherry-pick') >= 0]
for label in labels:
new_pr.add_to_labels(label)
if stopped:
notify_author_by_comment(ent_repo,
comm_repo,
comm_ci,
new_pr.number,
comm_ci.pr_num,
org_members,
conflict_files)
return (False, new_pr.number)
if not new_pr.mergeable:
return (False, new_pr.number)
commit_title = "{} (#{})".format(comm_ci.title, new_pr.number)
status = new_pr.merge(merge_method='squash', commit_title=commit_title)
if not status.merged:
return (False, new_pr.number)
return (True, new_pr.number)
except Exception as e:
print(">>> Fail to merge PR {}, cause: {}".format(comm_ci.pr_num, e))
return (False, -1 if new_pr is None else new_pr.number)
def get_org_name(repo):
l = repo.split('/')
assert len(l) == 2
return l[0]
def get_repo_name(repo):
l = repo.split('/')
assert len(l) == 2
return l[1]
def add_community_upstream(comm_repo):
remote_url = 'https://github.com/{}.git'.format(comm_repo.full_name)
remote_name = 'community'
try:
git.remote('-vv')
git.remote('rm', remote_name)
except:
print(">>> The remote upstream({}) not found.".format(remote_name))
try:
git.remote('add', remote_name, remote_url)
git.fetch(remote_name, 'master')
except Exception as e:
print(">>> Fail to add remote, cause: {}".format(e))
raise
def main(community_repo, enterprise_repo):
comm_repo = gh.get_repo(community_repo)
ent_repo = gh.get_repo(enterprise_repo)
org_members = get_org_members(get_org_name(community_repo))
unmerged_community_commits = find_unmerged_community_commits_in_ent_repo(comm_repo, ent_repo)
unmerged_community_commits.reverse()
add_community_upstream(comm_repo)
succ_pr_list = []
err_pr_list = []
for ci in unmerged_community_commits:
res = create_pr(comm_repo, ent_repo, ci, org_members)
md = pr_link(comm_repo, ci.pr_num)
if res[1] >= 0:
md += " -> " + pr_link(ent_repo, res[1])
md += " " + ci.login()
if res[0]:
succ_pr_list.append(md)
print(f">>> {pr_ref(ent_repo, res[1])} has been migrated from {pr_ref(comm_repo, ci.pr_num)}")
else:
err_pr_list.append(md)
print(f">>> {pr_ref(comm_repo, ci.pr_num)} could not be merged into {pr_ref(ent_repo, res[1])}")
break
succ_prs = '\n\n'.join(succ_pr_list) if succ_pr_list else "None"
err_prs = '\n\n'.join(err_pr_list) if err_pr_list else "None"
print(">>> Enable dingtalk notification: {}".format(enable_dingtalk_notification))
if enable_dingtalk_notification and (len(succ_pr_list) > 0 or len(err_pr_list) > 0):
text = f"### Auto Merge Status\nMerge successfully:\n\n{succ_prs}\n\nFailed to merge:\n\n{err_prs}"
dingtalk_bot.send_markdown(title='Auto Merge Status', text=text, is_at_all=False)
if len(unmerged_community_commits) == 0:
print(">>> There's no any PRs to sync")
if __name__ == "__main__":
src_repo = os.environ["INPUT_FROM_REPO"]
target_repo = os.environ["GITHUB_REPOSITORY"]
main(src_repo, target_repo)