-
Notifications
You must be signed in to change notification settings - Fork 1
BED-9434: harden repository GraphQL collection retries #37
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
Merged
jaredcatkinson
merged 2 commits into
main
from
fix/BED-9434-repositories-graphql-retries
Aug 20, 2026
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 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
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,102 @@ | ||
| import json | ||
|
|
||
| import requests | ||
| from dlt.sources.helpers.rest_client.auth import BearerTokenAuth | ||
| from dlt.sources.helpers.requests.retry import Client | ||
| from dlt.sources.helpers.requests.session import Session | ||
|
|
||
| from openhound_github.helpers import github_retry_policy | ||
|
|
||
|
|
||
| def graphql_response( | ||
| *, | ||
| headers: dict[str, str] | None = None, | ||
| body: dict[str, object] | None = None, | ||
| text: str | None = None, | ||
| url: str = "https://ghe.example/api/v3/graphql", | ||
| ) -> requests.Response: | ||
| response = requests.Response() | ||
| response.status_code = 200 | ||
| response.headers.update(headers or {}) | ||
| if text is not None: | ||
| response._content = text.encode("utf-8") | ||
| else: | ||
| response._content = json.dumps(body or {}).encode("utf-8") | ||
| response.request = requests.Request("POST", url).prepare() | ||
| return response | ||
|
|
||
|
|
||
| def test_retry_policy_recognizes_graphql_endpoint_without_resource_header() -> None: | ||
| response = graphql_response( | ||
| headers={"Retry-After": "0"}, | ||
| body={"errors": [{"message": "temporary GraphQL failure"}]}, | ||
| ) | ||
|
|
||
| should_retry = github_retry_policy(BearerTokenAuth(token="static-token"))( | ||
| response, | ||
| None, | ||
| ) | ||
|
|
||
| assert should_retry is True | ||
|
|
||
|
|
||
| def test_retry_policy_retries_malformed_graphql_json() -> None: | ||
| response = graphql_response(text='{"data":{"organization":') | ||
|
|
||
| should_retry = github_retry_policy(BearerTokenAuth(token="static-token"))( | ||
| response, | ||
| None, | ||
| ) | ||
|
|
||
| assert should_retry is True | ||
|
|
||
|
|
||
| def test_retry_policy_does_not_retry_malformed_non_graphql_json() -> None: | ||
| response = graphql_response( | ||
| text='{"data":{"organization":', | ||
| url="https://ghe.example/api/v3/repos/example/repo", | ||
| ) | ||
|
|
||
| should_retry = github_retry_policy(BearerTokenAuth(token="static-token"))( | ||
| response, | ||
| None, | ||
| ) | ||
|
|
||
| assert should_retry is False | ||
|
|
||
|
|
||
| def test_retry_client_recovers_from_malformed_graphql_json(monkeypatch) -> None: | ||
| responses = [ | ||
| graphql_response(text='{"data":{"organization":'), | ||
| graphql_response(body={"data": {"organization": {"repositories": {}}}}), | ||
| ] | ||
| requests_seen: list[requests.PreparedRequest] = [] | ||
|
|
||
| def fake_send( | ||
| _session: Session, | ||
| request: requests.PreparedRequest, | ||
| **_kwargs, | ||
| ) -> requests.Response: | ||
| response = responses[len(requests_seen)] | ||
| response.request = request | ||
| requests_seen.append(request) | ||
| return response | ||
|
|
||
| monkeypatch.setattr(Session, "send", fake_send) | ||
| session = Client( | ||
| raise_for_status=False, | ||
| status_codes=(), | ||
| exceptions=(), | ||
| request_max_attempts=2, | ||
| request_backoff_factor=0, | ||
| retry_condition=github_retry_policy(BearerTokenAuth(token="static-token")), | ||
| ).session | ||
| request = requests.Request( | ||
| "POST", | ||
| "https://ghe.example/api/v3/graphql", | ||
| ).prepare() | ||
|
|
||
| response = session.send(request) | ||
|
|
||
| assert response.json() == {"data": {"organization": {"repositories": {}}}} | ||
| assert len(requests_seen) == 2 |
Oops, something went wrong.
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.
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.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Use a test-managed path instead of
/tmp/github-app.pem.Ruff reports S108 at Line 138. Add the
tmp_pathfixture and build the private key path from it. The constructor does not read the key in this test.Proposed fix
📝 Committable suggestion
🧰 Tools
🪛 ast-grep (0.45.1)
[info] 137-137: Do not hardcode temporary file or directory names
Context: "/tmp/github-app.pem"
Note: [CWE-377] Insecure Temporary File.
(hardcoded-tmp-file)
🪛 Ruff (0.16.1)
[error] 138-138: Probable insecure usage of temporary file or directory: "/tmp/github-app.pem"
(S108)
🤖 Prompt for AI Agents
Source: Linters/SAST tools