Skip to content

Commit

Permalink
Merge pull request #62 from augustd/no-rate-limiting
Browse files Browse the repository at this point in the history
Handle cases where no rate limiting is used
  • Loading branch information
janeklb authored Aug 10, 2024
2 parents bdb535b + ce3b23f commit 244d13b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
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()

0 comments on commit 244d13b

Please sign in to comment.