Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions codeflash/cli_cmds/cmd_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,16 @@ def init_codeflash() -> None:

did_add_new_key = prompt_api_key()

if should_modify_pyproject_toml():
setup_info: SetupInfo = collect_setup_info()
should_modify, config = should_modify_pyproject_toml()

git_remote = config.get("git_remote", "origin") if config else "origin"

if should_modify:
setup_info: SetupInfo = collect_setup_info()
git_remote = setup_info.git_remote
configure_pyproject_toml(setup_info)

install_github_app()
install_github_app(git_remote)

install_github_actions(override_formatter_check=True)

Expand Down Expand Up @@ -151,7 +155,7 @@ def ask_run_end_to_end_test(args: Namespace) -> None:
run_end_to_end_test(args, bubble_sort_path, bubble_sort_test_path)


def should_modify_pyproject_toml() -> bool:
def should_modify_pyproject_toml() -> tuple[bool, dict[str, Any] | None]:
"""Check if the current directory contains a valid pyproject.toml file with codeflash config.

If it does, ask the user if they want to re-configure it.
Expand All @@ -160,22 +164,22 @@ def should_modify_pyproject_toml() -> bool:

pyproject_toml_path = Path.cwd() / "pyproject.toml"
if not pyproject_toml_path.exists():
return True
return True, None
try:
config, config_file_path = parse_config_file(pyproject_toml_path)
except Exception:
return True
return True, None

if "module_root" not in config or config["module_root"] is None or not Path(config["module_root"]).is_dir():
return True
return True, None
if "tests_root" not in config or config["tests_root"] is None or not Path(config["tests_root"]).is_dir():
return True
return True, None

return Confirm.ask(
"✅ A valid Codeflash config already exists in this project. Do you want to re-configure it?",
default=False,
show_default=True,
)
), config


# Custom theme for better UX
Expand Down Expand Up @@ -958,16 +962,18 @@ def configure_pyproject_toml(setup_info: SetupInfo) -> None:
click.echo()


def install_github_app() -> None:
def install_github_app(git_remote: str) -> None:
try:
git_repo = git.Repo(search_parent_directories=True)
except git.InvalidGitRepositoryError:
click.echo("Skipping GitHub app installation because you're not in a git repository.")
return
owner, repo = get_repo_owner_and_name(git_repo)
owner, repo = get_repo_owner_and_name(git_repo, git_remote)

if is_github_app_installed_on_repo(owner, repo, suppress_errors=True):
click.echo("🐙 Looks like you've already installed the Codeflash GitHub app on this repository! Continuing…")
click.echo(
f"🐙 Looks like you've already installed the Codeflash GitHub app on this repository ({owner}/{repo})! Continuing…"
)

else:
click.prompt(
Expand Down
Loading