From 8ba75e08707491dc481e47e6204aaadf4c8c58a8 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sun, 26 Nov 2023 19:58:15 -0600 Subject: [PATCH] Fix rate limiting from pygithub.RateLimitExceededException (#263) The "core rate limit reset" time is in UTC, while github actions may be running in another timezone, such as PST. The below example was run in my local timezone, CST: ```python >>> import os, datetime >>> import github as pygithub >>> GH_INTERFACE = pygithub.Github(os.environ.get("ADABOT_GITHUB_ACCESS_TOKEN")) >>> GH_INTERFACE.get_rate_limit().core.reset - datetime.datetime.now() datetime.timedelta(seconds=24750, microseconds=356986) >>> GH_INTERFACE.get_rate_limit().core.reset - datetime.datetime.utcnow() datetime.timedelta(seconds=3147, microseconds=87271) ``` Use utcnow() instead of now() so that the sleep is for the correct duration. (utcnow is deprecated, but the correct alternative, `now(tz=datetime.timezone.utc)` does not work: `GH_INTERFACE.get_rate_limit().core.reset` is a naive timestamp and can't be compared with a tz-aware object) --- adabot/circuitpython_libraries.py | 2 +- adabot/lib/bundle_announcer.py | 2 +- adabot/lib/circuitpython_library_validators.py | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/adabot/circuitpython_libraries.py b/adabot/circuitpython_libraries.py index 4dea2db..d6f940f 100644 --- a/adabot/circuitpython_libraries.py +++ b/adabot/circuitpython_libraries.py @@ -257,7 +257,7 @@ def run_library_checks(validators, kw_args, error_depth): break except pygithub.RateLimitExceededException: core_rate_limit_reset = GH_INTERFACE.get_rate_limit().core.reset - sleep_time = core_rate_limit_reset - datetime.datetime.now() + sleep_time = core_rate_limit_reset - datetime.datetime.utcnow() logging.warning("Rate Limit will reset at: %s", core_rate_limit_reset) time.sleep(sleep_time.seconds) continue diff --git a/adabot/lib/bundle_announcer.py b/adabot/lib/bundle_announcer.py index 9ad99e5..4b021eb 100644 --- a/adabot/lib/bundle_announcer.py +++ b/adabot/lib/bundle_announcer.py @@ -80,7 +80,7 @@ def get_bundle_updates(full_repo_name: str) -> Tuple[Set[RepoResult], Set[RepoRe except pygithub.RateLimitExceededException: core_rate_limit_reset = GH_INTERFACE.get_rate_limit().core.reset - sleep_time = core_rate_limit_reset - datetime.datetime.now() + sleep_time = core_rate_limit_reset - datetime.datetime.utcnow() logging.warning("Rate Limit will reset at: %s", core_rate_limit_reset) time.sleep(sleep_time.seconds) continue diff --git a/adabot/lib/circuitpython_library_validators.py b/adabot/lib/circuitpython_library_validators.py index 2c55682..a28abd4 100644 --- a/adabot/lib/circuitpython_library_validators.py +++ b/adabot/lib/circuitpython_library_validators.py @@ -916,7 +916,7 @@ def validate_readthedocs(self, repo): break except pygithub.RateLimitExceededException: core_rate_limit_reset = GH_INTERFACE.get_rate_limit().core.reset - sleep_time = core_rate_limit_reset - datetime.datetime.now() + sleep_time = core_rate_limit_reset - datetime.datetime.utcnow() logging.warning("Rate Limit will reset at: %s", core_rate_limit_reset) time.sleep(sleep_time.seconds) continue @@ -1275,7 +1275,7 @@ def validate_actions_state(self, repo): return [] except pygithub.RateLimitExceededException: core_rate_limit_reset = GH_INTERFACE.get_rate_limit().core.reset - sleep_time = core_rate_limit_reset - datetime.datetime.now() + sleep_time = core_rate_limit_reset - datetime.datetime.utcnow() logging.warning("Rate Limit will reset at: %s", core_rate_limit_reset) time.sleep(sleep_time.seconds)