Skip to content

Commit 244d13b

Browse files
authored
Merge pull request #62 from augustd/no-rate-limiting
Handle cases where no rate limiting is used
2 parents bdb535b + ce3b23f commit 244d13b

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

ghsearch/gh_search.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import click
44
import github
55
from github.ContentFile import ContentFile
6+
from github.GithubException import GithubException
67
from github.Rate import Rate
78
from github.RateLimit import RateLimit
89

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

56+
def get_rate_limit(self) -> RateLimit | None:
57+
try:
58+
return self.client.get_rate_limit()
59+
except GithubException as ge:
60+
# 404 means that rate limiting is disabled
61+
if ge.status == 404:
62+
return None
63+
raise ge
64+
5565
def get_filtered_results(self, query: List[str]) -> List[ContentFile]:
56-
rate_limit = self.client.get_rate_limit()
57-
if self.verbose:
66+
rate_limit = self.get_rate_limit()
67+
68+
if rate_limit and self.verbose:
5869
_echo_rate_limits(rate_limit)
5970

6071
results = self.client.search_code(query=" ".join(query))
61-
self._check_core_limit_threshold(results.totalCount, rate_limit.core)
72+
73+
if rate_limit:
74+
self._check_core_limit_threshold(results.totalCount, rate_limit.core)
6275

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

77-
if self.verbose:
78-
_echo_rate_limits(self.client.get_rate_limit())
90+
rate_limit = self.get_rate_limit()
91+
if rate_limit and self.verbose:
92+
_echo_rate_limits(rate_limit)
7993

8094
return filtered_results
8195

tests/unit/gh_search_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,15 @@ def test_get_filtered_results_many_calls(mock_client, mock_click):
127127
Do you want to continue?""".strip(),
128128
abort=True,
129129
)
130+
131+
132+
def test_get_filtered_results_rate_limiting_disabled(mock_client):
133+
mock_client.get_rate_limit.side_effect = github.GithubException(404, "Not Found")
134+
mock_filter = Mock()
135+
mock_filter.uses_core_api = True
136+
137+
ghsearch = GHSearch(mock_client, [])
138+
ghsearch.get_filtered_results(["query", "org:bort"])
139+
140+
# ensure get_rate_limit was called (and the side_effect above handled)
141+
mock_client.get_rate_limit.assert_called()

0 commit comments

Comments
 (0)