Skip to content

Commit

Permalink
[ADD] add option rename-to to allow rename add
Browse files Browse the repository at this point in the history
  • Loading branch information
trisdoan committed Dec 20, 2024
1 parent e7579cc commit f5d3ad4
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
1 change: 1 addition & 0 deletions oca_port/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class App(Output):
clear_cache: bool = False
github_token: str = None
cli: bool = False # Not documented, should not be used outside of the CLI
rename_to: str = None

_available_outputs = ("json",)

Expand Down
3 changes: 3 additions & 0 deletions oca_port/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
@click.option("--fetch", is_flag=True, help="Fetch remote branches from upstream.")
@click.option("--no-cache", is_flag=True, help="Disable user's cache.")
@click.option("--clear-cache", is_flag=True, help="Clear the user's cache.")
@click.option("--rename-to", help="New name for the addon")
def main(
addon_path: str,
source: str,
Expand All @@ -114,6 +115,7 @@ def main(
no_cache: bool,
clear_cache: bool,
dry_run: bool,
rename_to: str,
):
"""Migrate ADDON from SOURCE to TARGET or list Pull Requests to port.
Expand Down Expand Up @@ -148,6 +150,7 @@ def main(
clear_cache=clear_cache,
dry_run=dry_run,
cli=True,
rename_to=rename_to,
)
except ForkValueError as exc:
error_msg = prepare_remote_error_msg(*exc.args)
Expand Down
79 changes: 76 additions & 3 deletions oca_port/migrate_addon.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@
f"\t\t=> {bc.BOLD}" "{new_pr_url}" f"{bc.END}",
]
)
RENAME_TIPS = "\n".join(
[
f"\n{bc.BOLD}{bc.OKCYAN}The next steps are:{bc.END}",
(f"\t1) Change the filenames of the renamed addon as necessary {bc.END}"),
("\t2) Reduce the number of commits " f"('{bc.DIM}OCA Transbot...{bc.END}'):"),
f"\t\t=> {bc.BOLD}{MIG_MERGE_COMMITS_URL}{bc.END}",
"\t3) Adapt the module to the {version} version:",
f"\t\t=> {bc.BOLD}" "{mig_tasks_url}" f"{bc.END}",
(
"\t4) On a shell command, type this for uploading the content to GitHub:\n"
f"{bc.DIM}"
"\t\t$ git add --all\n"
'\t\t$ git commit -m "[MIG] {addon}: Migration to {version}"\n'
"\t\t$ git push {remote} {mig_branch} --set-upstream"
f"{bc.END}"
),
"\t5) Create the PR against {from_org}/{repo_name}:",
f"\t\t=> {bc.BOLD}" "{new_pr_url}" f"{bc.END}",
]
)


class MigrateAddon(Output):
Expand All @@ -76,6 +96,12 @@ def __init__(self, app):
self.app.repo,
(
self.app.destination.branch
or (
self.app.rename_to
and MIG_BRANCH_NAME.format(
branch=self.app.target_version, addon=self.app.rename_to
)
)
or MIG_BRANCH_NAME.format(
branch=self.app.target_version, addon=self.app.addon
)
Expand Down Expand Up @@ -141,7 +167,7 @@ def run(self):
if self.app.repo.untracked_files:
raise click.ClickException("Untracked files detected, abort")
self._checkout_base_branch()
adapted = False
renamed = adapted = False
if self._create_mig_branch():
# Case where the addon shouldn't be ported (blacklisted)
if self.app.storage.dirty:
Expand All @@ -155,12 +181,46 @@ def run(self):
g.run_pre_commit(self.app.repo, self.app.addon)
else:
adapted = self._apply_code_pattern()

renamed = self._rename_module()
# 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(adapted=adapted)
self._print_tips(adapted=adapted, renamed=renamed)
return True, None

def _rename_module(self):
if self.app.rename_to:
try:
import git_filter_repo as fr

repo = self.app.repo
addons_tree = repo.commit(self.mig_branch.ref()).tree
if self.app.addons_rootdir and self.app.addons_rootdir.name:
addons_tree /= str(self.app.addons_rootdir)
branch_addons = [t.path for t in addons_tree.trees]

if str(self.app.rename_to) in branch_addons:
raise ValueError(f"{self.app.rename_to} already exists")
# rename addon
os.rename(self.app.addon_path, self.app.rename_to)

# rewrite history with new name
args = fr.FilteringOptions.parse_args(
[
f"--path-rename={self.app.addon_path}:{self.app.rename_to}",
"--refs",
self.mig_branch.name,
"--force", # it's safe as it functions on mig_branch
]
)
filter = fr.RepoFilter(args)
filter.run()
return True
except ImportError:
raise SystemExit("Error: Couldn't find git-filter-repo")
return False

def _checkout_base_branch(self):
# Ensure to not start to work from a working branch
if self.app.to_branch.name in self.app.repo.heads:
Expand Down Expand Up @@ -218,7 +278,7 @@ def _apply_patches(self, patches_dir):
f"has been migrated."
)

def _print_tips(self, blacklisted=False, adapted=False):
def _print_tips(self, blacklisted=False, adapted=False, renamed=False):
mig_tasks_url = MIG_TASKS_URL.format(version=self.app.target_version)
pr_title_encoded = urllib.parse.quote(
MIG_NEW_PR_TITLE.format(
Expand All @@ -243,6 +303,19 @@ def _print_tips(self, blacklisted=False, adapted=False):
)
print(tips)
return
if renamed:
tips = RENAME_TIPS.format(
from_org=self.app.upstream_org,
repo_name=self.app.repo_name,
addon=self.app.addon,
version=self.app.target_version,
remote=self.app.destination.remote or "YOUR_REMOTE",
mig_branch=self.mig_branch.name,
mig_tasks_url=mig_tasks_url,
new_pr_url=new_pr_url,
)
print(tips)
return
if adapted:
tips = SIMPLIFIED_MIG_TIPS.format(
from_org=self.app.upstream_org,
Expand Down

0 comments on commit f5d3ad4

Please sign in to comment.