Skip to content

Commit

Permalink
Merge pull request #266 from acsone/setuptools-odoo-make-default-max-…
Browse files Browse the repository at this point in the history
…branch

Generate pyproject.toml instead of setup.py
  • Loading branch information
sbidoul authored Nov 12, 2023
2 parents 5687d24 + f646a6a commit 69906dd
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 13 deletions.
7 changes: 3 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ RUN set -x \
&& python3 -m venv /ocamt \
&& /ocamt/bin/pip install --no-cache-dir -U pip wheel
RUN set -x \
&& /ocamt/bin/pip install --no-cache-dir -e git+https://github.com/OCA/maintainer-tools@969238e47c07d0c40573acff81d170f63245d738#egg=oca-maintainers-tools \
&& /ocamt/bin/pip install --no-cache-dir -e git+https://github.com/OCA/maintainer-tools@568cacd1d6eef453063a524a5ce63dcd49c7259b#egg=oca-maintainers-tools \
&& ln -s /ocamt/bin/oca-gen-addons-table /usr/local/bin/ \
&& ln -s /ocamt/bin/oca-gen-addon-readme /usr/local/bin/ \
&& ln -s /ocamt/bin/oca-towncrier /usr/local/bin/
RUN set -x \
&& /ocamt/bin/pip install --no-cache-dir 'setuptools-odoo>=3.0.3' \
&& ln -s /ocamt/bin/oca-gen-metapackage /usr/local/bin/ \
&& ln -s /ocamt/bin/oca-towncrier /usr/local/bin/ \
&& ln -s /ocamt/bin/setuptools-odoo-make-default /usr/local/bin/

# isolate from system python libraries
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
"lxml",
# for setuptools-odoo-make-default
"setuptools-odoo",
# for whool-init
"whool",
# packaging
"packaging>=22",
],
Expand Down
8 changes: 7 additions & 1 deletion src/oca_github_bot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def func_wrapper(*args, **kwargs):
# Available tasks:
# delete_branch,tag_approved,tag_ready_to_merge,gen_addons_table,
# gen_addons_readme,gen_addons_icon,setuptools_odoo,merge_bot,tag_needs_review,
# migration_issue_bot
# migration_issue_bot,whool_init,gen_metapackage
BOT_TASKS = os.environ.get("BOT_TASKS", "all").split(",")

BOT_TASKS_DISABLED = os.environ.get("BOT_TASKS_DISABLED", "").split(",")
Expand Down Expand Up @@ -143,4 +143,10 @@ def func_wrapper(*args, **kwargs):
"build,pip,setuptools<58,wheel,setuptools-odoo,whool",
).split(",")

# minimum Odoo series supported by the bot
MAIN_BRANCH_BOT_MIN_VERSION = os.environ.get("MAIN_BRANCH_BOT_MIN_VERSION", "8.0")

# First Odoo Series for which the whool_init and gen_metapackage tasks are run on main
# branches. For previous versions, the setuptools_odoo task is run and generates
# setup.py instead of pyproject.toml.
GEN_PYPROJECT_MIN_VERSION = os.environ.get("GEN_PYPROJECT_MIN_VERSION", "17.0")
11 changes: 11 additions & 0 deletions src/oca_github_bot/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import shutil
import tempfile
from contextlib import contextmanager
from pathlib import Path

import appdirs
import github3
Expand Down Expand Up @@ -159,3 +160,13 @@ def git_get_head_sha(cwd):

def git_get_current_branch(cwd):
return check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"], cwd=cwd).strip()


def git_commit_if_needed(glob_pattern, msg, cwd):
files = [p.absolute() for p in Path(cwd).glob(glob_pattern)]
if not files:
return # no match nothing to commit
check_call(["git", "add", *files], cwd=cwd)
if call(["git", "diff", "--cached", "--quiet", "--exit-code"], cwd=cwd) == 0:
return # nothing added
return check_call(["git", "commit", "-m", msg], cwd=cwd)
40 changes: 36 additions & 4 deletions src/oca_github_bot/tasks/main_branch_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
GEN_ADDON_ICON_EXTRA_ARGS,
GEN_ADDON_README_EXTRA_ARGS,
GEN_ADDONS_TABLE_EXTRA_ARGS,
GEN_PYPROJECT_MIN_VERSION,
dist_publisher,
switchable,
)
from ..github import git_push_if_needed, temporary_clone
from ..github import git_commit_if_needed, git_push_if_needed, temporary_clone
from ..manifest import get_odoo_series_from_branch
from ..process import check_call
from ..queue import getLogger, task
from ..version_branch import is_main_branch_bot_branch
from ..version_branch import is_main_branch_bot_branch, is_supported_main_branch

_logger = getLogger(__name__)

Expand Down Expand Up @@ -73,6 +74,32 @@ def _setuptools_odoo_make_default(org, repo, branch, cwd):
)


@switchable("whool_init")
def _whool_init(org, repo, branch, cwd):
_logger.info(
"generate pyproejct.toml with whool init in %s/%s@%s\n", org, repo, branch
)
whool_init_cmd = ["whool", "init"]
check_call(whool_init_cmd, cwd=cwd)
git_commit_if_needed("*/pyproject.toml", "[BOT] add pyproject.toml", cwd=cwd)


