-
Notifications
You must be signed in to change notification settings - Fork 47
fix(cdk): require shared staging for file transfer #1029
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
devin-ai-integration
wants to merge
3
commits into
main
Choose a base branch
from
devin/1779128158-fix-file-transfer-staging-dir
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,49 @@ | ||
| # | ||
| # Copyright (c) 2025 Airbyte, Inc., all rights reserved. | ||
| # | ||
| import os | ||
| from os import getenv | ||
| from pathlib import Path | ||
|
|
||
| AIRBYTE_STAGING_DIRECTORY = os.getenv("AIRBYTE_STAGING_DIRECTORY", "/staging/files") | ||
| from airbyte_cdk.models import FailureType | ||
| from airbyte_cdk.utils.traced_exception import AirbyteTracedException | ||
|
|
||
| AIRBYTE_STAGING_DIRECTORY_ENV_VAR = "AIRBYTE_STAGING_DIRECTORY" | ||
| DEFAULT_AIRBYTE_STAGING_DIRECTORY = "/staging/files" | ||
| DEFAULT_LOCAL_DIRECTORY = "/tmp/airbyte-file-transfer" | ||
| UNAVAILABLE_STAGING_DIRECTORY_MESSAGE = "File transfer staging directory is unavailable." | ||
|
|
||
|
|
||
| def _validate_staging_directory(staging_directory: str) -> None: | ||
| staging_directory_path = Path(staging_directory) | ||
| if not staging_directory_path.exists(): | ||
| raise AirbyteTracedException( | ||
| message=UNAVAILABLE_STAGING_DIRECTORY_MESSAGE, | ||
| internal_message=( | ||
| f"Configured {AIRBYTE_STAGING_DIRECTORY_ENV_VAR} does not exist: " | ||
| f"{staging_directory}" | ||
| ), | ||
| failure_type=FailureType.system_error, | ||
| ) | ||
|
|
||
| if not staging_directory_path.is_dir(): | ||
| raise AirbyteTracedException( | ||
| message=UNAVAILABLE_STAGING_DIRECTORY_MESSAGE, | ||
| internal_message=( | ||
| f"Configured {AIRBYTE_STAGING_DIRECTORY_ENV_VAR} is not a directory: " | ||
| f"{staging_directory}" | ||
| ), | ||
| failure_type=FailureType.system_error, | ||
| ) | ||
|
|
||
|
|
||
| def get_files_directory(is_file_transfer: bool = False) -> str: | ||
| configured_staging_directory = getenv(AIRBYTE_STAGING_DIRECTORY_ENV_VAR) | ||
| if configured_staging_directory: | ||
| _validate_staging_directory(configured_staging_directory) | ||
| return configured_staging_directory | ||
|
|
||
| if not is_file_transfer: | ||
| return DEFAULT_LOCAL_DIRECTORY | ||
|
|
||
| def get_files_directory() -> str: | ||
| return ( | ||
| AIRBYTE_STAGING_DIRECTORY | ||
| if os.path.exists(AIRBYTE_STAGING_DIRECTORY) | ||
| else DEFAULT_LOCAL_DIRECTORY | ||
| ) | ||
| _validate_staging_directory(DEFAULT_AIRBYTE_STAGING_DIRECTORY) | ||
| return DEFAULT_AIRBYTE_STAGING_DIRECTORY |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
unit_tests/sources/file_based/file_types/test_file_transfer.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # | ||
| # Copyright (c) 2026 Airbyte, Inc., all rights reserved. | ||
| # | ||
|
|
||
| import pytest | ||
|
|
||
| from airbyte_cdk.models import FailureType | ||
| from airbyte_cdk.sources.file_based.file_types.file_transfer import FileTransfer | ||
| from airbyte_cdk.utils.traced_exception import AirbyteTracedException | ||
|
|
||
|
|
||
| def test_file_transfer_raises_when_shared_staging_directory_is_missing(monkeypatch) -> None: | ||
| monkeypatch.delenv("AIRBYTE_STAGING_DIRECTORY", raising=False) | ||
|
|
||
| with pytest.raises(AirbyteTracedException) as exc_info: | ||
| FileTransfer() | ||
|
|
||
| assert exc_info.value.failure_type == FailureType.system_error | ||
| assert exc_info.value.message == "File transfer staging directory is unavailable." | ||
| assert exc_info.value.internal_message == ( | ||
| "Configured AIRBYTE_STAGING_DIRECTORY does not exist: /staging/files" | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| # | ||
| # Copyright (c) 2026 Airbyte, Inc., all rights reserved. | ||
| # | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| from airbyte_cdk.models import FailureType | ||
| from airbyte_cdk.sources.utils.files_directory import get_files_directory | ||
| from airbyte_cdk.utils.traced_exception import AirbyteTracedException | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "is_file_transfer", | ||
| [ | ||
| pytest.param(False, id="local_usage"), | ||
| pytest.param(True, id="file_transfer"), | ||
| ], | ||
| ) | ||
| def test_get_files_directory_uses_configured_staging_directory( | ||
| monkeypatch, tmp_path, is_file_transfer | ||
| ) -> None: | ||
| monkeypatch.setenv("AIRBYTE_STAGING_DIRECTORY", str(tmp_path)) | ||
|
|
||
| assert get_files_directory(is_file_transfer=is_file_transfer) == str(tmp_path) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "is_file_transfer", | ||
| [ | ||
| pytest.param(False, id="local_usage"), | ||
| pytest.param(True, id="file_transfer"), | ||
| ], | ||
| ) | ||
| def test_get_files_directory_raises_when_explicit_staging_directory_is_missing( | ||
| monkeypatch, tmp_path, is_file_transfer | ||
| ) -> None: | ||
| missing_staging_directory = tmp_path / "missing" | ||
| monkeypatch.setenv("AIRBYTE_STAGING_DIRECTORY", str(missing_staging_directory)) | ||
|
|
||
| with pytest.raises(AirbyteTracedException) as exc_info: | ||
| get_files_directory(is_file_transfer=is_file_transfer) | ||
|
|
||
| assert exc_info.value.failure_type == FailureType.system_error | ||
| assert exc_info.value.message == "File transfer staging directory is unavailable." | ||
| assert exc_info.value.internal_message == ( | ||
| f"Configured AIRBYTE_STAGING_DIRECTORY does not exist: {missing_staging_directory}" | ||
| ) | ||
|
|
||
|
|
||
| def test_get_files_directory_falls_back_for_local_usage_without_configured_staging_directory( | ||
| monkeypatch, | ||
| ) -> None: | ||
| monkeypatch.delenv("AIRBYTE_STAGING_DIRECTORY", raising=False) | ||
|
|
||
| assert get_files_directory() == "/tmp/airbyte-file-transfer" | ||
|
|
||
|
|
||
| def test_get_files_directory_raises_for_file_transfer_without_shared_staging_directory( | ||
| monkeypatch, | ||
| ) -> None: | ||
| monkeypatch.delenv("AIRBYTE_STAGING_DIRECTORY", raising=False) | ||
|
|
||
| with pytest.raises(AirbyteTracedException) as exc_info: | ||
| get_files_directory(is_file_transfer=True) | ||
|
|
||
| assert exc_info.value.failure_type == FailureType.system_error | ||
| assert exc_info.value.message == "File transfer staging directory is unavailable." | ||
| assert exc_info.value.internal_message == ( | ||
| "Configured AIRBYTE_STAGING_DIRECTORY does not exist: /staging/files" | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "is_file_transfer", | ||
| [ | ||
| pytest.param(False, id="local_usage"), | ||
| pytest.param(True, id="file_transfer"), | ||
| ], | ||
| ) | ||
| def test_get_files_directory_raises_when_explicit_staging_directory_is_a_file( | ||
| monkeypatch, tmp_path, is_file_transfer | ||
| ) -> None: | ||
| staging_directory = tmp_path / "stage" | ||
| staging_directory.write_text("not a directory") | ||
| monkeypatch.setenv("AIRBYTE_STAGING_DIRECTORY", str(staging_directory)) | ||
|
|
||
| with pytest.raises(AirbyteTracedException) as exc_info: | ||
| get_files_directory(is_file_transfer=is_file_transfer) | ||
|
|
||
| assert exc_info.value.failure_type == FailureType.system_error | ||
| assert exc_info.value.message == "File transfer staging directory is unavailable." | ||
| assert exc_info.value.internal_message == ( | ||
| f"Configured AIRBYTE_STAGING_DIRECTORY is not a directory: {staging_directory}" | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.