diff --git a/cli/src/core/git.py b/cli/src/core/git.py index 5d99634..17638b2 100644 --- a/cli/src/core/git.py +++ b/cli/src/core/git.py @@ -6,23 +6,22 @@ import git logger = logging.getLogger() -def git_push_service_directory( - repo_root: str | Path, branch_name: str, service_dir_rel_path: str, commit_msg: str +def git_push_changes( + repo: git.Repo, + branch_name: str, + service_dir_rel_path: str, + registry_file_rel_path: str, + commit_msg: str, ): """ Push newly created service to Gitea """ - repo = git.Repo(repo_root) - original_branch = repo.active_branch.name - try: - switch_branch(repo, branch_name) - - logger.info(f"Staging files inside: {service_dir_rel_path}...") - repo.index.add([service_dir_rel_path]) + logger.info(f"Staging files inside: {service_dir_rel_path} and {registry_file_rel_path}...") + repo.index.add([service_dir_rel_path, registry_file_rel_path]) # Check if there are changes to avoid empty commit crashes - if not repo.is_dirty(index=True, working_tree=False, path=service_dir_rel_path): + if not repo.is_dirty(index=True, working_tree=False): logger.info("No changes detected. Skipping commit.") return @@ -34,11 +33,9 @@ def git_push_service_directory( origin.push(refspec=f"{branch_name}:{branch_name}").raise_if_error() logger.info("Successfully pushed!") - switch_branch(repo, original_branch) except Exception as e: logger.info(f"Git Operation Failed: {e}") - switch_branch(repo, original_branch) def switch_branch(repo: git.Repo, branch_name: str): @@ -51,3 +48,11 @@ def switch_branch(repo: git.Repo, branch_name: str): else: new_branch = repo.create_head(branch_name) new_branch.checkout() + + +def assert_clean_repo(repo: git.Repo): + if repo.is_dirty(index=True, working_tree=True): + raise RuntimeError( + "Your Git repository has uncommitted modifications to tracked files.\n" + "Please commit, stash, or discard them before proceeding." + ) diff --git a/cli/src/core/models.py b/cli/src/core/models.py index 7bfa7c3..d66416c 100644 --- a/cli/src/core/models.py +++ b/cli/src/core/models.py @@ -69,7 +69,9 @@ class ServiceRegistry(BaseModel): svc = Service(id=self.next_available_id, name=name, status=ServiceStatus.ACTIVE) self.services.update({svc.id: svc.to_dict()}) self.next_available_id += 1 - with open(self.config_path, "w") as f: - yaml.safe_dump(self.model_dump(), f) return svc + + def write_to_config(self) -> None: + with open(self.config_path, "w") as f: + yaml.safe_dump(self.model_dump(), f) diff --git a/cli/src/service.py b/cli/src/service.py index c3bfb2d..9c64488 100644 --- a/cli/src/service.py +++ b/cli/src/service.py @@ -1,6 +1,7 @@ +import git import typer -from core.git import git_push_service_directory +from core.git import assert_clean_repo, git_push_changes, switch_branch from core.models import ServiceRegistry from core.render_templates import render_new_service_templates from core.utils import get_git_root, init_logging @@ -15,9 +16,16 @@ def add( name: str = typer.Option(..., "--name", "-n"), ): init_logging() - # TODO: add restriction on name (maybe: [a-zA-Z0-9-]) + registry = ServiceRegistry.read_from_config() service = registry.add_service(name) + + repo = git.Repo(REPO_ROOT) + assert_clean_repo(repo) + original_branch = repo.active_branch.name + branch_name = f"feature/add-service-{service.name_lower}" + switch_branch(repo, branch_name) + render_new_service_templates( service_id=service.id_str, service_name_upper=service.name_upper, @@ -26,13 +34,18 @@ def add( ioc_port=9999, # TODO user="user", # TODO ) - git_push_service_directory( - repo_root=REPO_ROOT, + registry.write_to_config() + + git_push_changes( + repo=repo, branch_name=f"feature/add-service-{service.name_lower}", service_dir_rel_path=f"services/{service.dir_name}", + registry_file_rel_path="config/services_registry.yml", commit_msg=f"feature: add new service {service.name_lower}", ) + switch_branch(repo, original_branch) + @service.command() def list():