diff --git a/.bluish/bluish.yaml b/.bluish/bluish.yaml index f32d81c..d46781c 100644 --- a/.bluish/bluish.yaml +++ b/.bluish/bluish.yaml @@ -1,6 +1,6 @@ var: - project_version: "0.5.5" + project_version: "0.5.6" python_version: "3.12" jobs: diff --git a/src/bluish/commands/git.py b/src/bluish/commands/git.py index 2393b0d..67e061b 100644 --- a/src/bluish/commands/git.py +++ b/src/bluish/commands/git.py @@ -4,6 +4,7 @@ from bluish.context import StepContext from bluish.logging import error, info from bluish.process import ProcessResult, install_package +from bluish.utils import safe_string def run_git_command(command: str, step: StepContext) -> ProcessResult: @@ -17,11 +18,17 @@ def run_git_command(command: str, step: StepContext) -> ProcessResult: def prepare_environment(step: StepContext) -> ProcessResult: - if step.job.exec("which git", step).failed: - info("Installing git...") - result = install_package(step.job.runs_on_host, ["git"]) + REQUIRED = { + "git": "git", + "openssh-client": "ssh", + } + + required_packages = [package for package, binary in REQUIRED.items() if step.job.exec(f"which {binary}", step).failed] + if required_packages: + info(f"Installing missing packages: {required_packages}...") + result = install_package(step.job.runs_on_host, required_packages) if result.failed: - error(f"Failed to install git. Error: {result.error}") + error(f"Failed to install required packages. Error: {result.error}") return result return ProcessResult() @@ -38,12 +45,15 @@ def cleanup_environment(step: StepContext) -> None: ) def git_checkout(step: StepContext) -> ProcessResult: try: + inputs = step.inputs + + repository: str = step.expand_expr(inputs["repository"]) + repo_name = os.path.basename(repository) + result = prepare_environment(step) if result.failed: return result - inputs = step.inputs - options = "" if "depth" in inputs: options += f"--depth {inputs['depth']}" @@ -53,23 +63,8 @@ def git_checkout(step: StepContext) -> ProcessResult: if "branch" in inputs: options += f" --branch {inputs['branch']}" - repository: str = inputs["repository"] - protocol, repository = repository.split("://", maxsplit=1) - repo_name = os.path.basename(repository) - - if step.attrs.token: - command = f"git clone {protocol}://{step.attrs.token}@{repository} {options} ./{repo_name}" - elif step.attrs.username and step.attrs.password: - command = f"git clone {protocol}://{step.attrs.username}:{step.attrs.password}@{repository} {options} ./{repo_name}" - elif step.attrs.ssh_key_file: - key_file = step.inputs["ssh_key_file"] - command = f"ssh-agent sh -c 'ssh-add {key_file}; git clone git@{repository} ./{repo_name}'" - else: - repository = f"{protocol}://{repository}" - command = f"git clone {repository} {options} ./{repo_name}" - - info(f"Cloning repository: {repository}...") - clone_result = run_git_command(command, step) + info(f"Cloning repository: {safe_string(repository)}...") + clone_result = run_git_command(f"git clone {repository} {options} ./{repo_name}", step) if clone_result.failed: error(f"Failed to clone repository: {clone_result.error}") return clone_result diff --git a/test/test_git.py b/test/test_git.py index 4bef456..e26729f 100644 --- a/test/test_git.py +++ b/test/test_git.py @@ -10,25 +10,7 @@ def initialize_commands(): init_commands() -@pytest.mark.docker -def test_docker_checkout() -> None: - wf = create_workflow(""" -jobs: - checkout: - runs_on: docker://ubuntu:latest - steps: - - uses: git/checkout - with: - repository: https://github.com/luismedel/bluish.git - - run: | - head -n1 README.md -""") - _ = wf.dispatch() - assert wf.jobs["checkout"].result.stdout == "# Bluish" - - -@pytest.mark.docker -def test_docker_checkout_alpine() -> None: +def test_docker_public_checkout() -> None: wf = create_workflow(""" jobs: checkout: @@ -41,4 +23,5 @@ def test_docker_checkout_alpine() -> None: head -n1 README.md """) _ = wf.dispatch() - assert wf.jobs["checkout"].result.stdout == "# Bluish" + assert wf.jobs["checkout"].result.stdout == "[![justforfunnoreally.dev badge](https://img.shields.io/badge/justforfunnoreally-dev-9ff)](https://justforfunnoreally.dev)" +