Skip to content

🧹 Refactor: Extract response validation from _gh_get#802

Open
abhimehro wants to merge 2 commits into
mainfrom
jules-746428143095159515-df5935f5
Open

🧹 Refactor: Extract response validation from _gh_get#802
abhimehro wants to merge 2 commits into
mainfrom
jules-746428143095159515-df5935f5

Conversation

@abhimehro
Copy link
Copy Markdown
Owner

@abhimehro abhimehro commented May 14, 2026

🎯 What: Extracted the repetitive _parse_and_validate_response helper from _gh_get to handle content validation, size checking, and JSON parsing.
💡 Why: The _gh_get function was too long and complex due to nested try-catches and duplicate validation blocks for the 304 fallback branch. This extraction drastically reduces complexity and enhances readability and maintainability.
Verification: Verified by running the formatters/linters (ruff format / ruff check) and the full test suite (uv run pytest), ensuring 100% of the tests passed including benchmark thresholds.
Result: _gh_get is much shorter, more focused, and easier to follow without altering any core caching or HTTP behavior.


Open in Devin Review

Copilot AI review requested due to automatic review settings May 14, 2026 13:37
@trunk-io
Copy link
Copy Markdown

trunk-io Bot commented May 14, 2026

Merging to main in this repository is managed by Trunk.

  • To merge this pull request, check the box to the left or comment /trunk merge below.

After your PR is submitted to the merge queue, this comment will be automatically updated with its status. If the PR fails, failure details will also be posted here

codescene-delta-analysis[bot]

This comment was marked as outdated.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Copy Markdown

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 3 potential issues.

Open in Devin Review

Comment thread main.py
allowed_types = ["application/json", "text/json", "text/plain"]
if not any(t in content_type for t in allowed_types):
raise ValueError(
f"Invalid Content-Type from {sanitize_for_log(url)}: {content_type}. "
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Info: Refactoring fixes an unsanitized URL in the 304 fallback error message

The old 304 retry fallback code at the now-deleted line used {url} directly in the Content-Type error message (f"Invalid Content-Type from {url}: {content_type}.") while the main path used sanitize_for_log(url). The new shared _parse_and_validate_response function consistently uses sanitize_for_log(url) (line 1454), which is a correctness improvement — the old code was inconsistent.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Comment thread main.py
chunks = []
current_size = 0
# Optimization: Use 16KB chunks to reduce loop overhead/appends for large files
for chunk in r.iter_bytes(chunk_size=16 * 1024):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Info: Chunk size now consistent across both code paths

The old 304 retry fallback path called r_retry.iter_bytes() without a chunk_size argument (defaulting to httpx's internal default), while the main 200 OK path used chunk_size=16 * 1024. The new shared function uses chunk_size=16 * 1024 for both paths (main.py:1480), making behavior consistent. This is a minor improvement in the retry path.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Comment thread main.py
cl = r.headers.get("Content-Length")
if cl:
try:
if int(cl) > MAX_RESPONSE_SIZE:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚩 MAX_RESPONSE_SIZE is defined twice in main.py

Pre-existing issue: MAX_RESPONSE_SIZE is defined at both main.py:892 (10MB) and main.py:1068 (10MB). Both values are identical so there's no runtime impact, but the duplication is unnecessary and could lead to divergence if one is updated without the other. Not introduced by this PR but worth noting since the extracted function depends on this constant.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Co-authored-by: abhimehro <84992105+abhimehro@users.noreply.github.com>
Copy link
Copy Markdown

@codescene-delta-analysis codescene-delta-analysis Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Health Improved (1 files improve in Code Health)

Gates Passed
6 Quality Gates Passed

See analysis details in CodeScene

View Improvements
File Code Health Impact Categories Improved
main.py 1.66 → 1.70 Lines of Code in a Single File, Complex Method, Bumpy Road Ahead, Overall Code Complexity

Quality Gate Profile: Pay Down Tech Debt
Install CodeScene MCP: safeguard and uplift AI-generated code. Catch issues early with our IDE extension and CLI tool.

Copy link
Copy Markdown

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 new potential issue.

Open in Devin Review

Comment thread main.py

# _api_stats_lock, _api_get, _api_delete, _api_post, _api_post_form,
# retry_with_jitter, _retry_request imported from api_client above
def _parse_and_validate_response(r: httpx.Response, url: str) -> dict:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Info: Function placement after unrelated import comment is slightly misleading

The new _parse_and_validate_response function is defined immediately after the comment # _api_stats_lock, _api_get, _api_delete, _api_post, _api_post_form, # retry_with_jitter, _retry_request imported from api_client above at main.py:1444-1445. This comment describes imports and is unrelated to the new function, making it read as if the function is part of that import block. A blank line or moving the comment would improve readability.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants