f42bd7f2f369574199f7cfe33de47aabd6656677
[platform/upstream/gstreamer.git] / scripts / move_mrs_to_monorepo.py
1 #!/usr/bin/env python3
2
3 from urllib.parse import urlparse
4 from contextlib import contextmanager
5 import os
6 import re
7 import sys
8 try:
9     import gitlab
10 except ModuleNotFoundError:
11     print("========================================================================", file=sys.stderr)
12     print("ERROR: Install python-gitlab with `python3 -m pip install python-gitlab dateutil`", file=sys.stderr)
13     print("========================================================================", file=sys.stderr)
14     sys.exit(1)
15
16 try:
17     from dateutil import parser as dateparse
18 except ModuleNotFoundError:
19     print("========================================================================", file=sys.stderr)
20     print("ERROR: Install dateutil with `python3 -m pip install dateutil`", file=sys.stderr)
21     print("========================================================================", file=sys.stderr)
22     sys.exit(1)
23 import argparse
24 import requests
25
26 import subprocess
27
28 ROOT_DIR = os.path.realpath(os.path.join(os.path.dirname(__file__), ".."))
29
30 URL = "https://gitlab.freedesktop.org/"
31 SIGN_IN_URL = URL + 'sign_in'
32 LOGIN_URL = URL + 'users/sign_in'
33 LOGIN_URL_LDAP = URL + '/users/auth/ldapmain/callback'
34
35 MONOREPO_REMOTE_NAME = 'origin'
36 NAMESPACE = "gstreamer"
37 MONOREPO_NAME = 'gstreamer'
38 MONOREPO_REMOTE = URL + f'{NAMESPACE}/{MONOREPO_NAME}'
39 MONOREPO_BRANCH = 'main'
40 PING_SIGN = '@'
41 MOVING_NAMESPACE = NAMESPACE
42
43 PARSER = argparse.ArgumentParser(
44     description="Move merge request from old GStreamer module to the new"
45                 "GStreamer 'monorepo'.\n"
46                 " All your pending merge requests from all GStreamer modules will"
47                 " be moved the the mono repository."
48 )
49 PARSER.add_argument("--skip-branch", action="store", nargs="*",
50                     help="Ignore MRs for branches which match those names.", dest="skipped_branches")
51 PARSER.add_argument("--skip-on-failure", action="store_true", default=False)
52 PARSER.add_argument("--dry-run", "-n", action="store_true", default=False)
53 PARSER.add_argument("--use-branch-if-exists", action="store_true", default=False)
54 PARSER.add_argument(
55     "-c",
56     "--config-file",
57     action="append",
58     dest='config_files',
59     help="Configuration file to use. Can be used multiple times.",
60     required=False,
61 )
62 PARSER.add_argument(
63     "-g",
64     "--gitlab",
65     help=(
66         "Which configuration section should "
67         "be used. If not defined, the default selection "
68         "will be used."
69     ),
70     required=False,
71 )
72 PARSER.add_argument(
73     "-m",
74     "--module",
75     help="GStreamer module to move MRs for. All if none specified. Can be used multiple times.",
76     dest='modules',
77     action="append",
78     required=False,
79 )
80 PARSER.add_argument(
81     "--mr",
82     default=None,
83     type=int,
84     help=(
85         "Id of the MR to work on."
86         " One (and only one) module must be specified with `--module`."
87     ),
88     required=False,
89 )
90
91 GST_PROJECTS = [
92     'gstreamer',
93     'gst-plugins-base',
94     'gst-plugins-good',
95     'gst-plugins-bad',
96     'gst-plugins-ugly',
97     'gst-libav',
98     'gst-rtsp-server',
99     'gstreamer-vaapi',
100     'gstreamer-sharp',
101     'gst-python',
102     'gst-omx',
103     'gst-editing-services',
104     'gst-devtools',
105     'gst-integration-testsuites',
106     'gst-docs',
107     'gst-examples',
108     'gst-build',
109     'gst-ci',
110 ]
111
112 # We do not want to deal with LFS
113 os.environ["GIT_LFS_SKIP_SMUDGE"] = "1"
114
115
116 log_depth = []               # type: T.List[str]
117
118 @contextmanager
119 def nested(name=''):
120     global log_depth
121     log_depth.append(name)
122     try:
123         yield
124     finally:
125         log_depth.pop()
126
127 def bold(text: str):
128     return f"\033[1m{text}\033[0m"
129
130 def green(text: str):
131     return f"\033[1;32m{text}\033[0m"
132
133 def red(text: str):
134     return f"\033[1;31m{text}\033[0m"
135
136 def yellow(text: str):
137     return f"\033[1;33m{text}\033[0m"
138
139 def fprint(msg, nested=True):
140     if log_depth:
141         prepend = log_depth[-1] + ' | ' if nested else ''
142     else:
143         prepend = ''
144
145     print(prepend + msg, end="")
146     sys.stdout.flush()
147
148
149 class GstMRMover:
150     def __init__(self):
151
152         self.modules = []
153         self.gitlab = None
154         self.config_files = []
155         self.gl = None
156         self.mr = None
157         self.all_projects = []
158         self.skipped_branches = []
159         self.git_rename_limit = None
160         self.skip_on_failure = None
161         self.dry_run = False
162
163     def connect(self):
164         fprint("Logging into gitlab...")
165
166         if self.gitlab:
167             gl = gitlab.Gitlab.from_config(self.gitlab, self.config_files)
168             fprint(f"{green(' OK')}\n", nested=False)
169             return gl
170
171         gitlab_api_token = os.environ.get('GITLAB_API_TOKEN')
172         if gitlab_api_token:
173             gl = gitlab.Gitlab(URL, private_token=gitlab_api_token)
174             fprint(f"{green(' OK')}\n", nested=False)
175             return gl
176
177         session = requests.Session()
178         sign_in_page = session.get(SIGN_IN_URL).content.decode()
179         for l in sign_in_page.split('\n'):
180             m = re.search('name="authenticity_token" value="([^"]+)"', l)
181             if m:
182                 break
183
184         token = None
185         if m:
186             token = m.group(1)
187
188         if not token:
189             fprint(f"{red('Unable to find the authenticity token')}\n")
190             sys.exit(1)
191
192
193         for data, url in [
194             ({'user[login]': 'login_or_email',
195               'user[password]': 'SECRET',
196               'authenticity_token': token}, LOGIN_URL),
197             ({'username': 'login_or_email',
198               'password': 'SECRET',
199               'authenticity_token': token}, LOGIN_URL_LDAP)]:
200
201             r = session.post(url, data=data)
202             if r.status_code != 200:
203                 continue
204
205             try:
206                 gl = gitlab.Gitlab(URL, api_version=4, session=session)
207                 gl.auth()
208             except gitlab.exceptions.GitlabAuthenticationError as e:
209                 continue
210             return gl
211
212         sys.exit(bold(f"{red('FAILED')}.\n\nPlease go to:\n\n"
213             '   https://gitlab.freedesktop.org/-/profile/personal_access_tokens\n\n'
214             f'and generate a token {bold("with read/write access to all but the registry")},'
215             ' then set it in the "GITLAB_API_TOKEN" environment variable:"'
216             f'\n\n  $ GITLAB_API_TOKEN=<your token> {" ".join(sys.argv)}\n'))
217
218     def git(self, *args, can_fail=False, interaction_message=None, call=False, revert_operation=None):
219         cwd = ROOT_DIR
220         retry = True
221         while retry:
222             retry = False
223             try:
224                 if not call:
225                     try:
226                         return subprocess.check_output(["git"] + list(args), cwd=cwd,
227                                                     stdin=subprocess.DEVNULL,
228                                                     stderr=subprocess.STDOUT).decode()
229                     except:
230                         if not can_fail:
231                             fprint(f"\n\n{bold(red('ERROR'))}: `git {' '.join(args)}` failed" + "\n", nested=False)
232                         raise
233                 else:
234                     subprocess.call(["git"] + list(args), cwd=cwd)
235                     return "All good"
236             except Exception as e:
237                 if interaction_message:
238                     if self.skip_on_failure:
239                         return "SKIP"
240                     output = getattr(e, "output", b"")
241                     if output is not None:
242                         out = output.decode()
243                     else:
244                         out = "????"
245                     fprint(f"\n```"
246                           f"\n{out}\n"
247                           f"Entering a shell in {cwd} to fix:\n\n"
248                           f" {bold(interaction_message)}\n\n"
249                           f"You should then exit with the following codes:\n\n"
250                           f"  - {bold('`exit 0`')}: once you have fixed the problem and we can keep moving the merge request\n"
251                           f"  - {bold('`exit 1`')}: {bold('retry')}: once you have let the repo in a state where the operation should be to retried\n"
252                           f"  - {bold('`exit 2`')}: to skip that merge request\n"
253                           f"  - {bold('`exit 3`')}: stop the script and abandon moving your MRs\n"
254                           "\n```\n", nested=False)
255                     try:
256                         if os.name == 'nt':
257                             shell = os.environ.get(
258                                 "COMSPEC", r"C:\WINDOWS\system32\cmd.exe")
259                         else:
260                             shell = os.environ.get(
261                                 "SHELL", os.path.realpath("/bin/sh"))
262                         subprocess.check_call(shell, cwd=cwd)
263                     except subprocess.CalledProcessError as e:
264                         if e.returncode == 1:
265                             retry = True
266                             continue
267                         elif e.returncode == 2:
268                             if revert_operation:
269                                 self.git(*revert_operation, can_fail=True)
270                             return "SKIP"
271                         elif e.returncode == 3:
272                             if revert_operation:
273                                 self.git(*revert_operation, can_fail=True)
274                             sys.exit(3)
275                     except:
276                         # Result of subshell does not really matter
277                         pass
278
279                     return "User fixed it"
280
281                 if can_fail:
282                     return "Failed but we do not care"
283
284                 raise e
285
286     def cleanup_args(self):
287         if not self.modules:
288             if self.mr:
289                 sys.exit(f"{red(f'Merge request #{self.mr} specified without module')}\n\n"
290                          f"{bold(' -> Use `--module` to specify which module the MR is from.')}")
291
292             self.modules = GST_PROJECTS
293         else:
294             VALID_PROJECTS = GST_PROJECTS[1:]
295             for m in self.modules:
296                 if m not in VALID_PROJECTS:
297                     projects = '\n- '.join(VALID_PROJECTS)
298                     sys.exit(f"{red(f'Unknown module {m}')}\nModules are:\n- {projects}")
299             if self.mr and len(self.modules) > 1:
300                 sys.exit(f"{red(f'Merge request #{self.mr} specified but several modules where specified')}\n\n"
301                          f"{bold(' -> Use `--module` only once to specify an merge request.')}")
302             self.modules.append(GST_PROJECTS[0])
303
304     def run(self):
305         self.cleanup_args()
306         self.gl = self.connect()
307         self.gl.auth()
308
309         try:
310             prevbranch = self.git("rev-parse", "--abbrev-ref", "HEAD", can_fail=True).strip()
311         except:
312             fprint(bold(yellow("Not on a branch?\n")), indent=False)
313             prevbranch = None
314
315         try:
316             self.setup_repo()
317
318             from_projects, to_project = self.fetch_projects()
319
320             with nested('  '):
321                 self.move_mrs(from_projects, to_project)
322         finally:
323             if self.git_rename_limit is not None:
324                 self.git("config", "merge.renameLimit", str(self.git_rename_limit))
325             if prevbranch:
326                 fprint(f'Back to {prevbranch}\n')
327                 self.git("checkout", prevbranch)
328
329     def fetch_projects(self):
330         fprint("Fetching projects... ")
331         self.all_projects = [proj for proj in self.gl.projects.list(
332             membership=1, all=True) if proj.name in self.modules]
333         self.user_project, = [p for p in self.all_projects
334                                 if p.namespace['path'] == self.gl.user.username
335                                     and p.name == MONOREPO_NAME]
336         fprint(f"{green(' OK')}\n", nested=False)
337
338         from_projects = [proj for proj in self.all_projects if proj.namespace['path']
339                          == NAMESPACE and proj.name != "gstreamer"]
340         fprint(f"\nMoving MRs from:\n")
341         fprint(f"----------------\n")
342         for p in from_projects:
343             fprint(f"  - {bold(p.path_with_namespace)}\n")
344
345         to_project, = [p for p in self.all_projects if p.path_with_namespace ==
346                        MOVING_NAMESPACE + "/gstreamer"]
347
348         fprint(f"To: {bold(to_project.path_with_namespace)}\n\n")
349
350         return from_projects, to_project
351
352     def recreate_mr(self, project, to_project, mr):
353         branch = f"{project.name}-{mr.source_branch}"
354         if not self.create_branch_for_mr(branch, project, mr):
355             return None
356
357         description = f"**Copied from {URL}/{project.path_with_namespace}/-/merge_requests/{mr.iid}**\n\n{mr.description}"
358
359         title = mr.title
360         if ':' not in mr.title:
361             title = f"{project.name}: {mr.title}"
362
363         new_mr_dict = {
364             'source_branch': branch,
365             'allow_collaboration': True,
366             'remove_source_branch': True,
367             'target_project_id': to_project.id,
368             'target_branch': MONOREPO_BRANCH,
369             'title': title,
370             'labels': mr.labels,
371             'description': description,
372         }
373
374         try:
375             fprint(f"-> Recreating MR '{bold(mr.title)}'...")
376             if self.dry_run:
377                 fprint(f"\nDry info:\n{new_mr_dict}\n")
378             else:
379                 new_mr = self.user_project.mergerequests.create(new_mr_dict)
380                 fprint(f"{green(' OK')}\n", nested=False)
381         except gitlab.exceptions.GitlabCreateError as e:
382             fprint(f"{yellow('SKIPPED')} (An MR already exists)\n", nested=False)
383             return None
384
385         fprint(f"-> Adding discussings from MR '{mr.title}'...")
386         if self.dry_run:
387             fprint(f"{green(' OK')}\n", nested=False)
388             return None
389
390         new_mr_url = f"{URL}/{to_project.path_with_namespace}/-/merge_requests/{new_mr.iid}"
391         for issue in mr.closes_issues():
392             obj = {'body': f'Fixing MR moved to: {new_mr_url}'}
393             issue.discussions.create(obj)
394
395         mr_url = f"{URL}/{project.path_with_namespace}/-/merge_requests/{mr.iid}"
396         for discussion in mr.discussions.list():
397             # FIXME notes = [n for n in discussion.attributes['notes'] if n['type'] is not None]
398             notes = [n for n in discussion.attributes['notes']]
399             if not notes:
400                 continue
401
402             new_discussion = None
403             for note in notes:
404                 note = discussion.notes.get(note['id'])
405
406                 note_url = f"{mr_url}#note_{note.id}"
407                 when = dateparse.parse(note.created_at).strftime('on %d, %b %Y')
408                 body = f"**{note.author['name']} - {PING_SIGN}{note.author['username']} wrote [here]({note_url})** {when}:\n\n"
409                 body += '\n'.join([l for l in note.body.split('\n')])
410
411                 obj = {
412                     'body': body,
413                     'type': note.type,
414                     'resolvable': note.resolvable,
415                 }
416
417                 if new_discussion:
418                     new_discussion.notes.create(obj)
419                 else:
420                     new_discussion = new_mr.discussions.create(obj)
421
422                 if not note.resolvable or note.resolved:
423                     new_discussion.resolved = True
424                     new_discussion.save()
425
426         fprint(f"{green(' OK')}\n", nested=False)
427
428         print(f"New MR available at: {bold(new_mr_url)}\n")
429
430         return new_mr
431
432     def push_branch(self, branch):
433         fprint(f"-> Pushing branch {branch} to remote {self.gl.user.username}...")
434         if self.git("push", "--no-verify", self.gl.user.username, branch,
435                     interaction_message=f"pushing {branch} to {self.gl.user.username} with:\n  "
436                     f" `$git push {self.gl.user.username} {branch}`") == "SKIP":
437             fprint(yellow("'SKIPPED' (couldn't push)"), nested=False)
438
439             return False
440
441         fprint(f"{green(' OK')}\n", nested=False)
442
443         return True
444
445     def create_branch_for_mr(self, branch, project, mr):
446         remote_name = project.name + '-' + self.gl.user.username
447         remote_branch = f"{MONOREPO_REMOTE_NAME}/{MONOREPO_BRANCH}"
448         if self.use_branch_if_exists:
449             try:
450                 self.git("checkout", branch)
451                 self.git("show", remote_branch + "..", call=True)
452                 if self.dry_run:
453                     fprint("Dry run... not creating MR")
454                     return True
455                 cont = input('\n     Create MR [y/n]? ')
456                 if cont.strip().lower() != 'y':
457                     fprint("Cancelled")
458                     return False
459                 return self.push_branch(branch)
460             except subprocess.CalledProcessError as e:
461                 pass
462
463         self.git("remote", "add", remote_name,
464                  f"{URL}{self.gl.user.username}/{project.name}.git", can_fail=True)
465         self.git("fetch", remote_name)
466
467         if self.git("checkout", remote_branch, "-b", branch,
468                     interaction_message=f"checking out branch with `git checkout {remote_branch} -b {branch}`") == "SKIP":
469             fprint(bold(f"{red('SKIPPED')} (couldn't checkout)\n"), nested=False)
470             return False
471
472         for commit in reversed([c for c in mr.commits()]):
473             if self.git("cherry-pick", commit.id,
474                         interaction_message=f"cherry-picking {commit.id} onto {branch} with:\n  "
475                         f" `$ git cherry-pick {commit.id}`",
476                         revert_operation=["cherry-pick", "--abort"]) == "SKIP":
477                 fprint(f"{yellow('SKIPPED')} (couldn't cherry-pick).", nested=False)
478                 return False
479
480         self.git("show", remote_branch + "..", call=True)
481         if self.dry_run:
482             fprint("Dry run... not creating MR\n")
483             return True
484         cont = input('\n     Create MR [y/n]? ')
485         if cont.strip().lower() != 'y':
486             fprint(f"{red('Cancelled')}\n", nested=False)
487             return False
488
489         return self.push_branch(branch)
490
491     def move_mrs(self, from_projects, to_project):
492         failed_mrs = []
493         found_mr = None
494         for from_project in from_projects:
495             with nested(f'{bold(from_project.path_with_namespace)}'):
496                 fprint(f'Fetching mrs')
497                 mrs = [mr for mr in from_project.mergerequests.list(
498                     all=True, author_id=self.gl.user.id) if mr.author['username'] == self.gl.user.username and mr.state == "opened"]
499                 if not mrs:
500                     fprint(f"{yellow(' None')}\n", nested=False)
501                     continue
502
503                 fprint(f"{green(' DONE')}\n", nested=False)
504
505                 for mr in mrs:
506                     if self.mr:
507                         if self.mr != mr.iid:
508                             continue
509                         found_mr = True
510                     fprint(f'Moving {mr.source_branch} "{mr.title}": {URL}{from_project.path_with_namespace}/merge_requests/{mr.iid}... ')
511                     if mr.source_branch in self.skipped_branches:
512                         print(f"{yellow('SKIPPED')} (blacklisted branch)")
513                         failed_mrs.append(
514                             f"{URL}{from_project.path_with_namespace}/merge_requests/{mr.iid}")
515                         continue
516
517                     with nested(f'{bold(from_project.path_with_namespace)}: {mr.iid}'):
518                         new_mr = self.recreate_mr(from_project, to_project, mr)
519                         if not new_mr:
520                             if not self.dry_run:
521                                 failed_mrs.append(
522                                     f"{URL}{from_project.path_with_namespace}/merge_requests/{mr.iid}")
523                         else:
524                             fprint(f"{green(' OK')}\n", nested=False)
525
526                         self.close_mr(from_project, to_project, mr, new_mr)
527
528             fprint(f"\n{yellow('DONE')} with {from_project.path_with_namespace}\n\n", nested=False)
529
530         if self.mr and not found_mr:
531             sys.exit(bold(red(f"\n==> Couldn't find MR {self.mr} in {self.modules[0]}\n")))
532
533         for mr in failed_mrs:
534             fprint(f"Didn't move MR: {mr}\n")
535
536     def close_mr(self, project, to_project, mr, new_mr):
537         if new_mr:
538             new_mr_url = f"{URL}/{to_project.path_with_namespace}/-/merge_requests/{new_mr.iid}"
539         else:
540             new_mr_url = None
541         mr_url = f"{URL}/{project.path_with_namespace}/-/merge_requests/{mr.iid}"
542         cont = input(f'\n  Close old MR {mr_url} "{bold(mr.title)}" ? [y/n]')
543         if cont.strip().lower() != 'y':
544             fprint(f"{yellow('Not closing old MR')}\n")
545         else:
546             obj = None
547             if new_mr_url:
548                 obj = {'body': f"Moved to: {new_mr_url}"}
549             else:
550                 ret = input(f"Write a comment to add while closing MR {mr.iid} '{bold(mr.title)}':\n\n").strip()
551                 if ret:
552                     obj = {'body': ret}
553
554             if self.dry_run:
555                 fprint(f"{bold('Dry run, not closing')}\n", nested=False)
556             else:
557                 if obj:
558                     mr.discussions.create(obj)
559                 mr.state_event = 'close'
560                 mr.save()
561                 fprint(f'Old MR {mr_url} "{bold(mr.title)}" {yellow("CLOSED")}\n')
562
563     def setup_repo(self):
564         fprint(f"Setting up '{bold(ROOT_DIR)}'...")
565
566         try:
567             out = self.git("status", "--porcelain")
568             if out:
569                 fprint("\n" + red('Git repository is not clean:') + "\n```\n" + out + "\n```\n")
570                 sys.exit(1)
571
572         except Exception as e:
573             exit(
574                 f"Git repository{ROOT_DIR} is not clean. Clean it up before running {sys.argv[0]}\n ({e})")
575
576         self.git('remote', 'add', MONOREPO_REMOTE_NAME,
577                  MONOREPO_REMOTE, can_fail=True)
578         self.git('fetch', MONOREPO_REMOTE_NAME)
579
580         self.git('remote', 'add', self.gl.user.username,
581                  f"git@gitlab.freedesktop.org:{self.gl.user.username}/gstreamer.git", can_fail=True)
582         self.git('fetch', self.gl.user.username,
583                  interaction_message=f"Setup your fork of {URL}gstreamer/gstreamer as remote called {self.gl.user.username}")
584         fprint(f"{green(' OK')}\n", nested=False)
585
586         try:
587             git_rename_limit = int(self.git("config", "merge.renameLimit"))
588         except subprocess.CalledProcessError:
589             git_rename_limit = 0
590         if int(git_rename_limit) < 999999:
591             self.git_rename_limit = git_rename_limit
592             fprint("-> Setting git rename limit to 999999 so we can properly cherry-pick between repos\n")
593             self.git("config", "merge.renameLimit", "999999")
594
595
596 def main():
597     mover = GstMRMover()
598     PARSER.parse_args(namespace=mover)
599     mover.run()
600
601
602 if __name__ == '__main__':
603     main()