Skip to content

Commit

Permalink
oca-create-branch: carry over answers from previous branches
Browse files Browse the repository at this point in the history
  • Loading branch information
sbidoul committed Oct 3, 2022
1 parent eb76422 commit cdb9a3f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
'selenium',
'twine',
'wheel',
'copier',
],
python_requires='>=3.6',
classifiers=[
Expand Down
65 changes: 44 additions & 21 deletions tools/create_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,35 @@
TODO
- load copier answers from a previous branch
"""
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")
Expand All @@ -25,7 +48,11 @@
"repos",
multiple=True,
)
def main(new_branch, copier_template, copier_template_vcs_ref, repos):
@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 get_repositories():
print("=" * 10, repo, "=" * 10)
with temporary_clone(repo):
Expand All @@ -42,30 +69,26 @@ def main(new_branch, copier_template, copier_template_vcs_ref, repos):
subprocess.check_call(
["git", "config", "user.email", "[email protected]"],
)
# 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,
)
# pre-commit run -a
subprocess.check_call(["git", "add", "."])
subprocess.call(["pre-commit", "run", "-a"])
Expand Down

0 comments on commit cdb9a3f

Please sign in to comment.