Skip to content

Commit

Permalink
Merge pull request #238 from trz42/refresh_token_well_ahead
Browse files Browse the repository at this point in the history
renew GitHub access token ahead of time to prevent "`Bad credentials`" crashes
  • Loading branch information
boegel authored Jan 29, 2024
2 parents d02768c + f16d10a commit fd05f81
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions connections/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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'):
Expand All @@ -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

Expand Down

0 comments on commit fd05f81

Please sign in to comment.