From 00a7584b4451230b18aaf69bd88fd54a2851813d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Mon, 3 Oct 2022 09:12:20 +0200 Subject: [PATCH 1/3] oca-create-branch: carry over answers from previous branches --- setup.py | 1 + tools/create_branch.py | 74 +++++++++++++++++++++++++++--------------- 2 files changed, 48 insertions(+), 27 deletions(-) diff --git a/setup.py b/setup.py index 929a8ee5..9d24cd29 100644 --- a/setup.py +++ b/setup.py @@ -44,6 +44,7 @@ "pyproject_dependencies ; python_version>='3.7'", "setuptools-odoo", # for oca-gen-external-dependencies "whool", # for oca-gen-external-dependencies + "copier>=9", ], python_requires=">=3.6", classifiers=[ diff --git a/tools/create_branch.py b/tools/create_branch.py index 76b52390..8f70ba08 100644 --- a/tools/create_branch.py +++ b/tools/create_branch.py @@ -1,14 +1,33 @@ -"""Create a branch in all addons project. - -TODO -- load copier answers from a previous branch -""" +"""Create a branch in all addons project.""" +from pathlib import Path import subprocess +from typing import Dict import click +import yaml +import copier from .oca_projects import get_repositories, temporary_clone +COPIER_ANSWERS_FILE = ".copier-answers.yml" +COPIER_ANSWERS_TO_CARRY_OVER = ("repo_description", "repo_name") + + +def _read_prev_branch_answers(prev_branch: str, answers: Dict[str, str]) -> None: + try: + subprocess.check_call(["git", "checkout", prev_branch]) + except subprocess.CalledProcessError: + # likely branch not found + return + if not Path(COPIER_ANSWERS_FILE).is_file(): + return + with open(COPIER_ANSWERS_FILE) as f: + prev_branch_answers = yaml.load(f, Loader=yaml.SafeLoader) + for question in COPIER_ANSWERS_TO_CARRY_OVER: + if question not in prev_branch_answers: + continue + answers[question] = prev_branch_answers[question] + @click.command("Create an orphan branch from a 'copier' template") @click.argument("new_branch") @@ -25,8 +44,12 @@ "repos", multiple=True, ) -def main(new_branch, copier_template, copier_template_vcs_ref, repos): - for repo in repos or get_repositories(): +@click.option( + "--prev-branch", + help="Previous branch where to read some copier answers.", +) +def main(new_branch, copier_template, copier_template_vcs_ref, repos, prev_branch): + for repo in repos or sorted(get_repositories()): print("=" * 10, repo, "=" * 10) with temporary_clone(repo): # check if branch already exists @@ -42,30 +65,27 @@ def main(new_branch, copier_template, copier_template_vcs_ref, repos): subprocess.check_call( ["git", "config", "user.email", "oca-git-bot@odoo-community.org"], ) + # read answers from previous branch + answers = { + "odoo_version": float(new_branch), + "repo_slug": repo, + "repo_name": repo, + "ci": "GitHub", + } + if prev_branch: + _read_prev_branch_answers(prev_branch, answers) # create empty git branch subprocess.check_call(["git", "checkout", "--orphan", new_branch]) subprocess.check_call(["git", "reset", "--hard"]) # copier - copier_cmd = [ - "copier", - "--data", - f"odoo_version={new_branch}", - "--data", - f"repo_slug={repo}", - "--data", - f"repo_name={repo}", - "--data", - "repo_description=TODO: add repo description.", - "--data", - "dependency_installation_mode=PIP", - "--data", - "ci=GitHub", - "--force", - ] - if copier_template_vcs_ref: - copier_cmd += ["--vcs-ref", copier_template_vcs_ref] - copier_cmd += [copier_template, "."] - subprocess.check_call(copier_cmd) + copier.run_copy( + src_path=copier_template, + dst_path=".", + data=answers, + defaults=True, + vcs_ref=copier_template_vcs_ref, + unsafe=True, + ) # pre-commit run -a subprocess.check_call(["git", "add", "."]) subprocess.call(["pre-commit", "run", "-a"]) From dedbeb721cf20bb293588c39a1253c3aaf7d0bf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Sun, 12 Nov 2023 15:21:10 +0100 Subject: [PATCH 2/3] Don't depend on copier for python<3.8 Copier is required for oca-create-branch only, and copier 9 does not support python<3.8. --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 9d24cd29..ceccb1d3 100644 --- a/setup.py +++ b/setup.py @@ -44,7 +44,7 @@ "pyproject_dependencies ; python_version>='3.7'", "setuptools-odoo", # for oca-gen-external-dependencies "whool", # for oca-gen-external-dependencies - "copier>=9", + "copier>=9 ; python_version>='3.8'", # for oca-create-branch ], python_requires=">=3.6", classifiers=[ From 7d320047567bae6f94c7c9a252ec1e80667781b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Sun, 12 Nov 2023 15:33:17 +0100 Subject: [PATCH 3/3] create-branch: ignore null previous answers --- tools/create_branch.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/create_branch.py b/tools/create_branch.py index 8f70ba08..7bec9a6c 100644 --- a/tools/create_branch.py +++ b/tools/create_branch.py @@ -26,6 +26,8 @@ def _read_prev_branch_answers(prev_branch: str, answers: Dict[str, str]) -> None for question in COPIER_ANSWERS_TO_CARRY_OVER: if question not in prev_branch_answers: continue + if prev_branch_answers[question] is None: + continue answers[question] = prev_branch_answers[question] @@ -70,6 +72,7 @@ def main(new_branch, copier_template, copier_template_vcs_ref, repos, prev_branc "odoo_version": float(new_branch), "repo_slug": repo, "repo_name": repo, + "repo_description": "TODO: add repo description", "ci": "GitHub", } if prev_branch: