Skip to content
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

Handle cases where no rate limiting is used #62

Merged
merged 5 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions ghsearch/gh_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import click
import github
from github.ContentFile import ContentFile
from github.GithubException import GithubException
from github.Rate import Rate
from github.RateLimit import RateLimit

Expand Down Expand Up @@ -52,13 +53,25 @@ def __init__(self, client: github.Github, filters: List[Filter], verbose: bool =
self.filters = filters
self.verbose = verbose

def get_rate_limit(self) -> RateLimit | None:
try:
return self.client.get_rate_limit()
except GithubException as ge:
# 404 means that rate limiting is disabled
if ge.status == 404:
return None
raise ge

def get_filtered_results(self, query: List[str]) -> List[ContentFile]:
rate_limit = self.client.get_rate_limit()
if self.verbose:
rate_limit = self.get_rate_limit()

if rate_limit and self.verbose:
_echo_rate_limits(rate_limit)

results = self.client.search_code(query=" ".join(query))
self._check_core_limit_threshold(results.totalCount, rate_limit.core)

if rate_limit:
self._check_core_limit_threshold(results.totalCount, rate_limit.core)

filtered_results = []
with ProgressPrinter(overwrite=not self.verbose) as printer:
Expand All @@ -74,8 +87,9 @@ def get_filtered_results(self, query: List[str]) -> List[ContentFile]:
elif self.verbose:
click.echo(f"Skipping result for {result.repository.full_name} via {exclude_reason}")

if self.verbose:
_echo_rate_limits(self.client.get_rate_limit())
rate_limit = self.get_rate_limit()
if rate_limit and self.verbose:
_echo_rate_limits(rate_limit)

return filtered_results

Expand Down
12 changes: 12 additions & 0 deletions tests/unit/gh_search_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,15 @@ def test_get_filtered_results_many_calls(mock_client, mock_click):
Do you want to continue?""".strip(),
abort=True,
)


def test_get_filtered_results_rate_limiting_disabled(mock_client):
mock_client.get_rate_limit.side_effect = github.GithubException(404, "Not Found")
mock_filter = Mock()
mock_filter.uses_core_api = True

ghsearch = GHSearch(mock_client, [])
ghsearch.get_filtered_results(["query", "org:bort"])

# ensure get_rate_limit was called (and the side_effect above handled)
mock_client.get_rate_limit.assert_called()