chore: more robust git on add new service
Deploy bin / deploy (push) Canceled after 0s
Deploy service MASTER / deploy (push) Canceled after 0s

This commit is contained in:
Benjamin Labrecque
2026-07-03 15:48:02 +02:00
parent 469e2a9eab
commit 4afde31c30
3 changed files with 38 additions and 18 deletions
+17 -12
View File
@@ -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."
)
+4 -2
View File
@@ -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)
+17 -4
View File
@@ -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():