-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from simahawk/add-branch
Add new tool: oca-repo-add-branch
- Loading branch information
Showing
12 changed files
with
307 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Copyright 2024 Camptocamp SA | ||
# @author: Simone Orsi | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) | ||
|
||
import logging | ||
import sys | ||
|
||
from .utils import ConfLoader | ||
|
||
handler = logging.StreamHandler(sys.stdout) | ||
handler.setLevel(logging.INFO) | ||
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") | ||
handler.setFormatter(formatter) | ||
logging.basicConfig(level=logging.INFO, handlers=[handler]) | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
class ConfFileManager: | ||
"""Update existing configuration files.""" | ||
|
||
def __init__(self, conf_dir): | ||
self.conf_dir = conf_dir | ||
self.conf_loader = ConfLoader(self.conf_dir) | ||
self.conf_repo = self.conf_loader.load_conf( | ||
"repo", checksum=False, by_filepath=True | ||
) | ||
|
||
def add_branch(self, branch, default=True): | ||
"""Add a branch to all repositories in the configuration.""" | ||
for filepath, repo in self.conf_repo.items(): | ||
for repo_data in repo.values(): | ||
if self._has_manual_branch_mgmt(repo_data): | ||
_logger.info( | ||
"Skipping repo %s as manual_branch_mgmt is enabled.", | ||
filepath.as_posix(), | ||
) | ||
continue | ||
if self._can_add_new_branch(branch, repo_data): | ||
repo_data["branches"].append(branch) | ||
if default and self._can_change_default_branch(repo_data): | ||
repo_data["default_branch"] = branch | ||
self.conf_loader.save_conf(filepath, repo) | ||
_logger.info("Branch %s added to %s.", branch, filepath.as_posix()) | ||
|
||
def _has_manual_branch_mgmt(self, repo_data): | ||
return repo_data.get("manual_branch_mgmt") | ||
|
||
frozen_branches = ("master", "main") | ||
|
||
def _can_add_new_branch(self, branch, repo_data): | ||
branches = repo_data["branches"] | ||
return ( | ||
branch not in branches | ||
and all(x not in branches for x in self.frozen_branches) | ||
and repo_data.get("default_branch") not in self.frozen_branches | ||
) | ||
|
||
def _can_change_default_branch(self, repo_data): | ||
return ( | ||
# Only change if default branch is controlled via config file | ||
"default_branch" in repo_data | ||
# If the branch is "master" it means this is likely the repo of a tool | ||
# and we have only one working branch. | ||
and repo_data["default_branch"] not in self.frozen_branches | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from . import test_manager | ||
from . import test_conf_file_manager |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# For testing: this repo must exist already | ||
# but have a different default branch | ||
test-repo-for-addons: | ||
name: Test repo for addons | ||
description: Repo used to run real tests on oca-repo-manage tool. | ||
category: Logistics | ||
psc: test-team-1 | ||
maintainers: [] | ||
default_branch: "16.0" | ||
branches: | ||
- "16.0" | ||
- "15.0" | ||
test-repo-for-addons-manual: | ||
name: Test repo for addons with manual mgmt flag | ||
description: Repo used to run real tests on oca-repo-manage tool. | ||
category: Logistics | ||
psc: test-team-1 | ||
maintainers: [] | ||
default_branch: "16.0" | ||
branches: | ||
- "16.0" | ||
- "15.0" | ||
manual_branch_mgmt: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# For testing: this repo must NOT exist to test creation | ||
test-repo-for-tools-1: | ||
name: Repository for tools with branch = master | ||
description: Repo used to run real tests on oca-repo-manage tool. | ||
category: Accounting | ||
psc: test-team-2 | ||
maintainers: | ||
- simahawk | ||
default_branch: "master" | ||
branches: | ||
- "master" | ||
test-repo-for-tools-2: | ||
name: Repository for tools with branch = main | ||
description: Repo used to run real tests on oca-repo-manage tool. | ||
category: Accounting | ||
psc: test-team-2 | ||
maintainers: | ||
- simahawk | ||
default_branch: "main" | ||
branches: [] | ||
test-repo-for-tools-with-no-branches: | ||
name: Repository for tools | ||
description: Repo used to run real tests on oca-repo-manage tool. | ||
category: Accounting | ||
psc: test-team-2 | ||
maintainers: | ||
- simahawk | ||
default_branch: "master" | ||
branches: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# Copyright 2024 Camptocamp SA | ||
# @author: Simone Orsi | ||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl) | ||
|
||
import shutil | ||
import tempfile | ||
from unittest import TestCase | ||
|
||
from oca_repo_maintainer.tools.conf_file_manager import ConfFileManager | ||
|
||
from .common import conf_path, conf_path_with_tools | ||
|
||
|
||
class TestManager(TestCase): | ||
def test_add_branch(self): | ||
with tempfile.TemporaryDirectory() as temp_dir: | ||
shutil.copytree(conf_path.as_posix(), temp_dir, dirs_exist_ok=True) | ||
manager = ConfFileManager(temp_dir) | ||
manager.add_branch("100.0") | ||
|
||
conf = manager.conf_loader.load_conf("repo") | ||
self.assertTrue(conf) | ||
for __, repo_data in conf.items(): | ||
self.assertIn("100.0", repo_data["branches"]) | ||
if "default_branch" in repo_data: | ||
self.assertEqual(repo_data["default_branch"], "100.0") | ||
|
||
def test_add_branch_no_default(self): | ||
with tempfile.TemporaryDirectory() as temp_dir: | ||
shutil.copytree(conf_path.as_posix(), temp_dir, dirs_exist_ok=True) | ||
manager = ConfFileManager(temp_dir) | ||
manager.add_branch("100.0", default=False) | ||
|
||
conf = manager.conf_loader.load_conf("repo") | ||
self.assertTrue(conf) | ||
for __, repo_data in conf.items(): | ||
self.assertIn("100.0", repo_data["branches"]) | ||
if "default_branch" in repo_data: | ||
self.assertNotEqual(repo_data["default_branch"], "100.0") | ||
|
||
def test_preserve_master(self): | ||
with tempfile.TemporaryDirectory() as temp_dir: | ||
shutil.copytree( | ||
conf_path_with_tools.as_posix(), temp_dir, dirs_exist_ok=True | ||
) | ||
manager = ConfFileManager(temp_dir) | ||
conf = manager.conf_loader.load_conf("repo") | ||
|
||
self.assertEqual(conf["test-repo-for-addons"]["branches"], ["16.0", "15.0"]) | ||
self.assertEqual(conf["test-repo-for-addons"]["default_branch"], "16.0") | ||
self.assertEqual(conf["test-repo-for-tools-1"]["branches"], ["master"]) | ||
self.assertEqual(conf["test-repo-for-tools-1"]["default_branch"], "master") | ||
self.assertEqual(conf["test-repo-for-tools-2"]["branches"], []) | ||
self.assertEqual(conf["test-repo-for-tools-2"]["default_branch"], "main") | ||
self.assertEqual( | ||
conf["test-repo-for-tools-with-no-branches"]["branches"], [] | ||
) | ||
self.assertEqual( | ||
conf["test-repo-for-tools-with-no-branches"]["default_branch"], "master" | ||
) | ||
|
||
manager.add_branch("100.0") | ||
|
||
conf = manager.conf_loader.load_conf("repo") | ||
|
||
self.assertEqual( | ||
conf["test-repo-for-addons"]["branches"], ["16.0", "15.0", "100.0"] | ||
) | ||
self.assertEqual(conf["test-repo-for-addons"]["default_branch"], "100.0") | ||
self.assertEqual(conf["test-repo-for-tools-1"]["branches"], ["master"]) | ||
self.assertEqual(conf["test-repo-for-tools-1"]["default_branch"], "master") | ||
self.assertEqual(conf["test-repo-for-tools-2"]["branches"], []) | ||
self.assertEqual(conf["test-repo-for-tools-2"]["default_branch"], "main") | ||
self.assertEqual( | ||
conf["test-repo-for-tools-with-no-branches"]["branches"], [] | ||
) | ||
self.assertEqual( | ||
conf["test-repo-for-tools-with-no-branches"]["default_branch"], "master" | ||
) | ||
|
||
def test_skip_manual_mgmt(self): | ||
with tempfile.TemporaryDirectory() as temp_dir: | ||
shutil.copytree( | ||
conf_path_with_tools.as_posix(), temp_dir, dirs_exist_ok=True | ||
) | ||
manager = ConfFileManager(temp_dir) | ||
conf = manager.conf_loader.load_conf("repo") | ||
|
||
self.assertEqual(conf["test-repo-for-addons"]["branches"], ["16.0", "15.0"]) | ||
self.assertEqual(conf["test-repo-for-addons"]["default_branch"], "16.0") | ||
self.assertEqual( | ||
conf["test-repo-for-addons-manual"]["branches"], ["16.0", "15.0"] | ||
) | ||
self.assertEqual( | ||
conf["test-repo-for-addons-manual"]["default_branch"], "16.0" | ||
) | ||
self.assertEqual( | ||
conf["test-repo-for-addons-manual"]["manual_branch_mgmt"], True | ||
) | ||
|
||
manager.add_branch("100.0") | ||
|
||
conf = manager.conf_loader.load_conf("repo") | ||
|
||
self.assertEqual( | ||
conf["test-repo-for-addons"]["branches"], ["16.0", "15.0", "100.0"] | ||
) | ||
self.assertEqual(conf["test-repo-for-addons"]["default_branch"], "100.0") | ||
self.assertEqual( | ||
conf["test-repo-for-addons-manual"]["branches"], ["16.0", "15.0"] | ||
) | ||
self.assertEqual( | ||
conf["test-repo-for-addons-manual"]["default_branch"], "16.0" | ||
) |