Skip to content

Commit d1e2132

Browse files
authored
Merge pull request #292 from tekktrik/dev/improve-timeout-flexibility
Improve throttling flexibility
2 parents b5fc1d6 + 0403b1b commit d1e2132

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

adabot/lib/circuitpython_library_validators.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -849,12 +849,21 @@ def validate_readthedocs(self, repo):
849849
errors.append(ERROR_RTD_ADABOT_MISSING)
850850

851851
# Get the README file contents
852-
try:
853-
lib_repo = GH_INTERFACE.get_repo("Adafruit/" + repo["full_name"])
854-
except pygithub.GithubException:
855-
errors.append(ERROR_RTD_FAILED_TO_LOAD_BUILD_STATUS)
856-
return errors
857-
content_file = lib_repo.get_contents("README.rst")
852+
while True:
853+
try:
854+
lib_repo = GH_INTERFACE.get_repo("Adafruit/" + repo["full_name"])
855+
content_file = lib_repo.get_contents("README.rst")
856+
break
857+
except pygithub.RateLimitExceededException:
858+
core_rate_limit_reset = GH_INTERFACE.get_rate_limit().core.reset
859+
sleep_time = core_rate_limit_reset - datetime.datetime.now()
860+
logging.warning("Rate Limit will reset at: %s", core_rate_limit_reset)
861+
time.sleep(sleep_time.seconds)
862+
continue
863+
except pygithub.GithubException:
864+
errors.append(ERROR_RTD_FAILED_TO_LOAD_BUILD_STATUS)
865+
return errors
866+
858867
readme_text = content_file.decoded_content.decode("utf-8")
859868

860869
# Parse for the ReadTheDocs slug
@@ -864,12 +873,28 @@ def validate_readthedocs(self, repo):
864873
rtd_slug: str = search_results.named["slug"]
865874
rtd_slug = rtd_slug.replace("_", "-", -1)
866875

867-
# GET the latest documentation build runs
868-
url = f"https://readthedocs.org/api/v3/projects/{rtd_slug}/builds/"
869-
rtd_token = os.environ["RTD_TOKEN"]
870-
headers = {"Authorization": f"token {rtd_token}"}
871-
response = requests.get(url, headers=headers)
872-
json_response = response.json()
876+
while True:
877+
# GET the latest documentation build runs
878+
url = f"https://readthedocs.org/api/v3/projects/{rtd_slug}/builds/"
879+
rtd_token = os.environ["RTD_TOKEN"]
880+
headers = {"Authorization": f"token {rtd_token}"}
881+
response = requests.get(url, headers=headers)
882+
json_response = response.json()
883+
884+
error_message = json_response.get("detail")
885+
if error_message:
886+
if error_message == "Not found." or not error_message.startswith(
887+
"Request was throttled."
888+
):
889+
errors.append(ERROR_RTD_FAILED_TO_LOAD_BUILD_STATUS)
890+
return errors
891+
time_result = parse.search(
892+
"Request was throttled. Expected available in {throttled:d} seconds.",
893+
error_message,
894+
)
895+
time.sleep(time_result.named["throttled"] + 3)
896+
continue
897+
break
873898

874899
# Return the results of the latest run
875900
doc_build_results = json_response.get("results")

0 commit comments

Comments
 (0)