Skip to content

Commit a512f2e

Browse files
authored
Merge pull request #224 from laraPPr/Fix_new_version_PyGithub
Upgrade bot to be compatible with PyGithub v2.1.1
2 parents 532b6ea + 21e0249 commit a512f2e

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

connections/github.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
#
1212

1313
# Standard library imports
14-
import datetime
14+
from datetime import datetime, timezone
1515
import time
1616

1717
# Third party imports (anything installed into the local Python environment)
18-
from github import Github, GithubIntegration
18+
import github
1919

2020
# Local application imports (anything from EESSI/eessi-bot-software-layer)
2121
from tools import config, logging
@@ -57,7 +57,7 @@ def get_token():
5757
# If the config keys are not set, get_access_token will raise a NotImplementedError
5858
# Returning NoneType token will stop the connection in get_instance
5959
try:
60-
github_integration = GithubIntegration(app_id, private_key)
60+
github_integration = github.GithubIntegration(app_id, private_key)
6161
_token = github_integration.get_access_token(installation_id)
6262
break
6363
except NotImplementedError as err:
@@ -84,7 +84,7 @@ def connect():
8484
Returns:
8585
Instance of Github
8686
"""
87-
return Github(get_token().token)
87+
return github.Github(get_token().token)
8888

8989

9090
def get_instance():
@@ -101,7 +101,16 @@ def get_instance():
101101
global _gh, _token
102102
# TODO Possibly renew token already if expiry date is soon, not only
103103
# after it has expired.
104-
if not _gh or (_token and datetime.datetime.utcnow() > _token.expires_at):
104+
105+
# Check if PyGithub version is < 1.56
106+
if hasattr(github, 'GithubRetry'):
107+
# Pygithub 2.x
108+
time_now = datetime.now(timezone.utc)
109+
else:
110+
# Pygithub 1.x
111+
time_now = datetime.utcnow()
112+
113+
if not _gh or (_token and time_now > _token.expires_at):
105114
_gh = connect()
106115
return _gh
107116

0 commit comments

Comments
 (0)