@switchable("gen_metapackage")
def _gen_metapackage(org, repo, branch, cwd):
if not is_supported_main_branch(branch, min_version="15.0"):
# We don't support branches < 15 because I don't want to worry about
# the package name prefix. From 15.0 on, the package name is always prefixed
# with "odoo-addons-".
_logger.warning("gen_metapackage not supported for branch %s", branch)
return
_logger.info("oca-gen-metapackage in %s/%s@%s\n", org, repo, branch)
gen_metapackage_cmd = ["oca-gen-metapackage", f"odoo-addons-{org.lower()}-{repo}"]
check_call(gen_metapackage_cmd, cwd=cwd)
git_commit_if_needed(
"setup/_metapackage", "[BOT] add or update setup/_metapackage", cwd=cwd
)


def main_branch_bot_actions(org, repo, branch, cwd):
"""
Run main branch bot actions on a local git checkout.
Expand All @@ -86,8 +113,13 @@ def main_branch_bot_actions(org, repo, branch, cwd):
_gen_addons_readme(org, repo, branch, cwd)
# generate icon
_gen_addons_icon(org, repo, branch, cwd)
# generate/clean default setup.py
_setuptools_odoo_make_default(org, repo, branch, cwd)
if is_supported_main_branch(branch, min_version=GEN_PYPROJECT_MIN_VERSION):
# generate pyproject.toml for addons and metapackage
_whool_init(org, repo, branch, cwd)
_gen_metapackage(org, repo, branch, cwd)
else:
# generate/clean default setup.py and metapackage
_setuptools_odoo_make_default(org, repo, branch, cwd)


@task()
Expand Down
13 changes: 10 additions & 3 deletions src/oca_github_bot/version_branch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,18 @@
)


def is_main_branch_bot_branch(branch_name):
def is_supported_main_branch(branch_name, min_version=None):
if not ODOO_VERSION_RE.match(branch_name):
return False
return version.parse(branch_name) >= version.parse(
config.MAIN_BRANCH_BOT_MIN_VERSION
branch_version = version.parse(branch_name)
if min_version and branch_version < version.parse(min_version):
return False
return True


def is_main_branch_bot_branch(branch_name):
return is_supported_main_branch(
branch_name, min_version=config.MAIN_BRANCH_BOT_MIN_VERSION
)


Expand Down
40 changes: 39 additions & 1 deletion tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@

import re
import subprocess
from pathlib import Path

from oca_github_bot.github import git_get_current_branch, git_get_head_sha
from oca_github_bot.github import (
git_commit_if_needed,
git_get_current_branch,
git_get_head_sha,
)


def test_git_get_head_sha(git_clone):
Expand All @@ -16,3 +21,36 @@ def test_git_get_current_branch(git_clone):
assert git_get_current_branch(git_clone) == "master"
subprocess.check_call(["git", "checkout", "-b", "abranch"], cwd=git_clone)
assert git_get_current_branch(git_clone) == "abranch"


def test_git_commit_if_needed_no_change(tmp_path: Path) -> None:
subprocess.check_call(["git", "init"], cwd=tmp_path)
subprocess.check_call(
["git", "config", "user.email", "[email protected]"], cwd=tmp_path
)
subprocess.check_call(["git", "config", "user.name", "test"], cwd=tmp_path)
toto = tmp_path / "toto"
toto.touch()
git_commit_if_needed("toto", "initial commit", cwd=tmp_path)
head_sha = git_get_head_sha(tmp_path)
# no change
git_commit_if_needed("toto", "no commit", cwd=tmp_path)
assert git_get_head_sha(tmp_path) == head_sha
# change in existing file
toto.write_text("toto")
git_commit_if_needed("toto", "toto changed", cwd=tmp_path)
head_sha2 = git_get_head_sha(tmp_path)
assert head_sha2 != head_sha
# add subdirectory
subdir = tmp_path / "subdir"
subdir.mkdir()
titi = subdir / "titi"
titi.touch()
git_commit_if_needed("subdir", "titi added", cwd=tmp_path)
head_sha3 = git_get_head_sha(tmp_path)
assert head_sha3 != head_sha2
# add glob
subdir.joinpath("pyproject.toml").touch()
git_commit_if_needed("*/pyproject.toml", "pyproject.toml added", cwd=tmp_path)
head_sha4 = git_get_head_sha(tmp_path)
assert head_sha4 != head_sha3
15 changes: 15 additions & 0 deletions tests/test_version_branch.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# Copyright (c) ACSONE SA/NV 2018
# Distributed under the MIT License (http://opensource.org/licenses/MIT).

import pytest

from oca_github_bot.version_branch import (
is_main_branch_bot_branch,
is_merge_bot_branch,
is_protected_branch,
is_supported_main_branch,
make_merge_bot_branch,
parse_merge_bot_branch,
search_merge_bot_branch,
Expand Down Expand Up @@ -72,3 +75,15 @@ def test_search_merge_bot_branch():
assert search_merge_bot_branch(text) == "12.0-ocabot-merge-pr-100-by-toto-bump-no"
text = "blah blah more stuff"
assert search_merge_bot_branch(text) is None


@pytest.mark.parametrize(
("branch_name", "min_version", "expected"),
[
("8.0", None, True),
("8.0", "8.0", True),
("8.0", "9.0", False),
],
)
def test_is_supported_branch(branch_name, min_version, expected):
assert is_supported_main_branch(branch_name, min_version) is expected

0 comments on commit 69906dd

Please sign in to comment.