Skip to content

Commit 95ee9f5

Browse files
committed
Add support for retrying when GitHub rate limit the import
1 parent f191075 commit 95ee9f5

File tree

4 files changed

+50
-10
lines changed

4 files changed

+50
-10
lines changed

scripts/import_backends.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import TYPE_CHECKING, Dict, List, Sequence, cast
77

88
import github
9+
import retrying
910
from termcolor import cprint
1011
from ticket_type import Ticket
1112

@@ -224,12 +225,41 @@ def submit(self, ticket: Ticket) -> int:
224225
# larger gap for better reliability.
225226
time.sleep(5)
226227

227-
issue = self.repo.create_issue(
228-
ticket.summary,
229-
self.description_text(ticket),
230-
milestone=self.get_or_create_milestone(ticket.milestone),
231-
labels=self.labels(ticket),
228+
# We also want to wait and retry when GitHub then additionally rate
229+
# limit us anyway...
230+
231+
def retry_on_exception(exception: Exception) -> bool:
232+
return isinstance(exception, github.GithubException)
233+
234+
@retrying.retry(
235+
retry_on_exception=retry_on_exception,
236+
wait_fixed=20_000,
232237
)
238+
def create_issue() -> github.Issue.Issue:
239+
RATE_LIMIT_MESSAGE = "exceeded a secondary rate limit and have been temporarily blocked" # noqa:E501
240+
241+
try:
242+
return self.repo.create_issue(
243+
ticket.summary,
244+
self.description_text(ticket),
245+
milestone=self.get_or_create_milestone(ticket.milestone),
246+
labels=self.labels(ticket),
247+
)
248+
except github.GithubException as e:
249+
message = e.data.get('message')
250+
if (
251+
isinstance(message, str)
252+
and RATE_LIMIT_MESSAGE in message
253+
):
254+
print("... GitHub Rate Limited ...")
255+
raise github.RateLimitExceededException(
256+
status=e.status,
257+
data=e.data,
258+
headers=e.headers,
259+
) from e
260+
raise
261+
262+
issue = create_issue()
233263

234264
ticket_number: int = issue.number
235265
self._known_titles[ticket_number] = ticket.summary

scripts/requirements-dev.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#
2-
# This file is autogenerated by pip-compile with python 3.8
3-
# To update, run:
2+
# This file is autogenerated by pip-compile with Python 3.8
3+
# by the following command:
44
#
55
# pip-compile ./scripts/requirements-dev.in
66
#
@@ -87,8 +87,13 @@ requests==2.27.1
8787
# via
8888
# -r ./scripts/requirements.txt
8989
# pygithub
90+
retrying==1.3.3
91+
# via -r ./scripts/requirements.txt
9092
six==1.16.0
91-
# via flake8-tuple
93+
# via
94+
# -r ./scripts/requirements.txt
95+
# flake8-tuple
96+
# retrying
9297
termcolor==1.1.0
9398
# via -r ./scripts/requirements.txt
9499
testfixtures==6.18.5

scripts/requirements.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
PyGithub
22
PyYAML
3+
retrying
34
termcolor

scripts/requirements.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#
2-
# This file is autogenerated by pip-compile with python 3.8
3-
# To update, run:
2+
# This file is autogenerated by pip-compile with Python 3.8
3+
# by the following command:
44
#
55
# pip-compile ./scripts/requirements.in
66
#
@@ -26,6 +26,10 @@ pyyaml==6.0
2626
# via -r ./scripts/requirements.in
2727
requests==2.27.1
2828
# via pygithub
29+
retrying==1.3.3
30+
# via -r ./scripts/requirements.in
31+
six==1.16.0
32+
# via retrying
2933
termcolor==1.1.0
3034
# via -r ./scripts/requirements.in
3135
urllib3==1.26.9

0 commit comments

Comments
 (0)