From f16d10af9a091fb0660d1b3ab6b9db3aa3befcb3 Mon Sep 17 00:00:00 2001 From: Thomas Roeblitz Date: Mon, 29 Jan 2024 14:53:10 +0100 Subject: [PATCH] renew GH access token ahead of time to prevent Bad credentials crashes --- connections/github.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/connections/github.py b/connections/github.py index 2204fe89..3037c0fe 100644 --- a/connections/github.py +++ b/connections/github.py @@ -11,7 +11,7 @@ # # Standard library imports -from datetime import datetime, timezone +from datetime import datetime, timedelta, timezone import time # Third party imports (anything installed into the local Python environment) @@ -99,8 +99,6 @@ def get_instance(): Instance of Github """ global _gh, _token - # TODO Possibly renew token already if expiry date is soon, not only - # after it has expired. # Check if PyGithub version is < 1.56 if hasattr(github, 'GithubRetry'): @@ -110,7 +108,10 @@ def get_instance(): # Pygithub 1.x time_now = datetime.utcnow() - if not _gh or (_token and time_now > _token.expires_at): + # Renew token already if expiry date is less then 30 min away. + refresh_time = timedelta(minutes=30) + + if not _gh or (_token and time_now > (_token.expires_at - refresh_time)): _gh = connect() return _gh