From 30cd12851d22636776a059111acbb126ec810723 Mon Sep 17 00:00:00 2001 From: trisdoan Date: Thu, 3 Oct 2024 16:43:13 +0700 Subject: [PATCH] [IMP] integrate with odoo-module-migrator --- README.md | 4 ++++ oca_port/migrate_addon.py | 44 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1fc3d7d..99b2f47 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,10 @@ Installing $ cd oca-port $ pipx install . +To automatically apply code patterns with [odoo-module-migrator](https://github.com/OCA/odoo-module-migrator), install library with below syntax: + + $ pipx install git+https://github.com/OCA/odoo-module-migrator.git@master + Using ----- diff --git a/oca_port/migrate_addon.py b/oca_port/migrate_addon.py index 1b5258a..cbff6b2 100644 --- a/oca_port/migrate_addon.py +++ b/oca_port/migrate_addon.py @@ -4,6 +4,7 @@ import os import tempfile import urllib.parse +import pkg_resources import click @@ -56,6 +57,15 @@ f"\t\t=> {bc.BOLD}" "{new_pr_url}" f"{bc.END}", ] ) +SIMPLIFIED_MIG_TIPS = "\n".join( + [ + f"\n{bc.BOLD}{bc.OKCYAN}The next steps are:{bc.END}", + ("\t1) Reduce the number of commits " f"('{bc.DIM}OCA Transbot...{bc.END}'):"), + f"\t\t=> {bc.BOLD}{MIG_MERGE_COMMITS_URL}{bc.END}", + "\t2) Create the PR against {from_org}/{repo_name}:", + f"\t\t=> {bc.BOLD}" "{new_pr_url}" f"{bc.END}", + ] +) class MigrateAddon(Output): @@ -131,6 +141,7 @@ def run(self): if self.app.repo.untracked_files: raise click.ClickException("Untracked files detected, abort") self._checkout_base_branch() + adapted = False if self._create_mig_branch(): # Case where the addon shouldn't be ported (blacklisted) if self.app.storage.dirty: @@ -140,11 +151,14 @@ def run(self): with tempfile.TemporaryDirectory() as patches_dir: self._generate_patches(patches_dir) self._apply_patches(patches_dir) - g.run_pre_commit(self.app.repo, self.app.addon) + if pkg_resources.get_distribution("odoo-module-migrator") is None: + g.run_pre_commit(self.app.repo, self.app.addon) + else: + adapted = self._apply_code_pattern() # Check if the addon has commits that update neighboring addons to # make it work properly PortAddonPullRequest(self.app, push_branch=False).run() - self._print_tips() + self._print_tips(adapted=adapted) return True, None def _checkout_base_branch(self): @@ -204,7 +218,7 @@ def _apply_patches(self, patches_dir): f"has been migrated." ) - def _print_tips(self, blacklisted=False): + def _print_tips(self, blacklisted=False, adapted=False): mig_tasks_url = MIG_TASKS_URL.format(version=self.app.target_version) pr_title_encoded = urllib.parse.quote( MIG_NEW_PR_TITLE.format( @@ -229,6 +243,14 @@ def _print_tips(self, blacklisted=False): ) print(tips) return + if adapted: + tips = SIMPLIFIED_MIG_TIPS.format( + from_org=self.app.upstream_org, + repo_name=self.app.repo_name, + new_pr_url=new_pr_url, + ) + print(tips) + return tips = MIG_TIPS.format( from_org=self.app.upstream_org, repo_name=self.app.repo_name, @@ -240,3 +262,19 @@ def _print_tips(self, blacklisted=False): new_pr_url=new_pr_url, ) print(tips) + + def _apply_code_pattern(self): + print("Apply code pattern...") + from odoo_module_migrate.migration import Migration + + try: + migration = Migration( + self.app.addons_rootdir, + self.app.source_version, + self.app.target_version, + [self.app.addon], + ) + migration.run() + return True + except KeyboardInterrupt: + pass