Skip to content

Commit 5edb28c

Browse files
authored
Merge branch 'master' into feature/streaming-downloads
2 parents 37a39ff + 8f9594a commit 5edb28c

1 file changed

Lines changed: 34 additions & 45 deletions

File tree

forklet/services/github_api.py

Lines changed: 34 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -144,42 +144,43 @@ async def resolve_reference(self, owner: str, repo: str, ref: str) -> GitReferen
144144
ValueError: If reference cannot be resolved
145145
"""
146146

147-
try:
148-
# Try to get as branch first
149-
await self.rate_limiter.acquire()
150-
branch = await asyncio.to_thread(
151-
lambda: self.github_client.get_repo(f"{owner}/{repo}").get_branch(ref)
152-
)
153-
return GitReference(name=ref, ref_type="branch", sha=branch.commit.sha)
154-
except GithubException:
155-
pass # Not a branch, try other types
156-
157-
try:
158-
# Try to get as tag
159-
await self.rate_limiter.acquire()
160-
tags = await asyncio.to_thread(
161-
lambda: list(self.github_client.get_repo(f"{owner}/{repo}").get_tags())
162-
)
163-
for tag in tags:
164-
if tag.name == ref:
165-
return GitReference(name=ref, ref_type="tag", sha=tag.commit.sha)
166-
except GithubException:
167-
pass # Not a tag
168-
169-
try:
170-
# Try to get as commit
171-
await self.rate_limiter.acquire()
172-
commit = await asyncio.to_thread(
173-
lambda: self.github_client.get_repo(f"{owner}/{repo}").get_commit(ref)
174-
)
175-
return GitReference(name=ref, ref_type="commit", sha=commit.sha)
176-
except GithubException:
177-
pass # Not a valid commit
147+
# Define reference types to try in order
148+
reference_types = [
149+
(
150+
"branch",
151+
lambda: self.github_client.get_repo(f"{owner}/{repo}").get_branch(ref),
152+
),
153+
("tag", lambda: self._get_tag(ref, owner, repo)),
154+
(
155+
"commit",
156+
lambda: self.github_client.get_repo(f"{owner}/{repo}").get_commit(ref),
157+
),
158+
]
159+
160+
for ref_type, getter_func in reference_types:
161+
try:
162+
await self.rate_limiter.acquire()
163+
github_obj = await asyncio.to_thread(getter_func)
164+
return GitReference(
165+
name=ref, ref_type=ref_type, sha=github_obj.commit.sha
166+
)
167+
except GithubException:
168+
continue # Try next reference type
178169

179170
raise ValueError(
180171
f"Could not resolve reference '{ref}' for repository {owner}/{repo}"
181172
)
182173

174+
async def _get_tag(self, tag_name: str, owner: str, repo: str):
175+
"""Helper to get a tag by name - needed because get_tags() returns a list."""
176+
tags = await asyncio.to_thread(
177+
lambda: list(self.github_client.get_repo(f"{owner}/{repo}").get_tags())
178+
)
179+
for tag in tags:
180+
if tag.name == tag_name:
181+
return tag
182+
raise GithubException(status=404, data={"message": f"Tag {tag_name} not found"})
183+
183184
@handle_api_error
184185
async def get_repository_tree(
185186
self, owner: str, repo: str, ref: GitReference, recursive: bool = True
@@ -246,10 +247,9 @@ async def get_file_content(
246247
247248
Args:
248249
download_url: GitHub API URL for the file content
249-
stream: If True, returns an async iterator for streaming large files
250250
251251
Returns:
252-
File content as bytes (already decoded from base64) or async iterator of bytes
252+
File content as bytes (already decoded from base64)
253253
254254
Raises:
255255
DownloadError: If download fails
@@ -268,18 +268,7 @@ async def get_file_content(
268268
if "content" in content_data:
269269
import base64
270270

271-
decoded_content = base64.b64decode(content_data["content"])
272-
273-
if stream:
274-
# Return an async iterator that yields chunks of the content
275-
async def content_stream():
276-
chunk_size = 8192 # 8KB chunks
277-
for i in range(0, len(decoded_content), chunk_size):
278-
yield decoded_content[i : i + chunk_size]
279-
280-
return content_stream()
281-
else:
282-
return decoded_content
271+
return base64.b64decode(content_data["content"])
283272
else:
284273
raise DownloadError(
285274
f"No content found in API response for {download_url}"

0 commit comments

Comments
 (0)