Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle GitHub Rate Limits During Update Checks #449

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions ggshield/core/check_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,38 @@ def check_for_updates() -> Optional[str]:
try:
resp = requests.get(
"https://api.github.com/repos/GitGuardian/GGShield/releases/latest",
headers={
"Accept": "application/vnd.github+json",
"User-Agent": f"GGShield {__version__}",
"X-Github-Api-Version": "2022-11-28",
},
timeout=CHECK_TIMEOUT,
)
except Exception as e:
logger.error("Failed to connect to api.github.com: %s", e)
return None

if resp.status_code != 200:
# Handle GitHub rate limit responses gracefully
# https://docs.github.com/en/rest/overview/resources-in-the-rest-api?apiVersion=2022-11-28#rate-limiting
if int(resp.headers.get("X-RateLimit-Remaining", -1)) == 0:
logger.debug("GitHub rate limit exceeded - rescheduling update check")

# Reset the next update check based on when the GH API quota resets
check_at = int(resp.headers.get("X-RateLimit-Reset", -1)) - 24 * 60 * 60
if check_at < 0:
# Somehow we've hit the rate limit and the reset header is missing
# This can only happen if GH changes their responses
logger.error("Failed rescheduling update check")
rgajason marked this conversation as resolved.
Show resolved Hide resolved
return None

try:
save_yaml_dict({"check_at": check_at}, CACHE_FILE)
except Exception as e:
logger.error("Could not save time of version check to cache: %s", e)

return None

logger.error("Failed to check: %s", resp.text)
return None

Expand Down