Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oca-repo-add-branch: add --repo-whitelist option #22

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions oca_repo_maintainer/cli/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@ def manage(conf_dir, org, token):
@click.option(
"--default/--no-default", default=True, help="Set default branch as default."
)
def add_branch(conf_dir, branch, default=True):
@click.option("--repo-whitelist", help="CSV list of repo names to update")
def add_branch(conf_dir, branch, default=True, repo_whitelist=None):
"""Add a branch to all repositories in the configuration."""
ConfFileManager(conf_dir).add_branch(branch, default=default)
if repo_whitelist:
repo_whitelist = [x.strip() for x in repo_whitelist.split(",") if x.strip()]
ConfFileManager(conf_dir).add_branch(
branch, default=default, repo_whitelist=repo_whitelist
)


if __name__ == "__main__":
Expand Down
14 changes: 10 additions & 4 deletions oca_repo_maintainer/tools/conf_file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ def __init__(self, conf_dir):
"repo", checksum=False, by_filepath=True
)

def add_branch(self, branch, default=True):
def add_branch(self, branch, default=True, repo_whitelist=None):
"""Add a branch to all repositories in the configuration."""
for filepath, repo in self.conf_repo.items():
for repo_data in repo.values():
changed = False
for repo_slug, repo_data in repo.items():
if repo_whitelist and repo_slug not in repo_whitelist:
continue
if self._has_manual_branch_mgmt(repo_data):
_logger.info(
"Skipping repo %s as manual_branch_mgmt is enabled.",
Expand All @@ -38,10 +41,13 @@ def add_branch(self, branch, default=True):
continue
if self._can_add_new_branch(branch, repo_data):
repo_data["branches"].append(branch)
changed = True
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())
changed = True
if changed:
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")
Expand Down
15 changes: 15 additions & 0 deletions tests/test_conf_file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ def test_add_branch_no_default(self):
if "default_branch" in repo_data:
self.assertNotEqual(repo_data["default_branch"], "100.0")

def test_add_branch_repo_whitelist(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=True, repo_whitelist=["test-repo-2"])

conf = manager.conf_loader.load_conf("repo")
self.assertEqual(conf["test-repo-1"]["branches"], ["16.0", "15.0"])
self.assertEqual(conf["test-repo-2"]["branches"], ["13.0", "12.0", "100.0"])

def test_preserve_master(self):
with tempfile.TemporaryDirectory() as temp_dir:
shutil.copytree(
Expand All @@ -59,6 +69,11 @@ def test_preserve_master(self):
conf["test-repo-for-tools-with-no-branches"]["default_branch"], "master"
)

with tempfile.TemporaryDirectory() as temp_dir:
shutil.copytree(
conf_path_with_tools.as_posix(), temp_dir, dirs_exist_ok=True
)
manager = ConfFileManager(temp_dir)
manager.add_branch("100.0")

conf = manager.conf_loader.load_conf("repo")
Expand Down
Loading