Skip to content

Commit

Permalink
Don't mess with tokens/passwords. Let the user pick a proper git url
Browse files Browse the repository at this point in the history
  • Loading branch information
luismedel committed Sep 20, 2024
1 parent 241ddba commit 3b13494
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .bluish/bluish.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

var:
project_version: "0.5.5"
project_version: "0.5.6"
python_version: "3.12"

jobs:
Expand Down
41 changes: 18 additions & 23 deletions src/bluish/commands/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()
Expand All @@ -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']}"
Expand All @@ -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
Expand Down
23 changes: 3 additions & 20 deletions test/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)"

0 comments on commit 3b13494

Please sign in to comment.