import logging import subprocess import os import shlex from src.settings import GIT_PRIVATE_KEY_PATH _logger = logging.getLogger(__name__) _logger.addHandler(logging.NullHandler()) os.environ['GIT_SSH_COMMAND'] = f"ssh -i {shlex.quote(GIT_PRIVATE_KEY_PATH)} -o IdentitiesOnly=yes" def _check_preconditions(): assert subprocess.check_call(["git", "--version"]) == 0 def clone_repo(ssh_url: str, path: str): subprocess.check_call(["git", "clone", ssh_url, path], stdout=subprocess.DEVNULL) def create_and_open_branch(repo_path: str, branch_name: str): subprocess.check_call( ["git", "checkout", "-b", branch_name], cwd=repo_path, stdout=subprocess.DEVNULL ) def commit_and_push(repo_path: str, commit_message: str): subprocess.check_call(["git", "add", "."], cwd=repo_path, stdout=subprocess.DEVNULL) subprocess.check_call( ["git", "commit", "-m", commit_message], cwd=repo_path, stdout=subprocess.DEVNULL ) branches = subprocess.check_output( ["git", "branch"], cwd=repo_path ).decode("utf-8") current_branch = next(filter(lambda x: x.startswith("*"), branches.splitlines()))[1:].strip() _logger.info(f"pushing branch {current_branch} in repo {repo_path}") subprocess.check_call( ["git", "push", "--set-upstream", "origin", current_branch], cwd=repo_path, stdout=subprocess.DEVNULL, ) _check_preconditions()