Skip to content

Commit

Permalink
Ensure we set the working directory earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
bnchrch committed Jan 11, 2024
1 parent dd5d23b commit 0a65e41
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 56 deletions.
62 changes: 6 additions & 56 deletions airbyte-ci/connectors/pipelines/pipelines/cli/airbyte_ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()}")
Expand Down Expand Up @@ -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()
57 changes: 57 additions & 0 deletions airbyte-ci/connectors/pipelines/pipelines/cli/ensure_repo_root.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 0a65e41

Please sign in to comment.