-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add more urls to github + don't use raw api #31
Merged
Merged
Changes from all commits
Commits
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 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 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 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,214 @@ | ||
import pytest | ||
from httpx import Response | ||
|
||
from prefect_cloud.github import FileNotFound, GitHubFileRef, get_github_raw_content | ||
|
||
|
||
class TestGitHubFileRef: | ||
def test_from_url_blob(self): | ||
url = "https://github.com/PrefectHQ/prefect/blob/main/src/prefect/cli/root.py" | ||
ref = GitHubFileRef.from_url(url) | ||
|
||
assert ref.owner == "PrefectHQ" | ||
assert ref.repo == "prefect" | ||
assert ref.ref == "main" | ||
assert ref.filepath == "src/prefect/cli/root.py" | ||
assert ref.ref_type == "blob" | ||
|
||
def test_from_url_tree(self): | ||
url = "https://github.com/PrefectHQ/prefect/tree/main/src/prefect/cli" | ||
ref = GitHubFileRef.from_url(url) | ||
|
||
assert ref.owner == "PrefectHQ" | ||
assert ref.repo == "prefect" | ||
assert ref.ref == "main" | ||
assert ref.filepath == "src/prefect/cli" | ||
assert ref.ref_type == "tree" | ||
|
||
def test_from_url_invalid_github(self): | ||
with pytest.raises(ValueError, match="Not a GitHub URL"): | ||
GitHubFileRef.from_url("https://gitlab.com/owner/repo/blob/main/file.py") | ||
|
||
def test_from_url_invalid_format(self): | ||
with pytest.raises(ValueError, match="Invalid GitHub URL"): | ||
GitHubFileRef.from_url("https://github.com/owner/repo") | ||
|
||
def test_from_url_invalid_ref_type(self): | ||
with pytest.raises(ValueError, match="Invalid reference type"): | ||
GitHubFileRef.from_url("https://github.com/owner/repo/invalid/main/file.py") | ||
|
||
def test_clone_url(self): | ||
ref = GitHubFileRef( | ||
owner="PrefectHQ", | ||
repo="prefect", | ||
ref="main", | ||
filepath="README.md", | ||
ref_type="blob", | ||
) | ||
assert ref.clone_url == "https://github.com/PrefectHQ/prefect.git" | ||
|
||
def test_directory(self): | ||
ref = GitHubFileRef( | ||
owner="PrefectHQ", | ||
repo="prefect", | ||
ref="main", | ||
filepath="src/prefect/cli/root.py", | ||
ref_type="blob", | ||
) | ||
assert ref.directory == "src/prefect/cli" | ||
|
||
def test_api_url(self): | ||
ref = GitHubFileRef( | ||
owner="PrefectHQ", | ||
repo="prefect", | ||
ref="main", | ||
filepath="README.md", | ||
ref_type="blob", | ||
) | ||
assert ( | ||
ref.api_url | ||
== "https://api.github.com/repos/PrefectHQ/prefect/contents/README.md?ref=main" | ||
) | ||
|
||
def test_str_representation(self): | ||
ref = GitHubFileRef( | ||
owner="PrefectHQ", | ||
repo="prefect", | ||
ref="main", | ||
filepath="README.md", | ||
ref_type="blob", | ||
) | ||
assert str(ref) == "github.com/PrefectHQ/prefect @ main - README.md" | ||
|
||
def test_from_url_without_protocol(self): | ||
url = "github.com/PrefectHQ/prefect/blob/main/README.md" | ||
ref = GitHubFileRef.from_url(url) | ||
|
||
assert ref.owner == "PrefectHQ" | ||
assert ref.repo == "prefect" | ||
assert ref.ref == "main" | ||
assert ref.filepath == "README.md" | ||
assert ref.ref_type == "blob" | ||
|
||
def test_from_url_with_http(self): | ||
url = "http://github.com/PrefectHQ/prefect/blob/main/README.md" | ||
ref = GitHubFileRef.from_url(url) | ||
|
||
assert ref.owner == "PrefectHQ" | ||
assert ref.repo == "prefect" | ||
assert ref.ref == "main" | ||
assert ref.filepath == "README.md" | ||
assert ref.ref_type == "blob" | ||
|
||
def test_from_url_requires_github_domain(self): | ||
with pytest.raises(ValueError, match="Must include 'github.com' in the URL"): | ||
GitHubFileRef.from_url("PrefectHQ/prefect/blob/main/README.md") | ||
|
||
def test_from_url_with_multiple_path_segments(self): | ||
url = "github.com/PrefectHQ/prefect/blob/main/src/prefect/cli/root.py" | ||
ref = GitHubFileRef.from_url(url) | ||
|
||
assert ref.owner == "PrefectHQ" | ||
assert ref.repo == "prefect" | ||
assert ref.ref == "main" | ||
assert ref.filepath == "src/prefect/cli/root.py" | ||
assert ref.ref_type == "blob" | ||
|
||
def test_from_url_with_commit_sha(self): | ||
url = "github.com/PrefectHQ/prefect/blob/a1b2c3d4e5f6/src/prefect/cli/root.py" | ||
ref = GitHubFileRef.from_url(url) | ||
|
||
assert ref.owner == "PrefectHQ" | ||
assert ref.repo == "prefect" | ||
assert ref.ref == "a1b2c3d4e5f6" | ||
assert ref.filepath == "src/prefect/cli/root.py" | ||
assert ref.ref_type == "blob" | ||
|
||
def test_from_url_with_short_commit_sha(self): | ||
url = "github.com/PrefectHQ/prefect/blob/a1b2c3d/README.md" | ||
ref = GitHubFileRef.from_url(url) | ||
|
||
assert ref.owner == "PrefectHQ" | ||
assert ref.repo == "prefect" | ||
assert ref.ref == "a1b2c3d" | ||
assert ref.filepath == "README.md" | ||
assert ref.ref_type == "blob" | ||
|
||
def test_from_url_without_ref(self): | ||
url = "github.com/PrefectHQ/prefect/blob/README.md" | ||
with pytest.raises( | ||
ValueError, | ||
match="Invalid GitHub URL. Expected format: https://github.com/owner/repo/blob|tree/ref/path/to/file.py", | ||
): | ||
GitHubFileRef.from_url(url) | ||
|
||
def test_from_url_with_tree_without_ref(self): | ||
url = "github.com/PrefectHQ/prefect/tree/main/src/prefect" | ||
ref = GitHubFileRef.from_url(url) | ||
|
||
assert ref.owner == "PrefectHQ" | ||
assert ref.repo == "prefect" | ||
assert ref.ref == "main" | ||
assert ref.filepath == "src/prefect" | ||
assert ref.ref_type == "tree" | ||
|
||
|
||
class TestGitHubContent: | ||
@pytest.mark.asyncio | ||
async def test_get_github_raw_content(self, respx_mock): | ||
github_ref = GitHubFileRef( | ||
owner="PrefectHQ", | ||
repo="prefect", | ||
ref="main", | ||
filepath="README.md", | ||
ref_type="blob", | ||
) | ||
|
||
expected_content = "# Test Content" | ||
respx_mock.get(github_ref.api_url).mock( | ||
return_value=Response(status_code=200, text=expected_content) | ||
) | ||
|
||
content = await get_github_raw_content(github_ref) | ||
assert content == expected_content | ||
|
||
@pytest.mark.asyncio | ||
async def test_get_github_raw_content_with_credentials(self, respx_mock): | ||
github_ref = GitHubFileRef( | ||
owner="PrefectHQ", | ||
repo="prefect", | ||
ref="main", | ||
filepath="README.md", | ||
ref_type="blob", | ||
) | ||
|
||
test_token = "test-token" | ||
expected_content = "# Test Content" | ||
|
||
mock = respx_mock.get(github_ref.api_url).mock( | ||
return_value=Response(status_code=200, text=expected_content) | ||
) | ||
|
||
content = await get_github_raw_content(github_ref, credentials=test_token) | ||
assert content == expected_content | ||
|
||
# Verify authorization header was sent | ||
assert mock.calls[0].request.headers["Authorization"] == f"Bearer {test_token}" | ||
assert ( | ||
mock.calls[0].request.headers["Accept"] == "application/vnd.github.v3.raw" | ||
) | ||
|
||
@pytest.mark.asyncio | ||
async def test_get_github_raw_content_file_not_found(self, respx_mock): | ||
github_ref = GitHubFileRef( | ||
owner="PrefectHQ", | ||
repo="prefect", | ||
ref="main", | ||
filepath="NONEXISTENT.md", | ||
ref_type="blob", | ||
) | ||
|
||
respx_mock.get(github_ref.api_url).mock(return_value=Response(status_code=404)) | ||
|
||
with pytest.raises(FileNotFound): | ||
await get_github_raw_content(github_ref) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd love to surface this up to the
--help
of thedeploy
command