From 0a65e413cc4477dead06e48b9c8c3a79c4b18b82 Mon Sep 17 00:00:00 2001 From: Ben Church Date: Wed, 10 Jan 2024 16:53:11 -0800 Subject: [PATCH] Ensure we set the working directory earlier --- .../pipelines/pipelines/cli/airbyte_ci.py | 62 ++----------------- .../pipelines/cli/ensure_repo_root.py | 57 +++++++++++++++++ 2 files changed, 63 insertions(+), 56 deletions(-) create mode 100644 airbyte-ci/connectors/pipelines/pipelines/cli/ensure_repo_root.py diff --git a/airbyte-ci/connectors/pipelines/pipelines/cli/airbyte_ci.py b/airbyte-ci/connectors/pipelines/pipelines/cli/airbyte_ci.py index 028bb54df7c74..cb5226728929a 100644 --- a/airbyte-ci/connectors/pipelines/pipelines/cli/airbyte_ci.py +++ b/airbyte-ci/connectors/pipelines/pipelines/cli/airbyte_ci.py @@ -6,16 +6,20 @@ from __future__ import annotations +# Important: This import and function call must be the first import in this file +# This is needed to ensure that the working directory is the root of the airbyte repo +from pipelines.cli.ensure_repo_root import set_working_directory_to_root + +set_working_directory_to_root() + import logging import multiprocessing import os import sys -from pathlib import Path from typing import Optional import asyncclick as click import docker # type: ignore -import git from github import PullRequest from pipelines import main_logger from pipelines.cli.auto_update import __installed_version__, check_for_upgrade, pre_confirm_auto_update_flag @@ -30,58 +34,6 @@ from pipelines.helpers.utils import get_current_epoch_time -def _validate_airbyte_repo(repo: git.Repo) -> bool: - """Check if any of the remotes are the airbyte repo.""" - expected_repo_name = "airbytehq/airbyte" - for remote in repo.remotes: - if expected_repo_name in remote.url: - return True - - warning_message = f""" - ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️ - - It looks like you are not running this command from the airbyte repo ({expected_repo_name}). - - If this command is run from outside the airbyte repo, it will not work properly. - - Please run this command your local airbyte project. - - ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️ - """ - - logging.warning(warning_message) - - return False - - -def get_airbyte_repo() -> git.Repo: - """Get the airbyte repo.""" - repo = git.Repo(search_parent_directories=True) - _validate_airbyte_repo(repo) - return repo - - -def get_airbyte_repo_path_with_fallback() -> Path: - """Get the path to the airbyte repo.""" - try: - repo_path = get_airbyte_repo().working_tree_dir - if repo_path is not None: - return Path(str(get_airbyte_repo().working_tree_dir)) - except git.exc.InvalidGitRepositoryError: - pass - logging.warning("Could not find the airbyte repo, falling back to the current working directory.") - path = Path.cwd() - logging.warning(f"Using {path} as the airbyte repo path.") - return path - - -def set_working_directory_to_root() -> None: - """Set the working directory to the root of the airbyte repo.""" - working_dir = get_airbyte_repo_path_with_fallback() - logging.info(f"Setting working directory to {working_dir}") - os.chdir(working_dir) - - def log_context_info(ctx: click.Context) -> None: main_logger.info(f"Running airbyte-ci version {__installed_version__}") main_logger.info(f"Running dagger version {get_dagger_sdk_version()}") @@ -241,7 +193,5 @@ async def airbyte_ci(ctx: click.Context) -> None: # noqa D103 log_context_info(ctx) -set_working_directory_to_root() - if __name__ == "__main__": airbyte_ci() diff --git a/airbyte-ci/connectors/pipelines/pipelines/cli/ensure_repo_root.py b/airbyte-ci/connectors/pipelines/pipelines/cli/ensure_repo_root.py new file mode 100644 index 0000000000000..7b0c4b39576a1 --- /dev/null +++ b/airbyte-ci/connectors/pipelines/pipelines/cli/ensure_repo_root.py @@ -0,0 +1,57 @@ +import logging +import os +from pathlib import Path + +import git + + +def _validate_airbyte_repo(repo: git.Repo) -> bool: + """Check if any of the remotes are the airbyte repo.""" + expected_repo_name = "airbytehq/airbyte" + for remote in repo.remotes: + if expected_repo_name in remote.url: + return True + + warning_message = f""" + ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️ + + It looks like you are not running this command from the airbyte repo ({expected_repo_name}). + + If this command is run from outside the airbyte repo, it will not work properly. + + Please run this command your local airbyte project. + + ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️ + """ + + logging.warning(warning_message) + + return False + + +def get_airbyte_repo() -> git.Repo: + """Get the airbyte repo.""" + repo = git.Repo(search_parent_directories=True) + _validate_airbyte_repo(repo) + return repo + + +def get_airbyte_repo_path_with_fallback() -> Path: + """Get the path to the airbyte repo.""" + try: + repo_path = get_airbyte_repo().working_tree_dir + if repo_path is not None: + return Path(str(get_airbyte_repo().working_tree_dir)) + except git.exc.InvalidGitRepositoryError: + pass + logging.warning("Could not find the airbyte repo, falling back to the current working directory.") + path = Path.cwd() + logging.warning(f"Using {path} as the airbyte repo path.") + return path + + +def set_working_directory_to_root() -> None: + """Set the working directory to the root of the airbyte repo.""" + working_dir = get_airbyte_repo_path_with_fallback() + logging.info(f"Setting working directory to {working_dir}") + os.chdir(working_dir)