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

Upgrade bot to be compatible with PyGithub v2.1.1 #224

Merged
merged 8 commits into from
Nov 9, 2023
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
19 changes: 14 additions & 5 deletions connections/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
#

# Standard library imports
import datetime
from datetime import datetime, timezone
import time

# Third party imports (anything installed into the local Python environment)
from github import Github, GithubIntegration
import github

# Local application imports (anything from EESSI/eessi-bot-software-layer)
from tools import config, logging
Expand Down Expand Up @@ -57,7 +57,7 @@ def get_token():
# If the config keys are not set, get_access_token will raise a NotImplementedError
# Returning NoneType token will stop the connection in get_instance
try:
github_integration = GithubIntegration(app_id, private_key)
github_integration = github.GithubIntegration(app_id, private_key)
_token = github_integration.get_access_token(installation_id)
break
except NotImplementedError as err:
Expand All @@ -84,7 +84,7 @@ def connect():
Returns:
Instance of Github
"""
return Github(get_token().token)
return github.Github(get_token().token)


def get_instance():
Expand All @@ -101,7 +101,16 @@ def get_instance():
global _gh, _token
# TODO Possibly renew token already if expiry date is soon, not only
# after it has expired.
if not _gh or (_token and datetime.datetime.utcnow() > _token.expires_at):

# Check if PyGithub version is < 1.56
if hasattr(github, 'GithubRetry'):
# Pygithub 2.x
time_now = datetime.now(timezone.utc)
else:
# Pygithub 1.x
time_now = datetime.utcnow()

if not _gh or (_token and time_now > _token.expires_at):
_gh = connect()
return _gh

Expand Down